algorithmic trading for dummies _ paul's blog@wildbunny

13
3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-dummies/?v=1 1/13 Algorithmic trading for dummies Posted on March 15, 2013 by Paul Firth submit Tweet Tweet 5 6 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. Paul's blog@Wildbunny Knowledge centre for game development Send

Upload: akr659

Post on 13-Apr-2015

36 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

Algorithmic trading for dummiesPosted on March 15, 2013 by Paul Firth

submit TweetTweet 5 6

Hi guys,

I’m back with something completely different for this article! This one is about algorithmictrading; as in writing a trading algorithm which will automatically make trades on yourbehalf 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 exclusivelyabout 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 makecomplex 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 completelyfree, 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 buthave 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 alive account because you will lose a lot of money. So please don’t do it. Just use a paper trading account to getstarted and back-test using the Strategy Tester, which I will talk about later.

Paul's blog@WildbunnyKnowledge centre for gamedevelopment

Send

Page 2: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

Background

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

At its heart trading is about an exchange of an asset for a some amount of money; the buyer gains the assetand the seller gains the sale price. Assets involved could be almost anything, the most popular ones beingstocks and shares, foreign currency, gold, silver etc. The key is that the buyer only wants to pay a certainamount 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 ofthousands of people exchanging the same asset you need some way to manage the system so all the buyers andsellers 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 allthe 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 soldfor Euros. You can clearly see what the buyers are willing to pay (on the left) and what the sellers are willing tosell at (on the right). Another important quantity listed is the amount being sold or bought, this is selfexplanatory 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 ifthe values were the same, or if Ask prices were lower than Bid prices the exchange would have already taken

Page 3: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

place and the entries would have been removed from the order book (assuming the quantities were the same inboth 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 thecost of trading – if you wanted to buy and then a sell straight afterwards you would end up paying the cost ofthe spread for the convenience of an instant transaction, which brings us to our next definition. MarketOrders.

Market orders

A market order is a transaction which takes place instantly. For this to be possible, the buying price mustequal the lowest Ask in the order-book (for a buy) and for a sell, the selling price must equal the highest Bidprice. Obviously it makes no sense to buy and then sell instantly because you’d always be losing money (thespread) on each one. When you place a market order, you usually have some idea that the price will move inyour 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 thebest 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 orseller at the top of the order-book and their deal will be filled. People placing limit orders are happy to waituntil the market moves in their favour before they even make a deal – although this may never happen, ormight 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 tosell at or the maximum price someone is willing to pay. The top of the orderbook holds those values, as we’vealready learned, so its tempting to think this alone would define the price and therefore it would be trivial toartificially control the value of an asset by carefully placing limit orders in the order-book.

Page 4: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

However, there is a complication related to the quantity of the order. The quantity of an order defines it’ssignificance in setting the value of an asset, the reason for this is its longevity. The higher the quantity of anorder the longer it is likely to exist in the order-book – imagine someone placing a order to sell one millionapples at £0.25 per apple (the cheapest price). This order is likely to stay in the order-book for a much longertime than someone trying to sell 10 apples. So this huge order to sell apples cheaply starts taking all the tradeaway 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 anotherlarge 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 tonominal 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 hadhacked into the biggest bitcoin exchange MtGox, stolen a vast quantity of bitcoins and then attempted to sellthem on the same site. Prices went from 18 USD / bitcoin to virtually 0 in a matter of minutes. This happenedbecause bitcoin is still quite an illiquid currency, so large volumes can move prices substantially more than inother more liquid markets.

Excluding crashes like the one shown above, throughout an asset’s life, price movement is happening onmultiple 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 fractallike nature.

Page 5: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

Fractal-like market nature

Above you can see an example of this (again on USD vs GOLD) where the main trends are marked by theyellow line, the mid trends are shown by the white line and immediate trends shown in blue. The mid-trendscaused by the smaller orders revert back to the main trend price caused by the largest orders, so on and soforth. 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 oneoverall direction. This is caused when a sequence of events occurs similar to what I’ve described above, but ona massive scale. Often this can be triggered by some kind of external factor, like news; say there is a newsarticle which links eating apples to lower IQs, then the majority of sellers will want to get rid of their stocks ofapples quickly because no one will be buying, so they sell at a lower price and other sellers join in and thiscascades into a trend of lower prices.

Gold prices started trending strongly following the 2008financial crisis

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

A Ranging Market

A ranging market is one where prices oscillate between various different levels (again in a fractal like way) but

Page 6: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

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 economiesof 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 tradeGBP/USD and the prices would be listed in Pounds (base currency) per Dollar (quote currency). The wayprivate individuals gain access to these markets is via a broker. A broker is an intermediary between the endusers and the Electronic Communications Network which connects all the big investment banks, hedge andpension 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, orwill simply be hidden inside the spread (brokers will simply add their commission to Bid and Ask prices sousers placing a sell order will have their prices increased by a small amount which is then taken by the brokeras profit).

There are many different brokers in operation all with their own benefits and drawbacks which you shouldassess – compare things like which commission-free broker has the lowest spreads, which is regulated byfinancial 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 goingto be talking about in the rest of this article, because of its relative ease of use, its widespread support and itsC-like programming language MQL4 which provides API access to all the functionality of MetaTrader 4 (MT4from now on).

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

This has advantages and disadvantages. The disadvantage is it precludes certain trading algorithms frombeing possible – for example, you can’t run a Market-Maker algorithm on a Forex broker because you have toclose every trade with the opposite trade. The closest you can do is what’s referred to as grid-trading; but I’llget into these different techniques in a later article. The advantage of Forex is you can make money in a down-

Page 7: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

trending market because you can sell high and then buy back when the prices are low; this is what’s referred toas 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 availablecurrency-pair symbols shown in a pane on the left, the navigator (for choosing scripts, indicators andalgorithms) 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 pricesfrom the order-book for a given currency pair. The full order-book is unavailable for viewing – you only getaccess 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 andoutput 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. Movingaverages help to smooth out the noise in a price-series and make the over-all trend clearer at the expense ofadding lag.

Page 8: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

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 andMN 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 tradersonly deal with H4 or higher time-frames which are much easier to read and don’t require lightning reactiontimes.

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

OHLC

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

Page 9: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

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 rightcandle 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 onethat has fallen in price.

Ticks

A tick (in MQL4 terminology) is a single change in Bid price and is the highest possible resolution of viewingprice-action. There is no default tick view price series in MT4, although the Market Watch pane does have aTick Chart on it which you can use to see incoming changes. Ticks are most interesting when it comes toactually 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 brokersintroduced pipettes which are ten times smaller again, which are currently the smallest unit.

Points

Page 10: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

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

MQL4

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

DocumentationAPI Reference

The language is C-like and has a few basic built-in types, like doubles, ints and arrays, but no complex typeslike structs or classes. In MT4 you can write custom indicators and custom trading algorithms, which theyrefer 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’ isselected, then choose ‘Next’.

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

Page 11: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

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

//+------------------------------------------------------------------+//| HelloWorld.mq4 |//| Wildbunny Ltd |//| http://wildbunny.co.uk/blog |//+------------------------------------------------------------------+#property copyright "Wildbunny Ltd"#property link "http://wildbunny.co.uk/blog" //+------------------------------------------------------------------+//| expert initialization function |//+------------------------------------------------------------------+int init() //---- //---- return(0);//+------------------------------------------------------------------+//| expert deinitialization function |//+------------------------------------------------------------------+int deinit() //---- //---- return(0);//+------------------------------------------------------------------+//| expert start function |//+------------------------------------------------------------------+int start() //---- //---- return(0);//+------------------------------------------------------------------+

There are obvious initialisation/deinitialisation points which are called from MT4 when the program first runsand 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");

Page 12: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

//---- return(0);

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; itlets 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 anddebugging 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 shouldhave output similar to this:

Your first EA!

If you do, congratulations! You’ve just written your very first trading algorithm; although in the loosest

Page 13: Algorithmic Trading for Dummies _ Paul's Blog@Wildbunny

3/17/13 Algorithmic trading for dummies | Paul's blog@Wildbunny

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

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

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 Iwill talk about the programming of actual trading operations and even cover a few common trading strategies!

Until next time, have fun!

Cheers, Paul.

submit TweetTweet 5 6

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

Send

Jeroen Ruigrok van der Werven 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 developeron the use of intelligent agents for various markets, e.g. supply chain or energy markets. For those that are interested, thereare yearly competitions for writing these kinds of agents. You can find details for participation in three (energy trading, adauctions, 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 variouscomponents can be found over at https://github.com/powertac/

Reply

Paul Firth says:March 17, 2013 at 9:02 pm

Hi Jeroen,

Thanks for the links, I’ll take a look

Cheers, Paul.

Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but youcan opt-out if you wish. Read MoreAcceptAccept