Scaling Out of a Managed Position with SetStopLoss/SetProfitTarget

Hello everyone.
I’ve been testing a simple strategy for a few days now. I’m only processing two signals.
I set a stop loss and profit and activate a buy order for 5 contracts:

SetStopLoss(“Long1”, CalculationMode.Ticks, 100, false);
SetProfitTarget(“Long1”, CalculationMode.Ticks, 100, false);
EnterLong(5, “Long1”);

And a partial exit from a buy order of 5 contracts. For example, for 1 contract:

ExitLong(1, “PartialExit”, “Long1”);

What I’ve noticed: in some cases, everything works correctly. That is,
the open position is reduced by one contract. The stop loss and profit are also reduced by one contract, becoming equal to 4 contracts each. In most cases (and I’ve tested this dozens of times on the sim101 account),
the open position is reduced by one contract, and the stop loss and profit simply disappear from the chart.
I’m asking experienced experts to explain how to fix this. I need
both the stop loss and profit to be automatically reduced along with the open position.
Maybe I need to change something in the strategy settings in the
(State == State.SetDefaults) section or in the strategy settings on the chart?
I don’t think there are any errors in the code itself.
I’ve attached the log file.
I’m happy to answer any questions.
Thank you.
Sergey S.

Partial exits against one managed entry are fragile: when ExitLong(1, …) fills, the stop and target for ‘Long1’ have to be resized, and depending on timing the change can come out as a cancel instead. The approach NinjaTrader shows in its scale-out reference sample avoids that: split the position into separate entries, e.g. EnterLong(4, “Long1”) and EnterLong(1, “Long2”), each with its own SetStopLoss/SetProfitTarget using fromEntrySignal, and EntryHandling = UniqueEntries. To scale out, close one entry by name (ExitLong(“Long2”)) or give it a nearer target; the other entry’s brackets are untouched. Turn on TraceOrders to see why the brackets were cancelled in your current runs. Happy to help further, feel free to DM me.