Reputation: 358
I'm trying to learn pine script and cannot understand why the below script with IsFlat doesnt create any trade.
//@version=5
strategy("EMA Strategy", overlay=true)
ema20 = ta.ema(close,20)
ema50 = ta.ema(close,50)
ema100 = ta.ema(close,100)
ema200 = ta.ema(close,200)
d0 = close - ema20
d1 = ema20-ema50
d2 = ema50-ema100
d3 = ema100-ema200
IsFlat() =>
strategy.position_size == 0
if IsFlat() and ta.crossover(d3,0)
strategy.entry("MacdLE", strategy.long, comment="MacdLE")
if IsFlat() and ta.crossunder(d3,0)
strategy.entry("MacdSE", strategy.short, comment="MacdSE")
Upvotes: 0
Views: 286
Reputation: 21294
With this logic you will enter only once and will stay in that trade forever.
If I run your code on EURUSD 1W FXCM, the first entry is from 1981-08-16 and it is still OPEN.
if IsFlat() and ta.crossover(d3,0)
strategy.entry("MacdLE", strategy.long, comment="MacdLE")
if IsFlat() and ta.crossunder(d3,0)
strategy.entry("MacdSE", strategy.short, comment="MacdSE")
You don't have an exit condition here. And you cannot trigger the opposite direction entry because once you are in a trade IsFlat()
will never be true
.
Upvotes: 1