Problem with sigScaleOut, instead of Partial Exit, it Exit 100% Positon Size

Hi everyone,

I'm trying to use sigScaleOut, but instead of Partial Exit, it Exit 100% Positon Size.

Even I use code in https://www.amibroker.com/guide/h_pyramid.html or my own code below, it sill Exit 100%.

I test by compare 2 case: only use Buy/Short= sigScaleOut and only use take profit using Sell/Cover with Same Profit target. The Total profit of two cases are equal.

I dont know if I forget setting something?

I have tried any options below, but it still exit 100%.

Any suggestion is highly aprreciated.

//SetPositionSize(2, spsShares ); 
//SetPositionSize( 50, spsPercentOfPosition* ( Buy == sigScaleOut OR Short == sigScaleOut));
//SetPositionSize( 50, IIf( Short == sigScaleOut OR Buy == sigScaleOut, spsPercentOfPosition, spsNoChange ) );
//SetPositionSize( 1, IIf( Short == sigScaleOut OR Buy == sigScaleOut, spsShares, spsNoChange ) );

Full Code

Plot(MA( C, 10 ), "MA 10", colorRed);
Plot(MA( C, 50 ), "MA 50", colorBlue);

BuyE = Cross( MA( C, 10 ), MA( C, 50 ) ); 
SellE = Cross( MA( C, 50 ), MA( C, 10 ) ); 
ShortE = SellE;
CoverE = BuyE;

holdBuy = Flip(BuyE, SellE);
holdShort = Flip(ShortE, CoverE);

FirstProfitTarget = 5; // profit 
SecondProfitTarget = 10; // in point

//Because I will trade on next bar opem
LongEntryPrice = IIf(Ref(holdBuy, -1), ValueWhen(Ref(BuyE,-1) ==1, O, 1), 0);
ShortEntryPrice = IIf(Ref(holdShort, -1), ValueWhen(Ref(ShortE,-1)==1, O, 1), 0);

//TP =Take profit
BuyTP = Ref(holdBuy, -1) && C >= (LongEntryPrice + FirstProfitTarget);
ShortTP = Ref(holdShort, -1) && C  <= (ShortEntryPrice -FirstProfitTarget);
BuyTP = ExRem(BuyTP, SellE);
ShortTP = ExRem(ShortTP, CoverE);

BuyTP2 = Ref(holdBuy, -1) && C >= (LongEntryPrice + SecondProfitTarget);
ShortTP2 = Ref(holdShort, -1) && C  <= (ShortEntryPrice -SecondProfitTarget);
BuyTP2 = ExRem(BuyTP2, SellE);
ShortTP2 = ExRem(ShortTP2, CoverE);


Buy = IIf(BuyTP, sigScaleOut, BuyE); //Buy = BuyE  + BuyTP*sigScaleOut;
Short = IIf(ShortTP, sigScaleOut, ShortE); //Short = ShortE + ShortTP*sigScaleOut;
Sell = IIf(BuyTP2, 3, SellE);
Cover = IIf(ShortTP2, 3, CoverE); 

//SetPositionSize(2, spsShares ); 
//SetPositionSize( 50, spsPercentOfPosition* ( Buy == sigScaleOut OR Short == sigScaleOut));
//SetPositionSize( 50, IIf( Short == sigScaleOut OR Buy == sigScaleOut, spsPercentOfPosition, spsNoChange ) );
//SetPositionSize( 1, IIf( Short == sigScaleOut OR Buy == sigScaleOut, spsShares, spsNoChange ) );

Follow advice given here:

Thank you. Actually I already checked the log of Buy, Short, Sell, Cover and PositionSize varible. They work as expected.

For example: 1st Time Buy Trigger, Buy[i] =1 -> Buy[i+1]=0. When FirstTakeProfit trigger, Buy[j] = 99999. ( sigScaleOut ) -> Buy[j+1] =0 Again. (No duplicate signal)

Position Size: If i set SetPositionSize(2, spsShares ): PositionSize = -2002,
Only When sigScaleOut trigger it change to = -1050 (50% of the Position) and so on.

You need to be precise in writing what you did. With "log" you mean Exploration or Backtest Detailed Log? I don't see exploration code in the formulas you have sent. Again follow previous advice in a whole, not part. Run exploration. It is eye opener.

Thank you very much, I figure it out!!! Using Add Column to Explore.

Because I set Trade on Next Bar, it mean trade is delay 1 bar. However, backtester just apply delay to Buy, Sell, Short, Cover variable.

In my previous code, I need to add below line to make it work perfectly.

PositionSize = Ref(PositionSize, -1);

reference:
https://www.amibroker.com/guide/afl/settradedelays.html

=====================

Another concern is that Can we add Custom metric to Back test Detailed Log? (I found the tutorial for adding Custom metric to Trade List and Explore only)