Metatrader 101: Installing New Metatrader Code in Mt4

This is the one in a series of Metatrader 101 posts, partly so that I can have something to give to clients who are new to Mt4. It will cover the basics so people can get up and running quickly.
One of the great things about Metatrader is the amazing range of free code out there for extending and customising your charts with no end of scripts, indicators and EA out there, many of them free. But one of the first things that you need to learn is how to install these third-party pieces of code within your set-up. Here’s how to do it.
1. When installing code it will either be a .mq4 file which is the source code, or an .ex4 file which is the compiled code – with just the latter you won’t be able to see the underlying code. If people supply both all you need is the .mq4 file as your software will automatically compile it. Sometimes there are additional support files such as DLL libraries which you’ll need as well.
2. Unfortunately, the location on your hard drive of your Mt4 software is rather complicated as it’s split up into two locations. The bit where you need to copy your new software to is hidden away deep within the AppData section and you won’t easily find it by exploring the various folders. To help you with this there is a menu option which will open the location for you. From your Mt4 software go to File | Open Data Folder in order to bring up Windows Explorer opened at the correct folder location.
The folders within the MQL4 main folder
3.  You’ll see an MQL4 folder which you should open to reveal a series of sub-folders (see image above). Now what you do next depends on what you’re installing. If it’s an EA then it needs to go in the Experts folder, a script goes in the Scripts folder and indicators go (you guessed it) in the Indicators folder. Sometimes if the code is complicated you might also need to install various support files such as Dll’s etc which will go in the Libraries folder.
4. That’s basically it but now you need to get your Mt4 program to recognise the new files. There are two ways of doing this. The easiest way is simply to shut down and re-start the Mt4 platform. You should then see the new code in the appropriate section of the Navigator. The other way of doing it is to refresh the appropriate Navigator pane. For example, if you’ve installed a new EA then you need to go to the EA section on the Navigator pane, right-click and select Refresh. This will re-scan for all available EA code and your new EA should now be listed.
The various sections of the Navigator pane – right-click and select Refresh
That’s it! You can now make use of all the amazing pieces of Mt4 code that are out there.

Metatrader 101: Adding an EA to your Chart

This is the first in a series of Metatrader 101 posts, partly so that I can have something to give to clients who are new to Mt4. It will cover the basics so people can get up and running quickly.

An EA (Expert Advisor) is an automate trading strategy that will trade your account for you and hopefully make you money. In order to apply one to your chart you need to do the following:

1. Go to the Tools | Options menu option to bring up the Options dialog. On the Expert Advisors tab make sure that Allow automated trading has been ticked. You’ll see that there are other options there which disable automated trading when either the account or profile have been changed. It’s good to leave these on but it does mean that if you’re changing accounts regularly then you’ll need to check that automated trading is switched on each time you get started. Click OK once you’ve done this to close the dialog box.

Note, there is an alternative way of doing the above which you may prefer to use instead. Above the chart area there is space for some toolbars. Depending on what settings you have selected (View | Toolbars menu option to change these) you might have the “Standard” toolbar open. If you do then you’ll see this button:

If there is a green arrow next to the “man in a hat” icon then your EA’s are switched on. If there’s a red square then they’re switched off. Clicking this button toggles the EA’s on and off.

2. To add the EA to the chart simply drag and drop it from the Navigator pane on the left-hand side. The EA will first show a dialog box so that you can set it’s inputs. Once you’ve done this you should see a smiley face in the top right-hand corner of the screen which shows that the EA is active and working (see arrow in chart below).

EA smiley face in the top right-hand corner
3. That should be it. If you don’t see a smiley face then you need to remove the EA again by right clicking in the chart area and selecting Expert Advisors | Remove. The reason is probably because you’ve forgotten to do step 1 above so check that first and then repeat step 2. If it’s still not working then you need to look at the Experts tab in the Terminal window where the EA will output any error messages.

4. Removing an EA: as mentioned in step 3 right click in the chart area and select Expert Advisors | Remove. The smiley face icon will then disappear.

The Gambler’s Fallacy

I thought that I’d write about something known as the “Gambler’s Fallacy” as it’s something that crops up in trading (particularly FX trading) from time to time. The basic idea is that you can make money by betting cleverly and that somehow through doing this you can turn something that has no inherent edge into something that does. The clue as to whether this works is in the second half of the name – of course you can’t do this but it can be packaged up in a very alluring way that can trick the unwary.

 

Here’s how it works. Take any game of chance with a 50-50 win rate such as betting on black or red on roulette. Make a 1 unit bet on either and if you win then you simply collect your winnings and start again. If you lose then your P&L is currently -1 so this time you bet 2 units, one to cover your loss and one is the profit that you’re looking for. Again, if you win you’ve ended up +1 so far on the session but if you lose then you’re now -3 on the session. Once again if you lose you now bet 4 units to try and make back the 3 that you’re down and 1 for your net profit. I’m sure that you can see that pattern here, you keep doubling your bet each time you lose, hoping to make back your losses and your 1 unit profit. Eventually of course you will win and then you pocket your net +1 unit profit and start the whole process again.

 

It sounds fool proof and the appealing thing about it is that most of the time it works. In fact my daughter used this method at a college ball on a play money casino to break the bank, cleaning them out entirely! However, before you start reaching for your wallet and heading to the nearest casino, the downside to the method is that eventually you’ll hit a losing streak so big that you’ll hit either the house limit (the maximum bet size that a casino will accept) or you’ll run out of money. It’s unlikely but of course eventually it will happen just as eventually it will come up ten blacks in a row on roulette.

 

You also have to be aware of how quickly the bet stake grows. Say you come to the table with 100 units.

After 1 loss    you’re left with 99 and betting 2
After 2 losses you’re left with 97 and betting 4
After 3 losses you’re left with 93 and betting 8
After 4 losses you’re left with 85 and betting 16
After 5 losses you’re left with 69 and betting 32
After 6 losses you’re left with 37 and betting 64

… except that you can’t because you don’t have enough money left! So with 100 unit table stake, 6 losses in a row will leave you with only 37 units left and unable to recoup your losses. What’s more, this event will happen 1 in every 64 rolls so it’s more likely than you think!

The Trading Equivalent

So what has all this to do with trading? Well, the other day I came across a “foolproof” way of trading which was basically this concept though package up in a rather elaborate way. You can download the pdf here if you want. It basically involves setting up a breakout strategy. If it wins then you’re done for the day, if it loses you take a trade in the opposite direction and (you’ve guessed it) you double your size. Keep doing this until you’ve made your money for the day or you’ve wiped out your account.

Grid Trading

There is one other area where this sort of concept comes up which is grid trading. This basically involves buying and selling at fixed intervals on a chart usually in a mean reversion way and hoping that the market reverses after trading. Usually these strategies are offered as an EA (Metatrader Expert Advisor) of some kind which promises untold riches. There are all sorts of elaborate variations on this theme, which generally involves doubling your stake size or at least taking on increasingly large positions in order to try and make back your previous losses and a little bit extra for your profit. They all suffer from the same Achille’s Heel that the Gambler’s Fallacy does, namely that eventually the market will keep going against you and will wipe you out.
A client of mine spent quite a lot of time testing out various versions of these, trying to find a combination of parameters that would somehow avoid the inevitable. I kept telling him that no variation on this theme was going to work but that wasn’t what he wanted to hear. I don’t know whether he saw the light in the end, I hope that if he did it wasn’t at the expense of his entire account!

 

How Good is Your System?

How good is a trading system? How can you compare the performance of two different systems and make a judgement as to which you would rather trade? Originally published in “Traders Magazine”, in this article I explore the various different ways of assessing the merit of a trading strategy.

Strategy Development Process

There are many different software platforms available which offer the ability to develop and test trading strategies on historic data and many traders are out there working away trying to develop a winning system that will make them rich. Whilst the software functionality makes the process of testing a strategy relatively straight-forward many have found to their cost it is not as easy as it looks to translate these virtual test results into actual hard cash. Many a trader has come unstuck when his system, which performs wonderfully on historic data, falls flat on its face when trading with real-money. This leads to an important question: how do you measure how good your system actually is?

 

Typically when developing a system there is an iterative process which involves coding up an idea and seeing how it performs and then assessing that performance according to some figure of merit. The system is then adjusted or often parameters are optimised to achieve the best figure of merit for the test data. When system developers work on systems they often assume that the only figure of merit worth looking at is the net profits that are generated by a system and when optimising over a range of parameters they will often use this figure alone to select the optimal parameter set. However this can be a far from ideal approach to system development and can often be misleading. For example the system could have one huge winning trade and the rest are losers but still come out as the best profit figure. Whilst this system might have generated the highest total return over the test period it is relying on the one huge trade to make up for everything else and the circumstances which lead to that trade may have been unusual. This means that the test results won’t be very reliable. It is therefore important to look at a number of the strategy performance figures when assessing how good a system is.

Percentage Profit

There is an equation for the average profit per trade of any trading strategy which (before costs) is:

Avg Profit Per Trade = Pwin x Wavg – Plose x Lavg

where
Pwin =  percentage winners
Wavg = average win size
Plose =  percentage loses
Lavg =  average lose size

At break-even this average profit per trade is zero, so re-arranging this and using the fact that Plose=1-Pwin gives us:

Wavg/Lavg = (1-Pwin)/Pwin                    (Equation 1)

This represents the relationship between the respective average trade sizes and the percentage winners for a break-even system before costs. It is always amazing that system vendors (usually the less reputable ones) advertise systems as 90% winners in S&P’s etc. From the above equation it can be seen that just because a system has 90% winners there is no guarantee that it is profitable. For example a naked out of the money option writing strategy might have a 90% win rate but the losses when they occur will be many times the average premium collected and the strategy could still be a net loser. Without knowing the relative sizes of the wins and losses as well it is not possible to assess whether the system is net profitable or not on the basis of percentage profitability.

 

Whilst a high percentage win isn’t necessarily a pre-requisite to a profitable system, it does give an indication of how frequently you might expect profits to come along. It’s no good if you develop a long-term trend-following system if you are a short-term scalper at heart so this figure is important in giving you an idea of what your system is like. It helps to think of what sort of system you are developing: a long-term trend following system might for example only be profitable 20% of the time but the average winning trade is much larger than the average losing trade. Using our equation above we can see that for the system to break-even you need Wavg/Lavg to be 4, that is the average winner needs to be 4 times the average loser for the system to break even with this win percentage.

Case Study 1

Recently a client asked me to look at a set of parameters for a system that he was developing. He had chosen the parameter values based on optimisation of the total profit. When I looked at the performance figures the percentage profitability was only 7%! The optimisation process had resulted in the stops being incredibly tight which meant that the position sizing algorithm selected very large sizes to trade. This meant that most of the time he was being stopped out and in fact there were only three winning trades in the whole test but they had made very large profits compared to the numerous losses. Whilst this might still be a viable system it is important to think what it’s going to be like to trade this method. Day after day of losses waiting for that occasional big win – and what happens if you happen not to take that trade? That one trade makes up for all the losses and without it you have a losing strategy. This means that the system will psychologically be difficult to trade and also the test results are not very reliable because of the small number of winning trades.

The Number of Trades

This figure is not important in it’s own right except that too few trades means that the performance metrics of the system are less reliable. It also gives you an idea of what it will feel like to trade the system: will you be waiting for long periods for a trade to be generated or will you be constantly in and out of the market?

Case Study 2

A client had done an optimisation and wanted to know why the optimal parameters that he was getting were different from the ones that I suggested. I looked at his optimisation results from which he had simply picked the best Net Profit and noticed that his parameter set had only traded once during the entire test period and had held the position open for many months on what was supposed to be a short-term swing trading model. It is always a good idea to check that the trade results are sensible and what you expect from your model.

Average Profit Per Trade

This is always an important figure to look at because of trading costs. Equation 1 above did not include trading costs but these are an important part of the reality of being a trader. When testing you should always include realistic estimates of total trading costs including commissions and slippage. The average profit per trade after costs tells you how much profit there is left after you have paid all the costs. If this is rather small then it is important that you make sure that your trading cost estimate are correct. For example if you estimate that the total costs are 1.5 ticks for your system and your average profit per trade before costs is only 2.0 ticks per trade then you have better hope that your 1.5 tick estimate is correct because if the real costs are 2.0 ticks per trade then you won’t be making any money. This also tells you that your costs are 75% of your gross profits which is very high. For a high frequency trading strategy this could still be viable but you might want to work on reducing your costs in order to increase profitability.

Maximum Drawdown

Drawdowns are always an important figure to look at when assessing a system. They are a somewhat tricky figure to assess as they depend on a run of consecutive losing trades in order to create the drawdown which means that the order in which the trades occur makes a difference to the size of the drawdown. Now a run of losing trades can occur by pure chance just as when tossing a coin it is possible for it to come down as head eight times in a row, say. Alternatively a drawdown can occur because the prevailing market conditions at the time of the drawdown were not suited to the method. It is always useful to have a look at your system to try to work out whether a drawdown was generated by chance or because of the prevailing market conditions.

 

It is often useful to look at the ratio between the drawdown and the annualised profit return. For example the annualised return might be 20% and the maximum drawdown might be 15%. This is a ratio of 1.33 which is a reasonably healthy one. Anything over 2 is good, over 1 is ok, below 1 and your annual profits are starting to look a little small compared to the drawdown figure.

 

Drawdowns also have an impact on the initial account size: if you experience the maximum drawdown when you start trading a system you are obviously going to need at least the maximum drawdown plus margins to trade the system. However due to the uncertain nature of drawdowns there is always the possibility that the next drawdown will be even larger than the maximum one that occurred during your back-testing. You want to consider how large a drawdown you or your investing clients could stand before pulling the plug on the system. It is an unfortunate fact that investors tend to bail out of a system during a drawdown just before it turns round and tend to invest after a good run up just before it starts to draw down. It is therefore a good idea to keep the drawdowns to a manageable size even though this will mean trading smaller size and therefore reducing the profit potential. A professional fund manager for example might scale his trading size for a system down so that the maximum drawdown is only 5% if that is all he feels that his clients could stand.

 

It is also worth looking at the duration of a drawdown: a drawdown that lasts a year is pretty tough psychologically as well as meaning no income (from performance fees or from trading profits) for a whole year. How well could you handle such as situation? It’s one thing to look at an equity curve over 15 years with a good general up trend and to see a six month drawdown half way through this period in this context. It’s another thing actually to be in the middle of it wondering when or even if it will ever end. It’s at times like these that you can start to doubt your system. Therefore it is always worth studying your system’s drawdowns carefully.

Annualised Return

This is an often-overlooked performance figure but one which is very important as it tells you how much money you are going to make each year using it. If you are looking to make a living from a system you need to know whether it is going to make sufficient profits for you to live off. Professional money managers for example would are happy with a return of 20% with a maximum drawdown figure of 5% but if you have only 10,000 Euros or Pounds to trade then, unless your living standards are pretty modest, you are probably not going to be able to live off the profits that such a system would generate. Individual traders tend to tolerate larger drawdowns in order to gain increased profits but you still need to do this calculation and to check that you cal tolerate the larger drawdowns that go with larger profit potential.

Trade Distribution

When looking at the trade distribution you are looking for this to be fairly even. You don’t in general want all the profits to come from one or two huge trades though of course for some long-term trend following methods this may in fact be the way that that the system works. The more widely distributed the trades are the more volatile the equity curve is going to be. In fact when we come to look at the Sharpe ratio we’ll see that in general the lower the standard deviation of the trade results the better the system is.

 

The trade distribution will tend to vary according to the nature of the system anyway: a short term scalper will tend to have lots of trades all the same size whereas a long term trend follower is looking for a few huge winners to offset the numerous relatively small losses.

Sharpe Ratio

This is a measure that is particularly used by fund managers. It is a measure of the amount of profit that is being generated relative to the risk that is being taken.

Sharpe Ratio =     Rann / SDann

where
Rann   = Adjusted Annualised Return
SDann = Annualised Standard Deviation of returns

The Adjusted Annualised Return is meant to be the annual return over and above the risk-free rate of return, the idea being that you would otherwise have your capital in the bank earning risk-free interest. Whether you need to subtract off the risk-free interest rate from your rate of return depends on what instrument you are trading: for highly leveraged ones such as futures you are only tying up a small amount of your capital to the trade so you can still be earning interest on the bulk of your account whereas for an unleveraged stock position you are losing the interest that you would otherwise earn and so have to subtract it off from the return. In general to keep it simple and since I tend to work mostly with leveraged instruments I leave off the risk-free interest rate subtraction.

 

The annualised SD of returns is the standard deviation of the return figures multiplied by a scaling factor to annualise it. The scaling factor will be explained below.

 

The calculation of the Sharpe Ratio is best explained by way of a simple example. Consider the monthly returns of a hypothetical trading system over two years below:

Month  Year 1   Year 2
Jan    2.45%   -0.91%
Feb    0.84%    1.23%
Mar    1.20%   -0.24%
Apr   -1.34%    0.89%
May    0.67%   -0.28%
Jun    0.80%    0.44%
Jul   -0.20%   -0.61%
Aug    3.20%    1.16%
Sep   -0.59%    1.74%
Oct    0.65%   -0.41%
Nov    1.18%   -0.68%
Dec    1.35%    3.10%

Using standard spreadsheet functionality one can determine that the average monthly return is 0.65% and the standard deviation of the returns is 1.21%. To annualised the return you simply multiply the average monthly return by 12. To annualise the standard deviation of the monthly returns you multiply it by the square root of 12. This gives annualised figures of 7.81% and 4.18% respectively. Thus the Sharpe Ratio comes out to be 7.81 / 4.18 = 1.87 in this case which is a pretty reasonable figure.

 

You can probably see that in calculating the Sharpe Ratio the numerator has the scaling factor (12 in this case) and the denominator has the square root of the scaling factor which means that the formula can be written as:

SR = squareroot( Scaling Factor ) * Ravg  / SDret

Where
Ravg  = average of returns
SDret = standard deviation of returns.

For monthly returns the scaling factor is 12, for weekly it is 52 and for daily it is around 260. You will get slightly different answers for your Sharpe Ratio calculations according to your sampling interval. The standard that is usually used amongst fund managers is the monthly returns.

 

The point of the Sharpe Ratio is that it measures performance relative to risk. If you take the system results in the table above and leverage them all up by 10:1 then the annualised return goes up to 78.1% but the annualised standard deviation will go up by the same factor of 10 so the Sharpe Ratio will come out the same. This makes it an extremely useful figure for assessing relative performance figures when different leverage is being used.

 

A very good Sharpe Ratio is anything over 2 but anything over 1 is worth looking at and 1.5 is a reasonable figure. This is a very useful figure of merit and one which is well worth calculation. Performing optimisations using the Sharpe Ratio can often give better parameter selection than other methods.

K Ratio

This ratio was developed by Lars Kestner. It is based on the simple idea that the best equity curve is one which goes up as a straight-line with as steep a slope as possible. Therefore the best measure of a trading system is how closely it follows the ideal. A straight line fit is first made to the equity curve and the slope of this line is measured. Next the deviation of the equity curve from the straight-line fit is evaluated: usually the standard error of the straight-line fit is used. The ratio is then given by:

K-Ratio = Seqc / ( Eslp * Square Root (Np) )

Seqc = Slope of Equity Curve
Eslp  =  standard error in slope from straight-line fit
Np    = number of point in equity curve.

Again this is best illustrated by way of an example. We will take the same returns as in the previous table and use them to construct an equity curve starting with 100,000 in whatever currency you choose:

Month  Year1         Year2
Jan    102,450.00    109,595.18
Feb    103,306.46    110,942.01
Mar    104,548.96    110,671.60
Apr    103,149.93    111,656.57
May    103,842.94    111,344.48
Jun    104,669.91    111,834.40
Jul    104,456.87    111,148.68
Aug    107,799.49    112,438.00
Sep    107,159.13    114,394.42
Oct    107,859.80    113,929.28
Nov    109,130.45    113,154.18
Dec    110,599.76    116,661.96

Next because the equity curve is compounded (each 1% gain is applied to the previous equity value not just the original 100,000) we need to take the log of the equity value. Otherwise the ideal equity curve is going to resemble an exponentially increasing curve rather than a straight line. Note however that if the equity curve is uncompounded then there is no need to take logs first. Next we fit a straight-line to this log equity curve. In Excel this is easily done using the LINEST function. This will give you the slope of the curve which is 0.002221 and the error in this slope value which is 0.000118. The K-Ratio is then given by:

K-Ratio = 0.002221 / ( 0.000118 x sqrt(24) ) = 3.84

which is a very good value. From the plot below you can see that the curve looks good and the straight-line fit closely matches it which is why the K-Ratio value is so good.

This is another indicator which is a good general performance figure and again which can be used for optimisations.

Visual Inspection

We’ve looked a variety of different ways of assessing a system’s performance including some more sophisticated methods such as the Sharpe and K- Ratios. However, one of the best ways of assessing a system quickly is simply to look at the shape of the equity curve. A quick glance at this tells you a lot about the system and what it is like. Does the curve slope smoothly upwards or are there prolonged periods of drawdown? Are there a few large spikes upwards indicating that a few huge winners are making up the bulk of the profits? Are there periods of prolonged sideways movement and if so to what period in the market do they correspond? This can often tell you something about the nature of the system and what its weaknesses are. It is for this reason that the equity curve plot is the first thing that I look at for any system.

Spreadsheet and Equity Curve Model

To help you get an understanding of some of these metrics there is a spreadsheet available for download here. Here you can see the details of the calculations of the Sharpe Ratio and the K-Ratio.

 

In addition to the above calculations, in the spreadsheet there is a formula for generating hypothetical equity curves using the model:

Current Return = Previous Return * ( 1 +  Trend + Noise * (0.5-RAND))

where Trend and Noise and user-input parameters for the equity curve model and RAND is a random number between 0 and 1 so 0.5-RAND is a random number between +0.5 and –0.5. You can vary the Trend and Noise parameters and see what sort of equity curves are generated (they are plotted in a chart) and look at the Sharpe and K Ratio values and other performance metrics. From this you can see that the larger the noise factor compared to the trend factor the more variable the performance is and the lower the performance metrics. Nevertheless it is surprising that even with a low trend factor for some of these essentially random equity curves they look good and have good figures. This is always worth bearing in mind least we look back at a good period of trading and conclude that we must indeed be expert traders – it could all be random!

Conclusions

Unfortunately there is not single measure of system performance that can be used to give an indication of how good a system is. Instead the developer needs to go through a variety of different measures in order to get a feel for the strengths and weaknesses of a system. We have seen how the Sharpe Ratio gives a good measure of the returns for a given amount of risk and how the K-Ratio gives a good assessment of the overall equity curve shape. It is important to take time when assessing a system, to understand why it is behaving in the way that it is so that one can incorporate this information into improving it still further. With this insight you can develop better trading systems.

Web Resources: Forex Live

There are plenty of forex news portals around besides Forex Live but this is the one that I use to keep in touch with what’s going on in the forex world. It’s a pretty straight-forward site with one central page where all the latest news and comments come up in real-time without the need to refresh anything. If you want to drill down into a particular article to see more and to look at the comments then you just click on the headline. There are sub-headings across the top which filter the posts by sub-category but I generally just keep on the front page. There is a team of several reporters who maintain rolling 24/5 coverage across all time zones. Posts include previews of impending news releases, actual numbers when they’re released (and they’re usually pretty quick about getting this out), general breaking news that might affect the markets, key orders levels (obtained by phoning around the big banks), option expiry news and some light technical analysis. Many of the team trade their own accounts and occasional give their trading opinions.

All in all it’s just what you’d expect and need from a forex news portal and it’s an integral part of my daily trading routine.

click to enlarge