检查最近 10 根蜡烛期间是否发生交叉
Check if crossover occurred in the period of last 10 candles
我正在研究一种策略,当我在当前蜡烛收盘时收到信号时,要求我及时检查是否有特定的交叉几根蜡烛。
现在我基本上为每根蜡烛创建 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)
或者你可以只使用这个条件:
if ta.valuewhen(crossover(sma20, sma50), bar_index, 0) <= 10
//do whatever it is you want to do after the if condition
我正在研究一种策略,当我在当前蜡烛收盘时收到信号时,要求我及时检查是否有特定的交叉几根蜡烛。
现在我基本上为每根蜡烛创建 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)
或者你可以只使用这个条件:
if ta.valuewhen(crossover(sma20, sma50), bar_index, 0) <= 10
//do whatever it is you want to do after the if condition