Separating back-test into individual trades when scaled out to 0 shares

The code I am using to scales in and out of positions on every bar a condition is met. The code is working fine at scaling in and out the positions and is doing so at the correct places with correct values. I noticed however that when the positions are scaled out to 0 shares, the trade is still continuing instead of getting closed, as shown below:

Scaling

This makes the entire back-test look like a single open position with lots of scaled in and scaled out trades, which makes getting individual trade statistics in the report difficult. Please advise on how to close the positions when the strategy scales down to 0 shares, so that the individual trade metrics can be obtained. Entire code is below:

MA_top = EMA(H, 10);
MA_bot = EMA(L, 10);

SetTradeDelays(1, 1, 1, 1);
BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

Raw_Buy = C < MA_bot;
Raw_Sell = C > MA_top;

Buy = IIf(Raw_Buy, sigScaleIn,  IIf(Raw_Sell, sigScaleOut, False));
Sell = Short = Cover = False;

SetOption("MinShares", 1);
SetOption("InitialEquity", 100000);
SetOption("AccountMargin", 100);

SetPositionSize( 10000, spsValue );

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.