|
| 1 | +''' |
| 2 | +This file contains a animation demo using mplfinance "external axes mode", |
| 3 | +in which animate both the display of candlesticks as well as the display |
| 4 | +of MACD (Moving Average Convergence Divergence) visual analysis. |
| 5 | +
|
| 6 | +In this example, instead of creating the Figure and Axes external to mplfiance, |
| 7 | +we allow mplfinance to create the Figure and Axes using its "panel method", and |
| 8 | +set kwarg `returnfig=True` so that mplfinance will return the Figure and Axes. |
| 9 | +
|
| 10 | +We then take those Axes and pass them back into mplfinance ("external axes mode") |
| 11 | +as part of the animation. |
| 12 | +
|
| 13 | +Note that presently mplfinance does not support "blitting" (blitting makes animation |
| 14 | +more efficient). Nonetheless, the animation is efficient enough to update at least |
| 15 | +once per second, and typically more frequently depending on the size of the plot. |
| 16 | +''' |
| 17 | +import pandas as pd |
| 18 | +import mplfinance as mpf |
| 19 | +import matplotlib.animation as animation |
| 20 | + |
| 21 | +mpf.__version__ |
| 22 | + |
| 23 | +idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True) |
| 24 | +idf.shape |
| 25 | +idf.head(3) |
| 26 | +idf.tail(3) |
| 27 | +df = idf.loc['2011-07-01':'2011-12-30',:] |
| 28 | + |
| 29 | + |
| 30 | +# ======= |
| 31 | +# MACD: |
| 32 | + |
| 33 | +df = df.iloc[0:30] |
| 34 | + |
| 35 | +exp12 = df['Close'].ewm(span=12, adjust=False).mean() |
| 36 | +exp26 = df['Close'].ewm(span=26, adjust=False).mean() |
| 37 | +macd = exp12 - exp26 |
| 38 | +signal = macd.ewm(span=9, adjust=False).mean() |
| 39 | +histogram = macd - signal |
| 40 | + |
| 41 | +apds = [mpf.make_addplot(exp12,color='lime'), |
| 42 | + mpf.make_addplot(exp26,color='c'), |
| 43 | + mpf.make_addplot(histogram,type='bar',width=0.7,panel=1, |
| 44 | + color='dimgray',alpha=1,secondary_y=False), |
| 45 | + mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=True), |
| 46 | + mpf.make_addplot(signal,panel=1,color='b',secondary_y=True), |
| 47 | + ] |
| 48 | + |
| 49 | +s = mpf.make_mpf_style(base_mpf_style='classic',rc={'figure.facecolor':'lightgray'}) |
| 50 | + |
| 51 | +fig, axes = mpf.plot(df,type='candle',addplot=apds,figscale=1.5,figratio=(7,5),title='\n\nMACD', |
| 52 | + style=s,volume=True,volume_panel=2,panel_ratios=(6,3,2),returnfig=True) |
| 53 | + |
| 54 | +ax_main = axes[0] |
| 55 | +ax_emav = ax_main |
| 56 | +ax_hisg = axes[2] |
| 57 | +ax_macd = axes[3] |
| 58 | +ax_sign = ax_macd |
| 59 | +ax_volu = axes[4] |
| 60 | + |
| 61 | +df = idf.loc['2011-07-01':'2011-12-30',:] |
| 62 | + |
| 63 | +def animate(ival): |
| 64 | + if (20+ival) > len(df): |
| 65 | + print('no more data to plot') |
| 66 | + ani.event_source.interval *= 3 |
| 67 | + if ani.event_source.interval > 12000: |
| 68 | + exit() |
| 69 | + return |
| 70 | + data = df.iloc[0:(30+ival)] |
| 71 | + exp12 = data['Close'].ewm(span=12, adjust=False).mean() |
| 72 | + exp26 = data['Close'].ewm(span=26, adjust=False).mean() |
| 73 | + macd = exp12 - exp26 |
| 74 | + signal = macd.ewm(span=9, adjust=False).mean() |
| 75 | + histogram = macd - signal |
| 76 | + apds = [mpf.make_addplot(exp12,color='lime',ax=ax_emav), |
| 77 | + mpf.make_addplot(exp26,color='c',ax=ax_emav), |
| 78 | + mpf.make_addplot(histogram,type='bar',width=0.7, |
| 79 | + color='dimgray',alpha=1,ax=ax_hisg), |
| 80 | + mpf.make_addplot(macd,color='fuchsia',ax=ax_macd), |
| 81 | + mpf.make_addplot(signal,color='b',ax=ax_sign), |
| 82 | + ] |
| 83 | + |
| 84 | + for ax in axes: |
| 85 | + ax.clear() |
| 86 | + mpf.plot(data,type='candle',addplot=apds,ax=ax_main,volume=ax_volu) |
| 87 | + |
| 88 | +ani = animation.FuncAnimation(fig,animate,interval=100) |
| 89 | + |
| 90 | +mpf.show() |
0 commit comments