{"id":7,"date":"2006-03-06T15:50:38","date_gmt":"2006-03-06T20:50:38","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/2006\/03\/06\/re-balancing-open-positions\/"},"modified":"2021-01-27T09:39:54","modified_gmt":"2021-01-27T08:39:54","slug":"re-balancing-open-positions","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2006\/03\/06\/re-balancing-open-positions\/","title":{"rendered":"Re-balancing open positions"},"content":{"rendered":"

Here is an example that shows how to code rotational trading system with rebalancing. The system buys and shorts top 20 securities according to absolute value of positionscore (user definable – in this example we used 20 day rate-of-change) – each at 5% of equity then each day it rebalances existing positions to 5% if only the difference between current position value and “ideal” value is greater than 0.5% and bigger than one share.<\/p>

Note that this code sample uses Custom Backtester interface<\/a> that is documented here<\/a>.<\/p>EnableRotationalTrading<\/span>(); 

<\/span>EachPosPercent <\/span>= <\/span>5<\/span>

<\/span>PositionScore <\/span>= <\/span>ROC<\/span>( <\/span>C<\/span>, <\/span>20 <\/span>); 

<\/span>PositionSize <\/span>= -<\/span>EachPosPercent<\/span>

<\/span>SetOption<\/span>(<\/span>"WorstRankHeld"<\/span>, <\/span>40 <\/span>);
<\/span>SetOption<\/span>(<\/span>"MaxOpenPositions"<\/span>, <\/span>20 <\/span>); 

<\/span>SetOption<\/span>(<\/span>"UseCustomBacktestProc"<\/span>, <\/span>True <\/span>); 

if( <\/span>Status<\/span>(<\/span>"action"<\/span>) == <\/span>actionPortfolio <\/span>)
{
  <\/span>bo <\/span>= <\/span>GetBacktesterObject<\/span>();

  <\/span>bo<\/span>.<\/span>PreProcess<\/span>(); <\/span>\/\/ Initialize backtester

  <\/span>for(<\/span>bar<\/span>=<\/span>0<\/span>; <\/span>bar <\/span>< <\/span>BarCount<\/span>; <\/span>bar<\/span>++)
  {
   <\/span>bo<\/span>.<\/span>ProcessTradeSignals<\/span>( <\/span>bar <\/span>);
  
   <\/span>CurEquity <\/span>= <\/span>bo<\/span>.<\/span>Equity<\/span>;
  
   for( <\/span>pos <\/span>= <\/span>bo<\/span>.<\/span>GetFirstOpenPos<\/span>(); <\/span>pos<\/span>; <\/span>pos <\/span>= <\/span>bo<\/span>.<\/span>GetNextOpenPos<\/span>() )
   {
    <\/span>posval <\/span>= <\/span>pos<\/span>.<\/span>GetPositionValue<\/span>();
   
    <\/span>diff <\/span>= <\/span>posval <\/span>- <\/span>0.01 <\/span>* <\/span>EachPosPercent <\/span>* <\/span>CurEquity<\/span>;
    <\/span>price <\/span>= <\/span>pos<\/span>.<\/span>GetPrice<\/span>( <\/span>bar<\/span>, <\/span>"C" <\/span>);
   
    <\/span>\/\/ rebalance only if difference between desired and
    \/\/ current position value is greater than 0.5% of equity
    \/\/ and greater than price of single share
    <\/span>if( <\/span>diff <\/span>!= <\/span>0 <\/span>AND
        <\/span>abs<\/span>( <\/span>diff <\/span>) > <\/span>0.005 <\/span>* <\/span>CurEquity <\/span>AND
        <\/span>abs<\/span>( <\/span>diff <\/span>) > <\/span>price <\/span>)
    {
     <\/span>bo<\/span>.<\/span>ScaleTrade<\/span>( <\/span>bar<\/span>, <\/span>pos<\/span>.<\/span>Symbol<\/span>, <\/span>diff <\/span>< <\/span>0<\/span>, <\/span>price<\/span>, <\/span>abs<\/span>( <\/span>diff <\/span>) );
    }
   }
  }
  <\/span>bo<\/span>.<\/span>PostProcess<\/span>(); <\/span>\/\/ Finalize backtester
<\/span><\/code>","protected":false},"excerpt":{"rendered":"

Here is an example that shows how to code rotational trading system with rebalancing. The system buys and shorts top 20 securities according to absolute value of positionscore (user definable – in this example we used 20 day rate-of-change) – each at 5% of equity then each day it rebalances existing positions to 5% if […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[53,14,15,30],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/7"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":3,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":1565,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/7\/revisions\/1565"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}