Big O
Big O

Reputation: 417

Pinescript Backtesting with EMAs

What is wrong with this script

here is what i want

  1. Short my position when 5 EMA crosses under 10 EMA and 10 EMA Crossesunder 150 EMA
  2. Cover when 5 EMA crosses over 10EMA

Please suggest what am i missing

Thanks


ema5 = ema(close, 5)
ema10 = ema(close, 10)
ema150 = ema(close, 150)

plot (ema5, title ="EMA5", color = color.green, linewidth = 2)
plot (ema10, title ="EMA10", color = color.red, linewidth = 2)
plot (ema150, title ="EMA150", color = color.black, linewidth = 2)

_5CrossesUnder10 = crossunder(ema5, ema10)
_10CrossesUnder150 = crossunder(ema10, ema150)
ShortCondition = _5CrossesUnder10 and _10CrossesUnder150

_5CrossesAbove10 = crossover(ema5, ema10)

// plot(close)

strategy.entry("Short", strategy.short, 1, when= ShortCondition)
strategy.close("Cover", when= _5CrossesAbove10)```

Upvotes: 0

Views: 122

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6905

The term Short is the ID of your trade.
You must use the same ID to close it.
You're trying to close a trade with ID Cover that was never opened.
See the strategy.close() function definition.

This should work:

//@version=4
strategy("My Strategy", overlay=true)
ema5 = ema(close, 5)
ema10 = ema(close, 10)
ema150 = ema(close, 150)

plot (ema5, title ="EMA5", color = color.green, linewidth = 2)
plot (ema10, title ="EMA10", color = color.red, linewidth = 2)
plot (ema150, title ="EMA150", color = color.black, linewidth = 2)

_5CrossesUnder10 = crossunder(ema5, ema10)
_10CrossesUnder150 = crossunder(ema10, ema150)
ShortCondition = _5CrossesUnder10 and _10CrossesUnder150

_5CrossesAbove10 = crossover(ema5, ema10)

// plot(close)

strategy.entry("Short", strategy.short, 1, when= ShortCondition)
strategy.close("Short", when= _5CrossesAbove10)

Upvotes: 1

Related Questions