true_range() function are not propagating drift parameter in the inputs - NaN values.
See original GitHub issueWhich version are you running? The lastest version is on Github. Pip is for major releases.
import pandas_ta as ta
print(ta.version)
0.2.45b0
Describe the bug The true_range calculation is valid when its inputs ([high_low_range, high - prev_close, prev_close - low]) are valid. This assumption is not occurring in the beginning of the data series, due shift caused by drift parameter.
The true_range() function has the argument drift which is a shift in the data series. This causes introduction of NaN (depends on numbers of shifts) in the beginning of the data serie close_prev. So, every calculation that relies on first series are using NaN as input. The bug is that true_range() assumes NaN as a valid input, so the results are compromised to a partial calculation, which I think it is incorrect. This error propagates to subsequent calculations, for instance average.
To Reproduce Inputs: testDataframe.zip
if __name__ == '__main__':
# read input data with expected results
# columns are: close, close_prev, high, low, true_range, true_range_expected_result, ATR_SMA_20_expected_result
filenamePath = r'D:\scripts\trade\ta_analysis_pr\testDataframe.csv'
df = pd.read_csv(filenamePath, sep=';')
# calculate true range for all rows
df.ta.true_range(append=True)
# calculate SMA_20 based on true_range
atr = ta.sma(df.TRUERANGE_1, length=20)
# join SMA result
df = df.join(atr)
print(df[['close_prev', 'true_range_expected_result', 'TRUERANGE_1']])
# compare columns true_range_expected_result vs. TRUERANGE_1
# check df.TRUERANGE_1[0]=0.32, true_range() is returning a partial value, because df.close_prev[0]=NaN, i.e., the function doesn´t have all valid inputs (close, close_prev, high, low) to provide a valid value.
print(df[['TRUERANGE_1', 'ATR_SMA_20_expected_result', 'SMA_20']])
# compare columns ATR_SMA_20_expected_result vs. SMA_20
# check df.SMA_20[19]=0.9495, the result is partial because true_range assumed close_prev as NaN in the first cycle. Due previous error, the sma calculated incorrect.
df.to_csv(r'D:\scripts\trade\ta_analysis_pr\testDataframe_result.csv', index=True)
Expected behavior The attachment shows an example of calculation of arithmetic average calculation based on true_range results. testDataframe_result.xlsx
The changes in close_prev due shift (introducing NaN) should propagate to inputs of calculation of:
true_range = true_range.abs().max(axis=1)
I did a proposed solution in:
https://github.com/ffhirata/pandas-ta/tree/ffhirata-patch-1
Screenshots If applicable, add screenshots to help explain your problem.
Additional context Add any other context about the problem here.
Thanks for using Pandas TA!
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Happy to help!
Hello @ffhirata,
This should do the trick. It would be great if you test it and make a PR so you can be added as a contributor.
Thanks, KJ