Skip to content

Commit a9f4230

Browse files
committed
_utils.py
-corrected color mapping of hollow colors. -added alpha handler function - set marketcolors sytle default to hollow -updated marketcolors handling of hollow colors
1 parent 9f47ade commit a9f4230

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/mplfinance/_utils.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def _updownhollow_colors(upcolor,downcolor,uphollow,downhollow,opens,closes):
170170
return upcolor
171171
umap = {True : upcolor, False : uphollow}
172172
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 = umap[closes[0] < opens[0]]
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
175175
return [first] + _list
176176

177177

@@ -488,6 +488,17 @@ 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+
491502
def _construct_hollow_candlestick_collections(dates, opens, highs, lows, closes, marketcolors=None, config=None):
492503
"""Represent the open, close as a bar line and high low range as a
493504
vertical line. Same as basic candlestick, but utilizes solid and hollow candlesticks
@@ -519,7 +530,7 @@ def _construct_hollow_candlestick_collections(dates, opens, highs, lows, closes,
519530
_check_input(opens, highs, lows, closes)
520531

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

525536
datalen = len(dates)
@@ -544,10 +555,13 @@ def _construct_hollow_candlestick_collections(dates, opens, highs, lows, closes,
544555

545556
alpha = marketcolors['alpha']
546557

547-
uc = mcolors.to_rgba(marketcolors['candle'][ 'up' ], alpha)
548-
dc = mcolors.to_rgba(marketcolors['candle']['down'], alpha)
549-
uh = mcolors.to_rgba(marketcolors['candle']['up_hollow'], alpha)
550-
dh = mcolors.to_rgba(marketcolors['candle']['down_hollow'], alpha)
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
551565
colors = _updownhollow_colors(uc, dc, uh, dh, opens, closes)
552566

553567
uc = mcolors.to_rgba(marketcolors['edge'][ 'up' ], 1.0)

0 commit comments

Comments
 (0)