0

I created an indicator and then want to use it to test in strategy but my trades are only 2 or 3 while the indicator shows so many in the chart, not sure what am i doing wrong?

strategy('AMI short', overlay = true)

ema5 = ta.ema(close,100)
rsi = ta.rsi(close, 14)
plot(ema100, linewidth = 2, color = color.yellow)


currentcrossbelow = close < low[1]
previousdetachedabove = low[1] > ema100[1]


shortSignal = currentcrossbelow and previousdetachedabove and high < high[1]
bgcolor(shortSignal ? color.new(color.red, 40) : na)

SL = 0.05
TP = 0.10


shortStop = strategy.position_avg_price*(1+SL)
shortProfit = strategy.position_avg_price*(1-TP)

if shortSignal
    strategy.entry('Short', strategy.short, 1)
if strategy.position_avg_price > 0
    strategy.exit('closeShort', stop = shortStop, limit = shortProfit)

2 Answers2

0

That's because your bgcolor will indicate a signal whenever shortSignal is true. However, that does not mean that it will enter a trade.

By default, you will only have one open position. That means, if you are already in a position, it will NOT enter a new position even though your sellSignal is true.

If you want to have multiple orders in the same direction, you should increase the pyramiding number.

You can do this either via your strategy() call, or from the properties tab.

strategy('AMI short', overlay = true, pyramiding=99)

enter image description here

vitruvius
  • 15,740
  • 3
  • 16
  • 26
0

Check out the strategy definition to begin with.
If you enter a short position this position stays open as is until it gets closed somehow (exit in your case).

Your exit condition does not get fulfilled so often.
Moreover you may want to allow pyramiding ("pyramiding (const int) The maximum number of entries allowed in the same direction. If the value is 0, only one entry order in the same direction can be opened, and additional entry orders are rejected. This setting can also be changed in the strategy's "Settings/Properties" tab. Optional. The default is 0."). But it's up to your preference.
eg. strategy("My strategy", pyramiding=3)

elod008
  • 1,227
  • 1
  • 5
  • 15
  • Thank you so much, it worked! Although i must say the strategy tester isn't very reliable but it's a good indication – user20537640 Nov 20 '22 at 19:27