matplotlib / matplotlib/mplfinance

Bug Report:

Aperta
#686 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
4.4k
Fork
678
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in _utils.py, in _calculate_atr, e segui come vengono fornite le sue entrate highs, lows e closes per i grafici PNF e Renko. Riproduci entrambi i casi di grafico del report, quindi verifica che l’avviso sia scomparso per PNF e che il percorso Renko non generi più l’errore di attributo ndarray segnalato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
pandas, python
Ambito
data-visualization
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.