Skip to content

Commit 774a7f1

Browse files
committed
Testing done
1 parent 3becf41 commit 774a7f1

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

src/mplfinance/_utils.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ def _construct_mpf_collections(ptype,dates,xdates,opens,highs,lows,closes,volume
9797

9898
elif ptype =='hollow_and_filled':
9999
collections = _construct_hollow_candlestick_collections(xdates, opens, highs, lows, closes,
100-
marketcolors=style['marketcolors'],config=config )
100+
marketcolors=style['marketcolors'],config=config, colors=colors )
101101

102102
elif ptype == 'ohlc' or ptype == 'bars' or ptype == 'ohlc_bars':
103103
collections = _construct_ohlc_collections(xdates, opens, highs, lows, closes,
104-
marketcolors=style['marketcolors'],config=config )
104+
marketcolors=style['marketcolors'],config=config, colors=colors )
105105
elif ptype == 'renko':
106106
collections = _construct_renko_collections(
107107
dates, highs, lows, volumes, config['renko_params'], closes, marketcolors=style['marketcolors'])
@@ -480,7 +480,7 @@ def _valid_lines_kwargs():
480480
return vkwargs
481481

482482

483-
def _construct_ohlc_collections(dates, opens, highs, lows, closes, marketcolors=None, config=None):
483+
def _construct_ohlc_collections(dates, opens, highs, lows, closes, marketcolors=None, config=None, colors=None):
484484
"""Represent the time, open, high, low, close as a vertical line
485485
ranging from low to high. The left tick is the open and the right
486486
tick is the close.
@@ -505,8 +505,8 @@ def _construct_ohlc_collections(dates, opens, highs, lows, closes, marketcolors=
505505
ret : list
506506
a list or tuple of matplotlib collections to be added to the axes
507507
"""
508-
509-
_check_input(opens, highs, lows, closes)
508+
509+
_check_input(opens, highs, lows, closes, colors)
510510

511511
if marketcolors is None:
512512
mktcolors = _get_mpfstyle('classic')['marketcolors']['ohlc']
@@ -530,13 +530,25 @@ def _construct_ohlc_collections(dates, opens, highs, lows, closes, marketcolors=
530530
# we'll translate these to the date, close location
531531
closeSegments = [((dt, close), (dt+ticksize, close)) for dt, close in zip(dates, closes)]
532532

533-
if mktcolors['up'] == mktcolors['down']:
534-
colors = mktcolors['up']
535-
else:
536-
colorup = mcolors.to_rgba(mktcolors['up'])
537-
colordown = mcolors.to_rgba(mktcolors['down'])
538-
colord = {True: colorup, False: colordown}
539-
colors = [colord[open < close] for open, close in zip(opens, closes)]
533+
bar_c = None
534+
if colors:
535+
bar_c = []
536+
for color in colors:
537+
if color:
538+
bar_up = color['ohlc']['up']
539+
bar_down = color['ohlc']['down']
540+
if bar_up == 'k':
541+
bar_up = mktcolors['up']
542+
if bar_down == 'k':
543+
bar_down = mktcolors['down']
544+
545+
bar_c.append({'up': mcolors.to_rgba(bar_up, 1), 'down': mcolors.to_rgba(bar_down, 1)})
546+
else:
547+
bar_c.append(None)
548+
549+
uc = mcolors.to_rgba(mktcolors['up'])
550+
dc = mcolors.to_rgba(mktcolors['down'])
551+
colors = _updown_colors(uc, dc, opens, closes, colors=bar_c)
540552

541553
lw = config['_width_config']['ohlc_linewidth']
542554

@@ -623,9 +635,29 @@ def _construct_candlestick_collections(dates, opens, highs, lows, closes, market
623635
edge_c = []
624636
for color in colors:
625637
if color:
626-
candle_c.append({'up': mcolors.to_rgba(color['candle']['up'], alpha), 'down': mcolors.to_rgba(color['candle']['down'], alpha)})
627-
wick_c.append({'up': mcolors.to_rgba(color['wick']['up'], 1), 'down': mcolors.to_rgba(color['wick']['down'], 1)})
628-
edge_c.append({'up': mcolors.to_rgba(color['edge']['up'], 1), 'down': mcolors.to_rgba(color['edge']['down'], 1)})
638+
candle_up = color['candle']['up']
639+
candle_down = color['candle']['down']
640+
edge_up = color['edge']['up']
641+
edge_down = color['edge']['down']
642+
wick_up = color['wick']['up']
643+
wick_down = color['wick']['down']
644+
if candle_up == 'w':
645+
candle_up = marketcolors['candle']['up']
646+
if candle_down == 'k':
647+
candle_down = marketcolors['candle']['down']
648+
if edge_up == 'k':
649+
edge_up = candle_up
650+
if edge_down == 'k':
651+
edge_down = candle_down
652+
if wick_up == 'k':
653+
wick_up = candle_up
654+
if wick_down == 'k':
655+
wick_down = candle_down
656+
657+
candle_c.append({'up': mcolors.to_rgba(candle_up, alpha), 'down': mcolors.to_rgba(candle_down, alpha)})
658+
edge_c.append({'up': mcolors.to_rgba(edge_up, 1), 'down': mcolors.to_rgba(edge_down, 1)})
659+
wick_c.append({'up': mcolors.to_rgba(wick_up, 1), 'down': mcolors.to_rgba(wick_down, 1)})
660+
629661
else:
630662
candle_c.append(None)
631663
wick_c.append(None)
@@ -659,7 +691,7 @@ def _construct_candlestick_collections(dates, opens, highs, lows, closes, market
659691
return [rangeCollection, barCollection]
660692

661693

662-
def _construct_hollow_candlestick_collections(dates, opens, highs, lows, closes, marketcolors=None, config=None):
694+
def _construct_hollow_candlestick_collections(dates, opens, highs, lows, closes, marketcolors=None, config=None, colors=None):
663695
"""Represent today's open to close as a "bar" line (candle body)
664696
and high low range as a vertical line (candle wick)
665697

src/mplfinance/plotting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def plot( data, **kwargs ):
402402
colors = config['colors']
403403
for c in range(len(colors)):
404404
if isinstance(colors[c], str):
405-
config['colors'][c] = make_marketcolors(up=colors[c], down=colors[c], edge=colors[c], wick=colors[c])
405+
config['colors'][c] = make_marketcolors(up=colors[c], down=colors[c], edge=colors[c], wick=colors[c], ohlc=colors[c], volume=colors[c])
406406
else:
407407
config['colors'] = None
408408

0 commit comments

Comments
 (0)