【问题标题】:TradingView Pine Script : check previous strategy.entry price before new entryTradingView Pine 脚本:在新入场前检查以前的策略。入场价格
【发布时间】:2018-08-19 05:26:19
【问题描述】:

有人问了类似的问题,但没有回应,我不允许添加。

Tradingview Pine script save close price at time of strategy entry

我正在尝试建立一个策略,在收盘前多次买入(金字塔式)以平均下跌,但我想检查之前的入场价格以确保它低于配置的百分比。

到目前为止我所拥有的:

lastBuy=0

if (condition)
    if (lastBuy==0)
        lastBuy=close
        strategy.entry("buy", true)
    else
        if ((close*1.01)<lastBuy)
            lastBuy=close
            strategy.entry("buy", true)

每次传递代码时,它都会将 lastBuy 重置为零,我永远无法查看之前的收盘价。如果我不设置它,我会收到未声明的错误。

提前感谢您的帮助!

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    每次传递代码时,它都会将 lastBuy 重置为零,我永远无法查看之前的收盘价。如果我不设置它,我会收到未声明的错误。

    发生这种情况是因为您的代码尝试重复声明相同的 lastBuy 变量。这样做会得到TradingView's undeclared identifier error

    要解决这种情况,首先使用= 声明您的变量。然后用:= 更新它的值。请记住,您不能对同一个变量多次使用 = 运算符。

    所以把你的代码改成:

    lastBuy = 0
    
    if (condition)
        if (lastBuy == 0)
            lastBuy := close
            strategy.entry("buy", true)
        else
            if ((close*1.01)<lastBuy)
                lastBuy := close
                strategy.entry("buy", true)
    

    【讨论】:

    • second else Fives 以下错误:lines 129:135: Return type of one of the 'if' blocks is not compatible with return type of other block(s) (void; series[integer])
    【解决方案2】:

    我如何将入场价格保存到变量中:

    bought = strategy.opentrades[0] == 1 and strategy.position_size[0] > strategy.position_size[1]
    entry_price = valuewhen(bought, open, 0)
    

    【讨论】:

    • `valuewhen 函数也指什么?
    【解决方案3】:

    这适用于入门价格。

    entryPrice = valuewhen(strategy.opentrades == 1, strategy.position_avg_price, 0)

    【讨论】:

      【解决方案4】:

      var 关键字是一个特殊的修饰符,它指示编译器 只创建和初始化变量一次。这种行为很 在变量的值必须通过 跨连续小节的脚本迭代。

      https://www.tradingview.com/pine-script-docs/en/v4/language/Expressions_declarations_and_statements.html

      你应该把“var”放在“lastBuy”之前,这样它就不会被重置。

      var lastBuy = 0
      
      if (condition)
          if (lastBuy == 0)
              lastBuy := close
              strategy.entry("buy", true)
          else
              if ((close*1.01)<lastBuy)
                  lastBuy := close
                  strategy.entry("buy", true)
      

      【讨论】:

        猜你喜欢
        • 2018-08-04
        • 1970-01-01
        • 2023-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        相关资源
        最近更新 更多