banner



How To Create Ea Forex

9 min read

This article is part of a series. Here are the other parts:

  • Part 1: MT4 EA Forex Course: Intro
  • Part 2: MT4 EA Forex Course: our first forex expert advisor (current page)
  • Part 3: MT4 EA Forex Course: Trailing Stops
  • Part 4: MT4 EA Forex Course: buy and sell
  • Part 5: MT4 EA Forex Course: ATR stops and position sizing

Welcome to the second part of our MetaTrader4 Expert Advisor course! Missed the first part? Go read the MT4 EA course Intro now, I'll wait!

[includeme file="wp-content/post-includes/expertadvisorvault.php"]

In this part, we're going to learn how to use the MetaEditor – the Integrated Development Environment or IDE for MetaTrader4 – to develop those exceptional expert advisors! I'll show you around the editor and we're even going to create our first expert advisor. Let's call that EA theForex Wall-E.

Get it? Robots, automated programs. Ah, moooving on!

Forex Wall-E

After this article, you willfeel comfortable with the MetaEditor and know how to create a new expert advisor. That's almost halfway to raking in those pips with our automated strategy!

These are your first steps into a new world – with a huge potential pay-off.

Ready?

Let's do this.

"Invest in yourself. Your career is the engine of your wealth."

– Paul Clitheroe

Preface

Before we begin, please make sure that from now on, everything you run is on a demo account! I wouldn't want to see you wipe out your account because you made a coding error. We will use demo accounts and testing strategies to make sure everything is well tested, there's no need for a live account.

You should also be somewhat familiar with MetaTrader. If not, MetaTrader 4 is the trading software we're going to use. If you haven't yet set this up, find yourself a broker (the people at FxPro are solid, but any regulated big-name broker should be fine) and install according to the instructions by your broker.

The MetaTrader Strategy Tester

The toolbar in this screenshot should be a familiar sight to you. These are the default controls that more or less every MetaTrader application should have. If you don't see them, have a look at View – Toolbars to enable the right toolbars.

MetaTrader toolbar

Click on the Strategy Tester button (left circle) and you should see the following at the bottom of the window:

MT4 Strategy Tester

This is where we are going to run our expert advisors! You'll be able to configure which currency pair and timeframe you want to test it on and a couple of other settings we'll get back to later on. This is also where you will be able to backtest your strategies. You can think of this as the control center of your future expert advisors!

The MetaEditor

But first of all, let's go back to the toolbar and click the MetaQuotes Language Editor (right circle). This should open the editor I was talking about earlier on. A new window will open, which should look similar to this:

The MetaQuotes Language Editor

This is where the magic happens. This is like Disneyland for us EA developers. Unless you don't like Disneyland I guess. Next up: some wizardry!

The MQL Wizard

Here, you'll be able to create new Expert Advisors, write the code that makes them tick, compile and debug your strategy. Let's start with creating a new Expert Advisor, shall we?

After clicking the "New" button in the upper left corner, this window should appear:

The MQL4 Wizard

You can already see that you can do much more with MQL4 than just create Expert Advisors! You can also create your own indicators, create a script (which is just another name for a piece of code that runs just once in the client terminal) and create libraries (which are sets of functionality that can be used by multiple expert advisors).

Whoa, not too much in one go! Make sure "Expert Advisor (template)" is selected and click next.

MQL Wizard - step 2

Give the expert advisor a catchy name, such as ForexWall-E. Leave all other settings as they are and click Next.

Screen Shot 2016-07-18 at 19.45.01

Screen Shot 2016-07-18 at 19.44.49

A note on Events

The next 2 screens in the wizard will show you some options related to events. Events are moments in the execution of the expert advisor where we will be able to "hook into" the expert advisor by running some custom code. Let's quickly go over the different types of events that are available to us:

  • OnTimer: event that is executed when a timer is used. We can set the frequency in seconds ourselves. This is useful if you want to run or check something every X seconds.
  • OnChartEvent: handles a collection of events related to chart actions (click of mouse, chart changes, objects that get created, when something is dragged on the chart). Useful if you want to intercept user actions while the EA is running.
  • OnTester: this event gets called only when testing the EA, just before the test is done. This is where you could request testing data from the EA, such as profit, the balance, the number of trades your EA has made, etc.

For now, don't worry too much about these events. Leave everything on its default settings and click next and finish.

The MQL code

Ta-da! You have just created your first expert advisor. Well done, give yourself a quick pat on the back!

Still with me?

Good. Let's continue on our adventure!

Running an Expert Advisor

What you have just created is is a fully functioning Expert Advisor. Don't believe me? You can test it out, by clicking the big "Compile" button (compiling means that the MetaEditor will transform our human-readable code into machine instructions suited to actually running as an EA). On the bottom part of the MetaEditor, you should see something like this:

MT4 EA Compile done

Now go back to MetaTrader 4. You should still have the Strategy Tester on the bottom of the window. In the first dropdown, you should now be able to find your new EA:

Your new EA

When you use the same Strategy Tester settings (the From and To date are not too important yet), you can click Start to test your newly created strategy. Give it a moment to calculate, and you should be presented with a green bar on the bottom, indicating your EA finished running.

Notice the new tabs that have appeared at the bottom:

Strategy Tester tabs

Have a browse through them. For now, Results and Graph will be empty (since no trades have been made yet), but the Report and Journal tabs will show information about the expert advisor test run and some logging information, respectively:

Results of running your expert advisor

Pretty nice huh? The report tab is where you'll spend much time analysing the trades your expert advisor took. But of course, this expert advisor isn't actually doing much. Let's change that now.

Our first EA – ForexWall-E

Things might get a bit technical now, we're going to use actual code. But I promise, it will be easy to digest, we'll probably breeze through it.

First of all, let's open our MetaEditor again and edit thevoid OnTick()method so it looks like this:

void OnTick() {     if (OrdersTotal() > 0) {         return;     }          double lots = 0.01;     int stopLoss = 500;     int takeProfit = 500;     double ema = iMA(NULL, 0, 300, 0, MODE_EMA, PRICE_CLOSE, 0);          if (Ask + 500 * Point < ema) {         if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask - stopLoss * Point, Ask + takeProfit * Point, "my forex wall-e order", 12345, 0, Red)) {             Print("Buy order succeeded!");         }     } }        

After you've updated the code, hit the "Compile" button to make sure there are no errors. It should build without issues (difficulties with this? Let me know in the comments, I can help you out!).

The code may seem a bit complex, so let's go over it line by line. But first: what are ticks?

About forex ticks

A tick in the context of forex means the smallest increase or decrease in price that a currency pair can make. Therefore, every time you see the price move up or down in the smallest way, that is a tick. It is also when theOnTick event gets called.

Code walkthrough

void OnTick()        

This is our function declaration. We indicate the availability of this function, so it can be called.

          if (OrdersTotal() > 0) {         return;     }        

This if-statement makes sure that if there already is an open order, we don't open another one. OrdersTotal() is a built-in function in the MQL4 language and returns the number of orders that are currently open.

          double lots = 0.01;     int stopLoss = 500;     int takeProfit = 500;        

Here, we define some variables:

  • The lot size will be 0.01
  • The stop loss is 500 points, which equates to 50 pips on a broker that uses 5 digits, since in that case 1 pip = 1/10th of a point. If this confuses you right now, don't worry and just assume we use a stop loss of 50 pips.
  • The take profit will also be 50 pips (for the same reason as above).
          double ema = iMA(NULL, 0, 300, 0, MODE_EMA, PRICE_CLOSE, 0);        

This line means that we fetch the value of the 300 exponential moving average (or EMA). We will use this moving average value to determine if we should enter a trade or not. Now, a lot of parameters are used with this EMA! This is what they mean:

double iMA(  string symbol, // symbol  int timeframe, // timeframe  int ma_period, // MA averaging period  int ma_shift, // MA shift  int ma_method, // averaging method  int applied_price, // applied price  int shift // shift );        

This is the first time we're using the value of an indicator! There are loads more built-in indicator in MQL4, such as iStochastic and iRSI. In upcoming parts of this series, we'll cover those and lots more.

          if (Ask + 500 * Point < ema) {        

What happens here, is that we will only enter a trade if the Ask price + 50 pips is still lower than the EMA value. In other words: if the price is more than 50 pips below the 300 EMA line, we should buy.

          if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask - stopLoss * Point, Ask + takeProfit * Point, "my forex wall-e order", 12345, 0, Red)) {             Print("Buy order succeeded!");         }        

Finally, we get to make the actual order! The OrderSend() function is another built-in function that allows us to programmatically create an order. In this case, we create a buy order for 0.01 lots, with the stop loss and take profit we defined earlier.

That's it!

Click the Compile button once again and then go back to MetaTrader. Start the expert advisor test in the Strategy Tester.

Now, the Results, Graph and Report tabs are not so boring anymore:

EA results

EA result graph

EA Report tab

And as you can see, it even seems our first expert advisor even made a modest profit (largely by luck, no doubt. your results may be different depending on the currency pair, chosen timeframe and broker).

But it's an encouraging result nonetheless! Everyone loves a graph going up, right?

Conclusion

Whoop, part two of our MT4 expert advisor course packed a lot of information!

You've learned how to use the Strategy Tester.

You've familiarised yourself with the MetaEditor and created a new expert advisor.

You've built your first EA and simultaneously taken the first steps with MQL4.

That's it for this time. I encourage you to play with your newly created expert advisor, change some of the parameters we've defined and see how they influence the outcome of running your EA in the strategy tester.

Next time, we will continue with our expert advisor and step by step improve it and make it more solid. If you have questions, please let me know in the comments.

Subscribe to my newsletter below if you want to be kept up to date on when the next part of this course is published!

How To Create Ea Forex

Source: https://smartforexlearning.com/mt4-ea-course-first-forex-expert-advisor/

Posted by: smithwich1999.blogspot.com

0 Response to "How To Create Ea Forex"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel