Submit to StumbleUpon Share

Hi guys,

I'm back with something completely different for this article! This one is about algorithmic trading; as in writing a trading algorithm which will automatically make trades on your behalf on currency exchange markets.

Why algorithmic trading?

"This is a games programming blog!" I hear you cry... Well up to now I have been talking almost exclusively about algorithms and techniques in game development, but in truth I'm not just a games programmer; algorithms of all kinds interest me and more than that I'm always interested in small details that make complex systems work, and finance is completely full of small details and impenetrable sounding jargon.

But, in truth it's actually quite simple to get set up and write your first algorithm; all the software is completely free, almost every broker has a free practice account so the barrier of entry is basically zero.

Who is this article aimed at?

This article is aimed at programmers who have always been curious about finance and trading algorithms but have never looked into it in great detail.

Danger, Will Robinson, DANGER!

Of course, it must be stated that it would be a fantastically bad idea to let any of your first algorithms run on a live account because you will lose a lot of money. So please don't do it. Just use a paper trading account to get started and back-test using the Strategy Tester, which I will talk about later.


It makes sense to start with an overview of how financial trading, and in particular currency trading actually works.

At its heart trading is about an exchange of an asset for a some amount of money; the buyer gains the asset and the seller gains the sale price. Assets involved could be almost anything, the most popular ones being stocks and shares, foreign currency, gold, silver etc. The key is that the buyer only wants to pay a certain amount and the seller wants to earn a certain amount, and often these values don't match.

If you take this simple example of two parties attempting to make one exchange and extrapolate into tens of thousands of people exchanging the same asset you need some way to manage the system so all the buyers and sellers involved can get a clear view of every party's asking price or buying offer in order to get the best deal.

What you end up with is what's called the Order Book which is simply a list of all the buyer's Bid prices and all the seller's Asking prices (sometimes also called Offer prices).

An example order-book, this one is eur / bitcoins

Above is an example of what an order book looks like for a particular asset; in this case its bitcoins being sold for Euros. You can clearly see what the buyers are willing to pay (on the left) and what the sellers are willing to sell at (on the right). Another important quantity listed is the amount being sold or bought, this is self explanatory really; simply the quantity of the asset being offered for sale, or purchase.

You'll notice that the Ask prices are always higher than the Bid prices. This makes sense logically, because if the values were the same, or if Ask prices were lower than Bid prices the exchange would have already taken place and the entries would have been removed from the order book (assuming the quantities were the same in both Bid and Ask).

This brings us neatly to the first bit of jargon. The spread.

The Spread

The spread is simply the difference between the lowest Ask price and the highest Bid price. It represents the cost of trading - if you wanted to buy and then a sell straight afterwards you would end up paying the cost of the spread for the convenience of an instant transaction, which brings us to our next definition. Market Orders.

Market orders

A market order is a transaction which takes place instantly. For this to be possible, the buying price must equal the lowest Ask in the order-book (for a buy) and for a sell, the selling price must equal the highest Bid price. Obviously it makes no sense to buy and then sell instantly because you'd always be losing money (the spread) on each one. When you place a market order, you usually have some idea that the price will move in your favour before you then place the opposite order to close the deal.

Limit orders

The orders in the order-book are all limit orders; people's desired buying prices (which are always below the best Ask price) and selling prices (which are always above the best Bid price). After some amount of time (although, maybe never in extreme cases) an order will be submitted which will satisfy either the buyer or seller at the top of the order-book and their deal will be filled. People placing limit orders are happy to wait until the market moves in their favour before they even make a deal - although this may never happen, or might happen very quickly.

Moving prices

So how exactly do prices 'move' in the first place?

In a very real sense, the value of a given asset is directly defined by the minimum price someone is willing to sell at or the maximum price someone is willing to pay. The top of the orderbook holds those values, as we've already learned, so its tempting to think this alone would define the price and therefore it would be trivial to artificially control the value of an asset by carefully placing limit orders in the order-book.

However, there is a complication related to the quantity of the order. The quantity of an order defines it's significance in setting the value of an asset, the reason for this is its longevity. The higher the quantity of an order the longer it is likely to exist in the order-book - imagine someone placing a order to sell one million apples at £0.25 per apple (the cheapest price). This order is likely to stay in the order-book for a much longer time than someone trying to sell 10 apples. So this huge order to sell apples cheaply starts taking all the trade away from smaller sellers; their only choice is to try and undercut the huge order and sell even more cheaply, say at £0.24 per apple (or they can wait it out of course, but that might take 'too' long). Eventually another large order to sell will come along and undercut the original order, thereby driving prices even lower. Eventually all these huge orders will be completely filled and the prices will start to settle down again to nominal levels, although they may not move back up to where they were.

A great example of how large orders can move price was in the bitcoin crash of 19/6/2011 - someone had hacked into the biggest bitcoin exchange MtGox, stolen a vast quantity of bitcoins and then attempted to sell them on the same site. Prices went from 18 USD / bitcoin to virtually 0 in a matter of minutes. This happened because bitcoin is still quite an illiquid currency, so large volumes can move prices substantially more than in other more liquid markets.

Excluding crashes like the one shown above, throughout an asset's life, price movement is happening on multiple different scales; really big orders drive the large trends, followed by smaller orders driving the mid-trends and small orders driving the immediate price action. This behaviour is what gives a market a fractal like nature.

Fractal-like market nature

Above you can see an example of this (again on USD vs GOLD) where the main trends are marked by the yellow line, the mid trends are shown by the white line and immediate trends shown in blue. The mid-trends caused by the smaller orders revert back to the main trend price caused by the largest orders, so on and so forth. Mandlebrot studied the fractal nature of price-series in detail.

A Trending Market

What I've just described above is the basis for a trending market - where prices are moving strongly in one overall direction. This is caused when a sequence of events occurs similar to what I've described above, but on a massive scale. Often this can be triggered by some kind of external factor, like news; say there is a news article which links eating apples to lower IQs, then the majority of sellers will want to get rid of their stocks of apples quickly because no one will be buying, so they sell at a lower price and other sellers join in and this cascades into a trend of lower prices.

Gold prices started trending strongly following the 2008 financial crisis

The financial crisis of 2008 triggered such a trend in the price of gold as people lost confidence in traditional means of investment.

A Ranging Market

A ranging market is one where prices oscillate between various different levels (again in a fractal like way) but not necessarily in any clear overall upward or downward direction.

GBP vs USD is a historically ranging market due to the interrelated nature of the two economies

The foreign exchange symbol pair GBPUSD is a historically ranging market due to the interrelated economies of the two countries; although of late it's been in heavy down-trend due to the weakening pound.

Foreign exchange markets

Foreign exchange markets, or Forex markets work by trading currency pairs, for example you might trade GBP/USD and the prices would be listed in Pounds (base currency) per Dollar (quote currency). The way private individuals gain access to these markets is via a broker. A broker is an intermediary between the end users and the Electronic Communications Network which connects all the big investment banks, hedge and pension funds together and is the means by which they do their trading.

Brokers provide users access to trade in exchange for fees, which can be a fixed charge per volume traded, or will simply be hidden inside the spread (brokers will simply add their commission to Bid and Ask prices so users placing a sell order will have their prices increased by a small amount which is then taken by the broker as profit).

There are many different brokers in operation all with their own benefits and drawbacks which you should assess - compare things like which commission-free broker has the lowest spreads, which is regulated by financial authorities or which provides the best connection to the ECN (some are not even connected at all).

The most popular platform which users use and brokers support is called MetaTrader 4 and is what I'm going to be talking about in the rest of this article, because of its relative ease of use, its widespread support and its C-like programming language MQL4 which provides API access to all the functionality of MetaTrader 4 (MT4 from now on).

Example forex broker (Affiliated)

The user accessible Forex markets are slightly different in their operation than what I've described so far in this article principally because you never end up owning the asset you're purchasing. This seems rather odd because it breaks from reality - how can you sell something you never actually owned, for example? Well in Forex you can! Every buy must be closed with a sell and every sell must be closed with a buy, so you always end up owning the base currency, never the quote currency.

This has advantages and disadvantages. The disadvantage is it precludes certain trading algorithms from being possible - for example, you can't run a Market-Maker algorithm on a Forex broker because you have to close every trade with the opposite trade. The closest you can do is what's referred to as grid-trading; but I'll get into these different techniques in a later article. The advantage of Forex is you can make money in a down-trending market because you can sell high and then buy back when the prices are low; this is what's referred to as Shorting.

MetaTrader 4

The MT4 interface looks daunting at first, but its really quite simple.

MT4 user interface

The main part of the display is taken up by the quote prices of your chosen currency pair, with the available currency-pair symbols shown in a pane on the left, the navigator (for choosing scripts, indicators and algorithms) under that and - in my set up - the strategy tester right at the bottom.

It is important to note that the quote prices shown in the graphs in MT4 represent only the highest Bid prices from the order-book for a given currency pair. The full order-book is unavailable for viewing - you only get access to the top of the order book in the Market Watch pane on the left.

MT4 provides a lot of built-in indicators, which are small programs which run over price-series data and output something visual overlaid over the prices. An simple example would be the Moving Average indicator, which shows an average of the price-series with a given period (number of samples) shown in red. Moving averages help to smooth out the noise in a price-series and make the over-all trend clearer at the expense of adding lag.

Moving average indicator


MT4 provides a number of different time-frames through which to view price-series of a particular symbol: M1, M5, M15, M30, H1, H4, D1, W1 and MN. M1 to M30 are minutes, H1 to H4 are hours, D1 is days and MN is months. Each individual unit of these time-series are referred to as 'Bars'.

Various different time-frames available

The reason for providing so many different views of a price series is that it helps traders judge the long-term, mid-term and short-term trends in a currency. In general, the lower minute time-frames also contain the most 'noise' which is defined as trades which obscure the general trend, which is why a lot of professional traders only deal with H4 or higher time-frames which are much easier to read and don't require lightning reaction times.

It should be clear that what these time-frames represent are in-fact a normalised view of the price-series; in reality trades do not occur on such regularly spaced intervals in time, they occur as and when. Therefore what you see in MT4 is actually an interpolated view of the true price action.


As well as bid prices in MT4 you also have access to Open prices, High prices, Low prices and Close prices sometimes referred to as OHLC. This is an artefact of the normalisation of the price-series; because prices have been normalised into bars it stands to reason that traders might like to know what was the starting price of the bar (Open), where the high and low points were and what the last price in the bar was (Close). All this information can be encoded into the price-charts as candles.

Two candles on a chart, one bullish, one bearish

In the above diagram, the left candle is coloured black to indicate a bullish motion and the right candle is white indicating a bearish motion.

Many candles on a price chart

Bearish and Bullish

Trading terms: a bullish market (or candle) is one that is or has risen in price, whereas a bearish market is one that has fallen in price.


A tick (in MQL4 terminology) is a single change in Bid price and is the highest possible resolution of viewing price-action. There is no default tick view price series in MT4, although the Market Watch pane does have a Tick Chart on it which you can use to see incoming changes. Ticks are most interesting when it comes to actually writing an algorithm.

Pips and pipettes

A pip is 0.0001 units of the quote currency, which used to be the lowest possible unit until some brokers introduced pipettes which are ten times smaller again, which are currently the smallest unit.


A point in MT4 is the smallest possible unit of the quote currency. What this is actually depends on what your broker supports, but for example on 5 digit broker Oanda, a Point is 0.00001 in EUR/USR and 0.001 in USD/JPY.


The most interesting part of MT4 for programmers is the MQL4 language. I suggest you take a look at the excellent documentation and reference material provided on

API Reference

The language is C-like and has a few basic built-in types, like doubles, ints and arrays, but no complex types like structs or classes. In MT4 you can write custom indicators and custom trading algorithms, which they refer to as Expert Advisors, or EAs.

Let's get started with our first EA!

Right click the 'Expert Advisors' tree in the Navigator and chose 'Create'. Make sure 'Expert Advisor' is selected, then choose 'Next'.

Give you EA an inspiring name, such as 'HelloWorld' and then click 'Finish'.

You should then be presented with the MetaEditor (which is where you'll do all your programming) containing the skeleton for your first EA which should look similar to this:

//|                                                   HelloWorld.mq4 |
//|                                                    Wildbunny Ltd |
//|                             |
#property copyright "Wildbunny Ltd"
#property link      ""
//| expert initialization function                                   |
int init()
//| expert deinitialization function                                 |
int deinit()
//| expert start function                                            |
int start()

There are obvious initialisation/deinitialisation points which are called from MT4 when the program first runs and when it shuts-down. And the entry point start() which is called once per tick.

Lets add something simple to get up and running with a Hello World type example. Just change the start() function to the following:

int start()
        Print("Hello world");

Then press the Compile button and you should have output at the bottom of the screen which reads:

Compiling 'HelloWorld.mq4'...
0 error(s), 0 warning(s)

Now, switch back to the main MT4 interface and choose View->Strategy Tester from the main menu.

The strategy tester is where you'll spend a lot of your time as a creator of trading algorithms; it lets you test your programmed strategy over previous price-series data on any of the time-frames you want. This is called back-testing and it's a completely invaluable time-saving and debugging tool which enables you to test the profitability of your trading strategy.

You should then be presented with a pane which looks like this at the bottom of the MT4 interface:

The strategy tester

If 'Hello World' isn't selected in the first drop-down menu, click on it and select it.

Now press the large 'Start' button in the bottom right, and then click on the tab labelled 'Journal', you should have output similar to this:

Your first EA!

If you do, congratulations! You've just written your very first trading algorithm; although in the loosest possible sense since it doesn't trade.

Next time

I've covered an awful lot of ground in this article so there should be a lot to sink your teeth into. Next time I will talk about the programming of actual trading operations and even cover a few common trading strategies!

Until next time, have fun!

Cheers, Paul.

Submit to StumbleUpon Share