Pine script 尝试在if语句中保存值,以便以后重新使用

Pine script 尝试在if语句中保存值,以便以后重新使用,pine-script,Pine Script,我想: 将我的输入价格保存在变量Lowenty中 计算变量exitShort的目标价格,以使我的空头价格比进场价格低0.5% 使用exitShort作为触发器关闭我的短消息 if(strategy.position\u size>=0) 如果(关闭=0) 如果(关闭close[1]时) strategy.entry(id=“EnterShort”,long=false) 下入口:=关闭 其他的 Lowenty:=Lowenty[1] 出口短=下入口*0.005 策略关闭(id=“EnterSho

我想:

  • 将我的输入价格保存在变量
    Lowenty
  • 计算变量
    exitShort
    的目标价格,以使我的空头价格比进场价格低0.5%
  • 使用
    exitShort
    作为触发器关闭我的短消息
  • if(strategy.position\u size>=0)
    如果(关闭<关闭[1])
    strategy.entry(id=“EnterShort”,long=false)
    Lowenty=关闭
    出口短=下入口*0.005
    strategy.close(id=“EnterShort”,when=close局部范围外使用的局部变量(循环中的变量或if语句)需要事先声明,在您的情况下,您希望在
    if
    语句之前声明
    lowEntery

    lowEntery = 0.
    if (strategy.position_size >= 0)
        if (close < close[1])
            strategy.entry(id="EnterShort", long=false)
            lowEntery := close
    
    exitShort = lowEntery *0.005 
    strategy.close(id="EnterShort", when = close<= exitShort)
    
    lowenty=0。
    如果(strategy.position\u size>=0)
    如果(关闭<关闭[1])
    strategy.entry(id=“EnterShort”,long=false)
    下入口:=关闭
    出口短=下入口*0.005
    strategy.close(id=“EnterShort”,当=close=0且close>close[1]时)
    strategy.entry(id=“EnterShort”,long=false)
    下入口:=关闭
    其他的
    Lowenty:=Lowenty[1]
    出口短=下入口*0.005
    策略关闭(id=“EnterShort”,当=关闭快速回答

    entryPrice = valuewhen(strategy.position_size[0] > strategy.position_size[1], open, 0)
    

    错误消失了。谢谢。但是现在我只是输入一个firsttrade,但从未退出它。感觉就像Lowenty=0->exitShort=0,因此只要不关闭,交易就不会关闭。我稍微更改了示例:if(strategy.position\u size>=0)if(close==9800)strategy.entry(id=“EnterShort”,long=false)Lowenty:=close exitShort=Lowenty*0.005策略。close(id=“EnterShort”,when=close)我想我知道你想要做什么,我会编辑我的答案谢谢它现在可以工作了。但我不明白为什么:-(为什么添加以下代码“else Lowenty:=Lowenty[1]”会有区别因为我认为这几行是在交易处于活跃头寸或9800时执行的-一旦满足“strategy.position\u size>=0和close>close[1]”的条件,这应该不会对保存收盘价产生影响。我想我不太懂这里的一些基本内容。你能告诉我吗?
    lowEntery = 0.
    if strategy.position_size >= 0 and close > close[1]
        strategy.entry(id="EnterShort", long=false)
        lowEntery := close
    else
        lowEntery := lowEntery[1]
    
    exitShort = lowEntery*0.005
    strategy.close(id="EnterShort", when = close<= exitShort)
    
    entryPrice = valuewhen(strategy.position_size[0] > strategy.position_size[1], open, 0)