matplotlib / matplotlib/mplfinance
Plotting a Swing chart over the generic OHLC chart
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 4.4k
- Forks
- 678
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
I'm want to visualize a swings/price movement chart over a normal OHLC chart, e.g., like the attachment below.

I have written the following code:
```py
def get_swing_dataframe(daily: DataFrame):
last_high = -999999999
last_low = abs(last_high)
raw_dftb = []
dip = True
for row in daily.iterrows():
close = row[1]["Close"]
mark = False
if dip:
if close > last_high:
dip = False
mark = True
last_high = close
else:
if close < last_low:
dip = True
mark = True
last_low = close
if mark:
raw_dftb.append(row[1])
df = pd.DataFrame(raw_dftb)
df.index.name = "Date"
return df
def main():
daily = pd.read_csv("ES___CCB.csv", index_col=0, parse_dates=True)
daily.index.name = "Date"
swing = get_swing_dataframe(daily)
mpf.plot(
daily,
style="binance",
type="ohlc",
addplot=[mpf.make_addplot(swing, type="line", color="g", panel=1)],
)
if __name__ == "__main__":
main()
```
The issue here is that the "Swing" dataframe does not have a data entry for every single date, it only marks where and when the price changes, spitting out the following error:
```
File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
yield from self._plot_args(this, kwargs)
File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (6232,) and (23,)
```
How can I achieve this?
Recap: I want to visualize a swings/price movement chart over a normal OHLC chart, e.g., like the attachment below.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den Aufrufen von mpf.plot und mpf.make_addplot im Beispiel und reproduziere dann die Abweichung mit den beschriebenen täglichen und Swing-DataFrames. Verfolge, wie addplot-Daten an der OHLC-x-Achse ausgerichtet werden; als abgeschlossen gilt die Aufgabe, wenn eine sparse Swing-Serie ohne den x/y-Dimensionsfehler im normalen OHLC-Chart überlagert werden kann und der angeforderten Swing-Visualisierung entspricht.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- data-visualization
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100