Skip to content

Commit

Permalink
Create TrueRange.py
Browse files Browse the repository at this point in the history
  • Loading branch information
geovedi authored Nov 8, 2023
1 parent ad2c6d3 commit 010f292
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions indicators/TrueRange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pandas as pd
import talib

def TrueRange(dataframe):
"""
Calculate True Range (TR) for a given pandas DataFrame.
Args:
dataframe (pd.DataFrame): DataFrame with columns ['high', 'low', 'open', 'close', 'volume'].
Returns:
pd.Series: True Range values.
"""
df = dataframe.copy()

# Initialize True Range buffer
true_range = [0.0] * len(df)

for i in range(len(df)):
cur_high = df['high'][i]
cur_low = df['low'][i]
close1 = df['close'][i - 1] if i > 0 else df['close'][i]

true_high = max(close1, cur_high)
true_low = min(close1, cur_low)

tr = true_high - true_low
true_range[i] = tr

return pd.Series(true_range, name='TrueRange')

# Example usage:
# Replace df with your pandas DataFrame containing columns ['open', 'high', 'low', 'close', 'volume']
# Call the function as follows:
# true_range_values = TrueRange(df)

0 comments on commit 010f292

Please sign in to comment.