0

i have made the below codes to exit a strategy. however the strategy is not exiting even when conditions are met. the entry point of long is correct based on the entry conditions specified. however exit is not happening inspite of all 4 conditions being met screen shot of the same is also attached. pls advice as to how to rectify the same

if strategy.position_size > 0 
    if close[1] < sl 
        strategy.close (id="lx", qty=1, comment="sl")
    if close <= hardstop
        strategy.close (id="lx", qty=1, comment="hardstop")
    if long_exit
        strategy.close (id="lx", qty=1, comment="MA based exit")
    if macd_long_exit
        strategy.close(id="lx", qty = 1, comment="MACD reversal")

if strategy.position_size < 0 
    if close[1] > sl 
        strategy.close (id="sx", qty=1, comment="sl")
    if close >= hardstop
        strategy.close (id="sx", qty=1, comment="hardstop")
    if short_exit
        strategy.close (id="sx", qty=1, comment = "trail")
    if macd_short_exit
        strategy.close(id="sx", qty = 1, comment="MACD reversal")

enter image description here

  • i also found that the exit and entry are happening only at points where the long and short entry conditions are being met and not at designated exit points. – RAGHU RAM G Nov 07 '22 at 05:11
  • 3
    The first parameter of the `strategy.close()` function is the entry id you want to close. It looks like the your entry id is `LE` and you wrote in you code the entry id `lx`. This might be the issue? – mr_statler Nov 07 '22 at 08:03
  • THANKS SIR FOR POINTING OUT. IT INDEED WAS THE ISSUE. AND NOW ITS TAKING THE EXIT AS INTENDED. THANKS A LOT – RAGHU RAM G Nov 07 '22 at 09:56
  • you ought to close out this question – John Baron Nov 07 '22 at 16:11
  • 2
    Nice catch @mr_statler. You should convert your comment into an answer. – vitruvius Nov 07 '22 at 23:46

1 Answers1

0

The first parameter of the strategy.close() function is the entry id you want to close. It looks like the your entry id is LE and you wrote in you code the entry id lx:

if strategy.position_size > 0 
    if close[1] < sl 
        strategy.close (id="LE", qty=1, comment="sl")
    if close <= hardstop
        strategy.close (id="LE", qty=1, comment="hardstop")
    if long_exit
        strategy.close (id="LE", qty=1, comment="MA based exit")
    if macd_long_exit
        strategy.close(id="LE", qty = 1, comment="MACD reversal")
mr_statler
  • 1,913
  • 2
  • 3
  • 14