Loading…

Place Order With ATR Stop

A fixed 20 pip stop is the right distance in one market regime and noise in the next. Sizing the stop from the ATR keeps it proportional to what the market is actually doing; risk-sizing the volume against that distance keeps the loss constant in money terms. This script does both in one step: stop at N times ATR, optional target at M times ATR, volume computed so the stop costs a fixed percentage of balance or equity. The pip value used for sizing is derived from tick value and tick size (pip value per lot = tick value * pip size / tick size), never from naive Point arithmetic, so JPY crosses, gold and index CFDs are sized correctly. The volume is floored to the lot grid and never rounded up, and a result below the minimum lot is a refusal with the corrected numbers, not a silent promotion to the minimum. The ATR is always taken from the last COMPLETED bar. The forming bar's range is still growing, so an ATR read from it just after a bar opens is systematically too small - and a too-small stop distance means a too-large position. That is the quiet failure mode of most "ATR position sizer" scripts, and avoiding it costs one shift parameter that is easy to forget. Because the stop distance comes from data, one news candle can make it enormous. The script accepts an optional cap in pips and refuses when the computed stop exceeds it, printing the exact sizing arithmetic: the stop in pips, the risk budget in money, and the dust volume that budget would buy at that distance. Without the cap a 300 pip volatility spike quietly turns a 0.5 percent risk trade into a position too small to matter - the settings still look right while the trade no longer does what you think it does. The default cap is 0 (no cap) so the behaviour is explicit, but setting one is recommended. Notes and limitations - The ATR is read once, at run time, from the last completed bar. The stop does not trail and does not re-adapt after placement. - The stop is placed relative to the quote at send time; slippage moves the fill, so the realised risk can differ from the budget by roughly the slippage. The post-fill summary reports the true figure. - A very quiet ATR can produce a stop inside the broker's minimum stop distance; the script refuses rather than widening the stop silently, because widening would change the risk without telling you. - Tick value is read from the broker; on unsynchronised symbols it can be zero, in which case the script refuses and asks you to open the chart first. - The script sizes one trade in isolation and does not know about the risk already open on the account. Swap and commission are not part of the risk figure.

Script · MT5 · v1.0 · Free

About Place Order With ATR Stop

Place Order With ATR Stop is an expert advisor (trading robot) for MetaTrader 5 based on ATR. It can size trades from your balance and risk percent. It has 13 settings, such as Side, Risk base and Risk percent. MQL source code included.

Files in the download: OrderWithAtrStop.ex5, OrderWithAtrStop.mq5, OrderWithAtrStop.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
SideTRADE_SIDE_BUYDirection
Risk baseRISK_BASE_BALANCERisk is a percent of
Risk percent1.0Risk per trade, percent
Atr period14ATR period
Atr timeframePERIOD_CURRENTATR timeframe
Stop atr1.5Stop loss, ATR multiple
Take profit atr0.0Take profit, ATR multiple (0 = none)
Max stop pips0.0Max stop distance, pips (0 = no cap)
Magic0Magic number
Slippage pips3Slippage tolerance, pips
Max retries5Attempts before giving up
Retry wait ms500Pause between attempts, ms
Confirm firsttrueAsk for confirmation first

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: ATR · Volume · Risk & Lot Size · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria