question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

true_range() function are not propagating drift parameter in the inputs - NaN values.

See original GitHub issue

Which 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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ffhiratacommented, Mar 5, 2021

Happy to help!

0reactions
twopirllccommented, Mar 7, 2021

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.

from numpy import nan as npNaN # include

def true_range(...):
    # ...
    true_range = true_range.abs().max(axis=1)
    true_range.iloc[:drift] = npNaN # << fix
    # ...

Thanks, KJ

Read more comments on GitHub >

github_iconTop Results From Across the Web

Floating point exception tracking and NAN propagation
Abstract. The most common methods for detecting floating point errors are based on exception trapping or a global status register.
Read more >
TrueRange - thinkorswim Learning Center
Returns the true range (TR). TR is the greatest of the following: the difference between the current high and the current low; the...
Read more >
"ValueError: The input contains nan values" when using lmfit
I'm using lmfit to fit a four parameter logistic curve to my data, with my current code ...
Read more >
72 questions with answers in VARIOGRAM | Science topic
I am doing a master thesis on spatio temporal data and I would like to perform a spatio temporal variogram using R. I...
Read more >
Modeling - MuJoCo documentation
When a joint is defined inside a body, its function is not to connect the ... The quat attribute has a default value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found