Click here to Login





                                                   Enhanced Dynamic Position Sizing money mgmt - some issue

  0

0
Kiran
2016-02-22 13:56:53


I enhanced the Dynamic Position Sizing object to support 4 different rebalance modes - based on StdDev (current), 1/StdDev (inverse volatility), Sharpe Ratio, Sq-rt(StdDev).
Below is the code I added. The code compiles with no errors when in the Money Mgmt Compile menu .. but when i backtest, it throws a compilation error - "OnEndPeriod : Compilation error in 'OnEndPeriod':
Specified cast is not valid."

It seems to choke on this line below, as it cant cast the GetVariable object to int
int rebalance_mode = (int)Variables.GetVariable("rebalance_mode");

How do i convert this object to int?
Also, once i get this working, how do i submit this package to the community?

///////////////////////////////////////////////////////////////////////////////////////////////
//OnStartSimulation
Functions.SetNumericInput("reblance_mode", 0, "0-vol, 1- 1/vol, 2-Sharpe, 3-sqrt(vol)");
Functions.SetTextInput("period", "60", "Period");

//OnEndPeriod
string period = (string)Variables.GetVariable("period");
int rebalance_mode = (int)Variables.GetVariable("rebalance_mode");


_TradingOrder[] orders = Orders.GetPendingBuyOrders();
if(orders.Length > 1) {
double sum = 0;
for(int i=0;i < orders.Length;i++) {
_TradingOrder pos = orders[i];
TimeSeries ts = Data.ParseFormula("a = stddev(close,"+period+");").GetTimeSeries(pos.Symbol, "a"); ;
switch (rebalance_mode) {
case 0:
ts = Data.ParseFormula("a = stddev(close,"+period+");").GetTimeSeries(pos.Symbol, "a");
break;
case 1:
ts = Data.ParseFormula("a = 1/stddev(close,"+period+");").GetTimeSeries(pos.Symbol, "a");
break;
case 2:
ts = Data.ParseFormula("a = Math.Abs(sharpe(close,"+period+"));").GetTimeSeries(pos.Symbol, "a");
break;
case 3:
ts = Data.ParseFormula("a = Math.Pow(stddev(close,"+period+"),0.5);").GetTimeSeries(pos.Symbol, "a");
break;

}
sum += ts[0];
}

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

double weight = 0;
for(int i=0;i < orders.Length;i++)
{
_TradingOrder pos = orders[i];
Divers.Output("Symbol: " + pos.Symbol);
TimeSeries ts = Data.ParseFormula("a = stddev(close,"+period+");").GetTimeSeries(pos.Symbol, "a");
switch (rebalance_mode) {
case 0:
ts = Data.ParseFormula("a = stddev(close,"+period+");").GetTimeSeries(pos.Symbol, "a");
break;
case 1:
ts = Data.ParseFormula("a = 1/stddev(close,"+period+");").GetTimeSeries(pos.Symbol, "a");
break;
case 2:
ts = Data.ParseFormula("a = Math.Abs(sharpe(close,"+period+"));").GetTimeSeries(pos.Symbol, "a");
break;
case 3:
ts = Data.ParseFormula("a = Math.Pow(stddev(close,"+period+"),0.5);").GetTimeSeries(pos.Symbol, "a");
break;
//TimeSeries ts = Data.ParseFormula("a = stddev(close,"+perioda_stddev+");").GetTimeSeries(pos.Symbol, "a");
}
double temp2 = ts[0];
weight = temp2 / sum;

double cash = Portfolio.GetTotalEquity("long")*weight;
Divers.Output("Cash: "+ cash);

TimeSeries close = Data.GetPriceSeries(pos.Symbol, "close");
int numberofshares = (int)(cash/close[0]);
Divers.Output("numberofshares: "+numberofshares);

if(cash <= 0)
{
pos.CancelOrder("Cash Negatif");
}
else
{
pos.NbShares = numberofshares;
pos.SubmitChanges();
}
//Functions.AddLongPosition(pos.Symbol,numberofshares,Orders.CloseMarketOrder());
}





Dynamic Position Sizing (by clonex, uploaded several months ago)
No notes

Rate an item Rate an item Rate an item Rate an item Rate an item Number of downloads Notes Report an item

QuantShare
2016-02-23 02:16:10

  0

You should cast it to double first
int rebalance_mode = (int)(double)Variables.GetVariable("rebalance_mode");

Once working, you can upload the script using "Tools -> Sharing Server"



Kiran
2016-02-24 08:57:10

  0

Thanks,

There's another issue i'm running into - i need to calculate relative strength of Close/SPY, but the script gets confused with the extra "" in "SPY" within the formula. How do i escape the "" within the formula?

ts = Data.ParseFormula("a=Sma(close/GetSeries("SPY",close,LastData),20);").GetTimeSeries(pos.Symbol, "a");



QuantShare
2016-02-25 02:42:12

  0

Escace using "\" or better use single quotes inside the "ParseFormula".
ts = Data.ParseFormula("a=Sma(close/GetSeries('SPY',close,LastData),20);").GetTimeSeries(pos.Symbol, "a");



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

How-to Lessons
How to debug a trading system using the money management tool
How to create a volatility-based Stop - Dynamic stop based on the...
How to calculate the average of a time series using the money man...
How to add custom position-based metrics to your trading system
How to debug a downloader

Related Forum Threads
NaN error in Dynamic Position Sizing MM object
Dynamic Position Sizing
Dynamic Position
dynamic position sizing based on stocks selected by the buy rules
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
Basic trading system implemented using the money management tool
Create a trading strategy using the money management tool - Part ...
Money Management: Scale-in Trading Strategy









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.