Colored Candles - Assign Price Color

D

dvorakm

New member
2019 Donor
VIP
I am wanting to add instruction to a script to only color the first candle in a series.

Below you will see BULL condition will color the candles green and BEAR condition will color red. But...for example in an uptrend there will be many green candles painted and then change to red candles when trend changes. I am looking to color only the first candle (GREEN) that meets the BULL condition and only the first (RED) that meets BEAR condition. I hope I explained this sufficiently.

Code:
declare upper;
input price = close;
input TF1 = AggregationPeriod.MIN;
input TF2 = AggregationPeriod.FIVE_MIN;
input TF3 = AggregationPeriod.FIFTEEN_MIN;
input TF4 = AggregationPeriod.HOUR;
def o1 = open(period = TF1);
def c1 = close(period = TF1);
def o2 = open(period = TF2);
def c2 = close(period = TF2);
def o3 = open(period = TF3);
def c3 = close(period = TF3);
def o4 = open(period = TF4);
def c4 = close(period = TF4);
def BULL = (o1 < c1) and (o2 < c2) and (o3 < c3) and (o4 < c4);
def BEAR = (o1 > c1) and (o2 > c2) and (o3 > c3) and (o4 > c4);
DefineGlobalColor("BULL", Color.GREEN);
DefineGlobalColor("BEAR", Color.RED);
DefineGlobalColor( "default", Color.GRAY );
AssignPriceColor( if BULL then globalColor( "BULL" ) else if BEAR then globalColor( "BEAR" ) else globalColor( "default" ) );
hen globalColor( "BULL" ) else if BEAR then globalColor( "BEAR" ) else globalColor( "default" ) );
 
F

Froejank12

New member
I used lowercase letters to show what is added.

Code:
DefineGlobalColor("BULL", Color.GREEN);
DefineGlobalColor("BEAR", Color.RED);
DefineGlobalColor( "default", Color.GRAY );
AssignPriceColor( if BULL and !bull[2] and !bull[3] and !bull[4] and !bull[5] and !bull[6] and !bull[7] and !bull[8] and !bull[9] then globalColor( "BULL" ) else if BEAR and !bear[2] and !bear[3] and !bear[4] and !bear[5] and !bear[6] and !bear[7] and !bear[8] and !bear[9] then globalColor( "BEAR" ) else globalColor( "default" ) );
I also left the first most previous bar out because I was looking at an entire day of 1m candles and it looked better that way. You can add [1] back in if you are looking at a very small time interval.
 

Similar threads

Top