Switching strategy after a date

I want to switch strategies after a certain date, for example July 1, 2025, but generate one report for the backtest in terms of actual trades, etc. As far as combining equity performance, I do this as per the suggestion below.

Combine equity curves

Using iif for buy and sell conditions is not easy in my case. This is what I tried, but I think it does not work, plus it takes a long time to run:

datetoswitch = 10000 * (2025 - 1900) + 100 * 7 + 1;

dateindex=DateNum();

for( i = 1; i < BarCount; i++ ) 
   { 
   
   if (Dateindex[i] < datetoswitch )  
 {

// AFL for old strategy

}
else
{

//AFL for new strategy
}
}

Any suggestions on how to properly switch strategy in AFL after a specific date? Thanks.

You don't need a loop for that

datetoswitch = 1250701;
dn = DateNum();

Buy_old = ...
Buy_new = ...
Sell_old = ...
Sell_new = ...

Buy = IIf( dn >= datetoswitch, Buy_new, Buy_old );
Sell = IIf( dn >= datetoswitch, Sell_new, Sell_old );
1 Like

A potential issue with this approach is that any open positions entered using Buy_old before datetoswitch will still be exited according to the sell_new rules after the datetoswitch.

Depending on your intent, this may or may not be the desired behavior.

1 Like

You might need to sell all positions at the boundary date.

... your code
Sell = Sell OR dn == datetoswitch;
1 Like

Yes, I tried something similar, but I had a typo, but I will try again. Thanks.

Thank you, Tomasz; very useful. I appreciate your support.

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