matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.48k stars 613 forks source link

I just don't know who to ask…. #655

Closed wordsword251 closed 6 months ago

wordsword251 commented 6 months ago

I just don't know who to ask…. Everything in the program works, but there is one annoying thing. Doesn't clear axes[2] during animation. I don't understand what's wrong. ax_hisg this is an indicator BuySell Volume. When removing the first elements(candles, volumes, BuySell Volumes) the first indicator value BuySell remains in place. I want to understand why. screen

` import datetime import pandas as pd import time import mplfinance as mpf import matplotlib.animation as animation

pd.options.mode.chained_assignment = None constVolume = 100000000 # объём собираемых свечей в деньгах t_begin = datetime.datetime(2023, 11, 22, 14, 30, 0, 0) delta = datetime.timedelta(seconds=30) fin = pd.read_csv('/home/home/PycharmProjects/pythonProject/AAPL.csv', parse_dates=True) fin['Date'] = fin['Date'].astype("datetime64[ns]") df0 = pd.DataFrame(columns=['Date', 'Open', 'Close', 'High', 'Low', 'Volume', 'Buy', 'Sell'], dtype=float) df0['Date'] = df0['Date'].astype("datetime64[ns]")

def copy_ticks_range(t_begin, t_current, fin)

def make_candles(df_ticks, constVolume, df0):

t_current = t_begin + delta df_ticks = copy_ticks_range(t_begin, t_current, fin) t_begin = t_current df0 = make_candles(df_ticks, constVolume, df0) print(df0)

bs_calc = df0['Buy'] - df0['Sell'] apds = [mpf.make_addplot(bs_calc, panel=1, color='b', type='bar', width=0.5, alpha=1, secondary_y=True)]

fig, axes = mpf.plot(df0, type='candle', addplot=apds, figscale=1.5, figratio=(7, 5), title='\n\nAAPL', scale_padding={'left': 0.31, 'top': 0.1, 'right': 0.21, 'bottom': 0.3}, style='starsandstripes', volume=True, volume_panel=2, panel_ratios=(12, 3, 1), returnfig=True)

ax_main = axes[0] ax_hisg = axes[2] ax_volu = axes[4]

def animate(ival): global t_begin, df0 t_current = t_begin + delta df_ticks = copy_ticks_range(t_begin, t_current, fin) t_begin = t_current df0 = make_candles(df_ticks, constVolume, df0) bs_calc = df0['Buy'] - df0['Sell'] apds = [mpf.make_addplot(bs_calc, panel=1, color='b', type='bar', width=0.5, alpha=1, secondary_y=True, ax=ax_hisg)] print(df0) data = df0.iloc[0:(20 + ival)] if len(data) > 4: df0.drop(index=data.index[0], axis=0, inplace=True) ax_main.clear() ax_hisg.clear() ax_volu.clear() mpf.plot(data, type='candle', addplot=apds, ax=ax_main, volume=ax_volu)

ani = animation.FuncAnimation(fig, animate, cache_frame_data=False) mpf.show() time.sleep(1) `

wordsword251 commented 6 months ago

Нашел ошибку. Не все axes очищались. Вместо ax_main.clear() ax_hisg.clear() ax_volu.clear() надо for ax in axes: ax.clear()