简体   繁体   中英

Pine Script bgcolor - Need continuous color until 2 conditions are met

I'm trying to shade the background green when I trigger 2 conditions (moving average slope and RSI value) for a long entry and keep it green until I trigger 2 conditions (moving average slope and RSI value) for a short entry.

The result is not keeping the desired color until the next 2 conditions are met. The circled areas on the chart show a red background, but should be green because 2 short conditions were not met.

Thanks in advance for any suggestions.

Image attached for reference. 显示问题的图表

//@version=5
strategy(title="Bryan Test2", overlay=true, pyramiding=5, initial_capital=10000, commission_type=strategy.commission.cash_per_order, commission_value=4, slippage=2, calc_on_every_tick=true)
     
// STEP 1:
// Inputs in the Settng Window

MAPeriod = input.int(66, "Exponential Moving Average Period", defval=66, minval=1, group="Moving Average")
RSIPeriod = input.int(11, "RSI Period", defval=11, minval=1, group="RSI")

RSIConfirmBuy = input.int(55, "RSI Buy Value", defval=55, minval=1, group="RSI")
RSIConfirmSell = input.int(34, "RSI Sell Value", defval=34, minval=1, group="RSI")

TP1Perc = input.float(3, "Take Profit 1 (%)", minval=0.0, step=0.1, defval=3, group="TP & SL") 
//TP2Perc = input.float(4, "Take Profit 2 (%)",minval=0.0, step=0.1, defval=4, group="TP & SL") 
SLPerc = input.float(1.5, "Stop Loss (%)", minval=0.0, step=0.1, defval=1.5, group="TP & SL")

TP1_Ratio = input.float(90, "TP 1 Postion Size %", defval=90, step=1, group="TP & SL", tooltip="Example: 50 closing 50% of the position once TP1 is reached")/100


// Calculate moving averages and RSI
maNow = ta.ema(close, MAPeriod)
maLast = ta.ema(close[1], MAPeriod)
myRSI = ta.rsi(close, RSIPeriod)


// What is the current signal?
longCondition1 = maNow > maLast
longCondition2 = myRSI >= RSIConfirmBuy
enterLong = longCondition1 and longCondition2


shortCondition1 = maNow < maLast
shortCondition2 = myRSI <= RSIConfirmSell
enterShort = shortCondition1 and shortCondition2


//Why Doesn't this work?
plotshape(enterLong, title=" Long ", style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white)


exitLong = enterShort
exitShort = enterLong


// Plot moving averages
plot(maNow, linewidth=3, color=maNow > maLast ? color.new(color.green,0) : color.new(color.red, 0))


// The background should be continuous, either red or green.
// Once an enterLong condition is met, the background should be green continuously until a enterShort condition is met
// then the background should be continuous red until the next enterLong signal.
// ERROR:  There are areas of the background where longCondition1 (above) is triggered and it changes the background.
//         This should not happen.

backColor = enterLong ? color.new(color.green, 90) : color.new(color.red, 90)

bgcolor(color=backColor)

You can use the var keyword to declare the backColor variable so that you can retain it's value across bars until your conditions are met for changing the color.

var color backColor = na
var color green = color.new(color.green, 90)
var color red = color.new(color.red, 90)

if enterLong
    backColor := green
if enterShort
    backColor := red

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