Coding

MQL5 & MT5

MQL5 is the programming language for MetaTrader 5. Learn to build your own indicators and Expert Advisors to automate and enhance your trading.

100% Free Lifetime Access No Login Required
Live OANDA:EURUSD Chart — Example Powered by TradingView

MQL5 Basics

MQL5 (MetaQuotes Language 5) is the programming language used for writing EAs (Expert Advisors), custom indicators, scripts and libraries for MetaTrader 5.
  • Syntax similar to C++ — if you know any programming, it is easy to pick up
  • Main functions: OnInit() (setup), OnTick() (every price tick), OnDeinit() (cleanup)
  • Variables: int, double, string, bool, datetime — same as most languages
  • Built-in trading functions: OrderSend(), PositionOpen(), AccountInfoDouble()
  • MQL5 IDE (MetaEditor) is built into MT5 — press F4 to open
  • Compilation: press F7 to compile, errors shown in Errors tab

Your First Indicator

Start by creating a simple moving average indicator to understand the structure:
  • #property indicator_chart_window — places indicator on the main chart
  • #property indicator_buffers 1 — one line to draw
  • input int MA_Period = 14 — user-configurable parameter
  • In OnCalculate(), loop through bars and calculate MA values
  • ArraySetAsSeries() — makes arrays index from newest bar
  • Use iMA() built-in function for moving averages
  • Compile and drag from Navigator window onto chart to test

Pro Tip: The best way to learn MQL5 is to open a free indicator on MQL5.com, read the code, and modify one small thing at a time. Within a week of this approach you will understand the structure completely.

Strategy Tester & Optimization

  • Open Strategy Tester: Ctrl+R in MT5
  • Select your EA, symbol, timeframe and date range
  • Visual mode: watch EA trade in real-time — great for debugging
  • Optimization: check the box, set parameter ranges, MT5 tests every combination
  • Optimization results: sort by Profit Factor column — values above 1.5 are promising
  • Forward test: after optimizing on data 2015-2022, test on 2022-2024 to check if it holds up
  • Multi-threaded: MT5 uses all CPU cores for optimization — much faster than MT4

MQL5 Marketplace

  • Largest marketplace for MT4/MT5 products — over 10,000 indicators and EAs
  • Sell your products: create a seller account at mql5.com, upload, set price
  • MQL5 handles payments, customer service and delivery automatically
  • Revenue share: MQL5 takes 25-30% commission on each sale
  • Rental model: offer monthly subscriptions in addition to one-time purchase
  • Support and updates: respond to buyer questions to maintain ratings
  • Our MQL5 profile: visit mql5.com/en/users/kulvinder99/seller for our products
Previous
Indicators Guide
Next
cTrader & TradingView

Key Takeaways

  • MQL5 is C++ based — structured and logical
  • Start by modifying existing free code
  • Strategy Tester is the most powerful backtesting tool
  • Optimization finds the best parameter values
  • MQL5 Marketplace to sell your products globally
  • Use OnTick() for every price update logic
  • OnInit() runs once when EA loads
  • Always handle errors and use proper lot validation

All Free Topics

Want Live Classes?

Join our paid courses for live Q&A, video lessons & mentorship.

View Courses →