Skip to content

Commit bda054b

Browse files
simplify type="hollow"
finish simplify `type="hollow" wick also should match up/down color for type=`hollow`
1 parent 01ee397 commit bda054b

File tree

4 files changed

+1324
-36
lines changed

4 files changed

+1324
-36
lines changed

examples/hollow_candles.ipynb

Lines changed: 1297 additions & 0 deletions
Large diffs are not rendered by default.

src/mplfinance/_styledata/hollow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
style = dict(style_name = 'hollow',
22
base_mpl_style= 'seaborn-darkgrid',
3-
marketcolors = {'candle' : {'up':(0,0,0,0), 'down':'r'},
4-
'hollow' : {'up':'k', 'down':(0,0,0,0)},
3+
marketcolors = {'candle' : {'up':'k', 'down':'r'},
54
'edge' : {'up':'k', 'down':'r'},
65
'wick' : {'up':'k', 'down':'r'},
76
'ohlc' : {'up':'k', 'down':'r'},
7+
'hollow' : 'w',
88
'volume' : {'up':'#1f77b4', 'down':'#1f77b4'},
99
'vcedge' : {'up':'#1f77b4', 'down':'#1f77b4'},
1010
'vcdopcod': False, # Volume Color is Per Price Change On Day

src/mplfinance/_styles.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def _valid_make_mpf_style_kwargs():
9494
'rc' : { 'Default' : None,
9595
'Validator' : lambda value: isinstance(value,dict) },
9696

97+
'style_name' : { 'Default' : None,
98+
'Validator' : lambda value: isinstance(value,str) },
99+
97100
}
98101
_validate_vkwargs_dict(vkwargs)
99102
return vkwargs
@@ -160,7 +163,10 @@ def _valid_make_marketcolors_kwargs():
160163
'down' : { 'Default' : None,
161164
'Validator' : lambda value: mcolors.is_color_like(value) },
162165

163-
'alpha' : { 'Default' : None,
166+
'hollow' : { 'Default' : None,
167+
'Validator' : lambda value: mcolors.is_color_like(value) },
168+
169+
'alpha' : { 'Default' : None,
164170
'Validator' : lambda value: ( isinstance(value,float) and
165171
0.0 <= value and 1.0 >= value ) },
166172

@@ -230,7 +236,6 @@ def make_marketcolors(**kwargs):
230236
candle.update(down=down)
231237
marketcolors.update(down=down)
232238

233-
234239
def _check_and_set_mktcolor(candle,**kwarg):
235240
if len(kwarg) != 1:
236241
raise ValueError('Expect only ONE kwarg')
@@ -259,6 +264,9 @@ def _check_and_set_mktcolor(candle,**kwarg):
259264
c = _check_and_set_mktcolor(candle,**kwa)
260265
marketcolors.update([(kw,c)])
261266

267+
if config['hollow'] is not None:
268+
marketcolors.update({'hollow':config['hollow']})
269+
262270
if config['alpha'] is not None:
263271
marketcolors.update({'alpha':config['alpha']})
264272

src/mplfinance/_utils.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ def _updown_colors(upcolor,downcolor,opens,closes,use_prev_close=False):
165165
return [first] + _list
166166

167167

168-
def _updownhollow_colors(upcolor,downcolor,uphollow,downhollow,opens,closes):
169-
if upcolor == downcolor == downhollow== uphollow:
168+
def _updownhollow_colors(upcolor,downcolor,hollowcolor,opens,closes):
169+
if upcolor == downcolor:
170170
return upcolor
171-
umap = {True : upcolor, False : uphollow}
172-
dmap = {True : downcolor, False : downhollow}
173-
_list = [umap[cls > opn] if cls > cls0 else dmap[cls < opn] for opn0,cls0,opn,cls in zip(opens[0:-1],closes[0:-1],opens[1:],closes[1:]) ]
174-
first = upcolor if closes[0] > opens[0] else downcolor
171+
umap = {True : hollowcolor, False : upcolor }
172+
dmap = {True : hollowcolor, False : downcolor}
173+
first = umap[closes[0] > opens[0]]
174+
_list = [ umap[cls > opn] if cls > cls0 else dmap[cls > opn] for opn0,cls0,opn,cls in zip(opens[0:-1],closes[0:-1],opens[1:],closes[1:]) ]
175175
return [first] + _list
176176

177177

@@ -488,17 +488,6 @@ def _construct_candlestick_collections(dates, opens, highs, lows, closes, market
488488
return [rangeCollection, barCollection]
489489

490490

491-
def alpha_handler(color, alpha):
492-
"""Checks the color input for an alpha value. Returns the rgba color code with lesser
493-
of existing or provided alpha
494-
"""
495-
color_code = mcolors.to_rgba(color)
496-
if color_code[-1] <= alpha:
497-
return color_code
498-
else:
499-
return mcolors.to_rgba(color, alpha)
500-
501-
502491
def _construct_hollow_candlestick_collections(dates, opens, highs, lows, closes, marketcolors=None, config=None):
503492
"""Represent the open, close as a bar line and high low range as a
504493
vertical line. Same as basic candlestick, but utilizes solid and hollow candlesticks
@@ -530,7 +519,7 @@ def _construct_hollow_candlestick_collections(dates, opens, highs, lows, closes,
530519
_check_input(opens, highs, lows, closes)
531520

532521
if marketcolors is None:
533-
marketcolors = _get_mpfstyle('hollow')['marketcolors']
522+
marketcolors = _get_mpfstyle('classic')['marketcolors']
534523
#print('default market colors:',marketcolors)
535524

536525
datalen = len(dates)
@@ -555,22 +544,16 @@ def _construct_hollow_candlestick_collections(dates, opens, highs, lows, closes,
555544

556545
alpha = marketcolors['alpha']
557546

558-
uc = alpha_handler(marketcolors['candle'][ 'up' ], alpha)
559-
dc = alpha_handler(marketcolors['candle']['down'], alpha)
560-
try:
561-
uh = alpha_handler(marketcolors['hollow']['up'], alpha)
562-
dh = alpha_handler(marketcolors['hollow']['down'], alpha)
563-
except KeyError as e:
564-
raise Exception('Improper style definition for hollow candle type plotting') from e
565-
colors = _updownhollow_colors(uc, dc, uh, dh, opens, closes)
547+
uc = mcolors.to_rgba(marketcolors['candle'][ 'up' ], alpha)
548+
dc = mcolors.to_rgba(marketcolors['candle']['down'], alpha)
549+
550+
hc = mcolors.to_rgba(marketcolors['hollow']) if 'hollow' in marketcolors else (0,0,0,0)
551+
552+
colors = _updownhollow_colors(uc, dc, hc, opens, closes) # for candle body.
566553

567-
uc = mcolors.to_rgba(marketcolors['edge'][ 'up' ], 1.0)
568-
dc = mcolors.to_rgba(marketcolors['edge']['down'], 1.0)
569-
edgecolor = _updownhollow_colors(uc, dc, uc, dc, opens, closes)
554+
edgecolor = _updown_colors(uc, dc, opens, closes, use_prev_close=True)
570555

571-
uc = mcolors.to_rgba(marketcolors['wick'][ 'up' ], 1.0)
572-
dc = mcolors.to_rgba(marketcolors['wick']['down'], 1.0)
573-
wickcolor = _updownhollow_colors(uc, dc, uc, dc, opens, closes)
556+
wickcolor = _updown_colors(uc, dc, opens, closes, use_prev_close=True)
574557

575558
lw = config['_width_config']['candle_linewidth']
576559

0 commit comments

Comments
 (0)