TradeStation 8 Solutions   
TradeStation Solutions   
MetaStock Solutions   
Bloomberg Solutions   
Historical data tools   
Trading tools   
Genetic Optimizer   
Download demo   
ELA code convertor   
Buy now!   
Real-Time Portfolio Analyzer   
TradeStation to Excel Link   
Wavelet Transform   
TradeStation to MatLab Link   
Trendiness   
Custom software development   
Customer support   
Our partners   
About us   
Publications   
Discussion Forum   

    RT Soft Trading tools Genetic Optimizer 



This program is to incredible improving of TradeStation abilities of design, testing and optimization of trading strategies.

We can offer the add-on for TradeStation (TradeStation Group, Inc.) which realizes the advanced optimization methods based on Genetic Algorithms.

The main advantages of TS Genetic Optimizer for TradeStation:

  • Speeding-up of optimization, fast convergence to optimal solution;
  • Unlimited number of optimized parameters (up to 100 in the current version);
  • Unlimited calculation of parameter’s accuracy;
  • Usage of any complex optimization criteria (written in Easy Language), for example, considered maximal drawdown, equity line shape, etc.;
  • It is possible to give any constraints in optimization problem. For example, to reject strategies which have too many trades or few trades or exceeds the given threshold for maximal drawdown;
  • Structural optimization ability that is building of complex strategy, automatically switched to different assets and timeframes;
  • Visualization of strategy testing results in sample (optimization data) and out of sample (testing data) simultaneously.
  • Ability to get a set of Trading Systems which are close to optimal (last population). They can be composed as the trading system portfolio.

Buy it now!


What are Genetic Algorithms

Genetic Algorithms has appeared recently. They combine the best characteristics of other optimization methods such as speedy work that doesn’t depend on properties of optimization criteria (like smoothness). They provide optimal solution on a vast domain.

The name Genetic Algorithms is connected with the fact that their work is similar to natural selection in the Nature. Therefore it uses the Biology and Genetics terms like gene, chromosome, fitness, population etc. in the description of Genetic Algorithms.

Genetic Algorithms work is similar to random sort out (Monte Carlo method). In contrast to Monte Carlo method the search is led purposefully. The goal of Algorithm is to get some specimens (population) with the best fitness (optimization criteria) values.

Work of Genetic Optimizer can be considered as the growth of the best population of Trading Systems most adapted to the successful and stable functioning according to the given fitness criteria.

The Brand New approach to Trading System Design

The common approach to the trading system design is the following routine:

Begin (*)

Choose the parameters:
  1. Asset.
  2. Time Frame.
  3. Concept (trend following, patterns etc.).
  4. Formalization via indicators.
  5. Feasible sets of indicator’s parameters.
  6. Signal generation rules.
  7. Order types.
  8. Look through parameters in sample data.
  9. Estimation of parameters according to a criterion (Net Profit, Profit Factor etc.) to choose the best parameters’ set.
  10. If the results of item 9 are satisfactory load out-of-sample data and testing the trading system for the best parameters’ set.
  11. If the result of item 10 differs from the result of item 9 within acceptable bounds then go out else go to (*).
  12. End.


    It is possible to include the additional parameters:
  13. Position size calculation.
  14. Asset share calculation, or Portfolio optimization.

In fact all steps are led by the random search except for item 8 where parameters’ values are defined by enumeration of all possibilities. At the same time sorting is led by one of the TradeStation performance criteria (Profit Factor for example). The cycle stops if the satisfactory result is got. Using the algorithm man-hours are tremendous. The resulting trading system is hardly optimal.

Following the principle “human should thinking, machine should working” we suggest replace the above routine to the following:
  1. Choose the parameters:
    • Admitted asset candidates.
    • Possible time frames.
    • Admitted concept set.
    • possible indicator set by which admitted concepts can be formalized.
    • Feasible sets of indicator’s parameters.
    • Elementary rule set from which any signal can be generated.
    • All possible orders with its parameters.
  2. Assign optimization criterion of any complexity and with any constraints.
  3. Set Genetic Algorithm parameters.
  4. Set out-of-sample data interval.
  5. Run TS GO Genetic Optimizer.
For a few minutes we shall get a set of optimal trading systems, according to our fitness criterion, tested in out-of-sample data. All we need is to choose reasonable parameters’ bounds and meaningful optimization criterion.

System definition

The system concludes program with graphic interface assigned for examination parameters of received Trading System population and program module which provides interaction of set of strategy elements with genetic optimizer and written in Easy Language.

The Trading System population is saved in the file which user set. Later on one can see the results using graphic interface.
The system gives you a chance to continue optimization from the moment you stopped. For example, you can run optimization for 100 steps, look at the results, and then run for more 100 steps starting from the last result.

Besides, you can re-count received population and continue your calculation in external changes, For example, on the next bar. And all the members of population are optimized on the new bar. This let you make more precise definition of parameters on each bar and therefore let you build adaptive to market changes Trading System populations.

Program interface

To arrange the cycle by generations the default TradeStation optimizer is used. To do that one has to define input parameter Gen (generation) in the strategy:

Input: Gen(1);

To start genetic optimization it is necessary to set optimization in TradeStation by this parameter from 1 to required number of generations, for example, to 1000 (Format Strategy -> Inputs -> Edit Input -> Optimize).

On the first bar we start Genetic Optimizer for a strategy where genes (optimization parameters) and chromosomes (blocks of parameters) are defined. Besides, we get strategy parameter values from the module of Genetic Optimizer.

On the last bar the fitness function is called which informs about the results of Genetic Optimizer run. In accordance with the received values optimizer module changes current Trading System population and defines the candidate for the new run.

List of functions

Function name

Description

GO_Start (File)

Optimizer start.
File – The string that defines the file path and its name to store the current population. This function must be called in the first bar.
For example: GO_Start("C:\Program Files\TradeSmart\TSGO\Reports\TSGO_TEST")

GO_Set_Mode(ModeGO)

Set regimes.
ModeGO = 0 – start from empty population,
ModeGO = 0 – start from empty population,
ModeGO = 2 – continue the optimization without re-calculating the last population.
This function must be called in the first bar for Gen = 1.
For example: GO_Set_Mode(0)

GO_Set_Npar(Npar)

Set the number of genes
Maximal number of genes in the current version = 100.
For example: GO_Set_Npar (13)

GO_Set_Population(Npop)

Set the population size
Value by default = 50.
Maximal population size in the current version = 1000.
For example: GO_Set_Population(100)

GO_Def_Gen(N,Nchrom,Min,Max,Step,Name)

Gene definition
N – Gene (parameter) number,
Nchrom – chromosome number which Gene belongs to,
Min – minimal parameter value,
Max – maximal parameter value,
Step – parameter’s increment step (0 – arbitrary),
Name – parameter name (a string).
For example: GO_Def_Gen(11,5,0,50,1,"MACD.Len")

GO_Next(Gen)

Generate the new candidate in population
Gen – number of Generation (input parameter)
For example: GO_Next(Gen)

GO_Get(N)

Get parameter value
N – parameter (Gene) number.
For example: MACDLen = GO_Get(11)


An example of simplest strategy

Consider the simplest strategy as example for demonstration Genetic Optimizer use.

The strategy is based on moving averages crossover, for which moving average periods are selected. One pair is used for entry signals and another pair is used for exit signals. Besides, TradeStation built-in signals for stop-loss and trailing stop value fitting are used.

{ Description : Trade Smart Research Genetic Optimizer DLL for TradeStation
Used: ts_go.dll
Provided By : Trade Smart Research Group (c) Copyright 2001 - 2003 www.tsresearchgroup.com}

{ Gen - input parameter, that assigns the number of generations }


Input: Gen(1);

{ Declaration of DLL Functions } DefineDLLFunc: "ts_GO.dll", int, "GO_Start",LPSTR;
DefineDLLFunc: "ts_GO.dll", int, "GO_Set_Mode",int;
DefineDLLFunc: "ts_GO.dll", int, "GO_Set_Npar",int;
DefineDLLFunc: "ts_GO.dll", int, "GO_Def_Gen",int,int,float,float,float,LPSTR;
DefineDLLFunc: "ts_GO.dll", int, "GO_Set_Population",int;
DefineDLLFunc: "ts_GO.dll", int, "GO_Next",int;
DefineDLLFunc: "ts_GO.dll", float, "GO_Get",int;
DefineDLLFunc: "ts_GO.dll", int, "GO_Fitness",float;

{Declaration of variables. All of them except for Fitness are parameters of optimization}

Vars: Len1(0),Len2(0),Len3(0),Len4(0),SL(0),DT(0),FA(0),PC(0),Fitness(0);

{ ---------------------------------------------------------------------- }
{The Genetic Optimizer initialization and the definition of genes}


If CurrentBar = 1 Then Begin

{ This block runs on every run of strategy on the first bar. The function GO_Start is called having the Parameter that defines filename for milestones. All the tunings of an optimizer and current population are stored in the file, that allows to continue an optimization after break, or to draw the input/output signals after the opening the TradeStation workspace with the strategy. It is possible to open this file in graphic interface for viewing population.}

GO_Start("C:\Program Files\TradeSmart\TSGO\Reports\TSGO_TEST");
If Gen = 1 Then Begin
{ This block runs when the optimization is starting for the first bar only. The initializing of optimizer determination of genes and the population regime is executed (see the description of functions). We start optimizer with empty population in a given example. }

GO_Set_Mode(0);

{The number of genes-optimization parameters is assigned }

GO_Set_Npar(8);

{ The description of 8 genes is follows.
The First parameter - a gene number. It is used to get its value.
The Second parameter - a chromosome number that the gene belongs to.
The Third parameter - a minimum parameter value.
The Fourth parameter - a maximum parameter value.
The Fifth parameter - an increment of the parameter. It is possible to assign 0.
Sixth parameter - a name of the gene, one can use it as a commentary in the graphic interface. }


GO_Def_Gen( 1,1,1,50,1,"Len1");
GO_Def_Gen( 2,1,1,50,1,"Len2");
GO_Def_Gen( 3,2,1,50,1,"Len3");
GO_Def_Gen( 4,2,1,50,1,"Len4");
GO_Def_Gen( 5,3,1,1000,1,"SL");
GO_Def_Gen( 6,4,1,1000,1,"DT");
GO_Def_Gen( 7,5,1,1000,1,"FA");
GO_Def_Gen( 8,5,1,100,1,"PC");
End;

{The generation a new candidate in the population}

GO_Next(Gen);

{Values of genes for the new candidate in population are defined. }

Len1 = GO_Get(1);
Len2 = GO_Get(2);
Len3 = GO_Get(3);
Len4 = GO_Get(4);
SL = GO_Get(5);
DT = GO_Get(6);
FA = GO_Get(7);
PC = GO_Get(8);
End;

{ ---------------------------------------------------------------------- }
{ The basic strategy code. }
{ Set up the stop-loss and traling-stop parameters. }


SetStopPosition;
SetStopLoss(SL);
SetDollarTrailing(DT);
SetPercentTrailing(FA,PC);

{ The Moving Averages Calculation. }

Value1 = AverageFC(C,Len1);
Value2 = AverageFC(C,Len2);
Value3 = AverageFC(C,Len3);
Value4 = AverageFC(C,Len4);

{ Generation of signals by moving averages crossover. According the signal, short positions are reversed to long positions and vise versa. Besides, positions can be stopped by stop-loss and traling-stop orders. }

if Value1 cross over Value2 then buy;
if Value3 cross below Value4 then sell;

{Calculation an optimization criteria. The simplest criteria is used here. }

Fitness = NetProfit + OpenPositionProfit;

{ ---------------------------------------------------------------------- }
{End the basic strategy code.}


if LastBarOnChart Then Begin

{A fitness value is passed to a genetic optimizer on the last bar. The including of candidates to the current population is depend on the result of run.}

GO_Fitness(Fitness);

{ One can look at all tested variants, assigning a print of the gene values fot each generation. into PowerEditor in debug window to the debugger.}

{print(Gen,Fitness,Len1,Len2,Len3,Len4,SL,DT,FA,PC);}


End;

To start Genetic Optimizer it is necessary to apply a strategy to a graph and to set optimization for parameter Gen from 1 to some large number with a step 1. The number defines how many generations will passed. Usually it is from hundreds to thousands.

After the optimization start TradeStation alerts that input has a maximum value that is greater then the current MaxBarsBack setting. Do not care, push “Continue” button.

During the work TradeStation computes its own optimization criterion parallel to Genetic Optimizer that guided by a given fitness. Therefore optimal solution must not have the best TradeStation criterion value.

The results of strategy testing

Below there are given the result of strategy testing on hourly and daily FOREX EURJPY bars. Maximal number of generations was set 1111, so the same number of parameters combinations has been tested. The total number of possible combinations that will have to test in standard TradeStation optimizer is 6.25 * 1017. Genetic Optimizer spent for it about a minute.

Picture 1. The result of strategy testing on daily EURJPY bars in TS GO Viewer.

Here are shown some best Trading System copies from the last population. The best one from Picture 1 have the following parameters:

Len1 = 39
Len2 = 40
Len3 = 31
Len4 = 9
SL = 407
DT = 810
FA = 997
PC = 6

Below signals of the Trading System are shown.

Picture 2. The results on EURJPY (Daily).

It is tested on 1000 days.
Results are in pips.
Spread is 10 pips.
TradeStation Strategy Performance Report

Picture 2. The results on EURJPY (60 min.).

It is tested on 50 days.
Results are in pips.
Spread is 10 pips.
TradeStation Strategy Performance Report

Testing Out Of Sample

TO demonstrate optimization In Sample and testing Out Of Sample simultaneously we use the slightly modified example of strategy above.

In the signal are add 2 input parameters, assign beginning and end of area, on which is conduct optimization (In Sample). Beginning and end will be assign by the bar number, brushed off from the last bar a graphics.

{Optimization and OOS testing
Input Parameters: Gen - input parameter, that assigns the number of generations.
Dstart – Bar number from the end of data, start In Sample data.
Dstop - Bar number from the end of data, end In Sample data
for example - Dstop = 365 - OOS is 365 bars.}


Inputs: Gen(1),Dstart(60000),Dstop(0);

DefineDLLFunc: "ts_GO.dll", int, "GO_Start",LPSTR;
DefineDLLFunc: "ts_GO.dll", int, "GO_Set_Mode",int;
DefineDLLFunc: "ts_GO.dll", int, "GO_Set_Npar",int;
DefineDLLFunc: "ts_GO.dll", int, "GO_Def_Gen",int,int,float,float,float,LPSTR;
DefineDLLFunc: "ts_GO.dll", int, "GO_Set_Population",int;
DefineDLLFunc: "ts_GO.dll", int, "GO_Next",int;
DefineDLLFunc: "ts_GO.dll", float, "GO_Get",int;
DefineDLLFunc: "ts_GO.dll", int, "GO_Fitness",float;

Vars: Len1(0),Len2(0),Len3(0),Len4(0),SL(0),DT(0),FA(0),PC(0),Fitness(0);

{ ---------------------------------------------------------------------- }

If CurrentBar = 1 Then Begin

GO_Start("C:\Program Files\TradeSmart\TSGO\Reports\TSGO_TEST");
If Gen = 1 Then Begin

GO_Set_Mode(0);

GO_Set_Npar(8);

GO_Def_Gen( 1,1,1,50,1,"Len1");
GO_Def_Gen( 2,1,1,50,1,"Len2");
GO_Def_Gen( 3,2,1,50,1,"Len3");
GO_Def_Gen( 4,2,1,50,1,"Len4");
GO_Def_Gen( 5,3,1,1000,1,"SL");
GO_Def_Gen( 6,4,1,1000,1,"DT");
GO_Def_Gen( 7,5,1,1000,1,"FA");
GO_Def_Gen( 8,5,1,100,1,"PC");
End;

GO_Next(Gen);

Len1 = GO_Get(1);
Len2 = GO_Get(2);
Len3 = GO_Get(3);
Len4 = GO_Get(4);
SL = GO_Get(5);
DT = GO_Get(6);
FA = GO_Get(7);
PC = GO_Get(8);
End;

{ ---------------------------------------------------------------------- }

SetStopPosition;
SetStopLoss(SL);
SetDollarTrailing(DT);
SetPercentTrailing(FA,PC);

Value1 = AverageFC(C,Len1);
Value2 = AverageFC(C,Len2);
Value3 = AverageFC(C,Len3);
Value4 = AverageFC(C,Len4);

if Value1 cross over Value2 then buy;
if Value3 cross below Value4 then sell;

{ ---------------------------------------------------------------------- }

Var:R(0),Fitness(0),Fitness1(0),Fitness2(0);

{ Compute fitness }
Fitness = NetProfit + OpenPositionProfit;

{ Keep in mind the fitness value on the first bar In Sample,
Paint vertical bar marked the OOS period beginning. }

R = LastCalcJDate - DStart;
if DateToJulian(Date[1]) < R and DateToJulian(Date) >= R then Begin
Fitness1 = Fitness;
R = TL_New(Date, Time, High, Date, Time, Low);
TL_SetExtLeft (R, True);
TL_SetExtRight(R, True);
TL_SetColor(R, Blue);
end;

{ Pass the fitness value on the last bar In Sample, Paint vertical bar marked the OOS period finish }
R = LastCalcJDate - DStop;
if DateToJulian(Date[1]) < R and DateToJulian(Date) >= R then Begin
Fitness2 = Fitness;
GO_Fitness (Fitness - Fitness1);
R = TL_New(Date, Time, High, Date, Time, Low);
TL_SetExtLeft (R, True);
TL_SetExtRight(R, True);
TL_SetColor(R, Blue);
end;

The Signal is the same as above except for the last block.

As optimization criterion we use Fitness value difference for the bar that located DStart bars to the left from the last bar and for the bar that located DStop bars to the left from the last bar in contrast to standard TradeStation optimizer. As a result bars from DStart to DStop are not used in optimization. They are Out Of Sample.

Below there are shown signals of the Trading System. Vertical red bar in the center of the picture separates data on In Sample (on the left) è Out of Sample (on the right).

Picture 3. The results Out Of Sample on EURJPY (60 min).

TradeStation Strategy Performance Report

It is tested on 50 days, length of area OOS - 10 days.
Results are in pips.
Spread is 10 pips.

These elementary examples evidently show that modern computer methods can improve an arsenal of technical analytics.



All names and trademarks used on these web pages are registered by their respective companies
© 1998-2007 RT Soft, Ltd. All rights reserved.