Risk Based Market Order
Position sizing is the one calculation a trader repeats before every single trade, and it is also the one most position-size scripts get quietly wrong. This script does the arithmetic properly, places the order with its stop loss attached, and refuses - loudly and with the corrected numbers - whenever the requested risk cannot actually be expressed on the account. The money value of a pip is derived from tick value and tick size: pip value per lot = tick value * (pip size / tick size) and never from naive Point arithmetic. The naive version multiplies points by a hardcoded factor and happens to work on EURUSD, then sizes JPY crosses, gold and index CFDs wrongly - sometimes by an order of magnitude - because on those symbols the tick size is not the point, and the quote currency is not the account currency. Tick value already contains the conversion into the account currency, so scaling it by pip over tick is exact on every symbol the broker quotes. The computed volume is floored to the broker's lot step. It is never rounded up, and a result below the minimum lot is treated as a refusal rather than silently promoted to the minimum. Promotion is the classic failure: on a small account with a wide stop, "risk 1%" quietly becomes "risk 3%" because the script rounded 0.003 lots up to 0.01. This one prints what the minimum lot would actually risk, and the two legal ways out - raise the budget to a stated amount, or tighten the stop to a stated number of pips. Stops are validated against the broker's stop and freeze levels before sending, re-checked on every retry because stop levels widen during news, and snapped to the tick-size grid on the side that keeps the realised risk at or under budget. The optional take profit is placed at a multiple of the stop distance (R), so a 2R target is two stop-distances away. After the fill the summary reports the risk recomputed from the actual open price, so slippage is visible immediately instead of being discovered on the statement. Notes and limitations - The stop loss is placed relative to the quote at send time, and slippage moves the fill, so the realised risk can differ from the budget by approximately the slippage. The post-fill summary reports the true figure. - Tick value is read from the broker. On symbols the terminal has not synchronised it can be zero, in which case the script refuses and asks you to open the symbol's chart first rather than sizing from garbage. - A partial fill on MQL5 leaves a smaller position with proportionally smaller risk; the journal says so when it happens. - The script sizes one trade in isolation. It does not know about the risk already open on the rest of the account. - Swap and commission are not part of the risk figure; it measures the stop distance only.
Script · MT5 · v1.0 · Free
About Risk Based Market Order
Risk Based Market Order is an expert advisor (trading robot) for MetaTrader 5. It can size trades from your balance and risk percent. It has 10 settings, such as Side, Risk base and Risk percent. MQL source code included.
Files in the download: RiskBasedOrder.ex5, RiskBasedOrder.mq5, RiskBasedOrder.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Side | TRADE_SIDE_BUY | Direction |
| Risk base | RISK_BASE_BALANCE | Risk is a percent of |
| Risk percent | 1.0 | Risk per trade, percent |
| Stop pips | 20.0 | Stop distance, pips |
| Take profit r | 0.0 | Take profit, R multiple (0 = none) |
| Magic | 0 | Magic number |
| Slippage pips | 3 | Slippage tolerance, pips |
| Max retries | 5 | Attempts before giving up |
| Retry wait ms | 500 | Pause between attempts, ms |
| Confirm first | true | Ask for confirmation first |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Grid · Volume · Risk & Lot Size · MT5 Script · MQL5 Source Code