8.2 C
New York
Thursday, March 28, 2024

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

15 COMMENTS

Subscribe
Notify of
15 Comments
Inline Feedbacks
View all comments

Stay Connected

157,453FansLike
396,312FollowersFollow
2,280SubscribersSubscribe

Latest Articles

15
0
Would love your thoughts, please comment.x
()
x