Perfect Test Environment For Algorithmic Trading Enthusiasts

Testing algorithms is probably one of the most mi-understood and least talked about subject in the world of algorithmic trading.

In this article we will attempt to discuss the most efficient method that we have used over the years to build, test, analyse and perfect trading algorithms that are consistently profitable in real market conditions.

Building Forex Trading Algorithms

Regardless of whether you’re writing your algorithms for the MT4/Mt5 platform or cTrader or FIX API, you need to take into account the fundamental basics of how the algorithm will work. For the scope of this article, I will limit my discussion in this area to MQL based algos designed to work on MT4 or MT5 platforms.

Algos based on indicators?MQL provides a handy way of incorporating any indicator with output buffers into an expert advisor (EA) using the iCustom directive. However, before you decide to use an external indicator as the basis for trading decisions made by your EA, you need to be aware of the redraw factor of that indicator. Most indicators, as you know, are notorious for repainting indicator signals several candles back. Such indicators are clearly of no use to an EA because the time for making a trade decision would have lapsed by a few candles and the market would have already progressed from its optimum entry point by a long shot.

I found a very interesting tool using which you can check the redraw factor of any indicator before using it in an EA.

At any rate, you are always better off integrating the logic that runs the indicator, directly inside your EA to minimize external computation needs which will eventually speed up your EA.

Handling Stops
There are many schools of thoughts about whether or not an EA should place hard stops on a trade or open every trade with no stops and adjust things along the way. Debating either side of this argument is beyond the scope of this article but from my personal opinion, I never produce an EA without the option of implementing a hard stop with trailing including a step parameter for defining how many pips the stop should advance once a certain target number of pips has been achieved.

Trailing stop loss is a thing of art – especially when it comes to computing the trade off between risk and reward. Most of the algos I built always used a trailing step parameter and typically for EAs running on lower time frames like M1 or M15, I found that a trailing step between 5 and 20 gives optimum results – however, this cannot be seen as a rule of thumb and the trailing step parameter should be assessed carefully in conjunction with the EA you’re writing.

Testing Environment – Backtesting
As with any development project, you must have an appropriate sandbox or testing environment where you can reliably and efficiently test your algorithms before forward testing them in a real account. One of the biggest challenges with backtesting is the accuracy and reliability of tick data. Most brokers do not store tick data beyond 45 days on their servers and if you are planning to test your algo on your brokers’ tick data for the past 2 years, you are guaranteed to have “fake” results.

Hence, I recommend setting up a separate test environment using the steps below:

Download a plain and simple version of MT4 from the metaquotes website (not from your broker)?Open a demo account but do not login to that demo account – never login. If you do, it will overwrite the tick data you are about to store in there for testing
Download an install Tickstory Lite to export accurate tick data from a specific source
Now go back to your MT4 and you will have real, accurate tick data for all your backtesting.

I have deliberately not made a tutorial out of this because these are third party products and I am not sure how they will work in your own windows environment. I’m not a windows expert and hate that OS with a vengeance so the less I talk about it, the better my mental peace remains.

When you start a backtest, always start with the last 30 days as your first test period. If you don’t like the results, don’t start changing all the parameters at once. Change one parameter, test again and fine tune things one parameter at a time. Once you get to a level where you’re 50% satisfied with your EAs results for the 30 days, you need to go ahead an run an optimization test on it.

Then use the most optimized test over a longer period extending it 6 months out at a time.

Once you have tested your EA for 12 or 24 months in the past, the set that delivers the best performance should be your “A-Set”. The next best set should be your “B-Set” and so on. I usually go up to D in my backtests.

Testing Environment – Demo
Its a great fallacy to think that your backtested EA will work really well in a demo environment when you do a forward test. Backtesting was done based on historical data. There’s no guarantee that what worked yesterday will work today and also tomorrow. The other problem with demo testing is the “fake” market conditions that existing in all demo environments, especially MT4.

In a demo environment, your trade does not get executed by a bank. The MT4 platform simply accepts all your trades as a B-Book with random execution delays to give you the impression of real market conditions. The price feed is real though so forward testing the EA in a demo account is still a good way to make sure your code is functional. Beyond that, there’s not much more you can test accurately in a demo environment.

Testing – Live Account
The only way you can know for sure if your EA will make you money is if you test it on a live account with real money using your A, B, C and D sets. The best way to do it is to have 4 separate accounts with $100 each. Use 0.01 trade size or the lowest size possible with your EA. Run the test for 7 days simultaneously on all 4 accounts. Measure the performance of the 4 sets and make a decision after that.

Analysis Tools
Many aspiring traders go through the hard work of building and testing algos but give up on fine tuning them to their maximum potential. This is because the breakdown occurs when you try to analyse the results of your test. With every black box algo you need to analyze multiple factors. The most important being:

  • Drawdown – how much of your capital is tied up for each trade
  • Trade Stagnation Period – how long does a trade “stagnate” while trying to reach either the profit or the loss target
  • Profit Factor – simple. Anything over 1 means you’re going to make money.
  • Average Win Vs. Average Loss – Depending on the overall percentage of winning trades, you will need to judge whether or not its acceptable.
  • Trades per day – Very important to know how many trades a day is the EA placing (tells you which are the best days for this EA)
  • Trades per hour of the day – Same as above – tells you which sessions are best to trade for this EA
  • Pips of profit per trade – Use this to calculate your profitability for higher account balances
  • Pips of loss per trade – Use this to calculate the losses you might incur for higher account balances

Knowing these will help you fine tune your EA very quickly. For example, if you are seeing a very high trade stagnation period then you know that either a) your stops are too far away, b) your entry signal went horribly wrong, or c) your drawdown is very large.

A quick way to fix this problem is to use a lower stop loss value or introduce a Max_Trade_Duration parameter which will close the trade after X period regardless of whether it is in profit or not. But why? Because, if you want to reduce your trading risk, you never want to keep multiple positions open in the same direction – this will only compound your losses. An Algo’s objective should be to free up the trade context as quickly as possible so the next trade can be placed, even if that means to accept a loss – this is where the algo’s unemotional temperament replaces the human in a good way.

Recommended Tools

  • MT4 Terminal
  • Tickstory Tick Data
  • A Demo Account
  • A Live MT4 Account
  • MyFXBook or Quant Analyzer for analyses

I find myFXBook and Quant Analyzer by Strategy Quant to be good for doing this type of analysis. With Quant Analyzer, you can combine results from multiple accounts/symbols into a single unified portfolio. This is exceptionally powerful when you’re trying to take a strategic approach to building a portfolio of trading strategies.

Questions, suggestions and feedback welcome.

About
After having traded the markets for years and years, I got fed up of the skullduggery that I witnessed among brokers and decided to do something about all this injustice. Manhattan Global Markets is my initiative to provide good traders a great brokerage service.

Leave a Comment

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text.

Start typing and press Enter to search