support.tsresearchgroup.com Forum Index

TSResearch | Software Products | Online Help | Free Download | Publications | Research

Username:   Password:   
Log me on automatically each visit
 

FAQ |  Search |  Memberlist |  Usergroups |  Profile |  Log in to check your private messages  Register |  Log in 

Example Strategy TS.GO.12.PRO

 
Post new topic   Reply to topic    support.tsresearchgroup.com Forum Index -> TS Genetic Optimizer for TradeStation
View previous topic :: View next topic  
Author Message
VladGor
Support Team


Joined: 26 Jan 2004
Posts: 528

PostPosted: Thu Mar 04, 2004 6:43 pm    Post subject: Example Strategy TS.GO.12.PRO Reply with quote

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. It is very simple system and we don't recomend to trade it but it is very useful to understand the principles of genetic optimization. To do optimization in usual way all parameters will be tested.

That would include:
K = TS.GO.Chrom("Buy.Signal");
R = TS.GO.Gen("Buy.Signal.Len1",K,1,50,1); -- 1 to 50, together 50
R = TS.GO.Gen("Buy.Signal.Len2",K,1,50,1); -- 1 to 50, together 50

K = TS.GO.Chrom("Sell.Signal");
R = TS.GO.Gen("Sell.Signal.Len3",K,1,50,1); -- 1 to 50, together 50
R = TS.GO.Gen("Sell.Signal.Len4",K,1,50,1); -- 1 to 50, together 50

K = TS.GO.Chrom("StopLoss");
R = TS.GO.Gen("StopLoss.SL",K,100,1000,100); -- 100 to 1000, together 11

All together:
= 50õ50õ50õ50õ11 = 68.75 million tests

TS GO finds acceptable solution with ~1000 tests. Using our new function FresBlood it's possible to do it even faster, with ~100 to 300 tests.

It's obvious that using new technological possibilities it's easy to cut down time for searching solutions. New version of Genetic Optimizer for TradeStation v.1.2. supports up to 1000 genes (!), that allows to solve tasks that wouldn't be posible with other methods.

EasyLanguage:
 
{******************************************************************* 
Name: TS.GO.12.PRO 
Analysis Type: Strategy 
Description: Example Strategy for Genetic Optimizer v.1.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: TSGO12.dll 
Provided By: Trade Smart Research (c) Copyright 2001 - 2004 
         www.tsresearchgroup.com 
*******************************************************************}
 
 
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("MySystem1"); 
     
 
{ 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.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);  
 
{ Define User variables. } 
 
        R = TS.GO.Var("NetProfit"); 
        R = TS.GO.Var("PF"); 
        R = TS.GO.Var("MaxIDD"); 
 
{***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,100,1000,100); 
         
    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); 
    R = TS.GO.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. }
 
 
R = TS.GO.Stat; 
 
 
if LastBarOnChart Then Begin 
 
{ Save user defined data. } 
     
    R = TS.GO.Set("NetProfit",NetProfit); 
    R = TS.GO.Set("PF",Iff(GrossLoss < 0,-GrossProfit/GrossLoss,0)); 
    R = TS.GO.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. }
 
     
 
    Fitness = TS.GO.Get("%T_Test",0); 
    R = TS.GO.Fitness(Fitness); 
 
{ 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
 
{***** 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. *****}
 
 


Last edited by VladGor on Mon Oct 25, 2004 2:16 pm; edited 2 times in total
Back to top
View user's profile Send private message
kshultz
Guest


Joined: 12 Apr 2004
Posts: 12

PostPosted: Mon Apr 12, 2004 10:36 am    Post subject: Reply with quote

I would like to purchase GO, but I can not understand how to set up a complex optimization. For example:

If I want to test 3 uncorrelated "signals" such as a 2 moving average crossover (so this would have 2 inputs),

an oscillator ( with 3 inputs..length, obought,oversold and I do not know if it will be better to buy/sell at the

upper level or lower level),and a rate of change of advance/decline from Data2 ( with 3 inputs).

I would also have long and short exits that could be independent of the specific signals or depend on them.

I would also like each signal to have an input: usema(true/false) to see if using each or in combination is useful.

I do not understand what is chromosone and what is gene and how to group or number them? Any help would be appreciated.Ken
Back to top
View user's profile Send private message
Mak
Developers Team


Joined: 26 Jan 2004
Posts: 465

PostPosted: Mon Apr 12, 2004 9:31 pm    Post subject: Reply with quote

Example of code for your task see below

Two separate signals:
- MA crossover
- RSI oscillator

Other signals are to be add like manner.

at the bottom of page possible download EasyLanguage code for this strategy.

EasyLanguage:
{******************************************************************* 
Description    : Example of a complex optimization. 
Provided By    : TradeSmart Research (c) Copyright 2004 
********************************************************************}
 
 
{I would like to purchase GO,  
but I can not understand how to set up a complex optimization.  
 
For example: 
 
If I want to test 3 uncorrelated "signals" such as a 2 moving average crossover  
(so this would have 2 inputs), an oscillator ( with 3 inputs..length, obought, oversold  
and I do not know if it will be better to buy/sell at the upper level or lower level), 
and a rate of change of advance/decline from Data2 ( with 3 inputs). 
 
I would also have long and short exits that could be independent  
of the specific signals or depend on them. 
I would also like each signal to have an input:  
usema(true/falseto see if using each or in combination is useful. 
 
I do not understand what is chromosone and what is gene and how to group or number them?  
Any help would be appreciated. 
 
Ken 
}
 
 
Input: Gen(1),Show(1);  
Input parameters. 
    Gen - the number of current generation. 
        We must optimaze by that parameter from 1 to N (1000 - for exam.) 
    Show - the number of exemplar of population, 
        that we want to see after optimization is finished. 
}
 
 
{ Declaration of variables } 
 
{ Signals parameters } 
Vars: MA.On(0), MA.Len1(0), MA.Len2(0); 
VarsRSI.On(0), RSI.Length(0), RSI.Level1(0), RSI.Level2(0); 
 
{ Internal TSGO variables } 
Vars: R(0), K(0), Ind(0), Fitness(0); 
  
Begin of Genetic Optimizer initialization and the definition of genes and chromo} 
{ ----------------------------------------------------------------------------- } 
 
If CurrentBar = 1 Then Begin 
 
This block runs on every path of strategy on the first bar.} 
 
{ Start GO and define report file Example1.RGO } 
{ It will be placed in ..\TradeSmart\TS GO\reports folder } 
 
    R = TS.GO.Start("Example1"); 
 
    If Gen = 1 Then Begin 
 
This block runs when the optimization is starting for the first bar only. } 
 
        R = TS.GO.Mode(0);    { Set optimization mode } 
        R = TS.GO.Popul(100); { Set population size } 
 
{ Define chromosome and genes for MA crossover } 
{ K - the chromosome number is used in Gen definition } 
The names of all genes & chromo must be different } 
        K = TS.GO.Chrom("MA"); 
        R = TS.GO.Gen("MA.On",K,0,1,1); On/Off switch for chromo } 
        R = TS.GO.Gen("MA.Len1",K,1,50,1); { first  Length parameter } 
        R = TS.GO.Gen("MA.Len2",K,1,50,1); { second Length parameter } 
 
{ Define chromosome and genes for RSI oscillator } 
{ Take attention, RSI.On may have values -1, 0, +1 } 
        K = TS.GO.Chrom("RSI"); 
        R = TS.GO.Gen("RSI.On",K,0,-1,1); On/Off switch for chromo } 
        R = TS.GO.Gen("RSI.Length",K,1,50,1); { length of RSI } 
        R = TS.GO.Gen("RSI.Level1",K,1,99,1); { first  level } 
        R = TS.GO.Gen("RSI.Level2",K,1,99,1); { second level } 
 
{ Define chromosome and genes for other signals } 
{ .................. } 
 
    End;  
      
{ Generate a new candidate in the population } 
If this is last path function return 1 else 0 } 
 
    R = TS.GO.Next(Gen);       { generate new candidate } 
    Ind = Iff(R = 0, 0, Show); { choose what parameters to get } 
 
{ Get values of genes for the new candidate in population. } 
 
{ Get values for MA crossover. } 
    MA.On   = TS.GO.Get("MA.On",Ind); 
    MA.Len1 = TS.GO.Get("MA.Len1",Ind); 
    MA.Len2 = TS.GO.Get("MA.Len2",Ind); 
 
{ Get values for RSI. } 
    RSI.On     = TS.GO.Get("RSI.On",Ind); 
    RSI.Length = TS.GO.Get("RSI.Length",Ind); 
    RSI.Level1 = TS.GO.Get("RSI.Level1",Ind); 
    RSI.Level2 = TS.GO.Get("RSI.Level2",Ind); 
 
{ Get values for other signals. } 
{ ................ } 
 
End
{ --------------------------------------------------------------------------- } 
End of Genetic Optimizer initialization } 
  
 
 
Begin of the basic strategy code. } 
{ ---------------------------------------------------------------------- } 
 
The MA crossover signal. } 
If MA.On <> 0 Then Begin   If MA crossover is On } 
    Value1 = AverageFC(Close,MA.Len1); 
    Value2 = AverageFC(Close,MA.Len2); 
    If Value1 cross over  Value2 then Buy ("MA.B"This Bar on Close
    If Value1 cross below Value2 then Sell short ("MA.S"This Bar on Close
end
 
The RSI signal. } 
If RSI.On <> 0 Then Begin   If RSI is On } 
    Value3 = RSI(CloseRSI.Length); 
    If RSI.On > 0 Then Begin Buy when cross over } 
        If Value3 cross over  RSI.Level1 then Buy  ("RSI.OB"This Bar on Close
        If Value3 cross below RSI.Level2 then Sell short ("RSI.BS"This Bar on Close
    end
    If RSI.On < 0 Then Begin Buy when cross below } 
        If Value3 cross below RSI.Level1 then Buy  ("RSI.BB"This Bar on Close
        If Value3 cross over  RSI.Level2 then Sell short ("RSI.OS"This Bar on Close
    end
end
 
{ ---------------------------------------------------------------------- } 
End the basic strategy code. } 
 
{ Calculation an optimization criteria. } 
This is one of fitness examples. } 
 
Fitness = (NetProfit - LargestWinTrade)  
        / MaxList(500, -GrossLoss, -MaxIDDrawDown); 
 
A fitness value is passed to a genetic optimizer on the last bar. } 
if LastBarOnChart Then Begin 
    R = TS.GO.Set("Trades",TotalTrades); 
    R = TS.GO.Set("NetProfit",NetProfit); 
    R = TS.GO.Set("MaxDD",MaxIDDrawDown); 
    R = TS.GO.Set("PF",NetProfit/MaxList(1, -GrossLoss)); 
    R = TS.GO.Fitness(Fitness); 
end
Back to top
View user's profile Send private message
kshultz
Guest


Joined: 12 Apr 2004
Posts: 12

PostPosted: Mon Apr 12, 2004 11:08 pm    Post subject: Reply with quote

Thank you for the response to my question. As I look at this code it appears to take trades from the signals (ma, rsi, etc) separately. I would also like to find out how to test for interactions also, that is: if ma (of certain value) and rsi (of certain value)...then buy/sell. would this be another chromosone? Also, are there any problems with using a signal based on data2 ? Thank you. Ken
Back to top
View user's profile Send private message
kshultz
Guest


Joined: 12 Apr 2004
Posts: 12

PostPosted: Mon Apr 12, 2004 11:15 pm    Post subject: Reply with quote

Sorry, I have another question: In the code why does rsi on have 3 values -1,0,+1 and ma on has only 2 values? also what is "<>" in the code to begin testing ma or rsi? "If RSI.On < 0Then Begin { Buy when cross below } " or "If MA.On <> 0 Then Begin { If MA crossover is On }" Ken
Back to top
View user's profile Send private message
VladGor
Support Team


Joined: 26 Jan 2004
Posts: 528

PostPosted: Tue Apr 13, 2004 12:13 pm    Post subject: Reply with quote

Quote:
Thank you for the response to my question. As I look at this code it appears to take trades from the signals (ma, rsi, etc) separately. I would also like to find out how to test for interactions also, that is: if ma (of certain value) and rsi (of certain value)...then buy/sell. would this be another chromosone?

You can leave 2 chromosomes or combine then into the one.

The meaning of the chromosome is that inside chromosome we can unify related genes.

For example, RSI has 3 parameters that you can combine, not only Length. So you can unify all these 3 parameters into one chromosome to let them participate in new sample generation.

It allows to make the process of search faster.

If you put every gene in separete chromosome or put them all in one chromosome you would make the process slower but still it would work.

Quote:
Also, are there any problems with using a signal based on data2 ? Thank you.

Ñ data2 .. dataN No problems there.
Genetic optimizer doesn't put any additional limitations.

Quote:
Sorry, I have another question: In the code why does rsi on have 3 values -1,0,+1 and ma on has only 2 values?

MA crossover has only two positions - On and Off.

There are 3 positions for RSI:

0 - Signal Off
-1 - Buy when cross below, Sell when cross over
+1 - Buy when cross over, Sell when cross below

As Level1 and Level2 can have values from 1 to 99 (Level1 can be < Level2 and Level1 can be > Level2) all combinations of signals are covered.

Quote:
also what is "<>" in the code to begin testing ma or rsi? "If RSI.On < 0Then Begin { Buy when cross below } " or "If MA.On <> 0 Then Begin { If MA crossover is On }"


A <> B -- A not equal B,
If MA.On <> 0 Then -- it's test if the signal is turned on.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    support.tsresearchgroup.com Forum Index -> TS Genetic Optimizer for TradeStation All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Copyright © TS Research Group

Powered by phpBB © 2001, 2002 phpBB Group

Developed by: webdesign.tria.lv