This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

Algorithmic trading for dummies

Home/Finance/Algorithmic trading/Algorithmic trading for dummies

Algorithmic trading for dummies

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

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 |
//|                                      /blog |
#property copyright "Wildbunny Ltd"
#property link      "/blog"
//| 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
By | 2018-12-14T12:35:05+00:00 March 15th, 2013|Algorithmic trading, Finance|57 Comments

About the Author:

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.   1NobNQ88UoYePFi5QbibuRJP3TtLhh65Jp


  1. Jeroen Ruigrok van der Werven March 17, 2013 at 8:59 pm - Reply

    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

    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

    • Paul Firth March 17, 2013 at 9:02 pm - Reply

      Hi Jeroen,

      Thanks for the links, I’ll take a look 🙂

      Cheers, Paul.

      • Kevin Rea July 14, 2013 at 5:26 pm - Reply

        I 2nd that thought below from Jason. I am a EE that shouldve gotten more into Math and Programming.
        This is great info for guys like me who would love to get into this stuff but felt it was too complicated.
        Thanks Paul for the article and thank you Jeroen for providing great links!

  2. Jason March 17, 2013 at 10:19 pm - Reply

    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!

    • Paul Firth March 18, 2013 at 7:51 am - Reply

      No problem! Happy to help.

      • jay jay November 21, 2014 at 6:07 pm - Reply

        Hi ive just started trading i doubled my demo acc on plus im very good at it as this is easier than commoditys etc evreyone is always looking for a advantage id love to build one also ive just downlaoded mt4 from here what would this help with? How far can it go? Ie like what jp morgan goldsachs use or is that impossible? 1 company profited 287 out of 288 days using a algorythim can i do one like thteres? N how do i start if i got e in math e in english i pick up on things really quick though do u know where i can learn this and putting the algo together etc? I have 30k sat there ready to go cheers for artical tho easy understood here (im a dummy lol)

        • Paul Firth November 22, 2014 at 11:01 am - Reply

          Hi jay jay,

          I would advice extreme caution, the companies which have successful trading algorithms like you describe have armies of PHDs in quantitative finance who design their algorithms. They’re not using MT4 either, they will be trading directly using very expensive custom software and hardware which are out of our reach. The best advice is to find something safer to do with your 30k, because forex trading is extremely risky.

          Cheers, Paul.

  3. yelnatz March 18, 2013 at 7:33 am - Reply

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

    • Paul Firth March 18, 2013 at 7:52 am - Reply

      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.

    • Ian March 20, 2013 at 2:02 am - Reply

      I have been paper trading and I am working with brokers. I am a Canadian.
      e-mail me at for details.


  4. Gary Rowe March 18, 2013 at 7:54 am - Reply

    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.

    • Paul Firth March 18, 2013 at 4:00 pm - Reply

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

      • uri December 13, 2013 at 1:05 am - Reply

        is there a chance you could upload it? many thanks!

        • Paul Firth December 13, 2013 at 8:13 am - Reply

          Upload what, sorry?

          • casual June 23, 2014 at 12:02 am

            He clearly refers to your library.

  5. Johnicholas March 18, 2013 at 3:36 pm - Reply

    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.

    • Paul Firth March 18, 2013 at 4:07 pm - Reply

      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.

      Cheers, Paul.

  6. Mac April 16, 2013 at 7:42 am - Reply

    Still looking forward to part 2!!!

    • Paul Firth April 16, 2013 at 7:58 am - Reply

      Sorry, it’s in the pipe! 🙂

  7. Il Cannone April 18, 2013 at 1:47 am - Reply

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

    • Il Cannone April 18, 2013 at 1:48 am - Reply

      Unless you want to write a loosing algorithm. 😀

      • Basil August 1, 2014 at 8:37 am - Reply

        Or a losing algorithm even 😉

  8. Mike Shafer April 24, 2013 at 8:03 am - Reply

    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!


    • Paul Firth April 24, 2013 at 8:14 am - Reply

      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.

      • Mike Shafer April 24, 2013 at 8:54 am - Reply

        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.


        • Paul Firth April 24, 2013 at 9:34 am - Reply

          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:

          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.

          • Mike Shafer 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!).


          • Paul Firth 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 🙂

  9. Mike Shafer April 24, 2013 at 11:26 am - Reply

    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.

    • Paul Firth April 24, 2013 at 11:34 am - Reply

      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.

      • Mike Shafer April 24, 2013 at 11:56 am - Reply

        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
        CSNeuralNet(u32 numInputs, u32 numMiddleLayers, u32 neuronsPerMiddleLayer, scalar maxWeight);
        CSNeuralNet(s8 *filename);
        CSNeuralNet(MEHXMLNode *root);

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

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


        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.


        • Ally August 27, 2013 at 3:12 pm - Reply


          I developed a neural net 2 years ago in my university that could increase and decrease size automatically to adapt to the function and model.

          I am still trying to understand what information you are using to train your neural net. What is the input and output during the training phase? As input, my neural network can take any domain. But the trick is: how you train it? What should the inputs of a neural network be?


  10. Andy Flury June 18, 2013 at 9:20 am - Reply

    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
    – Automated Forex Hedging & Options Pricing Engine

    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)


  11. Kay July 24, 2013 at 6:48 am - Reply

    Whao. What an educative and informative article for a dummy like me. Looking forward to part 2. Welldone Paul, I like you simplified analysis of the forex market. Does anyone know where I can also learn about writing automated strategies for currenex platform or by utilizing the FIX API? I’ll even appreciate a book on it or better still, a tutor.

  12. Alex August 28, 2013 at 5:20 pm - Reply

    Does somebody use Automated Trading Engine from Tiger? (

  13. Chris Vermeulen May 17, 2014 at 7:02 pm - Reply

    This is probably one of the best written, clearest explanation of algorithmic trading software I’ve ever seen. It might be a gaming blog, but this article gets a solid recommendation. Should be on everyone’s “must read” list!

    • Paul Firth May 17, 2014 at 7:12 pm - Reply

      Many thanks, I appreciate it 🙂

  14. brent May 19, 2014 at 6:00 pm - Reply

    I have been trading derivatives for the past 30 years. Now I would like to incorporate those experiences into algorithms. Where do I get started ? Collaborate ?

    • Paul Firth May 19, 2014 at 6:04 pm - Reply

      If you’re a programmer, you should find a broker who supports an API, or runs metatrader. If you’re not, you should probably try and hire one to implement your strategy for you.

      Cheers, Paul.

  15. brent May 19, 2014 at 9:13 pm - Reply

    Thanks for the reply Paul. I’m not a programmer (Spent most of my career on the floor as a local), could you give me an idea on what it would cost to hire someone ? Is collaboration realistic ?

    • Paul Firth May 19, 2014 at 9:27 pm - Reply

      Have you looked at There is a jobs board there with many programmers bidding for jobs creating EA’s for people who want them built. I used to be a developer there myself, but couldn’t compete with the cheap prices the russian/indian programmers were charging.

  16. brent May 19, 2014 at 9:49 pm - Reply

    Thanks again Paul. Intra commodity spreads have always been the core of my strategies. can they be incorporated in algorithms, is HFT used to arbitrage outright orders and spreads ? Just not sure where to get started. Thanks.

    • Paul Firth May 19, 2014 at 9:58 pm - Reply

      Pretty much anything can be incorporated in an algorithm. HFT is really not something that’s accessible to the retail trader like you or I, though – it’s the domain of huge firms with millions of dollars.

  17. brent May 19, 2014 at 10:11 pm - Reply

    Yes I realize HFT limited to the huge firms, but huge firms sometimes start with an idea…can’t really figure out another use for HFT in commodities other than spreads vs. outright orders. Will try to sort through EA, thanks for the direction, anything else you can think of would be appreciated (


  18. Monte June 18, 2014 at 3:47 am - Reply

    Thanks for the intro, Paul.
    So, I take it that MT4 runs on your client machine? I come across a number of different technologies that run locally, and some that are services. They differ and overlap in their approaches and languages.

    Algorithmic trading (Python-heads)
    Algorithmic trading (C#-heads)
    OptionsCity Freeway (Java-heads)

    Many are cloud-based, offer some sort of algorithm swapping/sharing features, and have either an online IDE or a plugin.

    I’m tracking these and other investor resources with some interest on

    It seems there is not yet a gold standard; I’m still comparing options and test-driving demos and trials. As Theresa W. Carey over at Barron’s put it, “there’s a democratic move afoot, bringing professional-grade tools to any trader with a working knowledge of technical analysis.”

    Do you ever publish your returns? I share my trades with readers, though not the quantities. Variable n in stands for the amount invested. 🙂
    Cheers and thanks again,

  19. Peter Mathers June 26, 2014 at 1:25 pm - Reply

    “games programming blog!”

    Thanks for writing this! Really informative. Expecting more to hear from you.

  20. Steve H September 5, 2014 at 6:35 pm - Reply

    Hi Paul,

    I am like you UK based – I have a bit too invest. I want to place it with a fund that has a good track record and can get a few % per month – do you know of good any funds?


    • Paul Firth September 6, 2014 at 11:54 am - Reply

      Hi Steve,

      Sorry, I don’t have such recommendations. Best of luck with your search!

      Cheers, Paul.

  21. Rob January 3, 2015 at 4:08 am - Reply

    Having played with MTQ4 EA building now for about 12 months and someone who enjoys tinkering with Fuzzy Logic AI algorithms, I have determined that the movement of the price line is substantially a random-ish event each tick – although not completely random as charting a STD dev chart of @ tick price change data will demonstrate.
    That is if you compare the narrow @ tick “price change” bell curve” to that of a Std Dev curve of a proper random price change for the same price universe / sample – the curves are very different.

    So having made that [it is random like] assumption I now program ea’s that don’t rely on historical events, indicators or movements that are News driven but try to create a profit from the “random” swings up and down in the tick price.

    Grid – like trading is probably the closest I have found as examples of those attempts before me at a same philosophy.

    I must say it has been a hugely intellectually rewarding challenge to date, keeping the grey matter working and with the bonus that if I can crack it – it might make some $ as well.

    I recommend the sport for those up for that pseudo unsolvable problem.

    Rob W

    • Paul Firth January 3, 2015 at 6:16 pm - Reply

      Hi Rob,

      Interesting – how are you defining the narrow tick time frame? Simply tick data received under the 1M bar? Did you perform an analysis on the 1M or higher time frames for a comparison?

      How are you gathering your data, from live testing or backtest data?

      Would love to hear more about your findings, feel free to contact me via the blog if you’d like to discuss in more detail. 🙂

      Cheers, Paul.

  22. Jack January 21, 2015 at 5:41 pm - Reply

    Hi Paul, Im a UK based businessman new to Forex but Im learning quickly. Im fortunate enough to be able to dedicate my full time an attention to trading as well as surrounding myself with a few industry veterans from institutional trading backgrounds. I was hoping to be able to talk to you about building an EA. I thank you for your time reading this and look forward to your response.

    • Ben April 4, 2015 at 6:21 pm - Reply

      Hi Jack, how have you found your time with Forex so far? Do you have any tips or things I should be reading to immerse myself in this market? Sadly I will only get to involve myself in my spare time currently.



  23. Ben April 4, 2015 at 6:15 pm - Reply

    Thanks for posting this Paul. I’m a control systems engineer looking to get heavily involved in the world of finance. This could just be the inspiration I need. It’s definitely something I can play with in my spare time. I’ll hopefully be able to try any EAs using a practice account while taking advantage of previous data to get a feel for how the markets work and also get my head around a lot of the terminology.

  24. Larry Fairchild July 4, 2015 at 11:23 pm - Reply

    Thanks for the great article. I am a day trader focused on futures. I am very interested in Algo-trading. I found your article very interesting and perhaps a starting point for a revised approach to futures.
    I will be looking forward to part 2
    Good job~!

  25. Steve Martin March 21, 2016 at 4:00 am - Reply

    A very informative post to read. Thanks a lot for sharing this. Good Work. Keep it Up.

  26. KAMAL KHAN June 7, 2016 at 9:39 pm - Reply

    In My 30 Years of Experience I have found “Quants” And “Algorithimic Traders” Losing money in REAL TIME senerios . They are great in Simulations and Maybe Short Term ( 1 or 3 Years) But in The Longer Term 10 Years Plus They Lose money.

  27. Luka July 10, 2016 at 7:19 am - Reply

    This was very informative. Thanks

Leave A Comment