Click here to Login





                                                   Dynamic Position Sizing

  0

0
clonex
2012-05-17 18:40:54


Hello,


I have strategy with 10 markets and maximum 5 open positions
Im trying to create MM script wich can set number of shares for every position by value of sharpe ratio of every market.

For example on 30.1.2012 i want to 5 open positions on different markets. I want to set number of shares for every position(market) by sharpe ratio of market . Markets with higher sharpe ratio will open more shares and markets with lower sharpe ratio will have smaller number of shares.

Im trying to recode %u201EHistorical trading volatility-based system to adjust trade sizes%u201C but i thing that it doesnt work correct. I simply want position sizing based on sharpe ratio.

MMParser parser = Data.ParseFormula("a = sharpe(close,60);");
double ratio = parser.GetTimeSeries(NewPosition.Symbol, "ratio")[0];

double equity = (NewPosition.IsLong) ? Portfolio.GetTotalEquity("long") : Portfolio.GetTotalEquity("short");
double nbPos = (NewPosition.IsLong) ? Portfolio.GetAllowedNumberOfPositions("long") : Portfolio.GetAllowedNumberOfPositions("short");
double size = equity / nbPos;
double NbShares = size / NewPosition.Price;


if(ratio > 0)
{
double nbShares = NbShares * ratio;
Functions.UpdateNumberOfShares((int)nbShares);
}

Is my solution correct?

Thank you



QuantShare
2012-05-18 04:59:34

  0

Hi,

In the second line, you are referencing the variable "ratio", which does not exist. You should replace "ratio" with "a".

Or you can keep "ratio" and update the formula in the first line:
ratio = sharpe(close, 60);



clonex
2012-05-18 05:44:42

  0

Hi,

sorry i was wrong. if i good understand this script adjust each position size by its sharpe ratio?.

Thank you



QuantShare
2012-05-18 05:53:07

  0

One each new position, it multiply the number of shares to buy/short by the Sharpe ratio.

If a new position to buy 50 shares is initiated and its Sharpe ratio is 2 then the portfolio will end up buying 100 shares.



clonex
2012-05-18 06:34:22

  0

OK i understand, but what I need :

I have 5 assets with sharpe ratios: 1,2; 1,1; 0,5; 0,4; 0,3. Sum is 3,5. I have 100 000 USD free equity. I need adjust each position relative to sum of sharpe ratios

example: 1,2+1,1+0,5+0,4+0,3 = 3,5 = 100%

for asset with sharpe ratio 1,2 it means that its 1,2/ 3,5 = 34% = 34 000 USD free equity for first asset
for asset with sharpe ratio 1,1 it means that its 1,1/3,5 = 31% = 31 000 USD free equity for second asset
...


Thank You



QuantShare
2012-05-21 13:12:30

  0

For this, you have to:

- Loop through each position in "OnEndPeriod" (Once a day, week or month)
- Calculate the Sharpe ratio for each position
- Calculate the weight of each position
- Calculate the number of shares to buy/sell based on the expected size and the current position size
- Call "Functions.AddLongPosition" to scale-in and "Functions.SellPosition" to scale-out



clonex
2012-05-21 20:56:20

  0

Hello,

thank you for your advice.

I succesfuly passed your steps . Here is OnEndperiod code:

MMPosition[] positions = Portfolio.GetOpenPositions();
double[] arr = new double[] {};
double sum = 0;
for(int i=0;i < positions.Length;i++)

{
MMPosition pos = positions[i];

TimeSeries ts = Data.ParseFormula("a = sharpe(close,10);").GetTimeSeries(pos.Symbol, "a");
//Divers.Output("ts:"+ts[0]);
double sharpe =ts[0];
//Divers.Output("ts:"+sharpe);


sum += ts[i];

}

Divers.Output("sum: "+sum);

double weight = 0;
for(int i=0;i < positions.Length;i++)
{
MMPosition pos = positions[i];

TimeSeries ts = Data.ParseFormula("a = sharpe(close,10);").GetTimeSeries(pos.Symbol, "a");


double sharpe =ts[0];
weight = sharpe/sum;
//Divers.Output("weight"+weight);
double cash = Portfolio.GetTotalEquity("long")*weight;
//Divers.Output("numberofcash:"+numberofcash);


Divers.Output("Cash: "+cash);
TimeSeries close = Data.ParseFormula("a = close;").GetTimeSeries(pos.Symbol, "a");
Divers.Output("Close:"+close[0]);

double numberofshares = cash/close[0];
Divers.Output("numberofshares: "+numberofshares);
Functions.AddLongPosition(pos.Symbol,numberofshares,Orders.CloseMarketOrder());
}

I think that specific values (Sharpe ratio for each position, weight of each position, Calculate the number of shares to buy/sell based on the expected size and the current position size) are calculated correctly.

But now i have final problem. When i use this MM script strategy buys after some trades galactic numbers of shares and makes big losses. In my strategy i use N bar stop via vector language.
Other problem i see is that i need to calculate values only when my strategy send Market on CLose order next day. In current setup script calculates values every day...

Thank You

Here ist exported list of my trades:
Symbol Trade Entry Exit Perf Profit Shares Pos. value Nb bars Perf/Bar MAE MFE Scale In Scale Out Exit type
XLK (open) Long 2.2.2000 0:00 5.4.2012 0:00 0,707605118 2,8649E+12 2,29156E+11 6,91363E+12 3064 0,000230942 -0,780664395 0,258517888 547 0
XLV (open) Long 2.2.2000 0:00 5.4.2012 0:00 0,425682817 3,89068E+12 3,47388E+11 1,30305E+13 3064 0,00013893 -0,326030449 0,411065726 634 0
EWH (open) Long 2.2.2000 0:00 5.4.2012 0:00 0,997867183 9,35708E+11 1,06687E+11 1,87342E+12 3064 0,000325675 -0,468405216 1,137412237 282 0
EWJ (open) Long 2.2.2000 0:00 5.4.2012 0:00 0,210704575 1,80894E+11 1,05739E+11 1,03942E+12 3064 6,87678E-05 -0,566037736 0,099622642 288 0
EWJ Long 31.1.2000 0:00 2.2.2000 0:00 0,008371385 163,13 1483 19649,75 2 0,004185693 -0,01217656 0,008371385 0 0 N-Bars Stop
EWH Long 31.1.2000 0:00 2.2.2000 0:00 0,010131712 130,4 1304 13000,88 2 0,005065856 -0,028368794 0,014184397 0 0 N-Bars Stop
XLV Long 31.1.2000 0:00 2.2.2000 0:00 0,037764933 674,24 688 18527,84 2 0,018882466 -0,008477842 0,037764933 0 0 N-Bars Stop
XLK Long 31.1.2000 0:00 2.2.2000 0:00 0,02847131 560,3 431 20239,76 2 0,014235655 -0,053000438 0,02847131 0 0 N-Bars Stop







QuantShare
2012-05-22 08:30:43

  0

Hi,

- The "sum" calculation is wrong:

It should be:
sum += sharpe;

- At the end of the script, you are adding shares to every position, which is wrong
You should verify if the number of current shares is higher than "numberofshares" or not

If it is the case the you should short (Functions.AddShortPosition) the difference (pos.NbShares - numberofshares)
If not then you should buy the difference (Functions.AddLongPosition)

- Your script is executed every day. You should track new orders in "OnNewPosition" and then execute the script in "OnEndPeriod" only when needed.

Example:

OnNewPosition:
Variables.SetVariable("Updated", Divers.CurrentDate);

OnEndPeriod: (at the top)
if(Variables.IsVariableExists("Updated"))
{
DateTime date = (DateTime)Variables.GetVariable("Updated");
if(date != Divers.CurrentDate)
{
return;
}
}
else
{
return;
}








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
Dynamic Position Sizing
Fixed Fractional Position Sizing
Volatility-based Position Sizing Method with Adjustable Parameter...
Trading System Template: Position Sizing Techniques
Fixed Risk per Trade - Position Sizing

How-to Lessons
How to create a volatility-based Stop - Dynamic stop based on the...
How to create a real-time watchlist
How to create a trading system
How to display the number of notes per stock
How to perform a basic quantitative analysis using the S&P 500

Related Forum Threads
Enhanced Dynamic Position Sizing money mgmt - some issue
NaN error in Dynamic Position Sizing MM object
dynamic position sizing based on stocks selected by the buy rules
Dynamic Position
Simulation/Back testing - Default position sizing method

Blog Posts
Dynamic Position Sizing in your Trading System
5 position sizing techniques you can use in your trading system
QuantShare Trading Software: New Features in the 3.3.2 Version
The Ultimate Guide to Create Trading Systems in QuantShare
QuantShare Trading Software: New Features in the 3.1.2 Version









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 © 2024 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.