MetaTrader 5 – MQL5 for Beginners

Jun 25th, 2010 | Filed under MetaTrader 5

Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners

Introduction

This article is aimed at beginners who wish to learn how to write simple Expert Advisors in the new MQL5 language. We will begin first by defining what we want our EA (Expert advisor) to do, and then move on to how we want the EA to do it.

Figure 1. Starting a new MQL5 document

1. Trading Strategy

What our EA will do:

  • It will monitor a particular indicator, and when a certain condition is met (or certain conditions are met), it will place a trade (either a Short/Sell or Long/Buy), depending on the present condition that has been met.

The above is called a trading strategy. Before you can write an EA, you must first develop the strategy that you want to automate into the EA. So in this case, let us modify the above statement  so that it reflects  the strategy we want to develop into an EA.

  • We will use an indicator called Moving Average with a period of 8 ( You can choose any period, but for the purpose of our strategy, we will use 8 )
  • We want our EA to place a Long (Buy) trade when the Moving Average-8 (for the sake of our discussion, I will refer to it as MA-8) is increasing upwards and the price is close above it and it will place a Short (Sell) when MA-8 is decreasing downwards and the price is close below it.
  • We are also going to use another indicator called Average Directional Movement (ADX) with period 8 also to help us determine whether the market is trending or not. We are doing this because we only want to enter the trade when the market is trending and relax when the market is ranging (that is, not trending). To achieve this, we will only place our trade (Buy or Sell) when above conditions are met and the ADX value is greater that 22. If ADX is greater that 22 but decreasing, or ADX is less than 22, we will not trade, even though the condition B has been met.
  • We want to also protect ourselves by setting a Stop loss of 30 pips, and for our Profit target; we will target a profit of 100 pips.
  • We also want our EA to look for Buy/Sell opportunities only when a new bar has been formed and we will also make sure we open a Buy position if the Buy conditions are met and we do not already have one opened, and open a Sell position when the Sell conditions are met and we do not already have one opened.

We have now developed our strategy; it is now time to start writing our code.

View full article at http://www.mql5.com/en/articles/100

Related posts:

  1. MQL5: Create Your Own Indicator – MQL5 Wizard
  2. MetaTrader 5 – Testing Performance of Moving Averages Calculation in MQL5
  3. Transferring Indicators from MQL4 to MQL5
  4. MetaTrader 5 – How to call indicators in MQL5
  5. How to Quickly Create an Expert Advisor for Automated Trading Championship 2010

Comments are closed.