0

I have 10 condition like buy1 buy2 buy3 etc

I want to change bar color due to number of true conditions

3 condition is true bar color is red, 5 condition is true bar color is blue, 9 condition is true bar color is black

How can i code it, i tried if statements but it doest work.

1 Answer 1

0

For each buy/sell condition, create a boolean test... eg

close > close[1]  // current close is higher than the prior close

that corresponds to your condition requirement. Then sum the number of true conditions and color the bar depending your criteria... Here's a short sample to get you going. Note that the conditions are applicable to the chart's timeframe... eg if you want a different timeframe - 1D on 60 minute chart - you'll need additional code.

//@version=5
indicator("Condition Test", overlay=true)

// Code the conditions as booleans

// close is above the prior bar close
priceAdvance  = close > close[1]
// 10 period SMA is above the 20 period SMA
movAvgAdvance = ta.sma(close,10) > ta.sma(close,20)

// Condition Summation - set counter to zero before 
// adding each true condition for each bar

conditionSum = 0
conditionSum += (priceAdvance ? 1 : 0)
conditionSum += (movAvgAdvance ? 1 : 0)

// color the bar yellow if we have exactly two conditions that match
// otherwise do nothing... eg color = na if total conditions != 2
barcolor(color=(conditionSum == 2 ? color.yellow : na))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.