I'm trying to understand why my strategy will exit at the close of the candle rather than when it actually hits my Profit Level. I've tried using the strategy close and strategy exit but I seem to be getting the same results.
My script uses a function to define the 4 stages of my trades (no position, open position, TP1 hit, trailing stop). Once I have the right condition to open an order, I use the Strategy entry function. The exit logic validates if I have an open position (stage 1) and creates the TP limit order and stop loss order. Once TP1 is hit, we move to stage 2 which brings my stop to Break Even. Finally, stage 3 is a trailing stop
getCurrentStage() =>
var stage = 0
if strategy.position_size == 0
stage := 0
if stage == 0 and strategy.position_size != 0
stage := 1
else if stage == 1 and curProfitInPts() >= tp1
stage := 2
else if stage == 2 //and curProfitInPts() >= tp1
stage := 3
stage
// ==== ENTRY LOGIC ==== //
if OpenLong and strategy.position_size == 0 //startLongTrade and EntryinSession and strategy.opentrades < 1 and ha_close[0] > ha_open[0]
strategy.entry("ENTER LONG", strategy.long)
if OpenShort and strategy.position_size == 0 //startShortTrade and EntryinSession and strategy.opentrades < 1 and ha_close[0] < ha_open[0]
strategy.entry("ENTER SHORT", strategy.short)
// === EXIT LOGIC === //
float trailOffsetLevel = na
float profitLevel = activateTrailingOnThirdStep ? calcTrailingAmountLevel(TrailingP) : calcProfitTrgtPrice(tp1)
trailOffsetLevelTmp = calcTrailingOffsetLevel(TrailingP, tp1)
float stopLevel = na
curStage = getCurrentStage()
if curStage == 1
stopLevel := calcStopLossPrice(sl)
if strategy.position_size > 0
strategy.exit("Exit", "ENTER LONG", qty_percent = 50, limit = profitLevel, comment = "50% TP") //Add TP
strategy.exit("Exit", "ENTER LONG", stop = stopLevel, comment = "SL") //SL CONDITION
else if strategy.position_size < 0
strategy.exit("Exit", "ENTER SHORT", qty_percent = 50, limit = profitLevel, comment = "50% TP")
strategy.exit("Exit", "ENTER SHORT", stop = stopLevel, comment = "SL")
else if curStage == 2
stopLevel := calcStopLossPrice(0)
if strategy.position_size > 0
strategy.close("ENTER LONG", comment = "Close 50%" ,qty_percent = 50, immediately = true)
else if strategy.position_size < 0
strategy.close("ENTER SHORT", comment = "Close 50%", qty_percent = 50, immediately = true)
else if curStage == 3
if activateTrailingOnThirdStep
stopLevel := calcStopLossPrice(-tp1)
trailOffsetLevel := trailOffsetLevelTmp
strategy.exit("Exit", stop = stopLevel, trail_points = TrailingP, trail_offset = TrailingOff, comment = "Trailing Stop")
else
stopLevel := yhat1
if strategy.position_size > 0 and CloseLong
strategy.close("ENTER LONG", comment = "Close balance")
else if strategy.position_size < 0 and CloseShort
strategy.close("ENTER SHORT", comment = "Close balance")
Close 50%
is coming from strategy.close()
.
It is a command to exit from the entry with the specified ID. If there were multiple entry orders with the same ID, all of them are exited at once. If there are no open entries with the specified ID by the moment the command is triggered, the command will not come into effect. The command uses market order. Every entry is closed by a separate market order.
As the documentation states, it uses market orders. That's why it closes your position on candle close.
You should be using strategy.exit()
instead.