【问题标题】:PINE-SCRIPT: Creating a Histogram Average - 24 Day LookbackPINE-SCRIPT:创建直方图平均值 - 24 天回顾
【发布时间】:2021-11-09 23:38:13
【问题描述】:

我是用hist来表示基于MACD指标的柱状图数据。

我正在尝试创建一个买入信号,当直方图为正数且大于过去 24 天之前直方图数据的平均值时,将进行买入。

但是,直方图数据可以是正数或负数,数字是正数还是负数都无所谓,但我找不到比这更好的解决方案,但必须有。

目前我每天都取 hist * hist / 2 的倍数,然后除以整个。这似乎笨拙且不必要,但我不知道另一种方法。

任何帮助将不胜感激!

    hist         = macdDaily - signalDaily

    histGreater24 = hist[0] >   ((((hist[1]  * hist[1])  / 2) + ((hist[2]  * hist[2])  / 2) + ((hist[3]  * hist[3])  / 2) + ((hist[4]  * hist[4])  / 2) + ((hist[5]  * hist[5])  / 2) + ((hist[6]  * hist[6])  / 2) + ((hist[7]  * hist[7])  / 2) + ((hist[8]  * hist[8])  / 2) + ((hist[9]  * hist[9])  / 2) + ((hist[10] * hist[10]) / 2) + ((hist[11] * hist[11]) / 2) + ((hist[12] * hist[12]) / 2) + ((hist[13] * hist[13]) / 2) + ((hist[14] * hist[14]) / 2) + ((hist[15] * hist[15]) / 2) + ((hist[16] * hist[16]) / 2) + ((hist[17] * hist[17]) / 2) + ((hist[18] * hist[18]) / 2) + ((hist[19] * hist[19]) / 2) + ((hist[20] * hist[20]) / 2) + ((hist[21] * hist[21]) / 2) + ((hist[22] * hist[22]) / 2) + ((hist[23] * hist[23]) / 2) + ((hist[24] * hist[24]) / 2)) / 24)

【问题讨论】:

    标签: pine-script pine-script-v4


    【解决方案1】:

    sma(source,length) 函数将计算最后 'n' 个柱的平均值。

    使用sma() 函数代替您正在使用的代码,它将执行您想要的相同功能

    histGreater24 =hist>sma((hist*hist)/2,24)
    

    【讨论】:

      【解决方案2】:

      我认为您必须使用 security() 从更高的时间范围(每日)获取数据来计算 hist 的平均值,或者您可以使用不同的来源,例如 hl2 hlc3 ohlc4。 (V5) 试试这个代码 ->

      htf_src            = math.avg(number0 = ta.highest(source = hist, length = 1), number1 = ta.lowest(source = hist, length = 1))
      
      f_security(_symbol, _tf, _src) => request.security(symbol = _symbol, timeframe = _tf, expression = _src[barstate.isrealtime ? 1 : 0]) 
      
      htf_source         = f_security(syminfo.tickerid, "D", htf_src) // Here the expression hist could be replaced by hl2 hlc3 ohlc4 or any source you want to get from the "D" - Daily timeframe
      
      condition_1        = hist[1] > 0 
      condition_2        = hist[1] > htf_source
      
      signal_condition    = condition_1 and condition_2 
      
      plotchar(
       series     = signal_condition,
       title      = "HIST Condition",
       char       = "☀",
       location   = location.abovebar,
       color      = color.new(color = color.lime, transp = 0),
       size       = size.normal
       )
      
      // # ========================================================================= #
      // *   # The opposite condition  
      // *   > 
      // *   > condition_3 = hist[1] < 0
      // *   > signal_bottom_condition = condition_3 and not condition_2 
      // *   > plotchar(series = signal_bottom_condition, "Hist Down Conditoin", char = "*", location = location.abovebar, color = color.new(color = color.red, transp = 0), size = size.normal)
      // # ========================================================================= #
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        相关资源
        最近更新 更多