Pine Script bgcolor - 需要连续的颜色直到满足 2 个条件
Pine Script bgcolor - Need continuous color until 2 conditions are met
当我为长入场触发 2 个条件(移动平均斜率和 RSI 值)时,我试图将背景设为绿色并保持绿色,直到我为一个长入场触发 2 个条件(移动平均斜率和 RSI 值)简短条目。
结果是在满足接下来的 2 个条件之前不会保持所需的颜色。
图表上的圆圈区域显示为红色背景,但应为绿色,因为未满足 2 个短路条件。
提前感谢您的任何建议。
附上图片以供参考。
//@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)
您可以使用 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
当我为长入场触发 2 个条件(移动平均斜率和 RSI 值)时,我试图将背景设为绿色并保持绿色,直到我为一个长入场触发 2 个条件(移动平均斜率和 RSI 值)简短条目。
结果是在满足接下来的 2 个条件之前不会保持所需的颜色。 图表上的圆圈区域显示为红色背景,但应为绿色,因为未满足 2 个短路条件。
提前感谢您的任何建议。
附上图片以供参考。
//@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)
您可以使用 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