Machine learning can help us optimize automatic trading strategies. By studying the huge amount of past information, we can identify patterns that help us predict the evolution of the market to a sufficient extent. This is of course what some traders have been doing for a long time but the automatization of the process allows us to find much better strategies and much faster than it would take a human. Here we propose a speculative strategy that has been successfully tested and demonstrates the possibilities brought by machine-learning in forex.

Automatically finding a winning speculative strategy on eurusd

EUR/USD is a very lucrative pair for a speculative strategy built from machine-Learning algorithms, although our method is able to find winning strategies on other instruments and some that work across several instruments, the strategies developed for EUR/USD give the best returns. This is how the strategies are built.

We cannot feed the actual price to the algorithm because we want it to recognize patterns independently of their height on a chart. We therefore feed it price movements, from high to high and low to low (better than open to close). This is a simple kind of indicator with a surprising discriminative power between forex patterns. But what intervals should we choose for our indicators and using the high and low over what period?

Our algorithm answers this question for us by optimizing a set of indicators and scoring it by how good a strategy we can build upon it. The method used for optimization is a genetic algorithm. We build a few sets of indicators; the strongest ones (higher score) have a better chance of “reproducing” and “mutating” while the weaker ones are replaced. This method tends to optimize the indicator sets and it is thousands of times faster than simply trying out every single possibility.

We score a set of indicators by how good a strategy we can build with it. The strategies are automatically built using a second optimization step and a data structure, the artificial neural network. The neural network takes as input the indicator values for a certain period and outputs some information about the future of the instrument.

What does the artificial neural net predict

The neural nets attempt to predict a normalized profit factor (gross profit dividedby the gross loss) on a single trade over a certain period in the future. The period in question can range between 3 and 10 days, it is an optimizable parameter of the strategy. Therefore,our strategy doesn’t necessarily use stop losses and take profits, instead, we open a position for a predetermined amount of time and close the position at the end of that period, whatever happened. The net is graded by the percentage of correct predictions weighed by it’s accuracy.

Alternatively,our neural nets can predict what share of the account balance should be invested, the nets are then graded by the final balance of the account or the overall profit factor.

Common traps in automatictrading strategies

There are some common pitfalls to be aware of in such strategies where the strategy seems to offer amazing profits but is worthless in real life. The most important precaution is that the period on which the strategy is tested should not be the same as the period on which it is built. Otherwise we can simply generate thousands of complex random strategies and choose the one that works best on one particular period, but it’s only when we have a positive result on an independent set of data that we can start trusting our strategy. In reality we use three time-independent sets of data, the training set is used to build the system, the validation set is used to avoid "over-learning" and the test set is used for the results reported. In our case, we reach >60% correct bets on the test set that spans over the last year.

It is also bad practice to optimize a take-profit and stop loss from the start. When optimizing a strategy on a period that is too short, one can easily obtain an amazing overall profit by setting the take-profit very near and the stop-loss very far. When the stop-loss is eventually reached on a longer period, the consequences are devastating. Once a profitable strategy is found however, the take-profit and stop loss can be optimized but they should never be too distant from one another. Placing a take profit and stop loss is never a strategy in itself, rather a way to control risk.

An optimal strategy tested with a recognized simulator

Our strategy obtains a theoretical 62.5% correct bets on EUR/USD. But we can obtain a better assessment of the strategy with a good simulation and a real life application of the strategy. For this reason we implemented the strategy using the JForex API and tested it on the jForex platform. Once again, we were careful not to mix the period we used to optimize our strategy and the period we used to test it. We also refined our strategy some more adjusting the amount invested on each position to reflect the strategy’s
predictions. This greatly improved the profit factor (gross profit divided by the gross loss) of our strategy. We use a leverage to increase or decrease the risk and expected return.

Over 161 trades, the profit factor of our strategyon the test period is 2.87! That means we obtain 2.87 times more profit than drawdown in trades. Although we only get 60.24% profitable trades, they are much more profitable than the losing trades are un-profitable. The final statistics we find very telling is the maximum consecutive drawdown, 5%, and the maximum consecutive profit, 18% of the equity. We have a live account running the strategy but it has been doing so for far too small a time period to assess it this way.

We also optimized a take-profit and a stop-loss.Since we refuse to see those to factors as strategy parameters but would rather see them as risk control parameters, we always keep them equal to one-another. The opposite creates a disequilibrium that makes it difficult to assess the strategy. Once again, those parameters were optimized on a period different than the test period. The results show that a stop-loss and take-profit should indeed be used and that it should be placed very close, at around 18 pips. Placing those closer to the opening price does improve the overall profit factor but at those levels, commissions and fees become troublesome. The stop-loss and take-profit improve the profit factor and the overall stability of the strategy while they do impede on total profit but leverage can remedy the situation.




Drawbacks of an automatic trading strategy

A common critique concerning black-box strategies such as ours is that the market can always suddenly change and strategies that worked before won’t work indefinitely. We have to admit that this is totally founded, and it is our belief that nothing can be done to avoid this without a crystal ball to foresee the future. However it is also our feeling that this is the truth with any speculative Strategy, man-made or otherwise. It is clear that the forex has suffered major changes in the past. The volume is a great indicator for that matter; it really gives us an insight on the moment when the way an instrument is traded changes. On the chart below you can observe the evolution of volume for EURUSD in the last 16 years. A strategy built using data that is too distant doesn’t work anymore. However, our strategy has worked equally well on EUR/USD for the last few years and nothing hints that it will change anytime soon. There are two things we can do to guard against a sudden change in the way forex instruments are traded. First,
we can monitor the market and wait for that moment when our strategy doesn’t work anymore using the statistics that the strategy should follow like the maximum consecutive drawdown and by monitoring the volume. Secondly, we can do what’s called on-line learning where our strategy is continuously being optimized on new data. This second option is good practice but it doesn’t guard against the sudden changes that are typical in forex every few years. The best solution is to implement both of those methods by regularly optimizing our strategies while being aware that a more profound change in strategy will ultimately be necessary.


The other critique is that we never realy understand what a black-box model does. That is not the case for us as our model is quite simple indeed, however we shall take the secret to our grave or at least until the strategy isn't profitable anymore.

Thanks for reading,

Emeric Beaufays
Translate to English Show original