问题描述
我正在制定一项策略,要求我在当前蜡烛关闭时收到信号后,及时检查是否有交叉蜡烛。
现在我基本上为每个蜡烛创建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)