You are on page 1of 13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

Paul's blog@Wildbunny
Knowledgecentreforgame development

Algorithmic trading for dummies


Posted on March 15, 2013 by Paul Firth
submit

Tweet

Send

Hi guys, Im 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 Im not just a games programmer; algorithms of all kinds interest me and more than that Im 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 its 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 dont do it. Just use a paper trading account to get started and back-test using the Strategy Tester, which I will talk about later.

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

1/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

Background
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 dont 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 partys asking price or buying offer in order to get the best deal. What you end up with is whats called the Order Book which is simply a list of all the buyers Bid prices and all the sellers 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. Youll 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

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

2/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

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 youd 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; peoples 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 weve 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.

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

3/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

However, there is a complication related to the quantity of the order. The quantity of an order defines its 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 evenlower. 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 assets life, price movement is happening on multiple different scales; really big orders drive the large trends, followed by smaller orders driving the midtrends and small orders driving the immediate price action. This behaviour is what gives a market a fractal like nature.

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

4/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

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 Ive 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 Ive 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

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

5/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

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 its 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 Im 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). The user accessible Forex markets are slightly different in their operation than what Ive described so far in this article principally because you never end up owning the asset youre 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 cant 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 whats referred to as grid-trading; but Ill get into these different techniques in a later article. The advantage of Forex is you can make money in a down-

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

6/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

trending market because you can sell high and then buy back when the prices are low; this is whats 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.

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

7/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

Moving average indicator

Time-frames
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 dont require lightning reaction times. Itshouldbeclearthatwhatthesetimeframesrepresentareinfactanormalisedviewofthe priceseriesinrealitytradesdonotoccuronsuchregularlyspacedintervalsintime,they occurasandwhen.ThereforewhatyouseeinMT4isactuallyaninterpolatedviewofthetrue priceaction.

OHLC
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.

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

8/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

Two candles on a chart, one bullish, one bearish

Intheabovediagram,theleftcandleiscolouredblacktoindicateabullishmotionandtheright candleiswhiteindicatingabearishmotion.

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.

Ticks
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.

Points

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

9/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

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.

MQL4
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 mql4.com: Documentation 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. Lets 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.

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

10/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

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

/ / + + / / | H e l l o W o r l d . m q 4| / / | W i l d b u n n yL t d| / / | h t t p : / / w i l d b u n n y . c o . u k / b l o g| / / + + # p r o p e r t yc o p y r i g h t" W i l d b u n n yL t d " # p r o p e r t yl i n k " h t t p : / / w i l d b u n n y . c o . u k / b l o g " / / + + / / |e x p e r ti n i t i a l i z a t i o nf u n c t i o n | / / + + i n ti n i t ( ) { / / / / r e t u r n ( 0 ) ; } / / + + / / |e x p e r td e i n i t i a l i z a t i o nf u n c t i o n | / / + + i n td e i n i t ( ) { / / / / r e t u r n ( 0 ) ; } / / + + / / |e x p e r ts t a r tf u n c t i o n | / / + + i n ts t a r t ( ) { / / / / r e t u r n ( 0 ) ; } / / + +

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 oncepertick. Lets add something simple to get up and running with a Hello World type example. Just change the start() function to the following:

i n ts t a r t ( ) { / / P r i n t ( " H e l l ow o r l d " ) ;

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

11/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

/ / r e t u r n ( 0 ) ; }

Then press the Compile button and you should have output at the bottom of the screen which reads:
C o m p i l i n g' H e l l o W o r l d . m q 4 ' . . . 0e r r o r ( s ) ,0w a r n i n g ( s )

Now, switch back to the main MT4 interface and choose View>StrategyTester from the main menu. Thestrategytesteriswhereyoullspendalotofyourtimeasacreatoroftradingalgorithmsit letsyoutestyourprogrammedstrategyoverpreviouspriceseriesdataonanyofthetime framesyouwant.Thisiscalledbacktestinganditsacompletelyinvaluabletimesavingand debuggingtoolwhichenablesyoutotesttheprofitabilityofyourtradingstrategy. 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 isnt 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! Youve just written your very first trading algorithm; although in the loosest

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

12/13

3/17/13

Algorithmic trading for dummies | Paul's blog@Wildbunny

possible sense since it doesnt trade.

Next time
Ive 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

Tweet

Send

AboutPaulFirth A games industry veteran of ten years, seven of which spent at Sony Computer Entertainment Europe, he has had key technical roles on triple-A titles like the Bafta Award Winning Little Big Planet (PSP), 24: The Game (PS2), special effects work on Heavenly Sword (PS3), some in-show graphics on the BBCs version of Robot Wars, the TV show, as well as a few more obscure projects. Now joint CEO of Wildbunny, he is able to give himself hiccups simply by coughing. View all posts by Paul Firth

This entry was posted in Algorithmic trading, Finance and tagged algo, EA, Expert Advisor, finance, forex, MetaTrader, trading algorithm. Bookmark the permalink.

2 Responses to Algorithmic trading for dummies


JeroenRuigrokvanderWerven says:
March 17, 2013 at 8:59 pm

For what it is worth on the topic of algorithmic trading, I just spend the past 4,5 years supporting research as a developer on the use of intelligent agents for various markets, e.g. supply chain or energy markets. For those that are interested, there are yearly competitions for writing these kinds of agents. You can find details for participation in three (energy trading, ad auctions, and supply chain management) over at http://tradingagents.org/ Especially the energy trading is a very hot topic at the moment. Github respository with source code for various components can be found over at https://github.com/powertac/
can opt-out if you wish. Accept Read More
Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you

PaulFirth says:
March 17, 2013 at 9:02 pm

Hi Jeroen, Thanks for the links, Ill take a look Cheers, Paul.
Reply

www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1

13/13

You might also like