Click here to Login





                                                   Money Management Variables/optimization

  0

0
F Mazandarany
2011-01-12 11:20:08


How does one introduce variables and their optimization in MM scripts. I want to optimize variables "b", and "c" (lines 3 and 4) in the script below. I tried using the optimize function under strategy, it did not work.

string symbolLong = "spy";
string symbolShort = "^GSPC";
string rule1 = "a = close > sma(200)*b;";
string rule2 = "a = close < sma(200)*c;";
TimeSeries buy = Data.ParseFormula(rule1).GetTimeSeries(symbolLong, "a");
TimeSeries sell = Data.ParseFormula(rule2).GetTimeSeries(symbolLong, "a");

if(buy[0] > 0)
{
if(!Portfolio.IsInPortfolio(symbolLong, true))
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("short", 0, 1, null);
Portfolio.UpdateCategorySettings("long", 100, 1, null);
Functions.AddLongPosition(symbolLong, null);
}
}
else if(sell[0] > 0)
{
if(!Portfolio.IsInPortfolio(symbolShort, false))
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("long", 0, 1, null);
Portfolio.UpdateCategorySettings("short", 100, 1, null);

Functions.AddShortPosition(symbolShort, null);
}
}
if(Variables.IsVariableExists("equity"))
{
double equity = (double)Variables.GetVariable("equity");
double roc = Math.Round(((Portfolio.Equity / equity) - 1) * 100, 4);
string text = Divers.CurrentDate.ToString() + (char)Keys.Tab + roc;
Variables.SetVariable("output", Variables.GetVariable("output") + Environment.NewLine + text);
}
else
{
Variables.SetVariable("output", "");
}

Variables.SetVariable("equity", Portfolio.Equity);



QuantShare
2011-01-13 04:21:23

  0

- You have to create a new optimizable variable in the "OnStartSimulation" event:

Optimize.OptimizeDouble("b", 1, 10, 1);

Or you can create an input and then optimize this variable in the main Simulator form:

Functions.SetNumericInput("b", 1, "Variable B");

- Then you can get the variable "b" in the "OnEndPeriod" event using the "Variables" class:

double b = (double)Variables.GetVariable("b");

- After that replace the line:
string rule1 = "a = close > sma(200)*b;";
BY
string rule1 = "a = close > sma(200)*" + b + ";";



F Mazandarany
2011-01-13 14:02:14

  0

Thanks! This was very helpful. I used the the optimize function in the "OnStartSimulation" event successfully. I was not able to get the Input Variable approach to work.This is what I did;
1. Created input variable "b" in "OnStartSimulation" event: Functions.SetNumericInput("b", 1, "Variable B");
2. called variable "b" in "OnEndPeriod" event: double b = (double)Variables.GetVariable("b");
3 Changed rule1 to: string rule1 = "a = close > sma(200)*" + b + ";";
4. Added: "optimize("b",1,1.05,0.005);" to the Strategy tab in the main Simulator form

When I run the optimization, I see the variable "b' change as expected from 1 to 1,.05 in 0.005 increments, but the output/performance remains unchanged; the MM script does not get excersized. What did I do wrong?




QuantShare
2011-01-14 05:21:05

  0

Try to change "b" by -> b.ToString().Replace(",", ".")


F Mazandarany
2011-01-14 11:38:26

  0

Thanks. This did not solve the problem.


QuantShare
2011-01-15 04:54:23

  0

Best Answer
Use "Divers.Output" function to check that the value of the "rule1" variable is correctly updated.

Maybe values between 1 and 1.05 generate the same signals for this rule.
Try to increase it to 1.2 or more.



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
Withdraw a Fixed Amount Every Month - Money Management
Correlation Filter Money Management Strategy
Investing in stocks using the Kelly criterion money management st...
Return per Bar Stop - Money Management Strategy
Averaging Down 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 add a metric in the trading system simulation report
How to dynamically change the number of positions in a portfolio?
How to generate buy/sell signals from a trading system

Related Forum Threads
Example of Money Management
Advanced Money Management
Money management - C#
Hedging Money Management
Ability to edit multiple money management scripts at once

Blog Posts
Several money management strategies in a trading system
Create a trading strategy using the money management tool - Part ...
Create a trading strategy using the money management tool - Part ...
Basic trading system implemented using the money management tool
Money Management: Optimize the scale-in 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.