为 pine 脚本编写一个简单的条件

问题描述

我想为 pine 脚本编写一个简单的条件,如果价格达到加号,则背景颜色将为绿色,如下图所示。但我写了它,这不是我所期望的,它只为两根蜡烛制作背景,我希望它用于所有下一支蜡烛。我怎样才能做到这一点?谢谢This is the picture

//@version=4
study(title="My Indicator",overlay=true)
// Entry Point
entryPointLong = highest(high,100)
entryPointShort = lowest(low,100)
// Entry Point Condition
entryPointLongCond = close >= entryPointLong ? color.green : na
entryPointShortCond = close <= entryPointShort ? color.red : na
// Plot
plot(entryPointLong,title="Entry Long",style=plot.style_cross,linewidth=5,show_last=1,color=color.green)
plot(entryPointShort,title="Entry Short",color=color.red)
// Background Color
bgcolor(entryPointLongCond,transp=70)
bgcolor(entryPointShortCond,transp=70)

更新代码

//@version=4
study(title="My Indicator",100)
// Stop Loss
stopLossLongInput = input(title="Long Stop Loss (%)",type=input.float,minval=0.0,step=0.1,defval=3) * 0.01
stopLossLong = entryPointLong * (1 - stopLossLongInput)
// Entry Point Condition
entryPointLongTrue = close >= entryPointLong
if entryPointLongTrue
    entryPointLong := stopLossLong
    color.green
entryPointLongCond = close >= entryPointLong ? color.green : na
entryPointShortCond = close <= entryPointShort ? color.red : na
// Plot
plot(entryPointLong,color=color.red)
plot(stopLossLong,transp=70)

解决方法

//@version=4
study("My Script",overlay=true)

CO1 = "Crossover",CO2 = "Above"

priceLevel = input(1650)
condition  = input(CO1,options=[CO1,CO2])

var bool triggered = na

if condition == CO1
    triggered := crossover(close,priceLevel)
else if condition == CO2
    triggered := close > priceLevel
else
    triggered := false

bgcolor(triggered ? color.green : na)