Skip to content

Commit 1fdab44

Browse files
finish hollow_and_filled tutorial; add mpf.write_style_file()
1 parent 4d0af43 commit 1fdab44

File tree

4 files changed

+126
-141
lines changed

4 files changed

+126
-141
lines changed

examples/hollow_and_filled_candles.ipynb

Lines changed: 61 additions & 140 deletions
Large diffs are not rendered by default.

examples/scratch_pad/hollow_and_filled_images.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,33 @@
6262
mpf.plot(df ,style=st3,**kwargs,savefig='hollow_black_white.jpg')
6363
mpf.plot(dfs,style=st3,**kwargs,savefig='solid_black_white.jpg')
6464

65+
##
66+
## df = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
67+
##
68+
## df.loc['2019-11-07','Open' ] = 3085
69+
## df.loc['2019-11-07','Close'] = 3092
70+
## df.loc['2019-11-07','Low' ] = 3081
71+
## df.loc['2019-11-07','High' ] = 3096
72+
##
73+
## df.loc['2019-11-08','Open' ] = 3085
74+
## df.loc['2019-11-08','Close'] = 3092
75+
## df.loc['2019-11-08','Low' ] = 3081
76+
## df.loc['2019-11-08','High' ] = 3096
77+
##
78+
##
79+
## dfs = df.copy()
80+
## dfs.loc['2019-11-07','Open' ] = 3092
81+
## dfs.loc['2019-11-07','Close'] = 3085
82+
##
83+
## dfs.loc['2019-11-08','Open' ] = 3092
84+
## dfs.loc['2019-11-08','Close'] = 3085
85+
##
86+
## neoclassic = mpf.make_mpf_style(base_mpf_style='classic',facecolor='cyan')
87+
## st = neoclassic
88+
## st = mpf.make_mpf_style(base_mpf_style='nightclouds',gridstyle='')
89+
## #st = mpf.make_mpf_style(base_mpf_style='sas',gridstyle='') #figcolor='#3C8284'
90+
## st1 = mpf.make_mpf_style(base_mpf_style='classic',gridstyle='',facecolor='#56b0b3')
91+
## #st1 = mpf.make_mpf_style(base_mpf_style='classic',gridstyle='',facecolor='#ffd6dd')
92+
## mpf.plot(df.iloc[3:8],type='hollow_candle',volume=False,style=st1,figscale=1,update_width_config=dict(candle_linewidth=2.25))
93+
## mpf.plot(dfs.iloc[3:8],type='hollow_candle',volume=False,style=st1,figscale=1,update_width_config=dict(candle_linewidth=2.25))
94+
##

src/mplfinance/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import mplfinance._mpf_warnings
22
from mplfinance.plotting import plot, make_addplot
3-
from mplfinance._styles import make_mpf_style, make_marketcolors, available_styles
3+
from mplfinance._styles import make_mpf_style, make_marketcolors
4+
from mplfinance._styles import available_styles, write_style_file
45
from mplfinance._version import __version__
56
from mplfinance._mplwraps import figure, show

src/mplfinance/_styles.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import matplotlib.pyplot as plt
22
import matplotlib.colors as mcolors
33
import copy
4+
import pprint
5+
import os.path as path
46

57
from mplfinance._arg_validators import _process_kwargs, _validate_vkwargs_dict
68
from mplfinance._styledata import _styles
@@ -271,3 +273,34 @@ def _check_and_set_mktcolor(candle,**kwarg):
271273
marketcolors.update({'alpha':config['alpha']})
272274

273275
return marketcolors
276+
277+
def write_style_file(style,filename):
278+
pp = pprint.PrettyPrinter(indent=4,sort_dicts=False,compact=True)
279+
strl = pp.pformat(style).splitlines()
280+
281+
if not isinstance(style,dict):
282+
raise TypeError('Specified style must be in `dict` format')
283+
284+
if path.exists(filename):
285+
print('"'+filename+'" exists.')
286+
answer = input(' Overwrite(Y/N)? ')
287+
a = answer.lower()
288+
if a != 'y' and a != 'yes':
289+
raise FileExistsError
290+
291+
f = open(filename,'w')
292+
f.write('style = '+strl[0].replace('{','dict(',1).replace("'","",2).replace(':',' =',1)+'\n')
293+
for line in strl[1:-1]:
294+
if "'" in line[0:5]:
295+
f.write(' '+line.replace("'","",2).replace(':',' =',1)+'\n')
296+
else:
297+
f.write(' '+line+'\n')
298+
line = strl[-1]
299+
if "'" in line[0:5]:
300+
line = line.replace("'","",2).replace(':',' =',1)[::-1]
301+
else:
302+
line = line[::-1]
303+
f.write(' '+line.replace('}',')',1)[::-1]+'\n')
304+
f.close()
305+
print('Wrote style file "'+filename+'"')
306+
return

0 commit comments

Comments
 (0)