Skip to content
All library documents

Moving Average Expert Advisor with Trade-Result-Based Position Sizing

Article MQL5 code base

Summary

This tutorial explains a MetaTrader 5 example expert advisor that trades price crossings of a simple moving average. It opens a short position when a bar moves from above the average to below it, and a long position on the reverse crossing. The EA checks for signals on the first tick of a new bar, maintains at most one selected position per symbol, and closes positions when the opposite crossing condition occurs.

The article also describes its implementation structure, including indicator initialization, order handling, and a lot-sizing routine. That routine estimates volume from free margin and required margin, then reduces size after consecutive losing deals and adjusts it to the symbol’s permitted volume increments and bounds. It recommends using the platform’s strategy tester to optimize parameters and cites one example period and moving-average setting. That example is not evidence of durable profitability; the document offers no broader performance statistics and the sizing method is not a direct measure of market loss exposure.

Key ideas

  • The EA enters trades when a bar crosses the moving average and exits on a crossing against the open position.
  • Signal checks are limited to the first tick of a new bar.
  • Position size is estimated from free margin and required margin, then adjusted using recent losing trades.
  • The tutorial presents parameter optimization as a tester task but does not establish that an optimized setting will remain effective.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.