Overview: Usage
Using TSG2 is not hard. To adapt outgoing strategies into strategies for genetic optimization several different ways can be used, examples of which are given below.
Examples Strategy:
TS.G2.Simple
TS.G2.Multi
TS.G2.Include
TS.G2.Simple
This strategy is analog to TS.GO.12.PRO from the previous version TSGO. There is only one criterion of optimality (fitness) given in it.
Strategy Code:
EasyLanguage:
Inputs: Gen(1), {Gen - input parameter, that assigns the number of generations. Optimize in TradeStation with "Start = 1" and "Inc = 1"} ShowInd(1), {ShowInd - number of individual in population to show} ModeTSGO(0), Population(50), FreshBlood(0), MyReportName("MySystemSimple1"); { Declaration of variables } Vars: Len1(0),Len2(0),Len3(0),Len4(0),SL(0), Fitness(0),LastRun(0),R(0),K(0),Ind(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 TS.G2.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.} R = TS.G2.Start(MyReportName); { This block runs when the optimization is starting for the first bar only. } If Gen = 1 Then Begin { 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. } R = TS.G2.Mode(ModeTSGO); R = TS.G2.Popul(Population); R = TS.G2.FreshBlood(FreshBlood); { Define User variables. } R = TS.G2.Var("NetProfit"); R = TS.G2.Var("PF"); R = TS.G2.Var("MaxIDD"); {***Sets up new chromosomes and new genes. Chromosome Parameters: TS.G2.Chrom(Name) Name – name of chromosome. Gene Parameters: TS.G2.Gen(Name,Chrom,Min,Max,Incr) Name – name of gene. Chrom – number of chromosome that contains gene (if 0 then gene doesn’t participate in mutations, it’s fixed). Min – minimal value of gene. Max – maximal value of gene. Incr – value increase (step), if = 0 then any values in set range can be used.***} K = TS.G2.Chrom("Buy.Signal"); R = TS.G2.Gen("Buy.Signal.Len1",K,1,50,1); R = TS.G2.Gen("Buy.Signal.Len2",K,1,50,1); K = TS.G2.Chrom("Sell.Signal"); R = TS.G2.Gen("Sell.Signal.Len3",K,1,50,1); R = TS.G2.Gen("Sell.Signal.Len4",K,1,50,1); K = TS.G2.Chrom("StopLoss"); R = TS.G2.Gen("StopLoss.SL",K,100,1000,100); End; { The generation of a new candidate in the population } LastRun = TS.G2.Next(Gen); { If this is the last path, shows results for Ind = ShowInd; Else get the next candidate Ind = 0; } Ind = Iff(LastRun = 1,ShowInd,0); { Get values of genes for choosen candidate. } Len1 = TS.G2.Get("Buy.Signal.Len1",Ind); Len2 = TS.G2.Get("Buy.Signal.Len2",Ind); Len3 = TS.G2.Get("Sell.Signal.Len3",Ind); Len4 = TS.G2.Get("Sell.Signal.Len4",Ind); SL = TS.G2.Get("StopLoss.SL" ,Ind); R = TS.G2.ShowViewer; End; { ---------------------------------------------------------------------- } { The basic strategy code. } { Set up the stop-loss parameter. } SetStopPosition; SetStopLoss(SL); { 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 to the signal, short positions are reversed to long positions and vise versa. Besides, positions can be stopped by stop-loss and trailing-stop orders. } if Value1 cross over Value2 then Buy This Bar ; if Value3 cross below Value4 then Sell This Bar ; { End the basic strategy code. } { ---------------------------------------------------------------------- } { Calculation an optimization criteria. The simplest criteria is used here. } if LastBarOnChart Then Begin { Save user defined data. } R = TS.G2.Set("NetProfit",NetProfit); R = TS.G2.Set("PF",Iff(GrossLoss < 0,-GrossProfit/GrossLoss,0)); R = TS.G2.Set("MaxIDD",MaxIDDrawDown); { A fitness value is passed to the genetic optimizer on the last bar. If the candidates are included in the current population depends on the result of run. } R = TS.G2.Fitness(NetProfit); { One can look at all tested variants, assigning a print of the gene values for each generation. In PowerEditor in debug window to the debugger.} {print(Gen,Fitness,Len1,Len2,Len3,Len4,SL,DT,FA,PC);} end;
TS.G2.Multi
This strategy is also analogous to TS.G2.SIMPLE and TS.GO.12.PRO, but instead of one criterion of optimality three are used in these.
Strategy Code:
EasyLanguage:
{******************************************************************* Name: TS.G2.Multi Analysis Type: Strategy Description: Example Strategy for Genetic Optimizer v2.x Example of simple trading system to show the possibilities of Genetic Optimizer for TradeStation. The system is based on 2 moving average crossover. Buy signal is generated when fast moving average crosses over slow moving average. Additionaly Stop-loss is included in the system. Used: TSG2.dll Provided By: Trade Smart Research (c) Copyright 2001 - 2007 www.tsresearch.com *******************************************************************} Inputs: Gen(1), {Gen - input parameter, that assigns the number of generations. Optimize in TradeStation with "Start = 1" and "Inc = 1"} Population(100); { Declaration of variables } Vars: Len1(0),Len2(0),Len3(0),Len4(0),SL(0), Fitness(0),LastRun(0),R(0),K(0),Ind(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 TS.G2.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.} R = TS.G2.Start("MySystemMulti1"); R = TS.G2.ShowViewer; { This block runs when the optimization is starting for the first bar only. } If Gen = 1 Then Begin { 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. } R = TS.G2.Mode(0); R = TS.G2.Method(1); R = TS.G2.Popul(Population); { Define criteria } { Here we define the optimization criterion Define three criterions – NetProfit, ProfitFactor and MaxIDD Looking for maximum by all criterions } R = TS.G2.Criterion("NetProfit",1); R = TS.G2.Criterion("PF",1); R = TS.G2.Criterion("MaxIDD",1); {***Sets up new chromosomes and new genes. Chromosome Parameters: TS.G2.Chrom(Name) Name – name of chromosome. Gene Parameters: TS.G2.Gen(Name,Chrom,Min,Max,Incr) Name – name of gene. Chrom – number of chromosome that contains gene (if 0 then gene doesn’t participate in mutations, it’s fixed). Min – minimal value of gene. Max – maximal value of gene. Incr – value increase (step), if = 0 then any values in set range can be used.***} K = TS.G2.Chrom("Buy"); R = TS.G2.Gen("Buy.Len1",K,1,50,1); R = TS.G2.Gen("Buy.Len2",K,1,50,1); K = TS.G2.Chrom("Sell"); R = TS.G2.Gen("Sell.Len3",K,1,50,1); R = TS.G2.Gen("Sell.Len4",K,1,50,1); K = TS.G2.Chrom("StopLoss"); R = TS.G2.Gen("StopLoss.SL",K,100,1000,100); End; { The generation of a new candidate in the population } LastRun = TS.G2.Next(Gen); { Get values of genes for choosen candidate. } Len1 = TS.G2.Get("Buy.Len1",0); Len2 = TS.G2.Get("Buy.Len2",0); Len3 = TS.G2.Get("Sell.Len3",0); Len4 = TS.G2.Get("Sell.Len4",0); SL = TS.G2.Get("StopLoss.SL",0); End; { ---------------------------------------------------------------------- } { The basic strategy code. } { Set up the stop-loss parameter. } SetStopPosition; SetStopLoss(SL); { 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 to the signal, short positions are reversed to long positions and vise versa. Besides, positions can be stopped by stop-loss and trailing-stop orders. } if Value1 cross over Value2 then Buy This Bar ; if Value3 cross below Value4 then Sell This Bar ; { End the basic strategy code. } { ---------------------------------------------------------------------- } { Calculation an optimization criteria. The simplest criteria is used here. } if LastBarOnChart Then Begin { Set values for criteria } { Assign values to variables defined as optimization criterions } R = TS.G2.Set("NetProfit",NetProfit); R = TS.G2.Set("PF",Iff(GrossLoss < 0,-GrossProfit/GrossLoss,0)); R = TS.G2.Set("MaxIDD",MaxIDDrawDown); { If the candidates are included in the current population depends on the result of run } { When using an optimization with many criterions, values are assigned to variables defined as criterions with the help of the function TS.G2.Set() at run. On run’s finish the function TS.G2.Fitness ranges the copies into the population according to the values of optimization criterions } R = TS.G2.Fitness(0); end; {***** Copyright (c) 2001-2007 Trade Smart Research, Ltd. All rights reserved. www.tsresearch.com ***** ***** Trade Smart Research reserves the right to modify or overwrite this analysis technique with each release. *****}
TS.G2.Include
In this example of strategy, optimization also takes place by many criterions, but external signals are used with the help of IncludeSignal.
1. External signal including is used. This shows how it is possible to run your system’s optimization in TSG2 without changing it.
2. An example of how to use limitations on systems is given. In case a system’s Net Profit is less than zero or Profit Factor more than 10, the system is ignored.
Strategy Code:
EasyLanguage:
{******************************************************************* Name: TS.G2.Include Analysis Type: Strategy Description: Example Strategy for Genetic Optimizer v2.x Example of simple trading system to show the possibilities of Genetic Optimizer for TradeStation. The system is based on 2 moving average crossover. Buy signal is generated when fast moving average crosses over slow moving average. Additionaly Stop-loss is included in the system. Used: TSG2.dll Provided By: Trade Smart Research (c) Copyright 2001 - 2007 www.tsresearch.com *******************************************************************} Inputs: Gen(1), { Gen - input parameter, that assigns the number of generations. Optimize in TradeStation with "Start = 1" and "Inc = 1" } Population(100); { Declaration of variables } Vars: LenLE(0),LenSE(0),ConsLenLE(0),ConsLenSE(0), Fitness(0),LastRun(0),R(0),K(0),Ind(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 TS.G2.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.} R = TS.G2.Start("MySystemInclude1"); R = TS.G2.ShowViewer; { This block runs when the optimization is starting for the first bar only. } If Gen = 1 Then Begin { 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. } R = TS.G2.Mode(0); R = TS.G2.Method(1); R = TS.G2.Popul(Population); { Define User variables. } R = TS.G2.Criterion("NetProfit",1); R = TS.G2.Criterion("PF",1); R = TS.G2.Criterion("MaxIDD",1); {***Sets up new chromosomes and new genes. Chromosome Parameters: TS.G2.Chrom(Name) Name – name of chromosome. Gene Parameters: TS.G2.Gen(Name,Chrom,Min,Max,Incr) Name – name of gene. Chrom – number of chromosome that contains gene (if 0 then gene doesn’t participate in mutations, it’s fixed). Min – minimal value of gene. Max – maximal value of gene. Incr – value increase (step), if = 0 then any values in set range can be used.***} K = TS.G2.Chrom("Buy"); R = TS.G2.Gen("Buy.Len1",K,1,50,1); R = TS.G2.Gen("Buy.Len2",K,1,50,1); K = TS.G2.Chrom("Sell"); R = TS.G2.Gen("Sell.Len1",K,1,50,1); R = TS.G2.Gen("Sell.Len2",K,1,50,1); End; { The generation of a new candidate in the population } LastRun = TS.G2.Next(Gen); { Get values of genes for choosen candidate. } LenLE = TS.G2.Get("Buy.Len1",0); ConsLenLE = TS.G2.Get("Buy.Len2",0); LenSE = TS.G2.Get("Sell.Len1",0); ConsLenSE = TS.G2.Get("Sell.Len2",0); End; { ---------------------------------------------------------------------- } { The basic strategy code. } { This is an example of how to optimize an external system without modifying it A system composed of signals “MA Bull Simple Cross” and “MA Bear Simple Cross” is being optimized Source codes of those systems aren’t changed in the process It is done with IncludeSignal } IncludeSignal: "MA Bull Simple Cross",Close,LenLE,ConsLenLE; IncludeSignal: "MA Bear Simple Cross",Close,LenSE,ConsLenSE; { End the basic strategy code. } { ---------------------------------------------------------------------- } Var: PF(0); { Profit factor } { Calculation an optimization criteria. The simplest criteria is used here. } if LastBarOnChart Then Begin { Caculate Profit Factor } PF = Iff(GrossLoss < 0,-GrossProfit/GrossLoss,0); { We don’t want bad systems to leave heirs. In this case with help of the function TS.G2.BadIndividual we announce them as bad. Such a system won’t stay in the population and won’t leave heirs. } { Check constrains for bad individual } If NetProfit < 0 or PF > 10 Then R = TS.G2.BadIndividual; { Set value of optimisation criteria } R = TS.G2.Set("NetProfit",NetProfit); R = TS.G2.Set("PF",PF); R = TS.G2.Set("MaxIDD",MaxIDDrawDown); R = TS.G2.Set("Trades",TotalTrades); { If the candidates are included in the current population depends on the result of run. } R = TS.G2.Fitness(0); end; {***** Copyright (c) 2001-2007 Trade Smart Research, Ltd. All rights reserved. www.tsresearch.com ***** ***** Trade Smart Research reserves the right to modify or overwrite this analysis technique with each release. *****}
|