0% found this document useful (0 votes)
323 views17 pages

Algorithmic Trading For Dummies - Paul's Blog@Wildbunny

The document provides an overview of algorithmic trading, including explanations of key concepts like order books, market orders, limit orders, and how prices move in financial markets. It aims to explain these concepts to programmers who want to get into algorithmic trading but lack experience in finance.

Uploaded by

smartin1970
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
323 views17 pages

Algorithmic Trading For Dummies - Paul's Blog@Wildbunny

The document provides an overview of algorithmic trading, including explanations of key concepts like order books, market orders, limit orders, and how prices move in financial markets. It aims to explain these concepts to programmers who want to get into algorithmic trading but lack experience in finance.

Uploaded by

smartin1970
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

Paul's blog@Wildbunny
Knowledge centre for game development

Paul's blog@Wildbunny
Proudly powered by WordPress.
Algorithmic trading for dummies
Posted on March 15, 2013 by Paul Firth

submit Tweet 19 Like 50 22


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

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

1 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More

2 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More

3 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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.

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

4 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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

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.

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

5 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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

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

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

6 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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.

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.

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.

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

7 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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

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.

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

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

8 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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 |
//| 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 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()
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More

9 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

function to the following:

int start()
{
//----
Print("Hello world");
//----
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; 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.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More

10 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

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 Tweet 19 Like 50 22


Send

About Paul Firth


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 BBC’s 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.

25 Responses to Algorithmic trading for dummies

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

Jason says:
March 17, 2013 at 10:19 pm

This is something I have wanted to do for a while but its so hard (for me anyway) to find where the heck to start. Thanks a lot
for writing this!
Reply

Paul Firth says:


March 18, 2013 at 7:51 am

No problem! Happy to help.


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

11 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

Reply

yelnatz says:
March 18, 2013 at 7:33 am

Do you have any recommendations for paper trading brokers? I’m Canadian, does that limit me to any?
Reply

Paul Firth says:


March 18, 2013 at 7:52 am

I’m not sure what’s available in Canada (I’m UK based), but I’m sure you will have a lot of choice as there are many
brokers world-wide. I’m using Oanda personally.
Reply

Ian says:
March 20, 2013 at 2:02 am

yelnatz,
I have been paper trading and I am working with brokers. I am a Canadian.
e-mail me at [email protected] for details.

Ian
Reply

Gary Rowe says:


March 18, 2013 at 7:54 am

You can access many Bitcoin exchanges using the XChange Java library available on GitHub. It’s all free and open source
under MIT license and will save you lots of time getting your trading bot off the ground.
Reply

Paul Firth says:


March 18, 2013 at 4:00 pm

I wrote my own simple library in C#, but thanks for the link
Reply

Johnicholas says:
March 18, 2013 at 3:36 pm

Thanks for writing this!

I would love it if, for your “actually trying to trade” article, you would implement a market maker rather than a pairs strategy
or some technical analysis thing.

http://blog.oddhead.com/2006/10/30/implementing-hansons-market-maker/
Reply

Paul Firth says:


March 18, 2013 at 4:07 pm

Hi John,

In the publicly available forex markets, market-markers aren’t possible in the traditional sense because you never
hold a stock of what you buy; e.g. buying GBPUSD I’m forced to close that buy with a corresponding sell so I end up
with my base currency instead of USD.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More

12 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

Cheers, Paul.
Reply

Mac says:
April 16, 2013 at 7:42 am

Still looking forward to part 2!!!


Reply

Paul Firth says:


April 16, 2013 at 7:58 am

Sorry, it’s in the pipe!


Reply

Il Cannone says:
April 18, 2013 at 1:47 am

In order to write a trading algorithm you first have to know how to trade profitably.
Reply

Il Cannone says:
April 18, 2013 at 1:48 am

Unless you want to write a loosing algorithm.


Reply

Mike Shafer says:


April 24, 2013 at 8:03 am

Hi there,

Interesting that you are a video games programmer doing finance. I’m in the same exact boat. I did a game demo which you
can download from my web site featuring rag-doll physics, etc, etc. I’m now writing a neural network trading system that
runs exclusively on MT4 at the moment. Here’s a screenshot of the neural network editor: cseditor.png. Anyway, it’s funny
because your article is so new and I have been juggling neural nets and game physics for over a year. Thought I’d tell you we
have a lot in common, ha!

Mike
Reply

Paul Firth says:


April 24, 2013 at 8:14 am

Hi Mike,

How very interesting! Do the neural-nets allow your algorithms to adapt to changing market dynamics? The one
recurring problem I seem to have is over-fitting an algorithm to a particular year, or time of year.

I’d love to see something written about neural-nets and algorithmic trading.

Cheers, Paul.
Reply

Mike Shafer says:


April 24, 2013 at 8:54 am
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More

13 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

Well, mine don’t at least, haha. I know any robot would not be as good as a robot without a feedback loop
(control dynamic systems). So basically, ideally you’d want a base neural network that’s been trained and
then probably want to train it with a small time-step with current data (possibly as part of the tick-loop in
MT4). This is all in my head and I’m not even sure if it’ll work, but I’m currently testing EA’s for EURUSD
and USDCHF. I have to do the other major 4: GBPUSD, USDJPY, AUDUSD, and USDCAD.

I basically overpower through the problem you’re describing by training my neural network over the past 4
years. I have a hypothesis that if you overload your neural network with data, it is FORCED to generalize.
This is not what we were taught at Caltech–we were taught to take 10-20% of the data and not to train with
it, but use it to verify the other 80-90%. Nevertheless, I enjoy graphs like the following:
smooth graph. I’m hoping it will generalize (maybe it’s the law of large numbers I’m thinking of) given that
it’s only 14 neurons per middle layer and just 1 middle layer (in addition to the input layer and the outer
layer).

I don’t have any references handy, but my process is this: feed an equal number of trade and do-not-trade
examples as a starting point and then use the neural net you get. Then go through and reinforce it with
positive and negative examples you see fit. I’m not a bold trader, so I tend to have more negative examples
than positive examples. The darn little devil still manages to trade a lot though and making sure it trades
right can be hard. My stop loss is at 350 PIPS currently, ha! Anyway, let me know if you have any more
questions.

Mike
Reply

Paul Firth says:


April 24, 2013 at 9:34 am

Hi Mike,

It sounds interesting – something I definitely want to look into. A word of caution though, your graph
(although impressive looking) could be misleading due to bad tick data – I had a similar experience
where an algorithm of mine was making over 2 million in one year (with ‘n/a’ back-testing quality as
yours is showing), but once I got tick-by-tick data working in MT4 I ended up with an algorithm
which wasn’t in the least bit profitable.

To get tick by tick data, download TickStory Lite:

http://www.tickstory.com/

Then you will need to find your symbols and download the data. Tell tick-story where your MT4
install is, and then write protect the history data in tester/history and then *only* launch MT4 from
the menu option in tick-story as this patches the .exe so MT4 is able to use the tick data.

Here’s what my graphs looked like before and after using tick-data in the back-tests

Hope that helps!

Cheers, Paul.
Reply

Mike Shafer says:


April 24, 2013 at 9:50 am

Hmm.. nifty. I’m going to try it and let you know my results. I get my data from eSignal (5m
is what I use). I don’t know how getting data from tick story would change anything, but Ill
let you know. I’m currently downloading the last 4 years of data (taking forever!).

Mike

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

14 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

April 24, 2013 at 9:52 am

It actually comes from Dukascopy’s database, but tickstory allows you to get that data
exported and into MT4.

I’d very very interested to hear your results after you get set up with 99% quality back-test
data

Mike Shafer says:


April 24, 2013 at 11:26 am

Ok the results are in (unfortunately, I was unable to wait it out for 4 years data so I went with 1 year). You can see it, here.
Looks like it still works, thank goodness! I am going to get more data overnight and try again, I’ll post the results.
Reply

Paul Firth says:


April 24, 2013 at 11:34 am

Hi Mike,

Ahhh, that’s better! Glad your results are still positive. That graph is impressive; huge profit factor. IMO the only
thing to work on is reducing that draw-down… I’d like to see results for more than one year as well.

I might have to start digging through the literature on neural-nets!

Cheers, Paul.
Reply

Mike Shafer says:


April 24, 2013 at 11:56 am

Yeah, my dad says the same thing. He likes the accuracy, but the draw-down… that damned draw-down, lol.

Neural nets are neat things. They basically help you find a function given an input vector and (usually) a
boolean output (YES/NO). The more layers you put in them the more complex binary tree decision trees
they create (if I’m not mistaken). One of my classes at Caltech, they asked us “how does the number of layers
affect the neural network” and of course I never saw the solution, but I think the more layers you have, the
more sectors in the solution space of functions you cover. Anyway, the whole thing is still kind of magical for
me. I use it as a black box.

Let me know if you need help. It’s not that hard. Here is what my interface looks like:

class CSNeuralNet
{
public:
CSNeuralNet(u32 numInputs, u32 numMiddleLayers, u32 neuronsPerMiddleLayer, scalar maxWeight);
CSNeuralNet(s8 *filename);
CSNeuralNet(MEHXMLNode *root);
~CSNeuralNet();

inline MEHArray &GetDomainScale() { return m_domainScale; }
inline CRITICAL_SECTION &GetCriticalSection() { return m_cs; }
scalar GetError();

scalar ForwardFeed(MEHArray &inputs);
void BackPropagate(scalar desiredOutput, scalar learnRate);

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

15 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

void SaveToFile(s8 *filename);
void SaveToExternalXML(MEHXMLFile &xml, MEHXMLNode *root);
void MakeHeaderXML(MEHArray &attrib);
void LoadFromXML(MEHXMLNode *root);

protected:

void MakeLayers(u32 numInputs, u32 numMiddleLayers, u32 neuronsPerMiddleLayer, scalar maxWeight);

CRITICAL_SECTION m_cs;
MEHArray m_layers;
MEHArray m_domainScale;

s8 m_numInputsTxt[1024];
s8 m_numMiddleLayersTxt[1024];
s8 m_middleLayerNeuronsTxt[1024];
};

The main functions you need are a forward-feed and back-propagation (or learning) function. When you
forward-feed, you start at the input and work your way to the output. Then you calculate the error from the
output and back-propagate the error using error gradients. Turns out since the activation function at each
node is a hyperbolic (usually) function, the derivative is readily available (which is all the error gradient is).
Then you basically integrate the error gradient with a time-step (they call this a learning rate) and you’re
done with 1 “epoch” or cycle. How well it learns is based on how many epochs you take it through, but I
basically have a check that verifies that the results are what you expect for all test data points and that’s when
I stop running epochs.

Anyway, again, I implore you to find out about it yourself, but if you need pointers, let me know.

Cheers,
Mike
Reply

Andy Flury says:


June 18, 2013 at 9:20 am

Hi Paul

MetaTrader is a great tool if the strategy you would like to trade is based on technical indicators and charts. However these
days it is getting more and more difficult to find a successful trading strategy exclusively based on technical indicators. In my
opinion most successful strategies are nowadays based on economic facts and/or known market efficiencies.

You want to take a look at the Algorithmic Trading Platform – AlgoTrader.

AlgoTrader is a Java based Algorithmic Trading Platform that enables development, simulation and execution of multiple
strategies in parallel. The automated Trading Software can trade Forex, Options, Futures, Stocks & Commodities on any
market. The system is based on Complex Event Processing (CEP) and Event Stream Processing (ESP). CEP is a very good
technique to get started with algorithmic trading. With this technology time-based Market Data Analysis and Signal
Generation are coded in EPL (similar to SQL) statements, whereas procedural actions like placing an order are coded in
plain Java Code. The combination of the two provides a best-of-both-worlds approach and accommodates strategies that are
predominantly time-based and therefore cannot be programed with traditional procedural programming languages.

Some of the features of the system:


- 3 different GUI’s
- Different Broker Interfaces (Native and Fix)
- Support for custom Derivative Spreads
- Several built-in Execution Algorithms
- Support for Forex, Options, Futures, Stocks, Commodities, etc.
- Multi-Account Functionality & & Multi-Module Strategies
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More

16 of 17 7/12/2013 9:39 PM
Algorithmic trading for dummies | Paul's blog@Wildbunny http://www.wildbunny.co.uk/blog/2013/03/15/algorithmic-trading-for-...

There are two versions available of AlgoTrader:


– An Open Source Version that you can download for free
– A Commercial Version (with Support and Professional Services)

Andy
Reply

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

17 of 17 7/12/2013 9:39 PM

You might also like