简体   繁体   中英

Pine Script Multiple Conditions to Change Bar Colors

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM