The Basic Strategy

I wanted to create a very basic strategy as a reference point and explore its behaviour using MetaTrader.   The MQL code for this strategy is listed in the table below.   This strategy basically buys after an up candle and sells after a down candle, however some of the additional key characteristics are listed below:

  • It will not enter a trade unless the StopLoss is greater than I_MinimumStoplossToTradeInPoints.
  • The StopLoss must be outside the Bid/Ask spread by at least the Slippage amount.
  • The StopLoss is placed near the Bid or Ask of the previous candle.
  • A TakeProfit level is set based upon an input I_RiskRewardRatio.   This is a basic calculation that ignores commission and swap impacts.
  • The StopLoss risk and maximum lot size are fixed inputs.
MQL Code:  EA_BasicStrategy
Class:  C_TradeManagement

Initial Result

The initial result of running this strategy between the 12th of June 2017 and the 12th of Jan 2018 is shown in the following Graph:

As we can see the initial strategy is loss making.   I believe it is worth taking a look at some of the outputs of the Strategy Tester, in particular the Report.

Model Quality

When investigating the modelling quality in the strategy tester report, this simulation has a quality of only 64%:

The problem with modelling quality is that it is very dependent upon the amount of data present.   The Grey area on the modelling quality represents the data that has not been modelled.   I don’t believe this is too important because if I am running a model in the 1 hour timeframe, then I expect 1 day and 1 month data to go back further and for this to be exlcluded in the model.  The Green bar changes depending upon how much data is present within the lower timeframes.   The simulation result above represents a model run from the 12th of June 2017 to the 12th Jan 2018 and has a model quality of 63.91%.

If I look at the 1 minute candle data within the History Centre, I can see that the 1 minute candle data does not go back as far as the 12th of June 2017, but only goes back to the 27th of October:

So, if I now run the strategy tester from the 1st of November to the 12th of Jan, I get the following report:

To get a high modelling quality, such as 90%, I need 1 minute bar data that goes back to the start of the simulation.   The problem is that it is very common for brokers to only provide a small period of 1 minute data.  In general, the difference between the earliest broker record and latest broker record, increases as the time frame gets larger: 

Historic Data from other Sources

It is possible to download more data using the MetaTrader history centre, however great care needs to be taken to ensure that the broker data does not replace the newly downloaded data.   It can in fact be beneficial to download the data first, turn the internet off and have a separate testing environment for importing the data.    I have found that if 1 minute data is downloaded for the desired period, then it is important to do the following two things:

  • Adjust the data so that it is in the same timezone my original broker data.   This makes comparisons consistent.
  • Expand the timeframes available using the PeriodConverter script which comes with MetaTrader for free.   It may need to be adjusted if you use symbols with unusual characters in them such as ‘.’

The following Reports show a comparison between the Imported Data and Broker data over different time periods:

Broker Data since 01/11
Downloaded Data since 01/11

 

Broker Data since 01/03
Downloaded Data since 01/03

As can be seen from these results, I can improve the Modelling Quality by importing more data.   However when I look at the more recent data (01/11) and analyse characteristics like the Total Net Profit and the Bars in Test, I can see that the imported historic data doesn’t produce the same results as the Broker Data, i.e. imported Historic Data is not the same as the Broker Data and will produce different results.

Sample Size

If the modelling quality goes down as I test my algorithm further back in time and the quality of the simulation isn’t necessarily improved if I import historic data, this then raises the question, ‘How much data do I need to test my algorithm to get a reasonable result’.

The following websites helps highlight the relationship between the margin of error and the square root of the sample size.   They recommend a sample size of at least 200 trades, but 400 to 600 trades would be preferable:

How long should you backtest a trading system ?    Relationship between Sample Size and Margin of Error

Strategy Optimization

I decided to try out the Strategy Optimiser on the sample of historic data and initially obtained the following results:

 Chart of Optimized Results since 01/03
 Report of Optimized Results since 01/03

These results help highlight an important consideration when using the Strategy Optimiser, i.e. that it is possible to over-optimize an algorithm.   These results show how I have just changed the inputs to a strategy that essentially just buys after an up candle and sells after a down candle, so that it makes a profit.   It is worth noting however, that after optimisation, the algorithm only produces 58 trades, i.e. not enough to produce a statistically significant result.   When I run the Strategy Optimiser on a larger data set, I obtained only 1 result on data that started in 2014:

Chart of Optimized Results since Yr 2014
Report of Optimized Results since Yr 2014

Clearly if I backtest the strategy too far, then I can increase the sample size.   Unfortunately the Chart looks extremely unstable and the P&L from the strategy isn’t very large.   This isn’t really surprising given the basic design of the strategy.   However it does highlight how easy it is to over optimise a Strategy.

Strategy Evaluation

Given the previous analysis, I believe that if I am going to evaluate a trading strategy, then I need to do the following:

  • On one version of MetaTrader, make sure that the connection is good and update the History Centre so that it contains as much Broker Data as possible for the Symbol that I am backtesting.   Refresh and review the data on each chart over all of the timeframes to ensure the data has loaded correctly.   It can sometimes help to add a very old historic record into the History Centre, refresh each of the charts, then delete the old record.   This sometimes downloads a larger range of data from the broker servers.
  • Backtest and optimise my trading strategy so that I have at least 200 trades within my simulation.   This should provide a Start Date for my backtest.   I then need to expand the time range by 1/3 so that I can perform an out-of-sample test.   Please see the following link for more information: Backtesting and Forward Testing.
  • Ensure that both the in-sample and out-of-sample tests produce good results.
  • Install a second version of MetaTrader for a test environment.
  • Install all of the necessary EA’s and import the data in a similar manner to the previous results and ensure that I can get identical results to the original backtest.   It may be worth recording all of the inputs and all of the results from both environments and ensure that they produce identical results.   By creating an identical test environment, I help to double check the setup for my original backtest.
  • Download lots of 1 minute historical data, import it into MetaTrader and also update the 5 minute, 15 minute etc timeframes using the PeriodConverter Script.
  • Although running the backtest against the imported historical data is less important, it may help confirm the results of the previous simulations.   I may choose to run the backtest against a combination of recent broker data, but older imported data.
  • Assuming that I can find a reasonable strategy, Forward test this strategy within a Demo Account.
  • Before running the main strategy, forward test it in the live environment but using a very low risk profile.

It is also worth remembering that as far as my backtest is concerned, I am always fundamentally limited by the following restrictions, which limit the validity of the backtest:

  • There is a limit on how far back the Broker data will go and imported historic data isn’t necessarily the same as the Broker Data.
  • I cannot model variations in the spread over time or predict how they will change in the future.
  • I cannot model variations in the commission or swap rate over time or predict how they will change in the future.

Add a Comment