Skip to content

Commit

Permalink
style enhancements; and new styles from Chandrakant
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGoldfarb committed Jun 14, 2023
1 parent 111a0a5 commit 624d161
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 19 deletions.
193 changes: 180 additions & 13 deletions examples/scratch_pad/issues/issue#241_loop_all_styles.ipynb

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/mplfinance/_styledata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from mplfinance._styledata import binance
from mplfinance._styledata import kenan
from mplfinance._styledata import ibd
from mplfinance._styledata import binancedark
from mplfinance._styledata import tradingview

_style_names = [n for n in dir() if not n.startswith('_')]

Expand All @@ -25,19 +27,29 @@
eval(cmd)

def _validate_style(style):
# Check for mandatory style keys:
keys = ['base_mpl_style','marketcolors','mavcolors','y_on_right',
'gridcolor','gridstyle','facecolor','rc' ]
for key in keys:
if key not in style.keys():
err = f'Key "{key}" not found in style:\n\n {style}'
raise ValueError(err)

# Check for mandatory marketcolor keys:
mktckeys = ['candle','edge','wick','ohlc','volume','alpha']
for key in mktckeys:
if key not in style['marketcolors'].keys():
err = f'Key "{key}" not found in marketcolors for style:\n\n {style}'
raise ValueError(err)

# The following keys are not mandatory in the style file,
# but maybe mandatory in the code (to keep the code simpler)
# so we set default values here:
if 'vcedge' not in style['marketcolors']:
style['marketcolors']['vcedge'] = style['marketcolors']['volume']
if 'vcdopcod' not in style['marketcolors']:
style['marketcolors']['vcdopcod'] = False

#print('type(_styles)=',type(_styles))
#print('_styles=',_styles)
for s in _styles.keys():
Expand Down
27 changes: 27 additions & 0 deletions src/mplfinance/_styledata/binancedark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
style = dict(style_name = 'binancedark',
base_mpl_style= 'dark_background',
marketcolors = {'candle' : {'up': '#3dc985', 'down': '#ef4f60'},
'edge' : {'up': '#3dc985', 'down': '#ef4f60'},
'wick' : {'up': '#3dc985', 'down': '#ef4f60'},
'ohlc' : {'up': 'green', 'down': 'red'},
'volume' : {'up': '#247252', 'down': '#82333f'},
'vcedge' : {'up': '#247252', 'down': '#82333f'},
'vcdopcod' : False,
'alpha' : 1.0,
},
mavcolors = ['#ffc201','#ff10ff','#cd0468','#1f77b4',
'#ff7f0e','#2ca02c','#40e0d0'],
y_on_right = True,
gridcolor = None,
gridstyle = '--',
facecolor = None,
rc = [ ('axes.grid','True'),
('axes.grid.axis' , 'y'),
('axes.edgecolor' , '#474d56' ),
('axes.titlecolor','red'),
('figure.titlesize', 'x-large' ),
('figure.titleweight','semibold'),
('figure.facecolor', '#0a0a0a' ),
],
base_mpf_style= 'binancedark'
)
26 changes: 26 additions & 0 deletions src/mplfinance/_styledata/tradingview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
style = dict(style_name = 'tradingview',
base_mpl_style= 'fast',
marketcolors = {'candle' : {'up': '#26a69a', 'down': '#ef5350'},
'edge' : {'up': '#26a69a', 'down': '#ef5350'},
'wick' : {'up': '#26a69a', 'down': '#ef5350'},
'ohlc' : {'up': '#26a69a', 'down': '#ef5350'},
'volume' : {'up': '#26a69a', 'down': '#ef5350'},
'vcedge' : {'up': 'white' , 'down': 'white' },
'vcdopcod' : False,
'alpha' : 1.0,
'volume_alpha': 0.65,
},
mavcolors = ['#2962ff','#2962ff',],
y_on_right = True,
gridcolor = None,
gridstyle = '--',
facecolor = None,
rc = [ ('axes.grid','True'),
('axes.edgecolor' , 'grey' ),
('axes.titlecolor','red'),
('figure.titlesize', 'x-large' ),
('figure.titleweight','semibold'),
('figure.facecolor', 'white' ),
],
base_mpf_style = 'tradingview'
)
25 changes: 19 additions & 6 deletions src/mplfinance/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def _valid_plot_kwargs():
'Validator' : lambda value: isinstance(value, (list,tuple)) and len(value) == 2
and all([isinstance(v,(int,float)) for v in value])},

'volume_alpha' : { 'Default' : 1, # alpha of Volume bars
'volume_alpha' : { 'Default' : None, # alpha of Volume bars
'Description' : 'opacity for Volume bar: 0.0 (transparent) to 1.0 (opaque)',
'Validator' : lambda value: isinstance(value,(int,float)) or
all([isinstance(v,(int,float)) for v in value]) },
Expand Down Expand Up @@ -673,7 +673,8 @@ def plot( data, **kwargs ):

datalen = len(xdates)
if config['volume']:
vup,vdown = style['marketcolors']['volume'].values()
mc = style['marketcolors']
vup,vdown = mc['volume'].values()
#-- print('vup,vdown=',vup,vdown)
vcolors = _updown_colors(vup, vdown, opens, closes, use_prev_close=style['marketcolors']['vcdopcod'])
#-- print('len(vcolors),len(opens),len(closes)=',len(vcolors),len(opens),len(closes))
Expand All @@ -682,9 +683,21 @@ def plot( data, **kwargs ):
w = config['_width_config']['volume_width']
lw = config['_width_config']['volume_linewidth']

adjc = _adjust_color_brightness(vcolors,0.90)
valp = config['volume_alpha']
volumeAxes.bar(xdates,volumes,width=w,linewidth=lw,color=vcolors,ec=adjc,alpha=valp)
veup, vedown = mc['vcedge'].values()
if mc['volume'] == mc['vcedge']:
edgecolors = _adjust_color_brightness(vcolors,0.90)
elif veup != vedown:
edgecolors = _updown_colors(veup, vedown, opens, closes, use_prev_close=style['marketcolors']['vcdopcod'])
else:
edgecolors = veup

if config['volume_alpha']:
valp = config['volume_alpha']
elif 'volume_alpha' in mc:
valp = mc['volume_alpha']
else:
valp = 1.0
volumeAxes.bar(xdates,volumes,width=w,linewidth=lw,color=vcolors,ec=edgecolors,alpha=valp)
if config['volume_ylim'] is not None:
vymin = config['volume_ylim'][0]
vymax = config['volume_ylim'][1]
Expand Down Expand Up @@ -911,7 +924,7 @@ def plot( data, **kwargs ):
else:
title = config['title'] # config['title'] is a string
fig.suptitle(title,**title_kwargs)


if config['axtitle'] is not None:
axA1.set_title(config['axtitle'])
Expand Down

0 comments on commit 624d161

Please sign in to comment.