Sign up today for an exclusive discount along with our 30-day GUARANTEE — Love us or leave, with your money back! Click here to become a part of our growing community and learn how to stop gambling with your investments. We will teach you to BE THE HOUSE — Not the Gambler!

Click here to see some testimonials from our members!

Rage against the machines… by building your own machine!

This is intended to be an introduction to the world of robotic Forex trading.  By the end of this you should be able to construct a working automated Forex trading robot.  These automated trading bots are called Expert Advisors, EA’s for short.  I am going to assume that you don’t have much FX trading experience and walk you through the whole process of setting up your first EA.

Step 1.  Download and install MetaTrader4.  This is the platform our EA’s will run in.  Once MT4 is dowloaded go ahead and start it up.  Just click cancel on any dialogs that show up.  We are not going to hook it up to an account yet, we are just going to set it up so that you can play around and backtest your EA’s.  Once MT4 is up and running you should see something like the following screen.

(click for life size version)

Click Tools then History center (or hit F2).  Now we should be looking at this.

Expand Forex then EURUSD then click on M1 (the one minute candles).  Hit download.  This will download some of the historical data.  At this stage we need to download some additional data which will fill in the holes.  Here is a file which contains data for all of 2010, EURUSD1.  Now we need to import this data into our program.

To import this data click on the M1 data for the EURUSD just like in the previous step.  Hit import.  You should see the following screen.

If you dont see the candles in the import box then just close the history center and open it again.

At this point we are ready to build and backtest our first EA.

Step 2.  Building our first EA.

Before we hop right into it I want to give you a little bit of information on how an EA works.  So go read the following introduction to MQL4, the programming language we use to write these things.  Now that you understand the basics, lets create a simple EA that buys every time the market drops 20 pips, with a stoploss of 15 pips and a take profit of 10 pips.  To begin, right click on Expert Advisors,in the Navigator window, and choose create.  We should be looking at the following screen.

Click next, give our EA a name then click finish.  We should now be looking at

I coded up the program for you so all you have to do is replace your Start() function with this one.

double currentHigh=0.0;

double pipsDown=0.0020;

double stopLoss=0.0015;

double takeProfit=0.0010;

double volume=0.1;

double slippage=5;

int start()

{

//keep track of the current high of the market

if(Ask>currentHigh)

{

currentHigh=Ask;

}

//If we have dropped 20 pips down place a buy Order

if((currentHigh-Ask)>pipsDown)

{

OrderSend(Symbol(),OP_BUY,volume,Ask,slippage,Ask-stopLoss,Ask+takeProfit);

currentHigh=Ask;

}


return(0);

}

Hit compile and make sure there are no errors then click Terminal.  To backtest this guy you want to click View then strategy tester.  We should now be looking at

In the bottom portion of the window click on drop down next to Expert Advisor: and select the one we just created.  Make sure Use data is checked and that the date is set from 2010.01.01 to 2010.12.31.  Click start.  After it finishes click graph from the tabs at the bottom of the screen.  We should see the following graph

Congratulations!!

You have successfully coded up and backtested your first Automated Forex trading robot.  This particular robot is pretty terrible, since the graph does not take into account commisions.  But using what we learned as a blueprint our new goal is to build something that actually makes a profit!

Start coding up your own solutions and next weekend we can see if anyone can come up with a strategy that stays profitable for the entire year.  Next weekend look for the follow up “Choosing a Forex Broker that wont try to rob you blind”.


Tags: , , ,

Do you know someone who would benefit from this information? We can send your friend a strictly confidential, one-time email telling them about this information. Your privacy and your friend's privacy is your business... no spam! Click here and tell a friend!



Comments (reverse order)


    You must be logged in to make a comment.
    You can sign up for a membership or get a FREE Daily News membership or log in

    Sign up today for an exclusive discount along with our 30-day GUARANTEE — Love us or leave, with your money back! Click here to become a part of our growing community and learn how to stop gambling with your investments. We will teach you to BE THE HOUSE — Not the Gambler!

    Click here to see some testimonials from our members!


  1. Great step by step--but would a novice be able to trade?


  2. I dont see any reason why not.  The next step after completing this is selecting an ECN forex broker which allows MT4 and Expert Advisors.  After that you are ready to go.
     
    Before worrying about trading you should develop a strategy which returns a decent profit when backtested against the previous year.


  3. Craig, thanks for the explanation! I see that this is not just straight programming as they have some specialized keywords! That should make it easier. I’ll have a look and see if I can put together some of the indicators I am using. 


  4.  I dont claim to no everything about building EA’s, but if you need any help I would be happy to lend a hand.


  5. tx


  6. craig, so the script you write runs on ‘their’ server and not your own?  If so, they can subvert your logic if they want to-  Otherwise, very cool. 


  7. Matt1966,  the script runs on your computer inside your client, not theirs.  Basically, a tick in the EUR/USD gets sent from their server to your client,  this causes your function Start() to get called.  Then Start() decides whether to buy or sell or do nothing.  Their server never gets to see what is going on in your client.  


  8. Craig,
    That is such a great introduction; you put a lot of work into this presentation; thanks so much.  Do you know if TOS will accept orders for a client; what happens if the internet were to go down. You are placing stop loss orders with their server, or do they have to be sent by your client.  Thanks


  9.  humvee4me, unfortunately there is no way to use MT4 with TOS :( .  I was really hoping there would be but they also dont have any plans to allow it. 
     
    If your internet goes down then bot is just shut off.  There are solutions to this if your bot begins to make some serious cash.  Virtual hosting runs about $60 per month then you are pretty much guaranteed your bot will never go down.
     
    Once an order goes through it is placed with there server so you dont have to manage it anymore.  The stoploss and takeprofit will automatically be executed whether you are connected or not.


  10.  By the way, I am hoping to build a little community of forex EA programmers here.  It would be sweet if we had our own tab.  That way each idea we have for an EA could get its own post.  THen we could work on it together under that post until we have something polished.


  11. Craig, can you run optimization to look for the best possible parameters? For example search for the best profit, stop loss etc… I already use Amibroker,  NeuroShell Trader and Chaos Hunter where programming new systems is quite simple. Amibroker has its own script language that is very adapted to technical analysis and they link to IB. But I use TOS so I never used the trading interface. So I am limited on the type of order I can backtest. NeuroShell allows for some different types of orders and they also have a link to IB. Bot software have some sort of neural network implementation which helps with optimization. You could technically optimize with large numbers of variables although usually the simplest system work well…


  12. Hi Craig,
    cool system loaded but can not uplaod m1 2010 memory in to the system any idea thks


  13. Nice!  Not a forex trader, but very cool indeed.


  14.  @stjeanluc, yeah you can optimize.  I didn’t cover how to do it this post, i was gonna save it for the next post.  The basics are, you declare a global variable as
    extern double takeProfit;
     
    Then you can set the start value, increment, and end value of takeProfit.  When you run it, the MT4 backtester will try all of those values and spit out a nice report for you so you can choose the best value.


  15.  @yodi,  If you have skype I can go through the procedure with you some time this afternoon.  I will be back home today at 3pm eastern.