Loading…

Place Order With Structure Stop

"Stop below the last swing low" is probably the most common discretionary stop rule there is, and doing it by hand means eyeballing the swing, typing a price, and separately working out the lot size for the risk. This script does the three steps as one: find the structure, put the stop beyond it, size the volume so hitting that stop costs a fixed percentage of balance or equity. A swing is confirmed by fractal logic: a bar whose low is strictly below the lows of the K bars on either side of it (K is an input, 2 by default - the classic Williams fractal). Two details matter. Only completed bars are considered, and a candidate needs K completed bars AFTER it - so the youngest possible swing is K+1 bars back, and the forming bar can never nominate itself. And the comparison is strict: a flat shelf of equal lows is not a turning point, so it does not confirm. If no swing confirms within the lookback window the script refuses and prints exactly what was searched - symbol, timeframe, bar range, K - because "no trade" with a stated reason is a better answer than a stop invented from nothing. The stop is an absolute price: swing low minus a pip buffer for buys, swing high plus the buffer for sells. Before sending, the script checks the structure is still on the protective side of the current quote - a swing the market has already traded through is not protection - and that the distance clears the broker's stop and freeze levels. Volume is computed from tick value and tick size, floored to the lot grid, never rounded up, and refused with corrected numbers when even the minimum lot would exceed the budget. Because the distance is set by the chart, it can be huge - after a strong run the nearest confirmed swing can be hundreds of pips away. An optional cap refuses such trades and prints the arithmetic: the distance, the risk budget, and the dust volume that budget would buy at that distance. Without the cap the settings still look right while the position quietly shrinks toward nothing. The default cap is 0 (no cap) so the behaviour is explicit, but setting one is recommended. Notes and limitations - The script finds the most recent CONFIRMED swing, which is by definition at least K+1 bars old. It does not predict whether the level will hold; it automates the bookkeeping around a decision you have already made. - Equal lows (or highs) do not confirm a swing because the comparison is strict. On instruments that often print identical extremes this is conservative by design. - The stop is an absolute price, so if the market moves between sizing and sending, the realised risk changes with it. The script refuses when that drift would exceed the confirmed risk by more than the slippage tolerance, and reports the true risk from the actual fill afterwards. - The buffer is not spread-aware beyond what you give it. Sell stops are triggered at the ask; include the typical spread in the buffer. - The script sizes one trade in isolation and does not know about the risk already open on the account. Swap and commission are not in the figure.

Script · MT5 · v1.0 · Free

About Place Order With Structure Stop

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

Files in the download: OrderWithStructureStop.ex5, OrderWithStructureStop.mq5, OrderWithStructureStop.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
TimeframePERIOD_CURRENTTimeframe for the swing search
Swing bars2Bars each side of a swing (K)
Lookback bars200Completed bars to search
Buffer pips2.0Buffer beyond the swing, pips
Take profit r0.0Take profit, R 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: Volume · Fractals · Risk & Lot Size · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria