|  | julie detriech 2017-05-28 16:34:41
 
 
 
 |  | 
	 Hello,
 I am quite new to Quantshare and still don't fully understand the intricacies. I am trying to combine a long/short portfolio based on past returns with a
 periodic rebalancing (monthly or quarterly) but I can't figure out first how to go about rebalancing?
 - Should I sell/cover existing positions in the strategy (in which case how do I sell/cover ALL existing positions and replace them with new ones?)? Here's my strategy code:
 topperc=Comp(Roc(close,30),"Percentile")>=90;
 botperc=Comp(Roc(close,30),"Percentile")<=10;
 
 Buy=topperc;
 Short=botperc;
 
 *I am not sure how to include the rebalancing here?
 
 
 - Or should it be included in the money management part? Here is the money management script I am working with:
 
 if( Divers.CurrentDate.Day <= 7 && Divers.CurrentDate.DayOfWeek == DayOfWeek.Monday)
 {
 Functions.AddMetric("Rebalanced", 1);
 // Rebalance Monthly
 double equity = Portfolio.GetTotalEquity("long") + Portfolio.GetTotalEquity("short");
 double nb = Portfolio.GetAllowedNumberOfPositions("long") + Portfolio.GetAllowedNumberOfPositions("short");
 double expectedSize = equity / nb;
 Divers.Output("Expected Size: " + expectedSize);
 MMPosition[] positions = Portfolio.GetOpenPositions();
 for(int i=0;i < positions.Length;i++)
 {
 MMPosition pos = positions[i];
 double sizeDiff = pos.PositionEquity - expectedSize;
 int diff = (int)(sizeDiff / pos.LastPrice);
 if(diff == 0)
 {
 // Nothing to do
 }
 else
 {
 bool enterShort = (diff > 0) ? true : false;
 enterShort = (pos.IsLong) ? enterShort : !enterShort; // Revert enter if the position is short
 diff = Math.Abs(diff); // Set positive number of shares
 if(enterShort)
 {
 Divers.Output(pos.Symbol + " (Short): " + diff + " Share(s)");
 Functions.AddShortPosition(pos.Symbol, diff, Orders.OpenMarketOrder());
 }
 else
 {
 Divers.Output(pos.Symbol + " (Long): " + diff + " Share(s)");
 Functions.AddLongPosition(pos.Symbol, diff, Orders.OpenMarketOrder());
 }
 }
 }
 }
 
 
 
 |  |