Genetic optimization. Application in TradeStation environment.:
Genetic Algorithms The given publication
was presented by Vladislav Gorbunov (one of the founders of
Trade Smart Research Ltd.) in system trading conference in
Moscow, Russia, 2004.
Genetic optimization. Application in TradeStation
environment.
GENETIC ALGORITHMS It is the modeling of
natural selection. Some time ago people noticed that
some processes in nature are very effectively arranged. Algorithms
in question are based on natural selection in wild nature.
Genetic Algorithms are adaptive methods of extremum seeking.
Recently they were frequently used for solving the optimization
tasks. They are based on imitation of evolution in wild nature:
biological populations evolve during some time, obeying the
natural selection law discovered by Charles Darvin and only fittest
survive. Genetic algorithms imitate this process and they are able
to solve real tasks if these ones are properly encoded.
Basic principles of GA’s were formulated by Holland
(Holland, 1975) and described in many papers. In contrast to the
natural evolution GA’s model only those processes in populations
that are essential for the evolution. Which processes are essential
for evolution and which are not is still an open question for the
researchers.
Let’s consider basic terms used in this paper
- Gene;
- Chromosome;
- Fitness;
- Population;
- Crossover;
- Mutation.
Let’s describe each of them with reference to tasks connected
with trading systems.
Gene
– it is one of the system’s parameters which could be optimized.
Type of moving average, averaging period or some parameter or
coefficient of the digital filter, for example, etc.
Chromosome – it is group of genes
(parameters) that have logical connection with each other,
parameters of one type of input or one signal. This group
participates in crossover as a whole, i.e. it is a set of parameters
which can migrate between specimens as a whole. Thus successful
combinations of genes have better chances to survive and reproduce.
It is appropriate to combine connected parameters (of one signal for
instance) into one chromosome.
Fitness - criterion of profit or
suitability of specimen, the criterion function. It is some value
that is connected with the specific trading system. By the means of
fitness we report to genetic optimizer how much we are satisfied
with that trading system. The greater it is – the better is the
specimen. In simplest case it can be NetProfit or ProfitFactor, as in original optimization used
in TradeStation for example, but we suppose that it is not correct.
Results obtained with use of such simple criterions hardly can be
stable. In our case user defines himself what is “vitality” of
specimen (trading system). In fact one can get trading system that
will show stable results in future only due to proper choice of
optimization criterion.
Population – it is group of survived
till present moment best specimens selected by best fitness. Usually
trading systems are built in such way that “population” consists of
most “vital”, stable systems.
Crossover – it is replacement of genes
to analogous ones from another specimen or replacement of chromosome
to analogous from another specimen of population.
Mutation – it is random change of some
genes (parameters of system).
Scheme of genetic algorithm work:

Genetic algorithm for TradeStation is quite simple for
users. After initialization optimizer begins to pass sets of
parameters to the system being optimized. After completion of
processing of historical data system reports to optimizer the
result, i.e. fitness value. On the basis of obtained results
optimizer forms the current population consisting of best specimens.
After that optimizer applies crossover and mutation and forms new
set of parameters for testing. Then system with those parameters
runs again on data in question. Then fitness is compared with
already existing ones of other specimens. If last fitness is better
than previous then “weak” ones are replaced. On the other hand if
last computed fitness is “weaker” than already existing ones then it
is deleted from population (it remains for the first run) and we
search for a new specimen (change parameters of the system), and so
on until the search is completed.
Genetic optimization. Application in TradeStation environment.:
Building the trading systems
Building the trading systems
There are several approaches to building the trading
systems:
- General approach;
- Smart “economic” approach.
General Approach to building trading
systems consists of the following steps:
Begin loop Selection of
following parameters:
- Asset.
- Time interval.
- Concept (trend following for instance, or pattern
recogniton, etc.).
- Formalization of a concept in terms of rules and
indicators, etc.
- Possible set of indicator parameters values.
- Rules of signal generation.
- Type of orders (market, stop, limit) with values of
parameters (if any).
- Optimization of parameters on data series “in sample”.
- Results estimation by NetProfit, ProfitFactor and other
criteria for each set of parameters and selection of best
parameter set.
- If result is satisfactory then proceed to loading and
testing on “out of sample” data.
- If out of sample results differ from in sample results
but is still in admissible limits then exit loop or go to
begin of loop otherwise.
End of loop
It is
possible to include in loop the additional parameters:
- Position size computation algorithm.
- Computation of asset’s share in portfolio of portfolio
optimization.
| When using such
approach we in fact define our solutions randomly and time costs are
huge. Optimal solution of this task by enumeration of possibilities
is not possible.
“Economic” approach,
“Let the man think and let the machine
work”…
Selection of following parameters:
- Possible set of assets.
- Possible set of time intervals.
- Possible set of concepts.
- Possible set of indicators that are hypothetically
capable for formalization of these concepts.
- Possible set of indicator parameters values.
- Possible set of elementary rules that form the signal
generation rules.
- Possible variants of orders (with parameters).
- Definition of optimization criterion of any complexity
and with any limitations.
- For our tasks basic is monotonous growth of Equity.
- Definition of Genetic Algorithm parameters.
- Definition of out of sample time interval.
- Launch of TS GO – Genetic optimizer.
|
By the means of genetic optimization algorithm we can get the set
of optimal (according to our criterion) trading systems very
quickly. When using such approach our task reduces to selection of
reasonable rules variants, parameters alternatives and reasonable
optimization criterion.
As it often mentioned, estimation of
a system or a portfolio by some criterion like “NetProfit”
frequently lead to “overfitting”. Using of indirect criteria which
we can create solves this problem.
We can let genetic
optimizer to have possibility to select parameters and algorithms
for building trading systems.
Genetic optimization. Application in TradeStation environment.:
Practical realization for TradeStation.
Practical realization for TradeStation.
Let’s see how Genetic Optimization is practically realized for
TradeStation. Genetic optimizer consists of program with graphical
interface and dynamic library (Dll), which carries out genetic
selection functions. Interface of a program has the following
structure:
 On the left side there is workspace
of TradeStation platform, on the right side there is window for
viewing the selected by genetic optimization specimen’s
parameters.
The building of a system by the genetic selection method goes
like the following:
- First, we create the set of possible systems, which is encoded
in EasyLanguage in TradeStation environment.
- Then, parameters being optimized with the use of genetic
algorithms are encoded as a “genes” and organized in
“chromosomes”. Further we define possible parameters range and
step of parameter value change. If step is set to 0, parameters
will change continuously.
- When “genes” are specified and combined into “chromosomes” we
formulate our ideas of what is “good system” by setting the
fitness – optimization criterion.
- On final stage we apply the strategy to the graph and start
the search of parameters with genetic algorithm. Then we start
optimization mechanism by specifying the number of iterations in
“Inputs” bookmark, in “Gen” parameter (in TradeStation format
strategy window). I.e. internal TradeStation optimizer is used
only as a counter of “runs” and as an initializer of genetic
algorithm. For example we specify “Gen” parameter to optimize from
1 to 1000 with step equal to 1 as it shown below:
 It means that we run our system for 1000 times on given
asset and genetic algorithm will change parameters of our system for
1000 times.
The results of testing can be seen in the viewer
window, which opens automatically after testing is complete.
 One can view parameters of every specimen. It is
possible to sort specimens by any criterion used in TradeStation,
save or export all data to Excel for the further analysis.
Let’s consider simple example of building the system.
Let’s assume that we have classic reverse trading system
based on AverageTrueRange function. The principle of such system is
quite simple. According to average true range (AvgTrueRange) we
build some channel (multiply value of AvgTrueRange by a factor). If
current price “breaks up” this channel, then we buy, if “break down”
then we sell.
Source Easy Language code for calculation of
the AvgTrueRange value looks like the following:
 New signal is a bit more complex than old one, but
there is no much sense in the case of two parameters. In the given
example there are 5000 possible variants. In order to find
good-enough solution by usual way we would have to make 5000 runs,
and this could take a lot of time on long history. By the means of
GO it is sufficient to make 100-200 iterations and we will get
corresponding population with sufficient number of good-enough
specimens. In more complex real-life estimates the number of
parameters combinations could greatly exceed 5000. For instance
estimation of linear digital filter parameters of 100th order with
accuracy of estimation of coefficients equal to 0.01 might lead to
10300 combinations of parameters. It is not possible for the most
powerful supercomputer as far as to all existing computers working
simultaneously. By means of Genetic Optimizer it is possible to find
satisfying solution using home PC within ten minutes.
For the convenience one can use implemented
EasyLanguage functions:
- TS.GO.Start(FileName) - Launch of optimizer and
definition of file name for the storing of current population.
This function always must be executed on the first bar.
- TS.GO.Mode(ModeGO) - Setting of optimizer functioning
mode.
- TS.GO.Popul(NPop) - Definition of population size, from
10 to 1000.
- TS.GO.Chrom(Name) - Definition of new chromosome or
search by name of already existing one.
- TS.GO.Gen(Name,Chrom,Min,Max,Incr) – Definition of new
gene of search by name of already existing one.
- TS.GO.Next(Generation) - Addition new candidate to
population or selection of best specimen of population after
optimization is complete.
- TS.GO.Finish – Returns the indication of last run of a
system when optimization is complete and TradeStation is forming
the report.
- TS.GO.Error - Returns error code of last function.
- TS.GO.Var(Name) - Definition of user variable. Variable
is associated with current specimen of population and gives
possibility of saving user-defined data.
- TS.GO.Get(Name,Individ) - Gets the value of gene or
user-defined variable Name from specimen of population number
Individ.
- TS.GO.Set(Name,Val) - Sets new value of user-defined
variable Name for current specimen of population.
- TS.GO.Fitness(Fitness) - Inform the optimizer that run
of the system is complete.
- TS.GO.FreshBlood(FB_factor) – Setting of the “fresh
blood” factor of the system. This function increases the effective
size of population and does not allow leaders to dominate, which
gives a chance to other candidates. This speeds up optimization
due to richer variety of gene pool and decreases system’s
tendencies to overfitting.
- TS.GO.Stat – Computation of standard characteristics of
trading system.
- TS.GO.ShowViewer - Show Viewer immediately.
More detailed information with the description of functions
and principles of building of trading systems can be found on the
support forum: http://support.tsresearchgroup.com/
Let’s point on advances of genetic optimization once
more:
- Arbitrary optimization criteria;
- Computiation of criterion on the part of the history (In
Sample/Out Of Sample);
- Great extent of parameter space;
- Quick results;
- If system has variable number of parameters it is possible to
optimize that number.
Genetic optimization. Application in TradeStation environment.:
Curve Fitting. Fighting with the “OVERFITTING” of the system
Fighting with the “OVERFITTING” of the
systems (Curve Fitting)
There are different ways of fighting the so-called “overfitting”
of the systems or “overoptmimization” as it frequently called. Let’s
consider those variants which we apply in the field of genetic
optimization:
- “Strict” optimization conditions;
- Usage of “Fresh Blood” function from TSGO arsenal;
- Building of non-trivial optimization criterion.
“Strict” optimization conditions.
We interpret
strict optimization conditions as the following settings of systems
testing:
- Setting of significant slippage, one percent per deal for
example;
- Limitations of drawdowns;
- Big number of iterations;
- Big “leverage”.
In practice it was noticed that such testing sets hard conditions
for the systems and only really “tough” systems survive. It forces
genetic optimizer to select only those systems that are most stable
to adverse conditions and impacts. “Survived” systems showed most
stable results when were tested on out of sample data.
“FreshBlood” function
“FreshBlood” function causes purposeful perturbance to fitness.
As a result the effective size of population increases and
convergence improves. It can be considered as a launch of optimizer
on variety of price histories that are resulted from initial by
perturbance.
Building of non-trivial optimization
criterion (Fitness)
In our opinion, building of
non-trivial optimization criterion is one of the most essential
moments of trading systems building. As it we mentioned above in
order to lower the risk of overfitting it is necessary to use
another characteristics than NetProfit etc., but those which are
associated with desirable behavior of Equity as a whole, not only
with its absolute value at the end of the testing period. We can
maximize the average of the following value for example:
[log W(T)/W(0) + 2/T * sum { log (W(t)/Wmax(t) }], t =
0,..., T, where Wmax(t) = max{Wmax(t),W(t)} i.e. we tend to
condition the monotonous growth of Equity W(t)).
Optimization in case of large amount of parameters.
Disputes between “adherents” and “opponents” of the systems
with large amount of parameters don’t fade to this day. It practice
we noticed the following moments that can significantly influence
the results:
- The volume of data and “strictness” of the system.
- If the system has a lot of internal non-controllable degrees
of freedom then “overfitting” is possible when there is a little
number of parameters.
- Optimization of digital filer with 100 coefficients and taking
into account this number in fitness is not without reason (in our
humble opinion).
Genetic optimization. Application in TradeStation environment.:
Building the system consisting of blocks.
Building the system consisting of blocks.
New approach in trading systems building which was described
above was realized in development of genetic optimizer for
TradeStation. It uses TradeStation built-in approach to building of
strategies from already made signals in StrategyBuilder.
Principle of work is showed below:
 Basic moments:
- All signals including Manager are included in strategy in
TradeStation Strategy Builder.
- Manager is the standard block, which is included in every
strategy. Its purpose is to control the optimization process. It
can be the same for the many strategies. All strategy control
parameters are defined in it as input parameters or Inputs.
- Manager organizes the generation of the new specimen, the In
Sample/OOS calculation, calculates the fitness, etc.
- Each signal as usually realizes one variant of entry or exit
or the set of entries/exits of the same type.
- Each signal has standard structure and contains the
description of the optimization parameters in the beginning of the
code.
- In the beginning of the work each signal registers its
optimization parameters. During optimization process it selects
values of it’s parameters from the current specimen of population
and carries out its work with those parameters.
As a result of such approach we can get absolutely new system or
method of trade, which possibly would never be invented by us.
When we had converted basic signals based on technical
analysis indicators (moving averages, stochastics, momentums, etc.)
according to this method and made a block from each signal, then it
turned out that even among signals built on analogous basic
principles there are exact “leaders”. It points that perspectives of
creation and investigation of behavior of systems based on
traditional methods of technical analysis are far from exhausted.
I want to finish my presentation by the following
thesis:
- In wild nature usually the most adaptive to environment
organisms do survive, but not those ones who had learned best
skills and abilities some time before.
- The quickest adaptation to environment is provided by the
intellect, since it makes possible even the changing of the
environment.
Does it take place in the exchange trade?...
Genetic optimization. Application in TradeStation environment.:
Questions and Answer
Questions please.
Question: Which parameter it is better to
optimize and is it possible to optimize such parameter of the system
as “leverage”?
Answer: It is hard to
answer unambiguously. What one SHOULD NOT DO: One should not choose
NetProfit criterion since if we have such parameter as leverage then
it is clear that the best specimen will be the one with the maximal
lever because it will have the maximal profit.
Question: Then what criterion is better to
avoid overfitting and improve the stability of the system?
Answer: First, it is necessary to choose
another optimization criterion than that one which characterizes
only the profitability of the system. In another words it is
necessary to include in fitness indirect parameters, i.e. those
parameters that characterize the quality of the system indirectly.
It can be the number of deals or trading time for example. Second:
it is possible to apply “FreshBlood” function, which causes
perturbances and system that had maximal fitness and replaced other
specimens from population could be not the best as a result of usage
of this function. As a result we would keep in population other more
viable specimens which could show better results on “out of sample”
data for example. This problem as a whole is known as getting in the
local extremum. In TSGO search goes rather uniformly on the whole
plane. As a result we get population that is the section of the
whole plane that we have. Though if there is a local peak it can be
the first specimen in our population. In such case we might choose
not the best specimen in future. On the whole the stability of the
system can be conditioned by some limitations. One can set bigger
transaction costs for example. If the system generates too many
number of deals then such system will not survive. In reality such
strict conditions may not occur but I think one will feel more
comfortable with such system than with system that was tested under
more soft conditions.
Question:
What number of iterations it is necessary to choose when using
genetic optimization? Will it be better if we choose as big number
as possible?
Answer: Several hundreds of
iterations are enough for proper result. In order to estimate the
situation quickly one hundred is enough.
Question: When it is necessary to stop the
optimization?
Answer: It depends on number
of iterations we specified and what result was obtained. If the
number of signals, coefficients and filers is big and we set strict
limitations on population size but there are no specimens with good
fitness then we should continue the optimization.
Question: What is the reason of testing on
“sample” data in your program for genetic optimization in
TradeStation and then representing the results obtained on “out of
sample data” at the same time in your example of signal?
Answer: There is no specific reason. It is
just technical possibility to make useful search on “sample” data
and then without even pressing a key get the same result on “out of
sample” data. Actually it is the variant of technical solution for
those who need it.
Question:
How much time does it take to find a proper result in your
example of system?
Answer: The search of
proper solution from several thousands of variants takes for several
minutes.
Moscow, 2004.. Trade
Smart Research www.tsresearch.com
|