Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Report: #686

Open
zbig01 opened this issue Dec 22, 2024 · 2 comments
Open

Bug Report: #686

zbig01 opened this issue Dec 22, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@zbig01
Copy link

zbig01 commented Dec 22, 2024

Bug when using box_size = 'atr' in pnf charts in mplfinance : FutureWarning: Series.getitem treating keys as positions is deprecated

OS Win 11 Pro 24H2 Desktop
IDE Visual Studio Code 1.96.2
Python 3.12.8
mplfinance 0.12.10b0

When using the 'atr' box_size parameter in creating pnf charts in mplfinance the _utils.py script throws this warning

\mplfinance_utils.py:129: FutureWarning: Series.getitem treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos] high = highs[i]
\mplfinance_utils.py:130: FutureWarning: Series.getitem treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos] low = lows[i]
_utils.py:131: FutureWarning: Series.getitem treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos] close_prev = closes[i-1]

This is the responsible function:

def _calculate_atr(atr_length, highs, lows, closes):
    """Calculate the average true range
    atr_length : time period to calculate over
    all_highs : list of highs
    all_lows : list of lows
    all_closes : list of closes
    """
    if atr_length < 1:
        raise ValueError("Specified atr_length may not be less than 1")
    elif atr_length >= len(closes):
        raise ValueError("Specified atr_length is larger than the length of the dataset: " + str(len(closes)))
    atr = 0
    for i in range(len(highs)-atr_length, len(highs)):
        high = highs[i]
        low = lows[i]
        close_prev = closes[i-1]
        tr = max(abs(high-low), abs(high-close_prev), abs(low-close_prev))
        atr += tr
    return atr/atr_length

This warning is suppressed when you modify the code like so

high = highs.iloc[i]
low = lows.iloc[i]
close_prev = closes.iloc[i-1]

This however creates a new problem if you subsequently run a Renko chart, you get this error

LocalCache\local-packages\Python312\site-pa
high = highs.iloc[i]
^^^^^^^^^^
AttributeError: 'numpy.ndarray' object has no attribute 'iloc'

To fix this I created a small helper function

def safe_indexing(data, index):
    if isinstance(data, pd.Series):
        return data.iloc[index]
    else:
        return data[index]

and placed it in _utils.py
and updated _utils.py

high = highs[i]
low = lows[i]
close_prev = closes[i-1]

to

high = safe_indexing(highs, i)
low = safe_indexing(lows, i)
close_prev = safe_indexing(closes, i-1)

Now both charts render with no messages

@zbig01 zbig01 added the bug Something isn't working label Dec 22, 2024
@DanielGoldfarb
Copy link
Collaborator

@zbig01

Thanks for reporting this. And your solution is a good one. However I am wondering (knowing some of the history of this package) if the real issue is this: Once upon a time the values passed into _calculate_atr(atr_length, highs, lows, closes) for highs, lows, and closes were always lists. And then when I re-wrote the pnf code, I passed in pd.Series instead.

If so, then maybe the more appropriate solution will be to fix the pnf rewrite to pass in the values as lists. I will take a look.

@zbig01
Copy link
Author

zbig01 commented Dec 25, 2024

@DanielGoldfarb

Thanks for your comment. I realize my fix was more of a hack than a fix. Your suggestion of using lists will likely be a more elegant solution.

Cheers and Happy holidays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants