MQL5 Coding

Practical MQL5 lessons on events, indicator buffers and Expert Advisor implementation. Educational examples with explicit behavior and verification steps.

CopyBuffer in MQL5: Read Closed Bars Without Reversing Your Signals

CopyBuffer in MQL5: Read Closed Bars Without Reversing Your Signals

Technical revision: 1 October 2026 · Educational materialOn this pageFirst choose the barsUse a numerical test before using a chartRequire a complete and valid readDistinguish physical storage from series indexingA short verification sequenceA common MQL5 fault is to read two indicator values correctly, then interpret their array positions backward. Another is to evaluate an unfinished candle while describing the rule as a closed-bar signal. Both faults can change an EA's entries without producing a compiler error. First choose the bars Shift 0 refers to the current, unfinished bar. Shift 1 is the last closed bar; shift 2 is the closed…
Read More
MT5 Order Errors 10014, 10016 and 10030: Volume, Stops and Filling

MT5 Order Errors 10014, 10016 and 10030: Volume, Stops and Filling

Technical revision: 1 October 2026 · Educational materialOn this pageRead the server result10014: invalid volume10016: invalid stops10030: invalid fillingRun a read-only trade diagnosticUse a repeatable debugging sequenceWhen a trade request fails, inspect the server return code and the request values together. A generic “order failed” message hides whether the issue is volume, protective prices, execution policy or something else. Blind retries can create a new problem if the original response was uncertain. Read the server result CTrade method returns and ResultRetcode describe different layers. Log the method result, numeric server code, its description, symbol, requested volume and prices. A request…
Read More
Trailing Stops in MQL5: Filter Positions and Respect Broker Constraints

Trailing Stops in MQL5: Filter Positions and Respect Broker Constraints

Technical revision: 1 October 2026 · Educational materialOn this pageManage only the positions you ownDefine activation, distance and minimum improvementUse the downloadable managerCheck constraints before modifyingCompare exits, not selected success storiesA trailing stop changes the requested exit level after price moves in favor of a position. It changes the distribution of exits; it does not prove that a strategy has an edge. Compare it with a fixed-stop baseline using the same entries, costs and validation periods. Manage only the positions you own Iterating over all account positions while using the chart symbol's prices can modify an unrelated trade. The example…
Read More
Risk Management in MQL5: Position Sizing and Broker-Valid Stops

Risk Management in MQL5: Position Sizing and Broker-Valid Stops

Technical revision: 1 October 2026 · Educational materialOn this pageEstimate entry-to-stop loss in account currencyRound down and reject oversize minimumsRound prices on the tick gridAccount risk is broader than one stopPosition sizing should translate an explicit loss estimate into a broker-valid volume. It should also know when no valid order fits the budget. A stop defines a requested exit condition; it cannot guarantee the realized loss when execution gaps or costs differ from the estimate. Estimate entry-to-stop loss in account currency Using a fixed pip value can fail across symbols, contract types and account currencies. The helper uses OrderCalcProfit at…
Read More
How to Write a Custom MQL5 Indicator with Consistent Buffer Indexing

How to Write a Custom MQL5 Indicator with Consistent Buffer Indexing

Technical revision: 1 October 2026 · Educational materialOn this pageDefine the output and its unitsChoose one array conventionComplete indicator sourceWarm-up, updates and history changesVerify the numbers before interpreting the shapeA useful first custom indicator is the distance between a closing price and its simple moving average. It exposes the central implementation problem: every input array, output buffer and loop must agree on the direction of time. Define the output and its units For each bar, calculate the average of that bar's close and the preceding N-1 closes. Output close - average. A positive value means the close is above its…
Read More
MQL5 OnInit, OnTick and OnDeinit: Build a Reliable EA Lifecycle

MQL5 OnInit, OnTick and OnDeinit: Build a Reliable EA Lifecycle

Technical revision: 1 October 2026 · Educational materialOn this pageOnInit: validate inputs and own your handlesOnTick: postpone incomplete readsA complete logger with no ordersOnDeinit: release and explainWhat a lifecycle example leaves to the next lessonThe event lifecycle determines when an EA allocates resources, reads prices and releases them. A signal rule can be correct while the surrounding program processes stale history or leaks handles. Use a non-trading logger to learn these boundaries before adding orders. OnInit: validate inputs and own your handles Create indicator handles once during initialization. Check for INVALID_HANDLE and return INIT_FAILED if a required resource cannot be…
Read More
Your First MQL5 Expert Advisor: Closed-Bar Signals and Demo Orders

Your First MQL5 Expert Advisor: Closed-Bar Signals and Demo Orders

Technical revision: 1 October 2026 · Educational materialOn this pageMake the rules explicitRead the execution policyEstimate risk without forcing the minimum lotDo not confuse a request with a fillRun a behavior test before a performance testThis tutorial turns a simple moving-average crossing into a complete educational EA. Follow it at your own pace: installation, compilation and verification take longer when your broker has different symbol specifications. The source is a learning exercise, with no published profitability claim. Make the rules explicit A buy signal occurs when the fast EMA was at or below the slow EMA on bar 2 and…
Read More
MQL5 from Scratch: Build an Expert Advisor Step by Step

MQL5 from Scratch: Build an Expert Advisor Step by Step

Technical revision: 1 October 2026 · Educational materialOn this pageDefine the behavior before writing the entryCreate a small project in MetaEditorSeparate initialization, observation and executionAdd execution only after the log makes senseCheck observable behaviorA first Expert Advisor should help you see what the terminal is doing. Start with an observable program, then add one responsibility at a time. This guide takes you from event handlers to a moving-average demonstration whose order submission is disabled by default. Define the behavior before writing the entry Our example reads two exponential moving averages on the chart symbol and timeframe. A signal is a…
Read More
Building a Forex Trading System in MT5: A Practical MQL5 Checklist

Building a Forex Trading System in MT5: A Practical MQL5 Checklist

Technical revision: 1 October 2026 · Educational materialOn this pageWrite an executable specificationSeparate five responsibilitiesUse MQL5 indicator handles correctlyTreat broker specifications as inputsDesign validation before optimizationChoose the next concrete taskA trading system is a set of rules, data assumptions and execution behavior. A checklist helps make those parts observable. No combination of features makes a system “perfect” or guarantees profitability. Write an executable specification Describe the symbol universe, timeframe, signal calculation, information available at decision time, entry policy, exit policy and account constraints. “Buy when momentum is strong” is not precise enough. State the exact inputs, comparisons and bar shifts.…
Read More