Reputation: 33
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)
Upvotes: 2
Views: 861
Reputation: 21209
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)
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.
Upvotes: 1