检查最近10支蜡烛期间是否发生了交叉

问题描述

我正在制定一项策略,要求我在当前蜡烛关闭时收到信号后,及时检查是否有交叉蜡烛。

现在我基本上为每个蜡烛创建10个变量,因为我想返回10个蜡烛,看看是否发生了交叉(此示例中的任何交叉方法都可以使用)。

这行得通,但是会导致一些凌乱和冗长的代码,所以我想知道是否可以以某种方式创建“一个班轮”解决方案来在整个期间进行检查?

解决方法

这可能有帮助

//@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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...