| Every trader
dreams of acquiringa stable and
reliable mechanical trading system
that would not require much attention.
Technology forges ahead and offers
new problem solutions. Genetic
algorithm is a new and unexplored
trend which forms a new field
of study for traders. |
 |
Inspired by Mother Nature .
Genetic algorithms (GA) are, in essence,
effective optimization methods that
are inspired by Mother Nature. Basically,
they are used for solving those problems
that have a large set of parameters
and lack formalized solution methods.
It would take an unreasonable amount
of time to look through all the variants,
while GA helps to find a quick solution
with a given level of accuracy.
Every parameter of the problem becomes
a gene, and the full set of the parameters
– a set of genes, one chromosome,
one individual. E.g., a system has
five parameters that can change their
values in a range from 1 to 255. Then
our individual (or chromosome) would
be a set of five bytes following one
another in binary form looking like
a binary chain of 40 bits length.
At a first stage of GA a large population
of individuals with random parameters
is created. Next, the system counts
the result for each individual and
thus creates a fitness function. Therefore,
fitness is an indication of individual’s
suitability and its compliance with
the desired solution.
The population is sorted by fitness
and the best half is selected and
then internal GA methods – crossover,
mutation and inversion – are used.
Two parenting individuals are selected
for a crossover, and breaking points
are decided on a random basis, thus
creating two offsprings as parts of
different parents. Each offspring
has features of both parents. This
is the most important part of GA,
where the new individual with features
of both its parents is created that
might be more suitable than each of
his ancestors.
After all individuals go through a
crossover, their amount equals the
initial figure, next goes mutation.
Several individuals are picked on
a random basis, reversing every bit.
Inversion is also in use, when a chromosome
is divided randomly to two parts that
substitute one another. These two
GA methods are sources of new external
features not contained in base population.
Having performed all the above,
we obtain new population with the
same amount of individuals, though
more suitable for the desired solution.
Then the process is repeated over
and over – fitness determination,
crossover, mutation and inversion.
Having gone through a large number
if iterations we get individuals
containing genes with the best parameters
for the optimized system.
For Optimization of Existing
Strategies
Let’s look over the simplest mechanical
trading system (MTS). Two moving
averages, MA1 and MA2, cross one
another, giving us market entry
point. Crossing of another two averages,
MA3 and MA4, gives us exit point.
Each MA has two parameters – length
and shift n bars in the future.
Thus, the total amount of system
parameters equals 8. Four are responsible
for enter, and four – for exit.
To simplify, we would account for
the parameters to be in a range
from 1 to 255 (one byte). The length
of each chromosome is, therefore,
8 bytes or 64 bits.
The amount of individuals in the
population depends on the number
of genes in the chromosome and is
decided empirically. For a given
example, a population of 300 individuals
is enough. To simplify, we define
fitness as total aggregate return.
Launching GA, we reach the faze
of fitness calculation, where we
run each individual through historical
data (meaning our system MA1MA2-MA3MA4
with parameters of a current individual).
We get return, keeping in mind that
is our fitness of the individual.
After several iterations, the parameters
of individuals with maximum fitness
become almost constant. Now we can
consider the iteration process completed.
The individual with maximum fitness
from the resulting population is
the set of parameters that best
satisfy the system.
I have tested such an MTS on a
5-minute chart EUR/USD. The process
converges after about 100-150 generations.
The speed of convergence depends
on GA parameters – the possibility
of crossover, mutation and inversion.
By reducing the possibility of mutation
and inversion, we increase the speed
of process’ convergence, though
incurring risk of it converging
to one of the local maximums.
Having tested the system, I came
to the conclusion that it lacks
vitality (what was doubtless), though
GA had produced the set of parameters
to maximize aggregate return. Such
a system is taken into consideration
for the purposes of simplification
and demonstration the principles
of fundamental genetic algorithm.
Fitness Choice.
One of the major issues with
testing systems with GA is fitness
choice. As mentioned above,
fitness is a function of system
resulting values. The previous
example has fitness (Fit) equal
to profit (Profit). Nevertheless,
optimization by such fitness
leads to GA’s producing the
parameters for the system resulting,
for instance, in a single (for
a given trading period) profitable
trade, though providing the
system with no stability. A
good solution would be to add
to fitness calculation the amount
of trades performed by the system.
(CountTrade):
|
 |
Fit = Profit * CountTrade.
Unfortunately, this way we get some trades with a huge drawdown, therefore
reducing total system’s stability.
There is a simple solution to this
problem. Let’s incorporate maximum
drawdown for a single trade (Prosadka)
to the formula:
Fit = (Profit * CountTrade) / (Prosadka/10).
We have reduced the impact of the drawdown dividing it by 10 because it is
not that important for us. This formula
is good enough for the optimized systems
to produce appropriate results. Nevertheless,
we can add to the formula such variables
as, for instance, amount of loosing
trades (KolvoMinus) or stop level
(Stop) and also vary parameters’ impacts.
I use the following formulas to determine
fitness:
Fit = Profit/(1+Prosadka/10)* (CountTrade/20);
Fit = Profit/(1+Prosadka/10 + Stop / 10 ) * (CountTrade/10);
Fit = (Profit+KolvoPlus/5) / (1 + Prosadka / 10 + KolvoMinus/5).
In general, the fitness function choice is a creative process. Depending
on the desired outcome, the formula
should be developed. However, I should
warn the audience that an excessive
amount of parameters in the formula
with even the simplest optimized system
might lead to noticeable increase
in time of optimization.
If the formula was defined incorrectly, you would obtain no final result:
the process would be virtually stuck
in between several local maximums.
New
Viewpoint.
Let’s take our mind off GA for
a little while and think over
the following. Movement chart
for the currency pair we observe
on the screen - is it a random
process or does it contain some
rules, hidden from us? It would
most likely be the second. Otherwise,
technical analysis existence,
as well as existence of any kind
of market analysis, could be questioned.
From advanced math we are familiar
with some methods that allow us
to determine a formula quite precisely,
based on which, an unstructured
graph of unknown function is built.
Take into consideration Fourier
transformation. But, let’s not
stop at discussing these methods
and figuring out how good they
are, it is simply not that important
to us right now. |
 |
What important is that market is a system, which changes and it changes rather
smoothly. Having found consistent
patterns in market movement , we can
extrapolate them on to some distance
in the future, where they will still
work (although, probably, a little
worse)
Why, not a single system, invented
half a century ago, is working well
now? That is because conditions have
changed but systems haven’t. There
is no flexion. Whereas, we have to
develop a system that would constantly
be adjusting to market. Using GA and
continuously optimizing an existing
system, getting new incoming data,
can achieve this.
Earlier, we have examined an application
of genetic algorithms for optimizing
parameters of an existing mechanical
trading system. What prevents us from
making GA invent the system, which
would suit the market the best, on
its own?
Let’s try.
In Table 1, there are some possible signals and their parameters given, which
can be used for both market entry
and exit. This is not a full list,
but it is enough for us for now. In
a first column – there are names of
signals. We code each signal number
with one gene. Then - 4 parameters
(some signals have less, but it is
not that important), we code them
with four more genes. Therefore, we
have a chain consisting of 5 genes,
which describes the signal for market
entry. Let’s add filtering signals
or indicators.
The short list of such filters is given in Table 2. We code them the same
way: the number of each filter is
one gene; its parameter is four additional
ones. Hereby, we get an entry signal,
which should be confirmed by filtering
indicator. Complete market entry signal
consists of 10 genes, 5 – is a signal
itself and 5 – is a filter.
Exit signal is coded the same way.
There are also 10 genes, out of which
5 – is a signal and 5 – is a filter.
Altogether, we get one 20 genes long
chromosome. Start GA. For such a quantity
of genes, its population size should
not be less than 1000 individuals.
As a result of GA work in ?340 generations,
we get the results given in Table
3.
So, GA created mechanical trading platform and optimized its parameters for
specific historic period. Testing
it with the newer data (which was
not used for optimization), MTS
detects some instability. For more
than a week, it comes out as rather
profitable. Then, MTS stops making
profits and becomes losing. That
is not surprising taking into account
the system’s simplicity.
To make system work better, during
the filtration of entry and exit
signals, we would use not one but
four filters, set in a similar way.
Also, we could add the time period
for possible entry (for example,
entry from 3am to 5pm is coded with
2 genes) and possible exit. Stops
and limits are also possible (they
are not discussed in this article).
Altogether, we get 27 genes, which
describe market entry and 27 more
genes, which describe market exit
and that makes it a total of 54
genes in one chromosome. Due to
chromosome becoming longer, we should
also enlarge the quantity of individuals
in a single population. I used populations
of 2000 individuals .
Obtained system has rather positive
outcomes and based on the latest
historic data, it behaves properly
within a month after optimization
(9 trades, 2 losing ones, total
profit are 400 pips).
Comment: don’t try to use the mechanic trading systems given in tables, because
the divergence between the quotes
flow and the flow, which MTS will
work based on, is not acceptable.
Otherwise, system will not send
out correct signals in the right
places.
Some conclusions and
explanations.
The choice of the chart time interval
for MTS search is an interesting
question. Basically, the trading
system can be based on any chart,
but if we use charts, that have
a dimension of 4 hours, their shape
will be mainly influenced by fundamental
factors. With time, these factors
can convert into totally opposed
ones (for example, dominating trend
changes to horizontal corridor).
This will adversely affect the stability
of MTS work. The more we go down
to tick charts, the more behavioral
their shape is. Although, tick charts
are too congested and hardly defy
any analysis.
For testing, I used 5-minute EUR/USD charts
from September 2003 up to
June 2004. Here is the conception
for testing the complex, applying
the method described earlier.
1. Diverse signals are given and their parameters are described.
2. Fitness calculation function
is chosen.
3. Optimization is conducted
for a rather long historical
period.
4. The system, obtained as
a result of optimization,
is tested with data that follows
the optimization period directly.
5. If the system shows good
outcomes, the optimization
period shifts into the future
by a range of the latest data.
6. After that, the process
is being repeated from point
3 until there comes a certainty
in stability of the obtained
complex.
|
 |
The work with the obtained complex is conducted in a similar way. For example,
GA starts based on 3-month-old data.
Optimal MTS is determined which
sets trading for a week. After that,
optimization period shifts by one
week into the future and everything
is repeated. This way, we always
have an MTS that is adjusted to
the market accordingly.
It is worthwhile to mention some of the difficulties that come along while
developing such systems. Firstly,
there are quite understandable problems
with detecting mechanism for determining
such technical analysis instruments
as support and resistance levels;
trend, waves and price restraintment
lines. Secondly, systems that are
created using GA, are very sensitive
to computer productivity. For instance,
to calculate one iteration for a
population of 2000 individuals with
54 chromosome each on given data
of 18 000 candlesticks, it will
take Intel Pentium 4 with 2400hz
frequency about 40 minutes, and
the whole process would be finalized
in more than a week.
Then, we can move following directions.
1. Put all the technical analysis instruments, used by a human being, at
GA disposal.
2. Greatly complicate MTS
structure by adding a lot
of signals, which can not
only be present in the optimization
period but also be in the
system.
3. Introduce elements of artificial
intelligence into MTS logic,
for example, fuzzy logic.
This, and much more, will
allow to create (theoretically,
for now) such thing as quintessence
of technical analysis – the
ideal mechanical trading system.
.
Holy Grail is somewhere
near.
|
 |
Sergej Golovin.
First published at: http://www.spekulant.ru
|