Home Coding your first expert advisor – Writing the code
Basics & Industry, Forex Basics, Forex Software

Coding your first expert advisor – Writing the code

Once you have your trading strategy down on paper and you are aware of the principles that go into coding your first expert advisor, it’s time to start writing the code. Once MetaTrader4 is installed and open, bring up the MetaEditor (F4) and create a new EA from scratch.

MQL4, which is the language used to create expert advisors in MetaTrader has its own unique structure, as follows:

Guest post by  FXTM

  • Header
  • Special function initialise
  • Special function start
  • Special function de-initialise
  • Control

Header

The Header section is where your global settings go and it’s the obvious place to start when you build your EA. This is where you identify the indicators and parameters that you will be working with and the signals that your EA will be using. It’s also where you set your money management rules, where you name the EA and where you can set other global variables such as stops.

Example of Header for moving average EA:

// Header

// Example Header

extern double StopLoss     = 200;                                       // SL for an opened order in pips

extern double TakeProfit = 39;                                             // ТР for an opened order in pips

extern int Period_MA_1= 50;                                                 // Period of first MA

extern int Period_MA_2= 200;                                             // Period of second MA

extern double Rastvor = 25.0;                                             // Distance between MAs

extern double Lots = 0.1;                                                             // Set amount of lots position size

extern double Prots = 0.08;                                                     // Percent of free margin

bool Work=true;                                                                                         // EA will work.

string Symb;                                                                                                     // Security name

Special function initialise & start

In the next section of code you retrieve the value of your chosen indicators, initialize the trading logic and system criteria specified in the header, loop through current orders and signals and then check for signals.

Expert Advisor Code Example MT4

The process of the special function obtains the indicator value then cycles through the other processes. Next the code checks for open orders in OrdersAccounting. It then finds the value of the indicator in GetIndicatorValue.

OrdersAccounting

Void       OrdersAccounting ()

{                                                                    

                    Display EAinfo();                                         // Returns total number of positions already open

// OrdersAccounting

 

GetIndicatorValue

Void       GetIndicatorValue ()

{

MACDnow = iMACD(Symbol(),0, fast_EMA, slow_EMA, signal_period, MACDprice,0,1);

This returns the indicator value of a MACD which can then be used for buy and sell decisions.

Buy logic

If we have no open positions and the MACD just crossed over the 0 line we can enter a buy order into the market:

Void       TradingLogic()

{

if(Total==0 && justcrossed==1)

{

BuyOrder();

return;

}

 

Sell logic

If we have no open positions and the MACD just crossed under the 0 line we can enter a sell order into the market:

Void       TradingLogic()

{

if(Total==0 && justcrossed==2)

{

SellOrder();

return;

}

}

Exiting the trade

Exiting the trade is very similar to entering the trade but in reverse. Here, if we have a long position and the MACD crosses 2 we close the trade:

Void       ExitLogic()

{

if(Total>0 && Type==OP_BUY && justcrossed==2)

{

CloseIt();

return;

}

}

Here, if we have a short position and the MACD crosses 1 then we close the trade:

Void       ExitLogic()

{

if(Total>0 && Type==OP_SELL && justcrossed==1)

{

CloseIt();

return;

}

}

 

Now that you know the basics of a simple EA, the best thing to do is to head over to the MQL4 community where you can dive in and start learning from thousands of other traders.