Skip to content

Commit e544a04

Browse files
Merge branch 'external_axes'
2 parents 5cd1387 + e8d8aee commit e544a04

15 files changed

+4218
-112
lines changed

examples/external_axes.ipynb

Lines changed: 778 additions & 0 deletions
Large diffs are not rendered by default.

examples/mpf_animation_demo1.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
This file contains a simple animation demo using mplfinance "external axes mode".
3+
4+
Note that presently mplfinance does not support "blitting" (blitting makes animation
5+
more efficient). Nonetheless, the animation is efficient enough to update at least
6+
once per second, and typically more frequently depending on the size of the plot.
7+
'''
8+
import pandas as pd
9+
import mplfinance as mpf
10+
import matplotlib.animation as animation
11+
12+
idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
13+
idf.shape
14+
idf.head(3)
15+
idf.tail(3)
16+
df = idf.loc['2011-07-01':'2011-12-30',:]
17+
18+
fig = mpf.figure(style='charles',figsize=(7,8))
19+
ax1 = fig.add_subplot(2,1,1)
20+
ax2 = fig.add_subplot(3,1,3)
21+
22+
def animate(ival):
23+
if (20+ival) > len(df):
24+
print('no more data to plot')
25+
ani.event_source.interval *= 3
26+
if ani.event_source.interval > 12000:
27+
exit()
28+
return
29+
data = df.iloc[0:(20+ival)]
30+
ax1.clear()
31+
ax2.clear()
32+
mpf.plot(data,ax=ax1,volume=ax2,type='candle')
33+
34+
ani = animation.FuncAnimation(fig, animate, interval=250)
35+
36+
mpf.show()

examples/mpf_animation_demo2.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
This file contains a simple animation demo using mplfinance "external axes mode".
3+
4+
In this example, instead of creating the Figure and Axes external to mplfiance,
5+
we allow mplfinance to create the Figure and Axes using its "panel method", and
6+
set kwarg `returnfig=True` so that mplfinance will return the Figure and Axes.
7+
8+
We then take those Axes and pass them back into mplfinance ("external axes mode")
9+
as part of the animation.
10+
11+
Note that presently mplfinance does not support "blitting" (blitting makes animation
12+
more efficient). Nonetheless, the animation is efficient enough to update at least
13+
once per second, and typically more frequently depending on the size of the plot.
14+
'''
15+
import pandas as pd
16+
import mplfinance as mpf
17+
import matplotlib.animation as animation
18+
19+
idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
20+
21+
df = idf.loc['2011-07-01':'2011-12-30',:]
22+
23+
pkwargs=dict(type='candle',mav=(10,20))
24+
25+
fig, axes = mpf.plot(df.iloc[0:20],returnfig=True,volume=True,
26+
figsize=(11,8),panel_ratios=(2,1),
27+
title='\n\nS&P 500 ETF',**pkwargs)
28+
ax1 = axes[0]
29+
ax2 = axes[2]
30+
31+
def animate(ival):
32+
if (20+ival) > len(df):
33+
print('no more data to plot')
34+
ani.event_source.interval *= 3
35+
if ani.event_source.interval > 12000:
36+
exit()
37+
return
38+
data = df.iloc[0:(20+ival)]
39+
ax1.clear()
40+
ax2.clear()
41+
mpf.plot(data,ax=ax1,volume=ax2,**pkwargs)
42+
43+
ani = animation.FuncAnimation(fig, animate, interval=200)
44+
45+
mpf.show()

examples/mpf_animation_macd.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)