|
Chaim6
2014-10-08 09:38:48
|
|
I am trying to write a function to be used within the OnEndPeriod Event that rebalances the sizes of existing positions.
// Rebalances each open position to the targetPct of NAV
// 2014.10.07
// Do we need the first three parameters or is there a better way to acccess these from within a function?
public void Rebalance(MMOrders Orders, MMOnEndPeriod Functions, MMData Data, double[] targetPct)
{
// Is this the correct way to calculate the Net Asset Value of the entire portfolio
double portfolioNAV = Portfolio.GetTotalEquity("long") - Portfolio.GetAvailableCash("long");
MMPosition[] openPosition = Portfolio.GetOpenPositions();
for(int i = 0; i < openPosition.Length; i++)
{
// Divers.Output(openPosition[i].Symbol + "Current: " + (openPosition[i].PositionEquity / portfolioNAV).ToString("0%") + " Target: " + targetPct[i].ToString("0%") + " ");
double targetValue = portfolioNAV * targetPct[i];
double targetShares = targetValue / Data.GetPriceSeries(openPosition[i].Symbol, "close")[0];
double nShares2Add = targetShares - openPosition[i].NbShares;
if(nShares2Add < 0)
{
// Is this the correct way to reduce a position size?
Functions.AddShortPosition(openPosition[i].Symbol, Math.Abs(nShares2Add), Orders.OpenMarketOrder());
}
else
{
Functions.AddLongPosition(openPosition[i].Symbol, nShares2Add, Orders.OpenMarketOrder());
}
}
}
It is not working. What did I do wrong? There are two areas which may need improvement; the creation of Sell/Short orders, and the calculation of Net Asset (i.e. liquidation) value. Are those correct?
P.S. Is there a need to pass the first three parameters specifically to the function of is there a simpler way to access those objects?
Thanks.
|
|
|
|
QuantShare
2014-10-09 08:13:25
0
|
|
Hi,
- Do we need the first three parameters or is there a better way to acccess these from within a function?
Yes, you need them.
- Is this the correct way to calculate the Net Asset Value of the entire portfolio
Yes, that is one way to do it. You can also use "Portfolio.GetPositionsSize" function.
You can use "Divers.Output" to debug any value and see if your calculation is correct or not.
- Is this the correct way to reduce a position size?
You either do that or use "openPosition[i].ScaleOut" function.
Problem could be because of the "targetPct" values passed to the function.
|
|
|
|
Alexander Horn
2014-10-17 14:54:59
0
|
|
Best Answer
Chaim,
on the "Is this the correct way to reduce a position size?" For backtesting purposes it actually does not matter so much, but it will once you want to automate your system through a broker...
Functions.AddShortPosition() will create a SHORT order, while openPosition[i].ScaleOut or Functions.SellPosition(..shares) will create a SELL order... if you have a long position I'd prefer openPosition[i].ScaleOut or Functions.SellPosition(..nbshares)
|
|
|
|
Chaim6
2014-10-19 23:58:07
0
|
|
Thanks QS and thank Alexander. "Functions.SellPosition(..nbshares)" was exactly what I was looking for! I will look at it some more.
|
|
|
|
Chaim6
2014-10-24 12:47:44
0
|
|
FYI regarding the NAV for a 3 ETF long only simulation at the first bar:
OnStartSimulation:
Long Positions size: 0
Long Total Equity: 100000
Long Cash: 100000
Short Position Size: 0
Short Total Equity: 0
Short Cash: 100000
OnNewPosition:
Long Positions size: 0
Long Total Equity: 100000
Long Cash: 100000
Short Position Size: 0
Short Total Equity: 0
Short Cash: 100000
OnNewPosition:
Long Positions size: 0
Long Total Equity: 100000
Long Cash: 66669.76
Short Position Size: 0
Short Total Equity: 0
Short Cash: 66669.76
OnNewPosition:
Long Positions size: 0
Long Total Equity: 100000
Long Cash: 33340.46
Short Position Size: 0
Short Total Equity: 0
Short Cash: 33340.46
OnEndPeriod:
Long Positions size: 0
Long Total Equity: 100000
Long Cash: 3.79999999998836
Short Position Size: 0
Short Total Equity: 0
Short Cash: 3.79999999998836
|
|
|
|
Chaim6
2014-10-24 12:52:10
0
|
|
NAV continued:
At the second bar:
OnEndPeriod:
Long Positions size: 100401.37
Long Total Equity: 100000
Long Cash: -401.370000000024
|
|
|
|
Chaim6
2014-10-24 13:01:07
0
|
|
NAV at the third bar:
OnEndPeriod:
Long Positions size: 101146.41
Long Total Equity: 100745.04
Long Cash: -401.370000000024
Sample debugging code:
Divers.Output("OnStartSimulation:");
Divers.Output("Long Positions Size: " + Portfolio.GetPositionsSize("long"));
Divers.Output("Long Total Equity: " + Portfolio.GetTotalEquity("long"));
Divers.Output("Long Cash: " + Portfolio.GetAvailableCash("long"));
Divers.Output("Short Position Size: " + Portfolio.GetPositionsSize("short"));
Divers.Output("Short Total Equity: " + Portfolio.GetTotalEquity("short"));
Divers.Output("Short Cash: " + Portfolio.GetAvailableCash("short"));
Divers.Output("");
|
|
|
|
Chaim6
2014-10-24 13:02:50
0
|
|
NAV takeaways:
Long Cash = Short Cash; Cash is cash.
TotalEquity = PositionSize + Cash.
To estimate portfolio NAV (i.e. the net asset liquidation value):
double portfolioNAV = Portfolio.GetTotalEquity("long") + Portfolio.GetTotalEquity("short");
A portfolio that is set to be 100% long may actually be slightly leveraged if prices move from the time of the signal until the time of the execution.
|
|
|
|
Chaim6
2014-10-24 13:24:13
0
|
|
One more important takeaway:
*Check all the numbers* with Divers.Output(). It will save time.
|
|
|
|