Archive for the ‘Expert Advisors’ Category

MQL4 helper functions for calculating bar sizes

Sunday, February 21st, 2010

Here are some more MQL4 helper functions for MetaTrader. You can use them in your expert advisors and custom indicators. The three functions will allow you to quickly get the size of the upper and lower wicks of the candle stick, and also the size of the candle stick body. To use them you will also need some of the code I gave you in my simple MetaTrader MQL4 helper functions post.

If you use these functions in your code please give a reference to this blog post.

double UpperWickSize(int bar)
   if (UpBar(bar))
      return (High[bar]-Close[bar]);
   if (DownBar(bar))
      return (High[bar]-Open[bar]);
   return (0);
double LowerWickSize(int bar)
   if (UpBar(bar))
      return (Open[bar]-Low[bar]);
   if (DownBar(bar))
      return (Close[bar]-Low[bar]);
   return (0);
double BarBodySize(int bar)
  if (UpBar(bar))
    return (Close[bar]-Open[bar]);
  if (DownBar(bar))
    return (Open[bar]-Close[bar]);
  return (0);

Simple MetaTrader MQL4 helper functions

Saturday, January 30th, 2010

Here are some simple functions that you can use in your forex expert advisors and custom indicators written in MQL4 for MetaTrader.

If you split out commonly used functionality into separate functions with logical names (as I’ve done here) then you should find that your MQL4 code becomes more readable.

If you use these functions in your own code please reference this blog.

// returns the value of the top of the bar
double TopOfBar(int bar)
   return (MathMax(Open[bar], Close[bar]));
// returns the value of the bottom of the bar
double BottomOfBar(int bar)
   return (MathMin(Open[bar], Close[bar]));
// true if this is a down bar
bool DownBar(int bar)
  return (Close[bar]<Open[bar]);
// true if this is an up bar
bool UpBar(int bar)
  return (Close[bar]>Open[bar]);
// size of whole bar including the wicks
double HighLowSpread(int bar)
   return (High[bar]-Low[bar]);
// gives you the result of subtracting the lowest input value from the highest
double HighMinusLow(double value1, double value2)
   double high = MathMax(value1, value2);
   double low = MathMin(value1, value2);
   return (high-low);

Hope you find these useful.

Disadvantages of expert advisors

Friday, January 15th, 2010

Following on from my advantages of expert advisors post here are some disadvantages of trying to write and use expert advisors.

Curve fitting

Because it is easy to backtest expert advisors many times with different parameters it is very easy for them to suffer from curve fitting.

Curve fitting means that the EAs are highly optimised for the specific period of time for which they have been tested against, but may turn out to be loss making if run on actual live data.

A good expert advisor will not need to be micro-optimised in order to make a profit. There may well be certain values that perform better than others, but a range of values around the optimal ones should also be profitable to prove that the expert advisor isn’t just working because of some very specific circumstances that are unlikely to occur again in future.

As well as curve fitting the parameters it is very tempting to add new code branches into the expert advisor to filter out bad trades and allow though good trades. Again this is likely to produce an expert advisor which is highly optimised only for the historic data on which you are testing it.

Curve fitting over-optimisation is a very easy trap to fall into when writing expert advisors. It is a waste of time, as you won’t end up with a profitable expert advisor. Spend that time on writing quality expert advisors which can handle whatever data you throw at them.

Unrealistic backtests

Many pieces of trading software that allow you to backtest EAs do not allow you do it in a way that realistically simulates the real market. Doing so would be very difficult because there is a lot more information you need to know other than the price. E.g. you need to know about the spread, execution delay, slippage, and liquidity.

Backtesting data does not tend to give you access to this kind of information. In addition the granularity of backtest data is often much less fine than what you would get if you were trading live.

Most backtesting data is only available for 1 minute and above timeframes. This means that you miss out on the tick-level information that could be essential, especially if your trading strategy works on shorter timeframes.

Many backtesting software packages compensate for this by ‘filling in the gaps’, and create simulated ticks. This can be dangerous, as the algorithm used may well not generate ticks in a way that is realistic enough for your strategy.

It is quite possible (in fact easy) to create an expert advisor that works brilliantly on historical data using a backtest but consistently loses when used for real.

Difficult to write

Translating a working strategy from your brain into an expert advisor can be very difficult.

When you trade manually you can interpret huge amounts of data and news in a way which can be very difficult for a computer program to do. Interpreting simple facts such as the value of an indicator is easy for an EA to do. However interpreting something such as the current economic climate or the latest news that has just been released is extremely difficult.

Getting EAs to recognise complex chart shapes and patterns can be very difficult. It is all to easy to program the EA to recognise specific patterns that occurred in the past rather than getting it to understand the meaning of the chart patterns in the way that your brain will.

Expert advisor bugs

All software beyond trivial code snippets will contain bugs. For most pieces of software the consequences won’t be too bad. Maybe the program will crash, get stuck, or if you are unlucky perhaps you will lose some work.

Expert advisors for forex and CFDs are somewhat different as they have the power to make decisions that affect your bank balance! If your EA has a bug then it could make the wrong decision resulting in a loss of money. If the bug is bad then maybe you’ll lose a lot of money!

Would you trust a piece of computer software to buy and sell on your behalf? Even if you have done large amounts of testing how can you be sure that your EA won’t trip up when it is left to run by itself?

As EAs are often left to trade by themselves, any bug or defect in the code could be very costly. You might not spot that anything is wrong until you find your account has been emptied of all its money.

Think carefully before you let an EA lose on your live account. Make sure it is well tested, and keep a careful eye on what it is doing. To avoid it losing too much in the event that it doesn’t work as expected make its trade sizes small to start with and don’t keep to much money in your account.


Before you spend time creating an expert advisor make sure you don’t fall into any of the problems listed above. Doing so could cost you serious money.

The Expert Advisor advantage

Monday, December 21st, 2009

For most people trading in forex is a very manual process. It involves watching charts, indicators, and the news. They then have to work out in their head whether it is a good time to buy or sell.

For some strategies this makes a lot of sense. For example if you trade based on news events then you need to be there to interpret how the news will affect the price. This is fundamental analysis.

Many forex trading strategies rely on much simpler decisions. If you are basing your trading decisions on the shape of the chart, or on the values of your indicators then you are using technical analysis.

Many technical analysis strategies work by making decisions purely on indicators or price.

For example a strategy might be to buy if the price moves above a certain moving average line and sell if the price falls below the line.
Another strategy might involve buying or selling based on one or more technical indicators such as MACD, parabolic SAR, or RSI.

If your strategy is based on simple rules such as certain technical indicator values being reached then you may find that your trading is very robotic. You wait for a specified event to happen and then you act on it in a predefined way.

If you have a strategy that sounds like this then it could be a very good candidate for automating – i.e. creating an expert advisor to do the trading for you so you don’t have to do it yourself.

metatrader mql4

Expert advisor code in MetaTrader

This post will look at the advantages of expert advisors.

Expert advisors don’t get tired

If you are trading manually then the amount of time you can spend trading will be limited by how long you can concentrate or stay awake.

Forex is a 24 hour game with trading opportunities right around the clock. If you are trading manually then you are limiting yourself to only taking advantage of a fraction of these opportunities.

Throughout the day you will also need to take breaks for food, drink, to answer the telephone, etc. During one of these breaks something might happen which you don’t see until it is too late. This could cause you to buy or sell at a bad time, or miss out on a great trade altogether.

An expert advisor will be able to trade for you right through the night, through your lunch break, and when you are out of the house. It will never get tired and so will be able to capture all the trading opportunities that it is programmed to capture.

Of course if the expert advisor is doing your trading for you, then you will have more free time to do more important things like reading a book, or seeing your friends. Who wants to spend their life staring at a moving line on a screen, or watching numbers going up and down?


A lot of people have sound trading strategies but they fail to stick to them. You may find that your emotions get in the way and cause you to sell too early, or buy too late, because you don’t have the confidence to stick to your trading plan.

It is also possible for you to get stressed when trading which could lead to you making mistakes in your trading. How many of us have sold when we meant to buy, or entered the wrong amount into our trading ticket and then had to accept a loss in order to get out of the trade?

Once it is correctly programmed an expert advisor will have no such problems. It will follow your simple set of rules (or maybe a complicated set of rules!) without ever hesitating or letting its emotions cloud its judgement. Obviously an expert advisor is just a computer program so it will just do what it is told to.

This consistency means that it will stick perfectly to your trading plan and will never diverge from the rules that it has been given. The expert advisor won’t make mistakes – if it is supposed to buy then it will buy – it won’t press the wrong button and sell!

Speed of analysis

Human brains are great for many things, especially ‘fuzzy’ processing like thinking, and interpreting inputs from complex senses such as sight and sound. What they aren’t so good at is number crunching, or making decisions based on large amounts of data that are coming in at the same time.

If you are a manual forex trader then you will probably limit yourself to trading one or two currencies simultaneously. Trading more than this can get confusing. For one thing keeping an eye on multiple charts can be difficult, and if significant events start happening with multiple currencies at the same time (as they often do) you may not be able to keep up.

An expert advisor has no such problems. It can handle trading simultaneously in 10 different currency pairs just as easily as it can handle trading with one currency pair. Computers are able to process information so fast that they have no trouble keeping up with what is going on with multiple price feeds.

If you have a strategy that is applicable to multiple currencies then an expert advisor could allow you to trade more currencies simultaneously than you are able to using manual methods. Alternatively it can watch multiple currencies and then pick the best one to trade in. Imagine how fried your mind would get if you were trading in a short time frame and trying to keep your eyes on 10 charts!


Coming up with a manual trading strategy may involve eyeballing many charts and evolving the parameters you use over time.

If you write an expert advisor then you can very quickly test your strategy over many years worth of data in just a few minutes. This will give you a much better idea of how well your strategy will perform than just eyeballing the charts.

Trying to gauge how well the strategy will work using your eyes and brain is very dangerous because of hindsight bias. You may be feeling optimistic about your strategy and without realising it your brain will focus on areas of the chart where your strategy would work and will filter out parts of the chart where you would make a loss.

An automated backtest will get you much more accurate and consistent information on the performance of your strategy.

And because you can quickly test your strategy over many years worth of data you can also re-test your strategy using different parameters. You may think your strategy works best using a 11 day moving average, but some simple testing could reveal that a 13 day moving average is in fact the most profitable.

Are automated expert advisor the answer?

Expert advisors sound great don’t they? For many strategies then can prove to be much better at trading than a real person would be. They aren’t perfect though.

In a future post I’ll be looking at the disadvantages of expert advisors, and I’ll also be writing further about other issues to do with expert advisor.

If you want to have a go writing your own expert advisor you can get a MetaTrader forex account for free.

Implementing an assert in MetaTrader MQL4

Wednesday, November 25th, 2009

MQL4 is a very useful programming language for creating expert advisors to automatically trade FOREX. However the language does have many omissions which you will soon notice if you are used to programming a more complete language such as Java or C++.

One of the big omissions is that there is no built in way to do an ‘assert’ when running an expert advisor. If some kind of exceptional condition occurs that would make any further running of the expert meaningless there is nothing you can do as you can’t exit the current run programmatically.

This can waste a lot of time while you wait for the expert to complete. Something could have gone wrong in the depths of your code and you might not even realise it until the expert has completed – if at all!

On one website I did see the suggestion to implement an MQL4 assert by printing out the word ‘ASSERT’ along with your debugging message and then searching for it in the logs after the run has complete.

This is very unsatisfactory as it requires you to manually search for the tag, and you will only know if the assert has happened if you actually look for it.

My solution won’t stop the expert from running immediately (there is no way to do this in MQL4). But what it will do is to stop any further parts of your code from being executed by stopping the start() function from doing anything. This will mean that if an assert happens your expert will finish executing very quickly and the ‘assert’ message will be near the top of the log – so you don’t have to hunt for it.

Here are the code snippets you need.

// This goes near the top of your .mq4 file
bool iASSERT = false;
// Assert if the condition is true
void ASSERT_IF(bool condition, string message, string function)
   if (condition)
      ASSERT(message, function);
// Unconditional assert
void ASSERT(string message, string function)
   Alert("*** ASSERT: ", message, ", function=" + function + " ***");
   iASSERT = true;
// Rest of your code goes here…
// You add one line to the beginning of your start function. The rest of your
// start function is unaffected.
void start()
   if (iASSERT) return;
   // rest of your start function

How do you use these asserts? The first example is using the unconditional assert. Your expert will stop executing when the next tick is passed to the expert (via the start function).

if (type == StopTypeFast || type == StopTypeSlow)
      ASSERT("Stop type not valid here: " + type, "UpdateStops");

The next example is using the condition assert. The ‘assert’ only fires (and the expert execution is halted) if fractionalPips is bigger than 1.

int ToIntPips(double fractionalPips)
   ASSERT_IF(fractionalPips>1, "fractionalPips>1", "ToIntPips");
   return (fractionalPips*10000);

You can see from the code that the ASSERT function is not strictly necessary as both behaviour can be implemented in terms of ASSERT_IF. However having both versions gives you more flexibility in how your code works.