How to create an MQL4 Expert Advisor?

Dear trader, have you ever thought about how to create an MQL4 Expert Advisor? To create, not order from a professional programmer. In fact, the task is not that difficult. And the advantages are obvious: savings on freelance services and the ability to further create your own trading advisors. The skill won’t go anywhere! This is not to mention the pleasure that you yourself have created a working MQL4 Expert Advisor, which can potentially bring profit from Forex in automatic mode.

Starting to create an MQL4 Expert Advisor

To get started, you need basic knowledge of the MQL4 programming language. You can download a book describing the basics of this language here

http://docs.mql4.com/ru/index
The MT4 help (called by the F1 key in the terminal itself) and the MetaQuotes Language editor help (press F4 in the MT4 terminal, then F1) will also help beginners.

Forex Expert Advisor – BackTest

You can disassemble the finished program – what commands are used, how they work, etc. Or you can start by writing a simple trading algorithm, so that in the future you can add new commands to the ready-made backbone or change the ready ones. Those. from simple to more complex.

Also, when creating a trading robot, advice and recommendations of those traders (programmers) who already have some experience in this business will certainly come in handy. A lot of useful information can be found on the forums of traders using MQL4 (www.mql4.com) or MQL5 (www.mql5.com). Here you can get an answer to almost any question related to writing expert advisors and more.

//+----------------------+
//| Expert tick function                                         
//+----------------------+
void OnTick()
  {
   double op1=iCustom(NULL,0,IndName,2,Shift);
   double cl1=iCustom(NULL,0,IndName,3,Shift);
   double op2=iCustom(NULL,0,IndName,2,Shift+1);
   double cl2=iCustom(NULL,0,IndName,3,Shift+1);

   double adblu=iCustom(NULL,0,IndName2,ADXPeriod,0,Shift);
   double adred=iCustom(NULL,0,IndName2,ADXPeriod,1,Shift);

   double mablu=iCustom(NULL,0,IndName3,MAPeriod,Method,Price,Sdvig,1,Shift);
   double mared=iCustom(NULL,0,IndName3,MAPeriod,Method,Price,Sdvig,2,Shift);

   bool buy =  cl1>op1 && cl2>op2 && adblu!=0 && mablu!=EMPTY_VALUE;
   bool sell = cl1<op1 && cl2<op2 && adred!=0 && mared!=EMPTY_VALUE;

   if(CountTrades()<1)
     {
      if(buy && Buy)
        {
         PutOrder(0,Ask);
        }
      if(sell && Sell)
        {
         PutOrder(1,Bid);
        }
     }

   if(CloseSig>0)
     {
      if(buy)
        {
         CloseAll(1);
        }
      if(sell)
        {
         CloseAll(0);
        }
     }
  }    

We are developing a Expert Advisor for the following strategy.
The advisor’s work is based on the following indicators:
Main indicator Heiken Ashi
Indicators used: Advanced_ADX, VininI_HMA_sound_Alert

The BUY position is opened when the second candlestick is closed. Heiken Ashi indicator colored two candles blue, VininI_HMA line colored blue, Advanced_ADX indicator colored blue. If only the second Heiken Ashi candlestick is colored, we are waiting for confirmation by other indicators. The position is opened only when all three conditions are met.

The SELL position is opened when the second candle is closed. Heiken Ashi indicator colored two candles red, VininI_HMA line colored red, Advanced_ADX indicator colored red.
When the opposite signal appears, it is necessary that the previous one automatically closes and a new one opens.

Trading is possible either only LONG or only SHORT or in both directions.
This parameter must be made with the ability to disable.
LONG only: opening by BUY opening conditions. We close under the SELL conditions, we do not open down.
Likewise, only SHORT.

Creating a expert advisor for Metatrader 4