0

While setting up a strategy with previous values, close and stop triggers are not working for short order, but they are working for long order.

I'm writing a script to trade if the previous price bar is fully above or below the 5 ema, with stop losses based on the previous extreme values in a 1:3 ratio.

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)

var x = 0.0
var y = 0.0
buycon = (ta.ema(close, 5) > low[1]) and (ta.ema(close, 5) > high[1]) and ( close > high[1]) and (ta.ema(close, 5) > close )

selcon = (ta.ema(close, 5) < low[1]) and (ta.ema(close, 5) < high[1]) and( close < low[1]) and (ta.ema(close, 5) < close )

if (buycon)
    strategy.entry("My Long Entry Id", strategy.long, stop = low[1])
    x := (close - low[1])*3

if (selcon)
    strategy.entry("My Short Entry Id", strategy.short,stop = high[1] )
    y := (high[1] - close)*3 

plot(ta.ema(close, 5), title = '5 ta.ema(close, 5)', color = color.white)

if (close > x)
    strategy.close("My Long Entry Id", comment = "Sell Long")
    
if (close < y )
    strategy.close("My Short Entry Id", comment = "Buy Short")
    

    

enter image description here

As you can see, "My Short Entry ID -1" should have triggered the stop price, but it didn't. They do not appear to be executed in accordance with the strategy, as is the case with many other short trades.

Sewatech
  • 13
  • 3
  • 1
    That's because `y` is extremely low in some cases. Use `plot(y)` and check it for yourself. You might to change the logic a bit – mr_statler Nov 24 '22 at 22:14

0 Answers0