0

I need to use limit orders and stops. I got a code in which only one position is opened and it does not close. what is the problem?

screenshot from Tradingview here

var inLong = false
var inShort = false

IsFlat() =>
    strategy.position_size == 0

if IsFlat()
    inLong := false
    inShort := false
else
    if  strategy.position_size < 0
        inShort := true
    if  strategy.position_size > 0
        inLong := true

if Con1 and Con2 and Con3 == true 
    buyAlert := true
if Con4 and Con5 and Con6 == true
    sellAlert := true

//Signals 
if(IsFlat() and buyAlert)
    inLong := true
    strategy.entry("Enter Long", strategy.long)    
    strategy.exit("Long Exit", "Enter Long", limit = long_tp_price, stop = long_sl_price)

if(IsFlat() and sellAlert)
    inShort := true
    strategy.entry("Enter Short", strategy.short)
    strategy.exit("Exit Short", "Enter Short", limit = short_tp_price, stop = short_sl_price)
  

....................

Alex
  • 1
  • 2
  • 1
    strategy.exit should be called in the mainline script and not beneath a condition, think of it as a command that needs to happen on each bar – John Baron Nov 01 '22 at 17:50

0 Answers0