1

let's say that you enter a long position but the price goes down and the stoploss gets triggered, instead of just closing the long trade i want the script to open a short trade. How do i do that? I tried doing it myself, but i'm too dumb, so this is what i came up with.

strategy.exit("Exit short", from_entry="short", stop=low * stoplossforSell)

and then

if strategy.position_entry_name=="Exit short"
     strategy.entry("long from short",strategy.long)

edit:

if(bhigh[1]>upper[1] and bhigh[1]>bhigh and macd<signal2 )
strategy.close("long")
strategy.entry("short",strategy.short)
strategy.exit("Exit short", from_entry="short", stop=low * stoplossforSell)
strategy.entry("Short", strategy.short, stop=low * stoplossforSell)


if(blow[1]<lower[1] and blow[1]<blow and open[1]<close[1] and macd>signal2)
strategy.close("short")
strategy.entry("long",strategy.long)
strategy.exit("Exit long", from_entry="long", stop=low * stoplossforBuy)
strategy.entry("long", strategy.long, stop=low * stoplossforSell)

edit 2:

stoplossforSell= input(defval=1.02,title="stoploss for sell")
stoplossforBuy= input(defval=0.98,title="Stoploss for buy")

if(bhigh[1]>upper[1] and bhigh[1]>bhigh and open[1]>close[1] and macd<signal2 )
    strategy.close("long")
    strategy.entry("short",strategy.short)


if (strategy.position_size < 0)
    strategy.exit("Exit short", from_entry="short", stop=low * stoplossforSell)
    strategy.entry("long", strategy.long, stop=low * stoplossforSell)
    

if(blow[1]<lower[1] and blow[1]<blow and open[1]<close[1] and macd>signal2)
    strategy.close("short")
    strategy.entry("long",strategy.long)


if (strategy.position_size > 0)
    strategy.exit("Exit long", from_entry="long", stop=low * stoplossforBuy)
    strategy.entry("short", strategy.short, stop=low * stoplossforBuy)
Kristen
  • 23
  • 6

1 Answers1

1

You need to place a short limit order at your stop loss price.

Below is a simple example where it enters a long position whenever the price closes above the SMA line. Then it places a SL exit order at 5%. It also places a short limit order at the same price.

//@version=5
strategy("My script", overlay=true)

sma_val = ta.sma(close, 20)
long_cond = ta.crossover(close, sma_val)

if (long_cond)
    strategy.entry("Long", strategy.long)

long_sl_price = strategy.position_avg_price * (1 - 0.005)

if (strategy.position_size > 0)
    strategy.exit("LE", "Long", stop=long_sl_price)
    strategy.entry("Short", strategy.short, stop=long_sl_price)

plot(sma_val, color=color.blue)
plot(long_sl_price, color=color.red)

enter image description here

Edit:

You don't want to place your exit and limit orders under the same condition that you go long. Instead, just check if you are already in a long position and then place your orders unconditionally.

You probably want to have something like below:

// Check for your long condition
if(blow[1]<lower[1] and blow[1]<blow and open[1]<close[1] and macd>signal2)
    strategy.entry("long",strategy.long)

// If you are in a long position, place your long SL and short limit entry orders
if (strategy.position_size > 0)
    strategy.exit("Exit long", from_entry="long", stop=low * stoplossforBuy)
    strategy.entry("short", strategy.short, stop=low * stoplossforBuy)

Note: I changed stoplossforSell to stoplossforBuy in your short entry function. You should use the same variables as in your long stop loss calculations.

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • i don't know why, but it doesn't work, it just reduces the amount of trades placed. what i did was first try it pretty much exactly like you showed me then it tried it my own way. `if(bhigh[1]>upper[1] and bhigh[1]>bhigh and macd – Kristen Dec 11 '22 at 18:08
  • Please edit your question and add your code there. – vitruvius Dec 11 '22 at 18:22
  • edited it, should be better now – Kristen Dec 11 '22 at 18:28
  • i can't the `if` ontop is the original long trigger (i can't use the `strategy.position_size` since the same script will be placing orders for both long and short. the first market order is the original order, on that order's stop loss i want a long to trigger. – Kristen Dec 11 '22 at 18:37
  • i added a second edit, it seems to work for long but not for short. wait what, i have managed to make it worse somehow – Kristen Dec 11 '22 at 18:45
  • Yes because if `(strategy.position_size < 0)`, you place a short exit and short limit entry order. You should be placing a short exit and long limit entry order in that case. – vitruvius Dec 11 '22 at 18:51
  • i edited the post again, it seems to be the right way around but it is triggering at the wrong time, it seems as the stop loss is flipped, short postions close below the buy price from stop loss and opposite for long orders, the stoplosses are at defval – Kristen Dec 11 '22 at 18:59