'How do I generate one valid condition only after the other condition is true? (Pine, Tradingview)
I want to write an ema cross strategy on Pine.
Only buy signal will be generated once ema 50 cross over ema 200 on 4hrs TF. Only sell signal will be generated once ema 50 cross under ema 200 on 4hrs TF. For valid triger signals, I want to use a lower TF ema cross as valid long/short condition.
Forexample, after ema50 crossover 200 on 4hrs, only when 12 mins ema 50 cross over ema200, long signal is triggered, crossunder on 12 mins should be filtered out.
after ema50 crossunder 200 on 4hrs, only when 12 mins ema 50 cross under ema200, short signal is triggered, crossover on 12 mins should be filtered out.
I was using secerity to pull data from different TF, and I was using barssince to determine whether is should only do longs or shorts.
I don't know where it went wrong, Please help.
///CODE
ema50 = ema(close, 50) ema200 = ema(close, 200)
plot(ema50, color=color.blue, linewidth=4) plot(ema200, color=color.yellow, linewidth=4)
ltema50 = security(syminfo.tickerid, "12", ema50) ltema200 = security(syminfo.tickerid, "12", ema200)
plot(ltema50, color=color.green, linewidth=2) plot(ltema200, color=color.red, linewidth=2)
long = crossover(ema50, ema200) short = crossunder(ema50, ema200)
ltlong = crossover(ltema50, ltema200) ltshort = crossunder(ltema50, ltema200)
bars_since_long = barssince(long) bars_since_short = barssince(short)
longcondition = ltlong and (bars_since_long >= 1) shortcondition = ltshort and (bars_since_short >= 1)
if longcondition
strategy.entry("long", strategy.long)
strategy.exit("close long","long", loss = 10000)
if shortcondition
strategy.entry("short", strategy.short)
strategy.exit("close short", "short", loss = 10000)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
