TradeStaion Genetic Optimizer: Trading Strategy testing Out of Sample
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.
| EasyLanguage: | {******************************************************************* Name: TS.GO.12.Ex2 Analysis Type: Strategy Description: Example Strategy for Genetic Optimizer v.1.x with Out of Sample 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 *******************************************************************} Inputs: {Gen - input parameter, that assigns the number of generations. Optimize in TradeStation with "Start = 1" and "Inc = 1"} Gen(1), ShowInd(1), {ShowInd - number of individual in population to show} ModeTSGO(0), Population(50), FreshBlood(0), MyReportName("MySystem1"), Dstart(60000), {Day number from the end of data, start In Sample data} Dstop(0); {Day number from the end of data, end In Sample data} {For example - Dstop = 365 - OOS is 365 last days} { Declaration of variables } Vars: Len1(0),Len2(0),Len3(0),Len4(0),SL(0),DT(0),FA(0),PC(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.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(MyReportName + "(" + 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 empty population in a given example. } R = TS.GO.Mode(ModeTSGO); R = TS.GO.Popul(Population); R = TS.GO.FreshBlood(FreshBlood); {***Sets up new chromosomes and new genes. Chromosome Parameters: TS.GO.Chrom(Name) Name – name of chromosome. Gene Parameters: TS.GO.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.GO.Chrom("Buy.Signal"); R = TS.GO.Gen("Buy.Signal.Len1",K,1,50,1); R = TS.GO.Gen("Buy.Signal.Len2",K,1,50,1); K = TS.GO.Chrom("Sell.Signal"); R = TS.GO.Gen("Sell.Signal.Len3",K,1,50,1); R = TS.GO.Gen("Sell.Signal.Len4",K,1,50,1); K = TS.GO.Chrom("StopLoss"); R = TS.GO.Gen("StopLoss.SL",K,1,1000,1); K = TS.GO.Chrom("DollarTraling"); R = TS.GO.Gen("DollarTraling.DT",K,1,1000,1); K = TS.GO.Chrom("PercentTraling"); R = TS.GO.Gen("PercentTraling.FA",K,1,1000,1); R = TS.GO.Gen("PercentTraling.PC",K,1,100,1); End; { The generation of a new candidate in the population } LastRun = TS.GO.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.GO.Get("Buy.Signal.Len1",Ind); Len2 = TS.GO.Get("Buy.Signal.Len2",Ind); Len3 = TS.GO.Get("Sell.Signal.Len3",Ind); Len4 = TS.GO.Get("Sell.Signal.Len4",Ind); SL = TS.GO.Get("StopLoss.SL",Ind); DT = TS.GO.Get("DollarTraling.DT",Ind); FA = TS.GO.Get("PercentTraling.FA",Ind); PC = TS.GO.Get("PercentTraling.PC",Ind); R = TS.GO.ShowViewer; End; { ---------------------------------------------------------------------- } { The basic strategy code. } { Set up the stop-loss and traling-stop parameter. } 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 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; if Value3 cross below Value4 then Sell; { End the basic strategy code. } { ---------------------------------------------------------------------- } Var: 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; R = TS.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; {***** 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. *****}
|
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) and 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.
|