Stop Placement Toolkit
A calculated stop is frequently illegal to send: it sits closer to price than the broker's minimum stop distance, or inside the freeze-level band once a position exists. Most published stop-calculators skip this check entirely and let OrderSend fail with error 130 the first time the market is quiet enough for a tight ATR multiple to land inside that zone. Every path through this header runs the same AdjustForBrokerLimits pass regardless of which method produced the raw price, so the caller always gets back a price that is genuinely sendable, with an honest wasAdjustedForBroker flag saying whether it had to be pushed out to comply. SwingStopPrice finds the lowest low (buy) or highest high (sell) over a caller-chosen lookback window. AtrStopPrice offsets the entry by an ATR value times a multiplier, taking the ATR reading as a plain parameter so the header carries no indicator dependency and stays trivially testable. NoisePercentileStopPrice treats a sample of recent bar ranges as a distribution of normal market noise and places the stop at the requested percentile of that distribution - a 90th-percentile stop survives all but the ten percent widest recent bars; a 50th-percentile one will eat routine noise often, and that trade-off is left explicit rather than hidden behind a single "ATR x 2" default. ComputeStop dispatches to whichever method is requested and always finishes with the same legality pass, so a caller comparing all three methods for the same trade sees genuinely comparable, sendable prices. Self-contained: no order or position calls, no DLLs. Broker limits are read live via MarketInfo(symbol, MODE_STOPLEVEL/MODE_FREEZELEVEL) rather than assumed, since these vary by broker and by symbol. Notes and limitations - SwingStopPrice and NoisePercentileStopPrice need the caller's own low[]/high[] price series (e.g. from iLow/iHigh); this header does not fetch market data itself. - AtrStopPrice takes atrValue as a plain parameter rather than calling iATR() internally, so the caller controls which timeframe and period the ATR reading comes from. - ComputeStop's parameter list reuses lookbackOrSampleCount and atrValueOrPercentile across methods with method-specific meaning, documented in the header comments above each branch - check which method is being called before reading those two arguments. - AdjustForBrokerLimits only pushes a stop OUT to comply with the minimum distance; it never makes a stop tighter than what the caller computed. - No thread or state concerns beyond the live MarketInfo call inside AdjustForBrokerLimits.
Script · MT4 · v1.0 · Free
About Stop Placement Toolkit
Stop Placement Toolkit is a script for MetaTrader 4. MQL source code included.
Files in the download: StopPlacementToolkit.mqh, StopPlacementToolkit.txt, StopPlacementToolkitDemo.mq4.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: ATR · MT4 Script · MQL4 Source Code