How to use Applystop() to work with pyramided entryprice after ScaleIn?

I checked the forum messages but could not find a solution to my problem.

I have a strategy that uses ScaleIn logic to have multiple entries. When I open the detailed log of trades I see it works nicely: e.g. after first trade, I see 3 scale-ins and those will lead to a new average entryprice. When I do a manual check calculation, the average entry price equals the average of the 4 entry prices (each scale step has 25% of full position).

In the strategy I like to exit the position when the total scaled-in position reaches a 3% profit. For this I now use simple Applystop(stopTypeProft, stopmodePercent, 3, 1, True). What I get now is that the position will be closed at 3% gain compared to the first entry price where I want a 3% gain compared to the average entry price. Can this be done with Applystop or should I be using lower level CBT?

-QD-

I have included the sample code to replicate the problem. Running short trading signals on SPY with a 2% takeprofitstop you see that we end up with 2% change in price in case of no scale-in (Yellow highlighted). In the case of scale-in you see that it also hits a 2% MFE but the total profit is higher than 2% (Red). I like this case also to hit exactly 2% profit (at a lower % MFE as we scaled-in at higher entry prices).
Thanks for your thoughts.

  • QD -

Annotation%202020-01-07%20183019

SetOption("InitialEquity",100000); 
SetOption("AccountMargin",100); 
SetOption("MaxOpenPositions", 1 );  
SetBacktestMode(backtestRegular); 
SetOption("AllowSameBarExit", False); 
SetTradeDelays(0, 0, 0, 0); 

Buy=Sell=0;  
Short=0; // see Trade signals
Cover=RSI(5)<25; 

//--- 
// TPS calculations
//--- 

FirstScaleIn = 0.1; 
SecondScaleIn = 0.3; 
ThirdScaleIn = 0.3; 
FourthScaleIn = 0.3; 
MaxPositionSizePercent = 50;// 

BarsSinceCover = BarsSince(Cover); 
FirstEntry = C==HHV(C,5) ;

InFirstPos = Flip(FirstEntry, Cover); 
FirstTrigger = ExRem(InFirstPos, Cover); 
BarsSinceFirstTrigger = BarsSince(FirstTrigger); 
FirstTriggerPrice = IIf(BarsSinceFirstTrigger < BarsSinceCover, Ref(C,-BarsSinceFirstTrigger), 0 ); 
 
SecondEntry =  C>FirstTriggerPrice  AND InFirstPos AND Ref(InFirstPos,-1);
InSecondPos = Flip(SecondEntry, Cover); 
SecondTrigger = ExRem(InSecondPos, Cover); 
BarsSinceSecondTrigger = BarsSince(SecondTrigger); 
SecondTriggerPrice = IIf(BarsSinceSecondTrigger < BarsSinceCover, Ref(C,-BarsSinceSecondTrigger), 0); 
  
ThirdEntry =  C>SecondTriggerPrice AND InSecondPos AND Ref(InSecondPos,-1);
InThirdPos = Flip(ThirdEntry, Cover); 
ThirdTrigger = ExRem(InThirdPos, Cover); 
BarsSinceThirdTrigger = BarsSince(ThirdTrigger ); 
ThirdTriggerPrice = IIf(BarsSinceThirdTrigger < BarsSinceCover, Ref(C,-BarsSinceThirdTrigger), 0); 
 
FourthEntry =  C>ThirdTriggerPrice  AND InThirdPos AND Ref(InThirdPos,-1);
InFourthPos = Flip(FourthEntry, Cover);  
FourthTrigger = ExRem(InFourthPos, Cover); 
BarsSinceFourthTrigger = BarsSince(FourthTrigger); 
FourthTriggerPrice = IIf(BarsSinceFourthTrigger < BarsSinceCover, Ref(C,-BarsSinceFourthTrigger ), 0); 

//--- 
// Trade signals 
//--- 

if( FirstScaleIn + SecondScaleIn + ThirdScaleIn + FourthScaleIn == 1.0 ) 
{ 
  Short = IIf( FirstTrigger, 1,  
             IIf( SecondTrigger OR ThirdTrigger OR FourthTrigger, sigScaleIn, 0  ) ); 
  
  SetPositionSize(IIf( FirstTrigger, MaxPositionSizePercent*FirstScaleIn,  
                      IIf( SecondTrigger, MaxPositionSizePercent*SecondScaleIn,  
                         IIf( ThirdTrigger, MaxPositionSizePercent*ThirdScaleIn,  
                             MaxPositionSizePercent*FourthScaleIn ) ) ),  
                  IIf( Short > 0, spsPercentOfEquity, spsNoChange ) ); 
  
  Cover = ExRem(Cover,Short); 
} 

SetOption("ActivateStopsImmediately", False); 
TP=2; 
ApplyStop(stopTypeProfit,stopModePercent,TP,1,True); 
//ApplyStop(stopTypeNbar,stopModeBars,10,0); 
//SetStopPrecedence(stopTypeLoss,stopTypeProfit,stopTypeNBar, stopTypeTrailing);