简体   繁体   English

Pine Script bgcolor - 需要连续的颜色直到满足 2 个条件

[英]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.当我为长入场触发 2 个条件(移动平均斜率和 RSI 值)时,我试图将背景设为绿色,并在我为空头入场触发 2 个条件(移动平均斜率和 RSI 值)之前保持绿色。

The result is not keeping the desired color until the next 2 conditions are met.结果是在满足接下来的 2 个条件之前不会保持所需的颜色。 The circled areas on the chart show a red background, but should be green because 2 short conditions were not met.图表上的圆圈区域显示红色背景,但应该是绿色的,因为没有满足 2 个短路条件。

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 关键字来声明 backColor 变量,以便您可以在条形之间保留它的值,直到满足更改颜色的条件为止。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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