[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.