Australian (ASX) Stock Market Forum

Rebalancing code Amibroker

Joined
15 February 2017
Posts
9
Reactions
0
I have a nice monthly trading system which backtests well over 20 years I've live traded for one year. Now I am interested to see the effect of rebalancing once a year, probably at the end of May or December.

I am trying to find or write my own rebalancing code. I've got pretty vanilla requirements and I'd love to know if anyone has code to do this.

Strategy conditions:
1. If Position size is more than 5% greater than equal weight, then scale down to equal weight.
2. If Position size is more than 5% less than equal weight, then scale up to equal weight.
3. Limit rebalancing to once a year. Allow to optimize for best month of year.
4. Do not rebalance a position until it has been held for 12 months or longer.

I have found some examples but I'm struggling with them.

This example is rotational and quite frankly confuses me. Mine is not rotational.
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

Example 3 here is closer but it is a fixed 5%, not a relative 5%.
https://www.amibroker.com/guide/h_pyramid.html

Generally, I don't understand how my existing position size (equal weighting) and my buy and sell rules are supposed to not conflict with the ScaleTrade ones.

Hopefully this thread results in a nice function that others in my situation can bolt onto their strategies.
 
Solution.

I adapted the code from this example and pasted it at the end of my simple monthly buy/sell strategy.
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

Code:
// Rebalancing

// 1. If Position size reaches 5% (Optimize range 0-100%) out of line then scale  to equal weight.
// 2. Limit rebalancing to once a year in May (or optimize for best month of year).
  

AlignPC = Optimize("Align %", 0.05, 0, 1, 0.01); //Alignment percent, default 5%, optimize between 0-100%
EachPosPercent = 100/PosQty; // Define PosQty according to your strategy
TradeMonth = Month()==Optimize("Optimize Month", 5, 1, 12, 1); // Rebalance in May by default, optimize other months.
SetOption("UseCustomBacktestProc", True );

if( Status("action") == actionPortfolio )
{
  bo = GetBacktesterObject();
 
  bo.PreProcess(); // Initialize backtester
 
  for(bar=0; bar < BarCount; bar++) // Start loop
  {
   bo.ProcessTradeSignals( bar );
 
   CurEquity = bo.Equity;
  
   if(trademonth[bar] ==1) // Loop only on the month defined by TradeMonth
 
   for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() )
   {
    posval = pos.GetPositionValue();
  
    diff = posval - 0.01 * EachPosPercent * CurEquity;
    price = pos.GetPrice( bar, "O" );
      
    // rebalance if difference between desired and
    // current position value is greater than the value specified by AlignPC
    // and greater than price of single share

    if( diff != 0 AND
        abs( diff ) > (AlignPC) * CurEquity AND
        abs( diff ) > price )
    {
     bo.ScaleTrade ( bar, pos.Symbol, diff < 0, price, abs( diff ) );
    }
    
    
    
   }
  }
  bo.PostProcess(); // Finalize backtester
}
 
How does rebalancing compare to your previous backtest results?

Negligible for mine, less than one percent higher returns.

The month of year made a difference.

Probably works better for holdings with lower correlation.

I've decided not to rebalance the strategy I'm focussing on but I'm confident that I will employ it at a later stage on some other strategy.
 
Top