Search code examples
pine-scriptpine-script-v5

Never exits the trade


I am creating a v5 strategy. The first entry it makes on the lookback period is short, so I will use that. It enters the trade exactly correct, but never exits. There are 2 conditions that can cause an exit, 1 is that an indicator changes to a particular threshold, the other is ANY bullish candle. on the 1 trade entered in the lookback period, it enters the trade exactly at the right time, but never exits, and it should with the very next bullish candle. What am I doing wrong?

//create short condition
shortCondition = //logic here

//enter short trade
if strategy.position_size == 0 and shortCondition and barstate.isconfirmed
    strategy.entry("Short", strategy.short)

//create var to make short exit condition
var bool shortExitCondition = false

//set short exit to true if green candle or other condition happens
if strategy.position_size != 0 and //other condition happens
    shortExitCondition := true
if strategy.position_size != 0 and open < close
    shortExitCondition := true

//close trade, reset short exit condition to false
if strategy.position_size != 0 and shortExitCondition and barstate.isconfirmed
    strategy.close("Close Short")
    shortExitCondition := false

Solution

  • The first argument of strategy.close() is id.

    id (series string) A required parameter. The order identifier. It is possible to close an order by referencing its identifier.

    You are passing "Close Short" as id, which means you want to close an order with the id "Close Short". However, your entry id is "Short".

    Make it:

    strategy.close("Short")
    

    and it will be good.