Click here to Login





                                                   How do I reduce a position size from within the Money Management

  0

0
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.



No more messages
0




Reply:

No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"










QuantShare

Trading Items
Investing in stocks using the Kelly criterion money management st...
Long strategy created with the money management tool
How far are the close prices from the support line
Correlation Filter Money Management Strategy
Return per Bar Stop - Money Management Strategy

How-to Lessons
How to calculate the average of a time series using the money man...
How to debug a trading system using the money management tool
How to download a trading item from the Sharing Server
How to generate buy/sell signals from a trading system
How to add a metric in the trading system simulation report

Related Forum Threads
Set Position Size by Ticker Within Main Strategy
Money Management script and exit options in the wizard
How can I avoid UAE prompts when launching from the QS Task Manag...
How do I use variables in Money Management Scripts?
How to update the custom variables (var1,var2,...) in a position ...

Blog Posts
Create a trading strategy using the money management tool - Part ...
Basic trading system implemented using the money management tool
How to combine long, short and custom portfolio strategies within...
Money Management: Optimize the scale-in strategy
Create a trading strategy using the money management tool - Part ...









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright 2019 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.