【问题标题】:Pine Script : Entering after x consecutive Green Bars / Candles松树脚本:在 x 个连续的绿色条/蜡烛后进入
【发布时间】:2023-01-27 22:13:59
【问题描述】:

我试图在“X”个连续柱后输入策略。

greenCandle = barstate.isconfirmed and (close > open)
G = input(11, minval=1)

strategy.entry("buy", true, 1, when = greenCandle[G] and close[0]>open[0])

这让我在果岭后 11 个小节进入,但在连续 11 个果岭后没有给我进入。

【问题讨论】:

    标签: pine-script tradingview-api


    【解决方案1】:

    计算连续事件的最简单方法是使用math.sum()

    math.sum(source, length) → series float

    传递your_condition ? 1 : 0,这样如果您的条件是true,它会在计数中加 1,否则加 0。然后将此总和与您想要的目标计数进行比较。

    //@version=5
    strategy("My script", overlay=true, process_orders_on_close=true)
    
    in_cnt_long = input.int(5, "Consecutive long event count")
    in_cnt_short = input.int(3, "Consecutive short event count")
    
    is_green_candle = (close >= open)
    is_red_candle = (not is_green_candle)
    
    green_candle_cons_cnt = math.sum(is_green_candle ? 1 : 0, in_cnt_long)  // Sum the number of candles where is_green_candle was true over the lookback period 
    cons_green_cond = (green_candle_cons_cnt == in_cnt_long)                // Check if the consecutive count 
    
    red_candle_cons_cnt = math.sum(is_red_candle ? 1 : 0, in_cnt_short)     // Sum the number of candles where is_red_candle was true over the lookback period 
    cons_red_cond = (red_candle_cons_cnt == in_cnt_short)                   // Check if the consecutive count 
    
    if (cons_green_cond)
        strategy.entry("Long", strategy.long)
    
    if (cons_red_cond)
        strategy.entry("Short", strategy.short)
    

    【讨论】:

      【解决方案2】:

      我会这样编码:

      //@version=5
      x = input.int(11)
      red = close < open                       //red bar condition
      count = ta.barssince(red)                //count of bars since red
      
      if barstate.isconfirmed and count == x   //evaluates whether barstate is confirmed and count is equal to the given x
          strategy.entry("Long", strategy.long) //Enter Long
      
      

      【讨论】:

      • 谢谢,我尝试了下面的方法,但出现了错误:“不匹配的输入‘strategy.entry’期待‘没有行继续的行结束’。”代码://@version=5 strategy("GHa3", overlay=true) x = input.int(11) red = close < open //red bar condition count = ta.barssince(red) //以来的柱数red if barstate.isconfirmed and count == x //评估barstate是否已确认且计数是否等于给定的x strategy.entry("Long", strategy.long) //输入Long
      • 这对我来说很难用文字解释,所以看看这个picture...问题是空格数。
      • 我编辑了上面的代码,所以你应该可以复制它,它应该可以立即工作
      • 谢谢,非常感谢您的帮助。它现在带有错误“找不到函数或函数引用‘input.int’。”
      • 哦,好吧,那你不是在第 5 版的 pine 脚本上。只需使用input()
      猜你喜欢
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      相关资源
      最近更新 更多