How to write a forex advisor for MT4. Step-by-step instruction.

To write an Expert Advisor for MT4, you must use the MQL4 programming language. To get started, we need the MT4 terminal itself and the MQL4 development environment – MetaEditor.

Step 1: Launch MetaEditor

MetaEditor is an embedded development environment that can be found in the MT4 terminal. Press the F4 key or select “Tools” -> “MetaQuotes Language Editor” from the main menu of the terminal.

Step 2: Create a new file

To create a new file, select “File” -> “New” in the MetaEditor main menu or use the key combination Ctrl+N. Select “Expert Advisor” as the file type.

Step 3: Write the code

Write the Expert Advisor code in MQL4. The program must contain the OnInit(), OnDeinit(), OnTick() and OnTrade() functions. The OnInit() function is called once when the Expert Advisor is initialized, the OnDeinit() function is called when the Expert Advisor is closed, the OnTick() function is called every time the price changes, and the OnTrade() function is called every time an order is triggered.

Here is an example of a simple Expert Advisor that opens a position when the price crosses through a moving average:

					// Объявление переменных
double MA;
double Ask;
double Bid;
int MagicNumber = 12345;

// Инициализация советника
void OnInit()
    // Установка периода скользящего среднего
    int MA_period = 50;
    // Расчет скользящего среднего на основе цены закрытия
    MA = iMA(Symbol(), PERIOD_CURRENT, MA_period, 0, MODE_SMA, PRICE_CLOSE);

// Основной цикл советника
void OnTick()
    // Получение текущих цен Ask и Bid
    Ask = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK), Digits);
    Bid = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID), Digits);

    // Если цена Bid пересекает скользящее среднее сверху вниз, то открываем позицию Buy
    if (Bid < MA)
        // Размер лота для открытия позиции
        double LotSize = 0.1;
        // Открытие позиции Buy
        int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, "My EA", MagicNumber, 0, Green);

    // Если цена Ask пересекает скользящее среднее снизу вверх, то открываем позицию Sell
    if (Ask > MA)
        // Размер лота для открытия позиции
        double LotSize = 0.1;
        // Открытие позиции Sell
        int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, "My EA", MagicNumber, 0, Red);

Please note that this code is for demonstration purposes only and is not suitable for live trading without the necessary modifications and testing.

Step 4: Save the file

Save the Expert Advisor file using the Ctrl+S key combination or by selecting “File” -> “Save” from the MetaEditor main menu. Specify a file name and select the .mq4 extension.

Step 5: Compile the file

Compile the file using the “Compile” button in MetaEditor or use the key combination F7. If the compilation was successful, you will see the message “0 errors, 0 warnings” at the bottom of the window.

Step 6: Upload the file to MT4

Upload the compiled EA file to the MT4 terminal. To do this, select “File” -> “Open Data Folder” in the main menu of the terminal, go to the MQL4 -> Experts folder and copy the Expert Advisor file to this folder.

Step 7: Launch the Expert Advisor

Run the adviser on the chart in the MT4 terminal.


Although creating an Expert Advisor for MT4 may seem complicated, it is possible for anyone who has programming experience and an understanding of the financial markets. You can use the many resources available on the Internet to get more information and help in building your Expert Advisor.

Leave a Comment

Your email address will not be published. Required fields are marked *

📨 Subscribe to our newsletter

Scroll to Top

📨 Подпишись на рассылку новостей

Не шлем СПАМ! Гарантия

Subscribe to our Telegram channel
Открыть чат
Scan the code
Hello 👋
How can I help you?