Skip to content

Commit e4b2574

Browse files
Merge pull request #501 from DanielGoldfarb/master
tweak multicursor, and add ginput example
2 parents 1f710ca + 4d85947 commit e4b2574

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

examples/scratch_pad/multicursor_macd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
fig, axlist = mpf.plot(df,type='candle',addplot=apds,figscale=1.1,figratio=(8,5),title='\nMACD',
2626
style='blueskies',volume=True,volume_panel=2,panel_ratios=(6,3,2),returnfig=True)
2727

28-
multi = MultiCursor(fig.canvas, axlist, color='r',lw=1.2)
28+
multi = MultiCursor(fig.canvas, axlist, color='r',lw=1.2, horizOn=True, vertOn=True)
2929
mpf.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import pandas as pd
2+
import mplfinance as mpf
3+
from matplotlib.widgets import MultiCursor
4+
from matplotlib.collections import LineCollection
5+
6+
# read the data:
7+
idf = pd.read_csv('../data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
8+
df = idf.loc['2011-07-01':'2011-12-30',:]
9+
10+
11+
# macd related calculations:
12+
exp12 = df['Close'].ewm(span=12, adjust=False).mean()
13+
exp26 = df['Close'].ewm(span=26, adjust=False).mean()
14+
macd = exp12 - exp26
15+
signal = macd.ewm(span=9, adjust=False).mean()
16+
histogram = macd - signal
17+
18+
# initial plot:
19+
apds = [mpf.make_addplot(exp12,color='lime'),
20+
mpf.make_addplot(exp26,color='c'),
21+
mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,
22+
color='dimgray',alpha=1,secondary_y=False),
23+
mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=True),
24+
mpf.make_addplot(signal,panel=1,color='b',secondary_y=True),
25+
]
26+
27+
# For some reason, which i have yet to determine, MultiCursor somehow
28+
# causes ymin to be set to zero for the main candlestick Axes, but we
29+
# can correct that problem by passing in specific values:
30+
ymin = min(df['Low']) * 0.98
31+
ymax = max(df['High']) * 1.02
32+
33+
# initial plot with cursor:
34+
fig, axlist = mpf.plot(df,type='candle',addplot=apds,figscale=1.25,figratio=(8,6),title='\nMACD', ylim=(ymin,ymax),
35+
style='blueskies',volume=True,volume_panel=2,panel_ratios=(6,3,2),returnfig=True)
36+
multi = MultiCursor(fig.canvas, axlist[0:2], horizOn=True, vertOn=True, color='pink', lw=1.2)
37+
38+
# ---------------------------------------------------
39+
# set up an event loop where we wait for two
40+
# mouse clicks, and then draw a line in between them,
41+
# and then wait again for another two mouse clicks.
42+
43+
# This is a crude way to do it, but its quick and easy.
44+
# Disadvantage is: user has 8 seconds to provide two clicks
45+
# or the first click will be erased. But the 8 seconds
46+
# repeats as long as the user does not close the Figure,
47+
# so user can draw as many trend lines as they want.
48+
# The advantage of doing it this way is we don't have
49+
# to write all the mouse click handling stuff that's
50+
# already written in `Figure.ginput()`.
51+
52+
53+
alines = []
54+
55+
not_closed = True
56+
def on_close(event):
57+
global not_closed
58+
not_closed = False
59+
60+
fig.canvas.mpl_connect('close_event', on_close)
61+
62+
while not_closed:
63+
64+
vertices = fig.ginput(n=2,timeout=8)
65+
if len(vertices) < 2:
66+
continue
67+
p1 = vertices[0]
68+
p2 = vertices[1]
69+
70+
d1 = df.index[ round(p1[0]) ]
71+
d2 = df.index[ round(p2[0]) ]
72+
73+
alines.append( [ (d1,p1[1]), (d2,p2[1]) ] )
74+
75+
apds = [mpf.make_addplot(exp12,color='lime',ax=axlist[0]),
76+
mpf.make_addplot(exp26,color='c',ax=axlist[0]),
77+
mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,ax=axlist[2],color='dimgray',alpha=1),
78+
mpf.make_addplot(macd,panel=1,color='fuchsia',ax=axlist[3]),
79+
mpf.make_addplot(signal,panel=1,color='b',ax=axlist[3])
80+
]
81+
82+
mpf.plot(df,ax=axlist[0],type='candle',addplot=apds,ylim=(ymin,ymax),
83+
alines=dict(alines=alines,colors='r'),
84+
style='blueskies',volume=axlist[4],volume_panel=2,panel_ratios=(6,3,2))
85+
86+
fig.canvas.draw_idle()
87+

0 commit comments

Comments
 (0)