Riemann
Riemann

Reputation: 67

animation.FuncAnimation mplfinance (candlestick and line togheter)? (python)

I created a reproducible example of random data for candlestick chart ohlc that is working correctly.

The objects that mpf "will use" to plot are df and y0_arr and they have the "same format" (they are pd.DataFrame, have DateIndex as index, same dates, same dtypes=float).

if you comment(exclude) the part of the ##random line the code will work with no problem, only for the candlestick chart, but it won't if you include the random line. I've also tried to plot just/only the random line but it won't work also when it's only the random line.

this below is the code:

import numpy as np
import pandas as pd
import datetime
from datetime import timedelta
from datetime import date, datetime
import mplfinance as mpf
import matplotlib.animation as animation
import time


i=0
sign=1
def genData():
    global df0, df, i, close, sign, y0_arr
    sign = sign*-1

    ##  random ohlc generation
    if i==0:        #variable initialization
        y0_arr = pd.DataFrame()
        df = pd.DataFrame()
        i=0
        close = 0
        open_ = np.round(np.random.normal(10, 4), decimals=2)  ##initial open

    print("i:",i)
    dt = datetime.now() + timedelta(days=i)
    dt = dt.date()

    if i > 0:
        open_ = close
    high = np.round(open_ + np.random.normal(0.5, 2), decimals=2)
    low = np.round(open_ - np.random.normal(0.5, 2), decimals=2)
    close = np.round(open_ + sign*np.random.normal(0.2, 0.4), decimals=2)

    dfi = np.column_stack((dt, open_, high, low, close))
    dfi = pd.DataFrame(dfi)
    dfi.columns = ['date', 'open', 'high', 'low', 'close']
    dfi['date'] = pd.to_datetime(dfi['date'], format="%Y/%m/%d")
    dfi.set_index('date', inplace=True)
    dfi = dfi.convert_dtypes(float)

    df = pd.concat([df, dfi])
    ##  random ohlc generation

    ##random line (exclude from here...)
    y0 = np.round(np.random.normal(1,2), decimals=2)

    y0i = np.column_stack((dt,y0))
    y0i = pd.DataFrame(y0i)
    y0i.columns = ['date','open']
    y0i['date'] = pd.to_datetime(y0i['date'],format="%Y/%m/%d")
    y0i.set_index('date', inplace=True)
    y0i = y0i.convert_dtypes(float)

    y0_arr = pd.concat([y0_arr,y0i])
    ##random line (...to here)
    time.sleep(1)
    i=i+1


####    plotting
fig = mpf.figure(style="charles",figsize=(7,8))
ax1 = fig.add_subplot(1,1,1)

def animate(ival):
    global df, y0_arr
    print("animate()")
    genData()       ##create new data
    ax1.clear
    mpf.plot(df, ax=ax1, type='candle', ylabel='Price US$')     ##ohlc
    mpf.plot(y0_arr, ax=ax1, type='line',ylabel='Price US$')    ##random line (...exclude this line)

ani = animation.FuncAnimation(fig, animate, interval=250)
mpf.show()

Error

How can I solve this problem? Where to look? Thank You

Upvotes: 1

Views: 869

Answers (1)

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7714

Notice that the error message is KeyError: 'Open'. This is because mpf.plot() expects the first argument to be a DataFrame with columns 'Open', 'High', 'Low', and 'Close' (or with OHLC column names that you specify using kwarg columns=).

Apparently your y0_arr is not such a dataframe.

The correct way to add a line to a candlestick plot is with the mpf.make_addplot() call. <-Click here to see the documentation for addplot.

See also https://github.com/matplotlib/mplfinance/blob/master/examples/mpf_animation_macd.py for an example of how to add lines to an animated candlestick plot.

Upvotes: 2

Related Questions