51% winrate with this strategy

Profitable Trading Strategies with a Dash of Evolution

Hello Friend!

In case you missed it:

  • On Monday we started a project on using genetic algorithms to optimise the MACD parameters. The default parameters are 12,26 and 9.

  • Yesterday, we dropped a free bot using MACD and the 200 EMA with a 1:2 risk-reward ratio.

  • Today we look into optimising the MACD Parameters using GA.

Genetic Algorithms Steps

The experiment started off with an initial population of 100. A population is randomly initialized with MACD settings. Typically looks something like this:

1. [12,3,56] 
2. [43,2,55] 
3. [66,87,12] 
4. [98,77,104]
5. [24,67,88]
.
.
.
100. [1,44,66]

The second step was retrieving data from MT5 to use for backtesting. The EURUSD on the 5-minute timeframe was used, going back the last year. The win rate was used as the determining factor of a good combination. For example:

1. [12,3,56]    ---> 20% winrate
2. [43,2,55]    ---> 24% winrate
3. [66,87,12]   ---> 12% winrate
4. [98,77,104]  ---> 34% winrate
5. [24,67,88]   ---> 56% winrate
.
.
.
100. [1,44,66]  ---> 10% winrate

In the example above, the fifth set of settings would be considered the best combination. The win rates are then sorted from the highest to the lowest. From these winners, we select the best 20% ie 1 through 20. These become the “parents”. Since every population requires 100 participants, 80% is obtained through reproduction. Reproduction can be flipping the fast period of two parents to create a new “offspring”. Emulates how chromosomes from parents form an offspring.

Some of the offspring then go through mutation. Mutation occurs by randomly flipping an element on an offspring. Assume the offspring after reproduction between 4 and 5 yields this:

[98,67,88]

This offspring can undergo mutation by randomly changing an element to this:

[98,2,88]

Once we have a new set of population, we run a backtest again and get the best individuals. This is done for 50 Generations. In the end, we have the very best individual after going through 50 generations. Now on to the results.

Before we can get to the results. Some interesting challenges:

  1. The genetic algorithm code was written in Python programming therefore we made the MACD calculations in Pythoon which might differ from what MT5 does. The difference is not much but it might have affected the results.

  2. Backtest results did not account for slippage or server delays.

  3. After running the algorithm for 3 days, I could not get past a 51% win rate. Meaning I might have needed to either keep the random range initialization function higher or lower.

  4. The algorithm was very time and resource-intensive.

Results

After two days of running the algorithm, here are the winners


{
  'fast_ema_period': 94,
  'slow_ema_period': 182, 
  'signal_period': 170
} 

Winrate: 51.28205128205128 % 

{
  'fast_ema_period': 94,
  'slow_ema_period': 182, 
  'signal_period': 170
} 

Winrate: 51.42857142857142 %  

I’m yet to test this on MT5 however, you can use the free bot I sent out yesterday to test these win-rates on MT5.

Food for thought:

  1. Would filtering trade times to only work during Asian market hours make this a better strategy? Considering the high volume that is traded during this time.

  2. Would implementing a trend indicator to filter out the raging markets improve the win rate?

  3. Which other indicator can we optimize?

  4. Would we have a higher win rate on another currency pair or timeframe?

PS: You can reply directly to this email to answer the above questions 😉 

You can download yesterday’s free bot from here. The instructions for loading the bot on a VPS are also included in the downloaded zip file.

That’s all for today! Happy trading!

Wamaitha Nyamu

Say hi 👋 on Instagram, TikTok , YouTube or LinkedIn

👩‍💻 Work with me on your trading bot here.