Learn the essentials of MQL programming to automate trading strategies, create custom indicators, and enhance your trading performance.

MQL (MetaQuotes Language) is a programming language used to create automated trading systems for the MetaTrader platform. It allows traders to build custom indicators, scripts, and Expert Advisors (EAs) for executing trading strategies automatically. Here's what you need to know:

  • MQL Basics:
    • Custom Indicators: Visualize market trends for analysis.
    • Scripts: Execute one-time trading operations.
    • Expert Advisors (EAs): Automate trading processes entirely.
  • MQL4 vs. MQL5:
    • MQL4 is procedural, supports single timeframes, and is compatible with MetaTrader 4.
    • MQL5 adds object-oriented programming, multi-timeframe, and multi-currency support, working with MetaTrader 5.
  • Core Functions:
    • OnInit(): Initializes trading parameters.
    • OnTick(): Processes market data and executes logic.
    • OnDeinit(): Cleans up resources.
  • Key Tools:
  • Practical Tips:
    • Use SetIndexBuffer() for efficient memory use in indicators.
    • Optimize calculations with IndicatorCounted() to save resources.
    • Implement risk management with dynamic position sizing and stop-loss levels.

Quick Comparison: MQL4 vs MQL5

Feature MQL4 MQL5
Platform Compatibility MetaTrader 4 MetaTrader 5
Programming Paradigm Procedural Procedural, Object-oriented
Timeframe Support Single timeframe Multi-timeframe
Currency Pair Analysis Single currency Multi-currency
Execution Speed Standard Enhanced

Mastering MQL enables traders to automate strategies, create custom tools, and optimize trading performance. Whether you're a beginner or an advanced coder, consistent practice and thorough testing are crucial for success.

Master MQL5 Programming

MQL Programming Fundamentals

Mastering MQL fundamentals is key to building trading algorithms that perform well. MQL's structure is similar to C++, making it easier for developers familiar with modern programming languages to get started.

Basic Syntax and Data Types

MQL uses specific data types designed for performance:

Data Type Processing Speed Common Use
Integer Fastest Calculating price levels
Double Medium Handling precise price values
String Slowest Comments, alerts in trades

MQL automatically converts data types unless explicitly directed otherwise. For price data, use double for precision, while integer works well for loop counters or array indices. Special types like color and datetime help with visual parameters and time-based trading logic.

Program Structure and Functions

Every MQL program revolves around three main functions:

  1. OnInit()
    • Define trading parameters.
    • Initialize variables.
    • Set timers with EventSetTimer().
  2. OnTick()
    • Process market data.
    • Execute trading logic.
    • Manage orders.
  3. OnDeinit()
    • Close handles.
    • Remove timers with EventKillTimer().
    • Perform cleanup tasks.

Market Data and Indicator Access

MQL provides functions to retrieve market data asynchronously:

Function Purpose Example Use
CopyRates Full bar data Historical analysis
CopyClose Closing prices Identifying trends
iMA Moving Average Following trends
iRSI Relative Strength Index Measuring momentum

"DirFX.com user ‘Well’ demonstrated that when the RSI is below 30 (oversold) and the price is above the moving average, the strategy generates a buy signal, conversely, when the RSI exceeds 70 (overbought) and the price falls below the moving average, it results in a sell signal."

MQL indexes timeseries data from newest to oldest, which affects how historical values are accessed. When dealing with multiple timeframes or symbols, be prepared to handle the ERR_HISTORY_WILL_UPDATED error to ensure consistent data access.

These core principles lay the groundwork for creating custom indicators and expert advisors.

Creating Custom Trading Indicators

Custom trading indicators in MQL are designed to turn raw market data into visual insights. These tools are essential for traders looking to analyze trends and make informed decisions. To create effective indicators, a well-organized code structure is key.

Indicator Code Structure

Every custom indicator relies on a few fundamental components. It starts with #property directives, which define how the indicator looks and behaves. Two main functions handle its operation in MQL4:

Function Purpose Key Operations
init() One-time setup Assign buffers, configure style settings
start() Ongoing calculations Compute values, update data buffers

According to MetaQuotes documentation, MQL4 indicators can display up to eight lines at once, so plan your buffers accordingly.

Setting Up User Parameters

User parameters allow traders to adjust indicator settings directly from the interface. Declare them with the input modifier:

input int    InpMAPeriod  = 14;        // Moving Average Period
input int    InpMAShift   = 0;         // MA Horizontal Shift
input int    InpMAMethod  = MODE_SMA;  // Smoothing Method

These variables are read-only at runtime, enabling users to tweak settings without altering source code. For parameters that must remain fixed during optimization, use sinput.

Code Optimization Tips

To ensure smooth performance, consider these strategies:

  • Buffer Management
    Use SetIndexBuffer() to bind indicator arrays, keeping memory usage low.
  • Optimized Calculations
    IndicatorCounted() processes only bars that changed since the last update, saving CPU time.
  • Efficient Data Access
    Match built-in functions to your needs:
    Function Best Use Case Performance Impact
    CopyRates Full bar analysis Higher memory usage
    iClose Single price point Minimal overhead
    iMA Moving averages Optimized calculation

Limit calculation history during development to speed up debugging and conserve resources.

Building Expert Advisors

Expert Advisors (EAs) are automated trading programs that execute trades based on predefined rules. Reliable EAs require clear logic, robust error handling, and rigorous testing.

EA Main Functions

Core functions in an EA:

Function Purpose Key Operations
OnInit() Initialization Set variables, configure logs, issue alerts
OnTick() Main execution Process market data, manage orders, apply trading logic
OnTimer() Scheduled tasks Handle time-based operations
OnDeinit() Cleanup Generate reports, close files, release resources

Trading Rules in Code

Example buy condition using margin and spread checks:

double spreadPoints = MarketInfo(Symbol(), MODE_SPREAD);
if(AccountFreeMargin() > MinMargin && spreadPoints < MaxSpread)  {
    if(MACD_Main > 0 && MACD_Signal > 0)  {
        int ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, SL, TP,
                               "MACD_Buy", 0, 0, clrBlue);
        if(ticket < 0)  {
            Print("OrderSend error: ", GetLastError());
        }
    }
}

Replace MinMargin, MaxSpread, Lots, SL, and TP with values or inputs appropriate to your strategy.

Risk Management Code

Robust risk management protects trading capital. A simple example:

input double RiskPercent = 1.0;            // percent risk per trade
double atr = iATR(NULL, 0, 14, 0);         // 14-period ATR
double stopLoss = NormalizeDouble(Ask - atr * 2, Digits); // 2 × ATR stop
double riskPerLot = Ask - stopLoss;        // points at risk per lot
double positionSize = (AccountBalance() * RiskPercent / 100.0) / riskPerLot;
positionSize = NormalizeDouble(positionSize, 2);          // two decimals

This code sizes each trade so that risk equals a set percentage of account balance, with stop-loss distance based on market volatility.

Always validate order placement:

int ticket = OrderSend(Symbol(), OP_BUY, positionSize, Ask, 3,
                       stopLoss, takeProfit, "ATR_Risk", 0, 0, clrNone);
if(ticket < 0)  {
    int error = GetLastError();
    PrintFormat("OrderSend failed, error %d: %s", error,
                ErrorDescription(error));
}

Testing and Improving Trading Code

MetaTrader’s Strategy Tester validates and refines algorithms before live deployment.

Strategy Tester Guide

Strategy Tester

Open Strategy Tester in MetaTrader 4 with Ctrl+R.

// Initial test parameters
input double InitialDeposit = 10000;
input int    Slippage       = 3;
input ENUM_TIMEFRAMES TestPeriod = PERIOD_H1;
Modeling Method Accuracy Speed Best Use Case
Every tick Highest Slowest Final validation
Control points Medium Balanced Initial testing
Open prices only Lowest Fastest Quick checks

MetaQuotes notes that the “Every tick” model, combined with variable spread, can achieve up to 99.9 percent accuracy, ideal for precision strategies.

Testing Methods

Validate your code by combining:

  • Backtesting on historical data, both in-sample and out-of-sample.
  • Forward Testing on a demo account to catch live execution issues.
  • Walk-Forward Analysis across rolling time windows to reveal curve fitting.

Common Code Fixes

Issue Solution Validation Method
Data mismatch Download fresh history Compare results across timeframes
Parameter bloat Trim inputs Test on unseen data
Execution errors Add error handling Monitor with GetLastError()
if(!OrderSend(Symbol(), OP_BUY, Lots, Ask, 3,
              StopLoss, TakeProfit, NULL, 0, 0, clrNone)) {
    int error = GetLastError();
    PrintFormat("OrderSend failed, error %d: %s",
                error, ErrorDescription(error));
    return;
}

"Test Driven Development focuses on writing tests that define expected behaviour before implementing any code."

Conclusion: Next Steps in MQL

The algorithmic trading market is projected to grow at about 12.3 percent annually, reaching 8.6 billion dollars by 2027. There has never been a better time to sharpen your MQL skills through consistent practice and smart resource use.

MetaTrader offers excellent tools to get started. For example, MetaEditor provides syntax highlighting and debugging, making it easier to write and refine code in both MQL4 and MQL5.

Skill Level Focus Areas Recommended Resources
Beginner Basic syntax, simple indicators MQL4-5 documentation, built-in examples
Intermediate Custom EAs, risk management Darwinex code samples, community forums
Advanced Complex algorithms, optimization GitHub: advanced-mql-programming

For hands-on learning, explore the GitHub repository darwinex/advanced-mql-programming. It includes practical examples for risk management and Ichimoku strategies.

Ready to build your own trading systems? Follow these steps:

  • Start with simple projects in MQL4 to establish a solid foundation.
  • Use modular programming and include proper error handling.
  • Test thoroughly with Strategy Tester’s “Every tick” model for maximum accuracy.
  • Begin with a practice account before moving to live trading.

Stay current by visiting forums on MQL5.com and reviewing official documentation regularly. Consistent coding practice and exhaustive testing pave the way to effective, profitable trading systems.

References