Tutorial: Finding A Profitable System With The Built-In Optimizer

Automated Trading Systems

Tutorial: Finding A Profitable System With The Built-In Optimizer


asimon 07-12-2006, 1:38 PM

This tutorial will explain in detail how to use the optimizer built into the Forex Lab software to find a profitable trading system. You will learn how to find and select from a list of profitable parameters and perform walk forward testing.

Tools needed:

  • 4XLab.NET software: Download a compiled version, or create an account and download the source code.
  • Tick Data for a currency pair: This tutorial uses the Dec 05 EURUSD tick data. You can download some data from the Trading Systems Source Code forum, or get some data at http://ratedata.gaincapital.com

Trading System:

In this tutorial, we will optimize the "% Trend Change" system, which is based on the Four Percent Model, developed by Ned Davis and popularized by Martin Zweig. This is a very simple trend following model. It selects an initial direction when the price raises or falls by a specified percentage. Once an initial direction is set, it stays on the trade until the price retraces from the maximum or minimum by the specified percentage.

Rules:

  • The first trade is short if the prices move below the initial tick by the specified percentage. If the prices move above the initial tick by the specified percentage, the first trade is instead long.
  • If the system goes long, with each tick, a maximum value is calculated. If the current tick is lower than a percentage of the maximum value, the system closes the long position and opens a short position. This is a reversing system, it is always in the market.
  • If the system goes short, with each tick, a minimum value is calculated. If the current tick is higher than a percentage of the minimum value, the system closes the short position and opens a long position.

This system does not use candles since the time required for the percentage move to occur is unknown.

Steps:

  • After you have downloaded and extracted or compiled the necessary files, go to the folder where the 4XLab.NET.exe program is located and execute it.
  • Once the application launches, you need to load a full week of data, the compiled version ships with a small data file. To load another tick file, click File -> Load , select a file containing a full week of data. The lower left corner of the program will display the number of lines currently processed, and will display the file name when the process finishes.
  • From the Trading Systems dropdown menu, select the % Trend Change system. Note that the Trading System Parameters dropdown changes and lists parameters appropriate for this system.
  • The account parameters in the textbox can be left at the default values. The default values can be selected by pressing the Reset button. The account parameters are as follows:
    • AMG: account margin: the initial value in the simulated account
    • ALV: account leverage: the leverage to use, the default of 50 will make each dollar control 50 dollars.
    • AAL: account allocation: how much of the account to commit to one trade, the default is 1/3.
    • LOG: verbose level: 0 is extremely verbose, 2-3, only end of run results, 4, only optimization results. 
    • ALAG: account lag in seconds. Simulates lag between broker and analysis algorithm.
    • ACOC: account cost of carry. Not implemented, will reflect the cost of having a position open.
  • In the Trading Parameters dropdown, type PERCENT:0.004;
    • This parameter will specify a 0.4% change in price as the signal to reverse a position. For the first week of December of 2005, an EURUSD value of 1.1712 would translate to approximately 46 pips using this formula.
    • This is an attempt to make this system adaptable. Instead of selecting a fixed pip threshold, the system uses a percentage, which will translate to more pips as the currency heads higher, or less pips as it goes lower.
  • Once the tick file is loaded, the trading system and the proper parameter are both selected, click the Execute button to analyze the results of trading this system on the data loaded.
  • With the default parameters, after the trading system goes over the entire data, you will be presented with a report similar to this one:
  • Iteration Ended:
    Tick Source: D:\FOREXDATA\Ticks\EURUSD\05DecW1.csv
    Parameters Sent: PERCENT:0.004;AMG:1000;ALV:50;AAL:0.3;LOG:2;ALAG:0;ACOC:0.03;
    Parameters Reported By Transform: PERCENT:0.004;
    Margin Stats: O:1000.00, H:1213.46, L:1000.00, C:1008.46 Drawdown:-204.99, 16.89 %
    PIP Stats: Pips Captured:0.00190, H:0.01600, L:0.00000 Drawdown:-0.01410
    Volume moved on account:46306.82
    Orders Placed:16
    Winning Trades:3
    Maximum Winning Streak:2
    Losing Trades:5
    Maximum Losing Streak:1
    Winning Long Trades:1 Winning Short Trades:2
    Losing Long Trades:3 Losing Short Trades:2
    Maximum Gain:0.00920
    Maximum Loss:-0.00430
    Minimum Gain:0.00150
    Minimum Loss:-0.00110
    Average Gain:0.00583
    Average Loss:-0.00312
    Total Gain:0.01750
    Total Loss:-0.01560
  • This particular parameter, for this particular time period, resulted in the system going from $1000 to $1213, then back to $1008. To find out what other parameters are profitable for this time period, an optimization run is needed.
  • Before conducting a huge optimization run with multiple files, one needs to stop and think what will the range of parameters for the optimization run be.

Selecting a range for the optimization parameters:

  • In this particular system, the only parameter is a percentage of the exchange rate, which will be used to create a reversing threshold in pips. For the EURUSD at 1.1728, a percentage value of 0.0001 results in a threshold of 1.1 pips. This value is almost guaranteed to produce bad results, but it is a good starting point for a preliminary parameter selection.
  • Manually executing the trading system with a starting percentage of 0.0010 and incrementing it by 0.0010 results in the system not taking any trades when the percentage gets to 0.0060. This will be a good point to end the optimization run.
  • A small optimization run can be conducted by setting the Trading System Parameters to: PERCENT:0.0001-0.0010,0.0001;log:3;
  • This will result in the program running the system with percentage values of 0.0001, 0.0002, ... , 0.0010
  • The end of trading result for one parameter are posted below:
  • Iteration Ended:
    Tick Source: D:\FOREXDATA\Ticks\EURUSD\05DecW2.csv
    Parameters Sent: PERCENT:0.0002;LOG:3;AMG:1000;ALV:50;AAL:0.3;LOG:3;ALAG:0;ACOC:0.03;
    Parameters Reported By Transform: PERCENT:0.0002;
    Margin Stats: O:1000.00, H:1000.00, L:0.00, C:0.00 Drawdown:-1000.00, 100.00 %
    PIP Stats: Pips Captured:-1.91140, H:0.00000, L:-1.91140 Drawdown:-1.91140
    Volume moved on account:141503.93
    Orders Placed:12442
    Winning Trades:738
    Maximum Winning Streak:5
    Losing Trades:5231
    Maximum Losing Streak:68
    Winning Long Trades:378 Winning Short Trades:360
    Losing Long Trades:2607 Losing Short Trades:2624
    Maximum Gain:0.00310
    Maximum Loss:-0.00200
    Minimum Gain:0.00010
    Minimum Loss:-0.00010
    Average Gain:0.00038
    Average Loss:-0.00042
    Total Gain:0.28170
    Total Loss:-2.19310
  • Trading this parameter fully drained the account, the system placed 12442 trades in one week, of those 738 were winners, but the end result was an account value of $0.0
  • Observing the end of optimization results, a percentage of 0.0010 resulted in a final account value of $131. These results serve to reduce the optimization range from 0.0010 to 0.0060.
  • The optimization run can be executed by setting the Trading System Parameters to: PERCENT:0.0010-0.0060,0.0001;log:4;
  • By using  log:4; the program is further quieted down and only an acknowledgement is given that an iteration of the trading system was executed. If more details are desired, change this to log:3;
  • Regardless of the log level selected, full details of the optimization run are saved to an output file of named: Optimization [date and time ran].opt.csv
  • Once the optimization run is finished, the program will output first all the profitable parameters found, an example is found below:
  • Profitable Parameters:
    PERCENT:0.0027;      ' Profit: $60.98 : 6.10 %  Drawdown: 27.69 %
    PERCENT:0.0028;      ' Profit: $233.16 : 23.32 %  Drawdown: 26.22 %
    PERCENT:0.0029;      ' Profit: $80.82 : 8.08 %  Drawdown: 32.27 %
    ----------------------
    ----------------------------
    For Maximum Margin Gain:
    Tick Source: D:\FOREXDATA\Ticks\EURUSD\05DecW2.csv
    Parameters Sent: PERCENT:0.0028;LOG:3;AMG:1000;ALV:50;AAL:0.3;LOG:3;ALAG:0;ACOC:0.03;
    Parameters Reported By Transform: PERCENT:0.0028;
    Margin Stats: O:1000.00, H:1481.07, L:993.65, C:1233.16 Drawdown:-388.31, 26.22 %
    PIP Stats: Pips Captured:0.02040, H:0.03360, L:-0.00050 Drawdown:-0.02310
    Volume moved on account:175236.76
    Orders Placed:78
    Winning Trades:15
    Maximum Winning Streak:3
    Losing Trades:24
    Maximum Losing Streak:7
    Winning Long Trades:6 Winning Short Trades:9
    Losing Long Trades:14 Losing Short Trades:10
    Maximum Gain:0.01510
    Maximum Loss:-0.00330
    Minimum Gain:0.00070
    Minimum Loss:-0.00020
    Average Gain:0.00453
    Average Loss:-0.00198
    Total Gain:0.06800
    Total Loss:-0.04760
    ----------------------------
    ----------------------------
    For Minimum Drawdown:
    Tick Source: D:\FOREXDATA\Ticks\EURUSD\05DecW2.csv
    Parameters Sent: PERCENT:0.0027;LOG:3;AMG:1000;ALV:50;AAL:0.3;LOG:3;ALAG:0;ACOC:0.03;
    Parameters Reported By Transform: PERCENT:0.0027;
    Margin Stats: O:1000.00, H:1401.66, L:971.71, C:1060.98 Drawdown:-388.08, 27.69 %
    PIP Stats: Pips Captured:0.00850, H:0.02900, L:-0.00210 Drawdown:-0.02460
    Volume moved on account:180646.49
    Orders Placed:90
    Winning Trades:16
    Maximum Winning Streak:3
    Losing Trades:29
    Maximum Losing Streak:7
    Winning Long Trades:6 Winning Short Trades:10
    Losing Long Trades:17 Losing Short Trades:12
    Maximum Gain:0.01530
    Maximum Loss:-0.00420
    Minimum Gain:0.00100
    Minimum Loss:-0.00020
    Average Gain:0.00422
    Average Loss:-0.00203
    Total Gain:0.06750
    Total Loss:-0.05900
    ----------------------------
    System Tester Ended
  • We have found something important in these optimization results: An island of profitable results which seems to fit a normal distribution curve.
  • One can perform a more detailed optimization run for this island of values with the following parameters: PERCENT:0.0027-0.0029,0.00005;log:3;
  • The following profitable parameters are found, reinforcing the fact that 0.0028 (32 pips at 1.1728) is a profitable parameter.
  • Profitable Parameters:
    PERCENT:0.0027;      ' Profit: $60.98 : 6.10 %  Drawdown: 27.69 %
    PERCENT:0.00275;      ' Profit: $166.04 : 16.60 %  Drawdown: 24.96 %
    PERCENT:0.0028;      ' Profit: $233.16 : 23.32 %  Drawdown: 26.22 %
    PERCENT:0.00285;      ' Profit: $172.21 : 17.22 %  Drawdown: 28.96 %
    PERCENT:0.0029;      ' Profit: $80.82 : 8.08 %  Drawdown: 32.27 %
  • To view more details about the optimization runs we just conducted, locate the Optimization <Date Time>.opt.csv files and open then in Excel or other spreadsheet program.
  • All the run results for the optimization parameters evaluated are included in the file.
  • The following parameters are recorded for each optimization run:
    • Parameter : The parameters used for this run.
    • Margin O : The opening account value.
    • Margin H : The highest account value reached.
    • Margin L : The lowest account value reached.
    • Margin C : The closing account value.
    • Margin DD : The highest retracement in account value from a local maximum.
    • Margin DD% : The previous value expressed as a percentage of the account value at the time. This is a more meaningful figure. If the Margin DD is $200, but the Margin C value is $1400, one might be inclined to think it is not too bad. However, when viewed as a percentage, the drawdown might be 20%, which means it ocurred when the account value was $1000.
    • Pips Captured : How many pips were captured during the trading interval.
    • Pips H : The highest number of pips captured.
    • Pips L : The lowest number of pips captured.
    • Pips DD : The biggest retracement in pips captured from a local maximum.
    • Volume Traded : The total volume of currencies traded. This figure includes leverage, and accounts for the opening and closing transactions.
    • Orders Placed : How many trades the system placed.
    • Winning Trades : The total number of winning trades.
    • Max Winning Streak : The maximum number of consecutive winning trades.
    • Losing Trades : The total number of losing trades.
    • Max Losing Streak : The maximum number of consecutive losing trades.
    • Winning Long Trades : How many long trades were winning ones.
    • Winning Short Trades : How many short trades were winning ones.
    • Losing Long Trades : How many long trades were losers.
    • Losing Short Trades : How many short trades were losers.
    • Max Win : Biggest winning trade (in PIPS)
    • Max Loss : Biggest losing trade (in PIPS)
    • Min Win : Smallest winning trade (in PIPS)
    • Min Loss : Smallest winning trade (in PIPS)
    • Win Avg : Average of all winning trades (in PIPS)
    • Loss Avg : Average of all losing trades (in PIPS)
    • Total Win : Total pips won.
    • Total Loss : Total pips lost.

Evaluating a longer time period:

  • To evaluate the system performance over a longer time period, multiple optimization runs, each over a different week need to be conducted.
  • To evaluate a month worth of data, repeat the following steps
    • Load a weekly file
    • Run an optimization loop with PERCENT:0.0010-0.0060,0.0001;log:4;
  • Once all the weekly files are processed, you will have in the executable directory, several Optimization <Date Time>.opt.csv files.
  • Launch Excel or another spreadsheet software and create a blank spreadsheet with multiple tabs. To do this in Excel, locate the Sheet1, Sheet2, Sheet3 tabs at the bottom, right click on any one, select Insert..., select Worksheet, then press Ok. Right clicking on them and choosing Rename allows you to rename them.
  • Leave the first tab empty, we will use this for the summary data adding all the weekly results.
  • Open the first optimization run, copy and paste the results into the second tab.
  • Open the second optimization run, copy and paste the results into the third tab. Repeat this step until all files are consolidated onto one spreadsheet.
  • Copy the first column (the parameters column) from the second tab into the first tab.
  • Create a formula in the second column adding all the closing - opening account values for all tabs.
  • Download and open the file attached to this thread for the final result.

Data Analysis:

  • Opening the attached excel spreadsheet, one can perform the following analysis of the trading data.
  • Over the entire month of December of 2005, the island of profitable parameter at 0.0027-0.0029 turns into a single profitable parameter at 0.0028, and small losses at 0.0027 and 0.0029. Overall, nothing too exciting or terribly robust here, analysis of the other months would reveal whether values around these parameters results in gains or losses over an entire year.
  • Another island of profitability for the month exists from 0.0057-0.0060. A parameter of 0.0060 results in as little as 0 (no) trades, to as many as 30 trades in a week.

Parameter Selection:

  • Parameter optimization for a robust system will result in islands of profitable parameters. Select the middle parameter from the widest island to have better odds of selecting a parameter which will work in the future.

Walkforward Testing:

  • Perform an optimization run on weekly data and select a parameter based on the above criteria. Test this selection against next week result.
  • This test can be performed offline with the excel spreadsheet.
     

Future Work:

  • Perform optimization runs on a year worth of data.

 

Forex Lab .NET | 4X Lab .NET © Copyright 2005-2006 ASCSE LLC | Disclaimer

Powered by Community Server, by Telligent Systems