The Problem
Retail traders using MetaTrader often calculate lot size manually, which leads to inconsistent risk exposure and wasted time. Small calculation errors can cause over‑leveraging or missed opportunities, especially when accounting for currency conversion, commission, and swap costs.
What This Does
Position Sizer is a free Expert Advisor (EA) for MT4 and MT5 that automates those calculations. The core executables are MQL4/Experts/Position Sizer/Position Sizer.mq4 and MQL5/Experts/Position Sizer/Position Sizer.mq5. They read user‑defined risk parameters (percent risk, account currency, stop‑loss distance, etc.) and compute the appropriate lot size for each trade.
Supporting files provide modular functionality:
Defines.mqh– constant definitions and utility macros used throughout the EA.HorizontalRadioGroup.mqh– UI widget code for the EA’s configuration panel.Position Sizer Trading.mqh– the implementation of the sizing algorithm and order‑submission helpers.Translations/*.mqh– language packs that populate the UI labels.
All image assets in Images/ are UI icons; they do not affect logic.
How It Is Wired
Execution begins when MetaTrader loads the compiled EA (Position Sizer.ex5/.ex4). The platform calls the standard entry points defined in the main file:
OnInit()(inPosition Sizer.mq5/.mq4) – registers the UI, loads translation files via#include "Translations/English.mqh"(or the user‑selected language), and reads stored input parameters.OnDeinit()– cleans up UI objects.OnTick()– invoked on every price tick. It callsCalculatePositionSize()(implemented inPosition Sizer Trading.mqh). This function pulls the current symbol data (price, spread, commission) via built‑in MT5/MT4 functions, applies the formulas fromDefines.mqh, and returns the lot size.- If the EA’s “Auto‑Trade” flag is set,
OnTick()then callsOpenTrade()(also inPosition Sizer Trading.mqh) which usesOrderSend()/OrderSendAsync()to place the trade with the computed lot size.
All side‑effects are confined to the MetaTrader terminal: no external files, databases, or network calls are made. The UI widgets (HorizontalRadioGroup.mqh) merely reflect and modify the EA’s input parameters; they do not introduce additional processing paths. Because the codebase is split by platform, the MT5 and MT4 branches are parallel copies with identical call graphs, making the hub of activity the Position Sizer Trading.mqh module.
How To Use It
- Install – Clone the repo and copy the appropriate platform folder into the MetaTrader data directory:
git clone https://github.com/moses-y/PositionSizer.git
# For MT5
cp -R PositionSizer/MQL5/Experts/Position\ Sizer/ <MetaTrader5>/MQL5/Experts/
# For MT4
cp -R PositionSizer/MQL4/Experts/Position\ Sizer/ <MetaTrader4>/MQL4/Experts/
- Compile – Open the
.mq5or.mq4file in MetaEditor and press F7 to build the EA. No external dependencies are required. - Configure – Attach the compiled EA to a chart. In the “Inputs” tab set:
RiskPercent– % of account equity to risk per trade.StopLoss– distance in points or ATR‑based value.Currency– account currency (defaults to the broker’s base). Optional toggles for commission, swap, and risk‑adjusted lot rounding. - Run – With “Auto‑Trade” enabled the EA will open positions automatically when the chart receives a tick; otherwise you can trigger trades manually via the EA’s on‑screen panel.
If any of the above files are missing (e.g., Position Sizer Trading.mqh), the EA will fail to compile; the repository currently contains all required sources.
Real‑World Use
A swing‑trader managing a $10,000 USD account wants to risk 2 % per trade with a 50‑point stop loss on EUR/USD. After installing the EA, they set RiskPercent = 2 and StopLoss = 50. On each tick, the EA computes the lot size (≈0.20 lots) and sends an order when the trader clicks “Buy”. The calculation automatically accounts for the EUR/USD pip value and any broker commission, ensuring the true risk stays at $200.
Code Health & Issues
- Tests – yes – a single test file is present, but it is not integrated into any CI workflow.
- CI – no – no
.githubor other CI configuration; changes are not automatically validated. - Dockerfile – no – not applicable for an MT4/MT5 EA.
- License – yes –
LICENSE.mdis included. - Lockfile – no – not relevant for MQL projects.
- Committed secrets – none – static scan found no embedded keys.
Additional observations
- Low coverage of automated testing – only one test file, likely focused on a small utility; core sizing logic lacks regression tests.
- No build automation – developers must compile manually in MetaEditor, which can lead to inconsistent builds.
- Documentation – README provides high‑level usage, but inline code comments are sparse; newcomers may need to read the MQL source to understand parameter defaults.
The Bottom Line
Position Sizer delivers a functional, ready‑to‑use lot‑size calculator for MT4/MT5 traders, with all core logic consolidated in Position Sizer Trading.mqh. The code is straightforward but lacks automated testing and CI, so any modification should be validated manually. It is suitable for individual traders or small firms that already use MetaTrader and need a free, configurable risk‑based sizing tool.