NewToThis
NewToThis

Reputation: 1

How to change the bar color of the biggest bodied candle for that day or during a specific session

Any help would be highly appreciated I am very new to coding

I want to determine and highlight the biggest bodied bar of the day. It must start looking at the beginning of the day and color the bar white if it is the biggest one so far.

I am struggling to figure out a way for it to make sure it is bigger than ALL the previous bars.

The code that I have come up with only works if I keep on comparing the current candle to to all the previous candles manually (in the A := if line....) This is not practical and will not work on smaller timeframes with more candles The other problem this represents is that if I for instance put in 100 candles to look back it will ALWAYS look back 100 candles so in the beginning of the day on the 15min timeframe it will look back till lets say mid day the previous day

Can anybody please help a newbie?

Current code:

1//Set values

OECop = open OECcl = close

//Determining Bull/Bear candles and the diffeence between their open and close

OECDif = if OECop < OECcl

OECcl - OECop

else

OECop - OECcl

/////////////////////////////////////////////////////////////////////

//Testing conditions - the largest candle must be bigger than ALL previous candles not just certain ones - it must be the biggest bodied candle of the day/session

var A = 0

A := if ((OECDif > OECDif[1]) and (OECDif > OECDif[2]) and (OECDif > OECDif[3]) and.....

1

else 0

OECcolor = if A > 0 color.new(color.white,10) else na

////////////////////////////////////////////////////////////////////

//Plot

barcolor((A > 0) ? OECcolor : na,title="Largest candle")

Upvotes: 0

Views: 354

Answers (1)

rumpypumpydumpy
rumpypumpydumpy

Reputation: 3813

new_day = change(time("D")) != 0
range = abs(close - open)

var float todays_largest_range = na
bool new_largest_range = false


if new_day
    todays_largest_range := range
else
    if range >= todays_largest_range
        todays_largest_range := range
        new_largest_range := true

barcolor(new_largest_range ? color.white : na)
bgcolor(new_day ? color.blue : na)

We can use the var declared variable todays_largest_range to store the day's highest range. On a new day, we reset it to the the first bar of the day's range to use as the starting comparison. Each new bar after that we compare the new bar's range to the stored value, and if it is larger it becomes the new value stored in todays_largest_range.

We can store whether a new larger range was found using the boolean new_largest_range. When it is true we can use it to set the bar color.

Upvotes: 1

Related Questions