If 语句不起作用 - select tp 和 sl 输入

If statements not working - select input for tp and sl

这是我的代码:

// This source code is subject to the terms of the Mozilla Public License 2.0 at 

https://mozilla.org/MPL/2.0/
// © VSPuzzler

//@version=5
strategy("My script", overlay = true)

pt = 0
sl = 0

pip_PT = input.int(0,minval=0)
percent_PT = input.int(7, minval=0)

if (percent_PT != 0)
    pt = percent_PT * 100
else
    pt = pip_PT
    
pip_SL = input.int(0,minval=0)
percent_SL = input.int(7, minval=0)

if (percent_SL != 0)
    sl = percent_SL * 100
else
    sl = pip_SL

buy_signal = (((ta.rsi(close,7)[1]+ta.rsi(close,7)[2]+ta.rsi(close,7)[3])/3) > ta.rsi(close,7))
if bar_index < 100
    strategy.entry("buy", strategy.long, when = strategy.position_size <= 0)
strategy.exit("bracket", "buy", profit = pt, stop = sl)

当我运行这个脚本时,if 语句跳过,pt 和 sl 变量保持为 0,不随输入改变。

变量可以用 = 声明,但是当它们稍后在脚本中 re-assigned 时,您需要使用 :=。当您将脚本添加到图表时,您将看到此警告。我在这里的意思的一个例子:

pt = 0 // we use equal sign here

if (percent_PT != 0)
    pt := percent_PT * 100 // we use assignment operator here 

干杯,祝你有美好的一天