@@ -97,11 +97,11 @@ def _construct_mpf_collections(ptype,dates,xdates,opens,highs,lows,closes,volume
97
97
98
98
elif ptype == 'hollow_and_filled' :
99
99
collections = _construct_hollow_candlestick_collections (xdates , opens , highs , lows , closes ,
100
- marketcolors = style ['marketcolors' ],config = config )
100
+ marketcolors = style ['marketcolors' ],config = config , colors = colors )
101
101
102
102
elif ptype == 'ohlc' or ptype == 'bars' or ptype == 'ohlc_bars' :
103
103
collections = _construct_ohlc_collections (xdates , opens , highs , lows , closes ,
104
- marketcolors = style ['marketcolors' ],config = config )
104
+ marketcolors = style ['marketcolors' ],config = config , colors = colors )
105
105
elif ptype == 'renko' :
106
106
collections = _construct_renko_collections (
107
107
dates , highs , lows , volumes , config ['renko_params' ], closes , marketcolors = style ['marketcolors' ])
@@ -480,7 +480,7 @@ def _valid_lines_kwargs():
480
480
return vkwargs
481
481
482
482
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 ):
484
484
"""Represent the time, open, high, low, close as a vertical line
485
485
ranging from low to high. The left tick is the open and the right
486
486
tick is the close.
@@ -505,8 +505,8 @@ def _construct_ohlc_collections(dates, opens, highs, lows, closes, marketcolors=
505
505
ret : list
506
506
a list or tuple of matplotlib collections to be added to the axes
507
507
"""
508
-
509
- _check_input (opens , highs , lows , closes )
508
+
509
+ _check_input (opens , highs , lows , closes , colors )
510
510
511
511
if marketcolors is None :
512
512
mktcolors = _get_mpfstyle ('classic' )['marketcolors' ]['ohlc' ]
@@ -530,13 +530,25 @@ def _construct_ohlc_collections(dates, opens, highs, lows, closes, marketcolors=
530
530
# we'll translate these to the date, close location
531
531
closeSegments = [((dt , close ), (dt + ticksize , close )) for dt , close in zip (dates , closes )]
532
532
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 )
540
552
541
553
lw = config ['_width_config' ]['ohlc_linewidth' ]
542
554
@@ -623,9 +635,29 @@ def _construct_candlestick_collections(dates, opens, highs, lows, closes, market
623
635
edge_c = []
624
636
for color in colors :
625
637
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
+
629
661
else :
630
662
candle_c .append (None )
631
663
wick_c .append (None )
@@ -659,7 +691,7 @@ def _construct_candlestick_collections(dates, opens, highs, lows, closes, market
659
691
return [rangeCollection , barCollection ]
660
692
661
693
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 ):
663
695
"""Represent today's open to close as a "bar" line (candle body)
664
696
and high low range as a vertical line (candle wick)
665
697
0 commit comments