matplotlib / matplotlib/mplfinance

Plotting a Swing chart over the generic OHLC chart

Abierto
#541 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

question
Lenguaje dominante
Python
Estrellas
4.4k
Forks
678
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

I'm want to visualize a swings/price movement chart over a normal OHLC chart, e.g., like the attachment below.

image

I have written the following code:

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con las llamadas a mpf.plot y mpf.make_addplot del ejemplo y, después, reproduce la discrepancia usando los DataFrames daily y swing descritos. Traza cómo se alinean los datos de addplot con el eje x de OHLC; se considera terminado cuando una serie swing dispersa puede superponerse en el gráfico OHLC normal sin el error de dimensiones x/y y coincide con la visualización swing solicitada.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
data-visualization
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.