Click here to Login




Money Management: Optimize the scale-in strategy

Updated on 2012-10-23





If you have read my previous blog post then you certainly know how to create a basic money management script. If this is not the case then you must first read that post, because this is the second part. Here is the link of the first post: Money Management: Scale-in Trading Strategy

In this second part, I will show you how to:

- Define an optimizable variable
- Optimize a money management variable
- Backtest several variations of a money management strategy


Different Types of Variables

In the money management tool of QuantShare, you can define two types of optimizable variables:

Numeric: This allows you to optimize a numeric value by specifying a start, end and step values. The variations begin at "Start" and then move to the next value by adding "Step" value until the "End" limit is reached.

Example:
Start: 10
End: 20
Step:5

This will generate 3 variations: 10, 15 and 20.

Text: This defines a list of variations in text format.

Example:
Yes
No


Define an Optimizable Variable

In a money management script, optimizable variables are defined in the "OnStartSimulation" event using either the "Optimize.OptimizeDouble" function or the "Optimize.OptimizeText" function.

To optimize a numeric variable:
Optimize.OptimizeDouble("Name1", 10, 20, 5); // Vary variable "Name1" from 10 to 20 with a 5 increment

To optimize a text variable:
string[] values = new string[2]; // Create an array
values[0] = "Yes"; // Set the first element of the array
values[1] = "No"; // Set the second element
Optimize.OptimizeText("Name2", values);



There is another way to optimize variables. It consists of creating an input variable (A variable that can be dynamically updated from the simulation control) then specifying optimization settings for this variable from the simulator control.

Example with a numeric variable; Add the following line in the "OnStartSimulation" event:
Functions.SetNumericInput("Name1", 10, "Description");

Save your trading system then select it the simulator manager.
Here is what you get:




To update a variable value, simply change it with its value in the editor then click on "Save Money Management Inputs".
To optimize a variable, click on the "+" icon then set the optimization parameters.


Optimize the Scale-in Strategy

Back to the scale-in trading system we have created in the previous post. Update it from the simulator manager then select "Money Management" tab. Click on "Update Script".

Even if the script code is small, we can optimize many variables here. Let me show you how to perform a basic optimization of the "Scale-In Bars" variable. This variable defines the number of bars to wait before checking the position return and performing the scale-in operation.

Steps:
- Open your trading system then go to the money management tab
- Update the script then go to the "OnStartSimulation" event
- Add the "Functions.SetNumericInput" line to create an input variable

Functions.SetNumericInput("Scale-in Bars", 2, "Scale-in position after the number of bars specified here");

- Go to the "OnEndPeriod" event then type the following line at the beginning:

double sbars = (double)Variables.GetVariable("Scale-in Bars");

// This instruction allows us to get the "Scale-in Bars" value that the user (or the optimizer) has defined.

- Now, instead of comparing the variable "pos.BarsSinceEntry" to "2", we simply replace the value "2" by "sbars":

if(pos.BarsSinceEntry == sbars)

- Done. You have just created a user-defined variable (that you can optimize later).


Let us practice again by creating the second user-defined variable. This time, we will create the "Entry %" variable, which defines the percentage of shares to enter with the first buy order. This example is a little bit more complicated because it consists of changing the formula of two events: "OnNewPosition" and "OnEndPeriod".


OnNewPosition:

- As we did with the previous variable (Scale-In Bars), we must first get the value of "Entry %" by calling the "Variables.GetVariable" function.

double perc = (double)Variables.GetVariable("Entry %");

Now, let us calculate the number of shares to buy based on the percentage of shares specified in the above line.

int halfShares = (int)(NewPosition.NbShares * (perc / 100));

// The "(int)" cast is used to change the type of the value returned by the above mathematical operation, from "double" to "int".


OnEndPeriod:

- By now, you already know that the first thing we must do is to retrieve the value of the "Entry %" variable:

double perc = (double)Variables.GetVariable("Entry %");

- Just before the "Orders.AddLongPosition" instruction, calculate the number of shares to enter by adding these lines:

double totalShares = (pos.NbShares / perc) * 100; // First, we calculate the total number of shares based on the current position size and the "Entry %" variable
double sharesScale = ((100 - perc) / 100) * totalShares; // Here we calculate the number of shares to scale-in the position



This money management script is already available in the sharing server. You can download it here:
Scale-in Trading Strategy












no comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1451 days ago

Profile Graphs
Posted 1556 days ago

QuantShare
Previous Posts

More Posts

Back







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.