策略测试员退出始终重新进入

时间:2019-03-23 13:48:12

标签: pine-script

我在策略测试器中有以下代码,当它在300止盈时自动退出时,总是会再次进入。退出触发后如何阻止它重新进入

isShort=false
isLong=false

if (goLong)
    if(isShort==false)
        strategy.entry("Long", strategy.long,100000,when=isShort?false:true)
        strategy.exit("b1","Long",profit=300)
        isShort:=true
        isLong:=false

else
    if(isLong==false)
        strategy.entry("Short", strategy.short,100000,when=isLong?false:true)
        strategy.exit("b2","Short",profit=300)
        isShort:=false
        isLong:=true

1 个答案:

答案 0 :(得分:0)

要访问变量的先前值,您需要使用历史引用运算符 []

您可以认为,对于pine脚本中的每个新栏,您的代码都会重新执行。因此,发生的情况是,每次在代码开始时都将isShortisLong设置为false。因此,如果您稍后在代码中进行更改,都没有关系。对于下一个小节,您的代码将重新执行,并且isShortisLong的值将在开始时立即获得值false

您需要做的是,只需在声明后将它们的旧值重新分配给那些变量。这样,您首先声明变量,然后将它们从previos bar / execution的最后一个值分配给它们。

isShort=false
isLong=false

isShort := nz(isShort[1], false)
isLong := nz(isLong[1], false)
相关问题