代码之家  ›  专栏  ›  技术社区  ›  Ilja

检查交叉是否发生在最后10个蜡烛的时间段内

  •  0
  • Ilja  · 技术社区  · 5 年前

    我正在研究一个策略,要求我检查是否有一个特定的交叉几个蜡烛回来的时间,当我得到我的信号对当前蜡烛关闭。

    现在我基本上为每个蜡烛创建10个变量,因为我想检查10个蜡烛,看看是否发生了交叉(本例中任何类型的交叉都适用)。

    这是可行的,但是会导致一些混乱和冗长的代码,所以我想知道我是否可以创建一个“一行程序”解决方案来检查整个周期?

    0 回复  |  直到 5 年前
        1
  •  5
  •   e2e4    5 年前

    这也许会有帮助

    //@version=4
    study("Sma cross during last n candles", overlay=true)
    
    sma20 = sma(close, 20)
    sma50 = sma(close, 50)
    
    plot(sma20)
    plot(sma50)
    
    cross = cross(sma20, sma50)
    
    // Highligh cross on the chart
    bgcolor(cross ? color.red : na)
    
    // Function looking for a happened condition during lookback period
    f_somethingHappened(_cond, _lookback) =>
        bool _crossed = false
        for i = 1 to _lookback
            if _cond[i]
                _crossed := true
        _crossed
    
    // The function could be called multiple times on different conditions, that should reduce the code
    crossed10 = f_somethingHappened(cross, 10)
    
    // Highligh background if cross happened during last 10 bars
    bgcolor(crossed10 ? color.green : na)