Skip to content

Commit 8e3a5cb

Browse files
save a few more experiments/examples
1 parent 01c5568 commit 8e3a5cb

File tree

3 files changed

+922
-0
lines changed

3 files changed

+922
-0
lines changed

examples/scratch_pad/lines.dill

420 Bytes
Binary file not shown.

examples/scratch_pad/macd_color_issue594.ipynb

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

0 commit comments

Comments
 (0)