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

saving a few examples and experiments into the repository #599

Merged
merged 5 commits into from
Mar 14, 2023
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
21 changes: 21 additions & 0 deletions examples/mpf_demo_autoclose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pandas as pd
import mplfinance as mpf

import time
import matplotlib.pyplot as plt

infile = 'data/yahoofinance-SPY-20200901-20210113.csv'

df = pd.read_csv(infile, index_col=0, parse_dates=True)

# print('len(df)=',len(df))


for jj in (0,1,2):
start = jj*35
stop = start + 35
tdf = df.iloc[start:stop]
fig,_ = mpf.plot(tdf,type='candle',volume=True,mav=(10,20),figscale=1.5,returnfig=True)
plt.pause(4)
plt.close(fig)
del fig
28 changes: 28 additions & 0 deletions examples/mpf_demo_axlabelsize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pandas as pd
import mplfinance as mpf

infile = 'data/yahoofinance-SPY-20200901-20210113.csv'

df = pd.read_csv(infile, index_col=0, parse_dates=True).iloc[0:60]

# mpf.plot(df,figscale=1.5,type='candle',mav=(10,20))


#mpf.plot(df,type='candle',figscale=1.5)

#df = pd.read_csv(infile, index_col=0, parse_dates=True).iloc[0:180]
#mpf.plot(df,type='renko',figscale=1.5)
#mpf.plot(df,type='pnf',figscale=1.5)

#mpf.plot(df,type='candle',figscale=1.5,mav=10)

mystyle=mpf.make_mpf_style(base_mpf_style='yahoo',rc={'axes.labelsize':'small'})
mpf.plot(df,type='candle',volume=True,mav=(10,20),figscale=1.5,style=mystyle)

mystyle=mpf.make_mpf_style(base_mpf_style='yahoo',rc={'axes.labelsize':'medium'})
mpf.plot(df,type='candle',volume=True,mav=(10,20),figscale=1.5,style=mystyle)

mystyle=mpf.make_mpf_style(base_mpf_style='yahoo',rc={'axes.labelsize':18})
mpf.plot(df,type='candle',volume=True,mav=(10,20),figscale=1.5,style=mystyle)


Binary file added examples/scratch_pad/lines.dill
Binary file not shown.
824 changes: 824 additions & 0 deletions examples/scratch_pad/macd_color_issue594.ipynb

Large diffs are not rendered by default.

444 changes: 444 additions & 0 deletions examples/scratch_pad/mpl.zorderaxes.ipynb

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions examples/scratch_pad/mpl.zorderbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_alpha(0.0)

ax0 = fig.add_axes( [0.1,0.1,0.8,0.8] )
ax1 = fig.add_axes( [0.1,0.1,0.8,0.8] )

print('ax0 zorder=',ax0.get_zorder())
print('ax1 zorder=',ax1.get_zorder())

ax0.set_zorder(0.1)

X = [0,1,2,3,4]

Y0 = [2,4,3,5,3.5]
Y1 = [4600,4400,4800,4800,5000]


ax0.plot(X, Y0, linewidth=5, color='lime')
ax1.plot(X, Y1, linewidth=5, color='magenta')

# ax1.set_zorder(1)
# ax0.set_zorder(2)
# ax0.patch.set_visible(False)

plt.show()
98 changes: 98 additions & 0 deletions examples/scratch_pad/multicursor_macd_ginput_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import pandas as pd
import mplfinance as mpf
import dill
import os
from matplotlib.widgets import MultiCursor

# 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:
if os.path.exists('lines.dill'):
alines = dill.load(open('lines.dill','rb'))
fig, axlist = mpf.plot(df,type='candle',addplot=apds,figscale=1.25,figratio=(8,6),title='\nMACD', ylim=(ymin,ymax),
alines=dict(alines=alines,colors='r'),
style='blueskies',volume=True,volume_panel=2,panel_ratios=(6,3,2),returnfig=True)
else:
alines = []
fig, axlist = mpf.plot(df,type='candle',addplot=apds,figscale=1.25,figratio=(8,6),title='\nMACD', ylim=(ymin,ymax),
alines=dict(alines=alines,colors='r'),
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)

fig.canvas.draw_idle()

# ---------------------------------------------------
# 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()`.


not_closed = True
def on_close(event):
global not_closed
global alines
dill.dump(alines, open('lines.dill','wb'))
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()

10 changes: 10 additions & 0 deletions examples/scratch_pad/simple_matplotlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import matplotlib.pyplot as plt
import random

fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
x = [x for x in range(0,50)]
y = [random.randint(10,30) for y in range(0,50)]
ax.bar(x,y)

plt.show()
24 changes: 24 additions & 0 deletions examples/scratch_pad/time_to_plot_line_vs_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import matplotlib.pyplot as plt
import timeit
import random

def pbar():
fig = plt.figure(figsize=(5,2))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
x = [x for x in range(0,150)]
y = [random.randint(10,30) for y in range(0,150)]
ax.bar(x,y)

def pline():
fig = plt.figure(figsize=(5,2))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
x = [x for x in range(0,150)]
y = [random.randint(10,30) for y in range(0,150)]
ax.plot(x,y)

timeline = timeit.timeit(pline,number=5)
timebar = timeit.timeit(pbar,number=5)
print('timeline=',timeline)
print('timebar =',timebar)
print('\ntimebar/timeline=',timebar/timeline)

Loading