Rotational backtest - systematic investment

Good morning,
Is there possibility to program succesive eqiuty growth together with initial equity parameter.

Eg. initial equity is 10000 and another 1000 is added every month.

I would like to check what is the impact of:

  • frequency of symbols rotation
  • frequency of cash injection.
  • amount of cash injection.

Kind regards
K.

You could do this using the custom backtester. The Cash property of the backtester object (aka bo.Cash) is both readable and writeable.

This example illustrates withdrawing cash, so adding it should be just as straightforward.
https://www.mail-archive.com/amibroker-ts@yahoogroups.com/msg00025.html

3 Likes

Thank you HelixTrader,
I’ve modified your example I think is working well (presented below).

As I understand, frequently injected cash is waiting till next buy signal.
Is there possibility to code proportionally increase of current positions (at the current price)?

SetCustomBacktestProc(""); 
qq = Month() != Ref(Month(), 1) AND (Month() == 3 OR Month() == 6 OR Month() == 9 OR Month() == 12);
Salary = 3000; 
if( Status("action") == actionPortfolio ) { 
    
   bo = GetBacktesterObject(); 
   bo.PreProcess(); 
   for( bar = 0; bar < BarCount; bar++ ) { 
      bo.ProcessTradeSignals( bar ); 
                     
      // last day of the quarter
      if (qq[ bar ] == 1 OR qq[ bar ] == -11 ) { 
          
        // current cash 
         currentCash = bo.Cash;           
        // add salary to your cash 
         bo.Cash = bo.Cash + Salary;      
      }  
   } 
   bo.PostProcess(); 
      
}
1 Like

Yes, as usual in Amibroker almost anything is possible! A lot of examples are already handily in the knowledge base too.

Here’s how to rebalance open positions.
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

Thank you, I think I have it.
It seems there are no additional commissions calculated due to rebalance, are there?

If you have defined per share commission or per value then they are added when scaling in/out.

Thank you Tomasz.
Further step is to divide trading profits and interest earnings. I've created something based on dedicated article, although I would like to add adjusted CAR for trade profit only. Issue is I don't know how to call "Days In Test" variable?

st = bo.GetPerformanceStats( 0 );
netProfit = st.GetValue( "NetProfit" );
tradeProfits = st.GetValue("WinnersTotalProfit") + st.GetValue("LosersTotalLoss");
initialCapital = st.GetValue( "InitialCapital" );
daysInTest = st.GetValue( "??DaysInTest???" );
adjAnnualizedPercReturn = 100 * ( ( (initialCapital + tradeProfits) / initialCapital) ^ ( 365 / daysInTest ) - 1);
bo.AddCustomMetric( "Trading profits", tradeProfits );
bo.AddCustomMetric( "Interest earnings", netProfit - tradeProfits );
bo.AddCustomMetric( " adjAnnualizedPercReturn", AdjAnnualizedPercReturn );

Here’s one way to calculate the number of days in the test period, using the range from date as the starting point.

dt = DateTime();	
TestStartDate = DateTimeConvert(2, Status("RangeFromDate"));
TestEndDate = LastValue(dt);
DaysInTest = DateTimeDiff(TestEndDate, TestStartDate) / 86400;  // divide by seconds in a day
2 Likes