{******************************************************************* Name: TS.GO.Ex.WFO Analysis Type: Strategy Description: Example Strategy for Genetic Optimizer v.1.x or higher 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: TSGO12.dll Provided By: Trade Smart Research (c) Copyright 2001 - 2004 www.tsresearchgroup.com *******************************************************************} Input: SampleStart(1), { Bar number for Sample start } SampleLen(100), { Length of Sample } OOSLen(20), { Length of Out of Sample } Gen(1), { Iteration nuber } MyStrategyName("MySystem1" ); { Declaration of variables } Vars: Len1(0),Len2(0),Len3(0),Len4(0),SL(0), LastRun(0),R(0),K(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.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.} R = TS.GO.Start(MyStrategyName + "(" + GetSymbolName + ").rgo" ); { 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 last population in a given example. } R = TS.GO.Mode(1); R = TS.GO.Popul(50); { Output results of WFO } If SampleStart > 1 Then print( TS.GO.Get("Sample.Start" ,1), TS.GO.Get("Sample.End" ,1), TS.GO.Get("OOS.Start" ,1), TS.GO.Get("OOS.End" ,1), TS.GO.Get("Sample.Fitness" ,1), TS.GO.Get("OOS.Fitness" ,1), TS.GO.Get("Sample.Profit" ,1), TS.GO.Get("OOS.Profit" ,1), TS.GO.Get("Len1" ,1), TS.GO.Get("Len2" ,1), TS.GO.Get("Len3" ,1), TS.GO.Get("Len4" ,1), TS.GO.Get("SL" ,1) ) else ClearDebug; { Define user variables } R = TS.GO.Var("Generation" ); R = TS.GO.Var("LastBar" ); R = TS.GO.Var("Sample.Start" ); R = TS.GO.Var("Sample.End" ); R = TS.GO.Var("Sample.Fitness" ); R = TS.GO.Var("Sample.Profit" ); R = TS.GO.Var("OOS.Start" ); R = TS.GO.Var("OOS.End" ); R = TS.GO.Var("OOS.Fitness" ); R = TS.GO.Var("OOS.Profit" ); { The description of 5 genes is as follows. The First parameter - a gene number. If it's zero, generate gene number. 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. } K = TS.GO.Chrom("Buy.Signal" ); R = TS.GO.Gen("Len1" ,K,1,50,1); R = TS.GO.Gen("Len2" ,K,1,50,1); K = TS.GO.Chrom("Sell.Signal" ); R = TS.GO.Gen("Len3" ,K,1,50,1); R = TS.GO.Gen("Len4" ,K,1,50,1); K = TS.GO.Chrom("StopLoss" ); R = TS.GO.Gen("SL" ,K,100,1000,100); R = TS.GO.ShowViewer; End; { Start the new Iteration } LastRun = TS.GO.Next(Gen); { Save generation number and parameters } R = TS.GO.Set("Generation" ,Gen); R = TS.GO.Set("Sample.Start" ,SampleStart); R = TS.GO.Set("Sample.End" ,SampleStart + SampleLen - 1); R = TS.GO.Set("OOS.Start" ,SampleStart + SampleLen); R = TS.GO.Set("OOS.End" ,SampleStart + SampleLen + OOSLen - 1); { Get values of genes } Len1 = TS.GO.Get("Len1" ,0); Len2 = TS.GO.Get("Len2" ,0); Len3 = TS.GO.Get("Len3" ,0); Len4 = TS.GO.Get("Len4" ,0); SL = TS.GO.Get("SL" ,0); End; { ---------------------------------------------------------------------- } { Begin 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. } { ---------------------------------------------------------------------- } Vars: Fitness(0),Fitness1(0),Fitness2(0),Fitness3(0), Profit1(0),Profit2(0),Profit3(0); { Calculation an optimization criteria. The simplest criteria is used here. } Fitness = NetProfit; { Save fitness for Sample Start } if CurrentBar = SampleStart Then Begin Fitness1 = Fitness; Profit1 = NetProfit; end; { Save fitness for Sample End } if CurrentBar = SampleStart + SampleLen Then Begin Fitness2 = Fitness; Profit2 = NetProfit; end; { Save fitness for Out of Sample End } if CurrentBar = SampleStart + SampleLen + OOSLen Then Begin Fitness3 = Fitness; Profit3 = NetProfit; end; if LastBarOnChart Then Begin { A fitness value is passed to the genetic optimizer on the last bar. The sample fitness is difference from start sample fitness and end sample fitness. } R = TS.GO.Set("LastBar" ,CurrentBar); R = TS.GO.Set("Sample.Fitness" , Fitness2 - Fitness1); R = TS.GO.Set("Sample.Profit" , Profit2 - Profit1); R = TS.GO.Set("OOS.Fitness" , Fitness3 - Fitness2); R = TS.GO.Set("OOS.Profit" , Profit3 - Profit2); R = TS.GO.Fitness(Fitness2 - Fitness1); { 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.} end; {***** Copyright (c) 2001-2004 Trade Smart Research, Ltd. All rights reserved. www.tsresearchgroup.com Trade Smart Research reserves the right to modify or overwrite this analysis technique with each release. *****}
|