Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak multicursor, and add ginput example #501

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/scratch_pad/multicursor_macd.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
fig, axlist = mpf.plot(df,type='candle',addplot=apds,figscale=1.1,figratio=(8,5),title='\nMACD',
style='blueskies',volume=True,volume_panel=2,panel_ratios=(6,3,2),returnfig=True)

multi = MultiCursor(fig.canvas, axlist, color='r',lw=1.2)
multi = MultiCursor(fig.canvas, axlist, color='r',lw=1.2, horizOn=True, vertOn=True)
mpf.show()
87 changes: 87 additions & 0 deletions examples/scratch_pad/multicursor_macd_ginput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import pandas as pd
import mplfinance as mpf
from matplotlib.widgets import MultiCursor
from matplotlib.collections import LineCollection

# read the data:
idf = pd.read_csv('../data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
df = idf.loc['2011-07-01':'2011-12-30',:]


# macd related calculations:
exp12 = df['Close'].ewm(span=12, adjust=False).mean()
exp26 = df['Close'].ewm(span=26, adjust=False).mean()
macd = exp12 - exp26
signal = macd.ewm(span=9, adjust=False).mean()
histogram = macd - signal

# initial plot:
apds = [mpf.make_addplot(exp12,color='lime'),
mpf.make_addplot(exp26,color='c'),
mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,
color='dimgray',alpha=1,secondary_y=False),
mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=True),
mpf.make_addplot(signal,panel=1,color='b',secondary_y=True),
]

# For some reason, which i have yet to determine, MultiCursor somehow
# causes ymin to be set to zero for the main candlestick Axes, but we
# can correct that problem by passing in specific values:
ymin = min(df['Low']) * 0.98
ymax = max(df['High']) * 1.02

# initial plot with cursor:
fig, axlist = mpf.plot(df,type='candle',addplot=apds,figscale=1.25,figratio=(8,6),title='\nMACD', ylim=(ymin,ymax),
style='blueskies',volume=True,volume_panel=2,panel_ratios=(6,3,2),returnfig=True)
multi = MultiCursor(fig.canvas, axlist[0:2], horizOn=True, vertOn=True, color='pink', lw=1.2)

# ---------------------------------------------------
# set up an event loop where we wait for two
# mouse clicks, and then draw a line in between them,
# and then wait again for another two mouse clicks.

# This is a crude way to do it, but its quick and easy.
# Disadvantage is: user has 8 seconds to provide two clicks
# or the first click will be erased. But the 8 seconds
# repeats as long as the user does not close the Figure,
# so user can draw as many trend lines as they want.
# The advantage of doing it this way is we don't have
# to write all the mouse click handling stuff that's
# already written in `Figure.ginput()`.


alines = []

not_closed = True
def on_close(event):
global not_closed
not_closed = False

fig.canvas.mpl_connect('close_event', on_close)

while not_closed:

vertices = fig.ginput(n=2,timeout=8)
if len(vertices) < 2:
continue
p1 = vertices[0]
p2 = vertices[1]

d1 = df.index[ round(p1[0]) ]
d2 = df.index[ round(p2[0]) ]

alines.append( [ (d1,p1[1]), (d2,p2[1]) ] )

apds = [mpf.make_addplot(exp12,color='lime',ax=axlist[0]),
mpf.make_addplot(exp26,color='c',ax=axlist[0]),
mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,ax=axlist[2],color='dimgray',alpha=1),
mpf.make_addplot(macd,panel=1,color='fuchsia',ax=axlist[3]),
mpf.make_addplot(signal,panel=1,color='b',ax=axlist[3])
]

mpf.plot(df,ax=axlist[0],type='candle',addplot=apds,ylim=(ymin,ymax),
alines=dict(alines=alines,colors='r'),
style='blueskies',volume=axlist[4],volume_panel=2,panel_ratios=(6,3,2))

fig.canvas.draw_idle()