Loading…

Constraint Order Shaper

A signal generator decides direction and roughly where to enter; how large a position that decision becomes, and whether it should be sent at all, is a separate concern governed by risk and broker limits that have nothing to do with the signal itself. Mixing the two inside one strategy function makes the risk rules hard to audit and impossible to reuse across strategies. ShapeOrder is that separate concern as one function: given a proposed lots, entry, stop and target plus an SOrderConstraints rulebook, it returns the final numbers that should actually be sent, or a clear rejection reason if no adjustment can make the trade compliant. The shaping order is fixed deliberately, because later steps depend on earlier ones. Lot size is normalised to the symbol's lot step first (with an epsilon guard on the step division - 0.1 / 0.01 does not land on an exact integer in binary floating point, and flooring that directly can silently halve the intended size). It is then capped to whatever size keeps the proposed stop distance's risk at or under maxRiskPercent of account balance, computed through the symbol's real tick value rather than a hardcoded per-point guess. Only after sizing is settled does the function check the stop distance against the broker's live STOPLEVEL/FREEZELEVEL - checking that distance against a not-yet-normalised or not-yet-risk-capped lot size would be validating the wrong figure, since the risk cap itself depends on the proposed stop distance, not the other way around. Two classes of rejection exist and are kept genuinely separate. Hard rejections - spread too wide, too many positions already open on the symbol, or a stop distance below the rulebook's own minStopPips policy - stop the function immediately with rejected=true and a reason, because no amount of resizing fixes them. Soft adjustments - lot size shrunk for risk, lot size capped to the rulebook maximum - are applied and appended to adjustmentLog as a semicolon-separated trail, so a clean trade that needed no changes produces an empty log and an adjusted one shows exactly what changed and why, rather than a caller having to diff the input and output structs themselves to find out. Notes and limitations - ShapeOrder only computes and logs - it never calls OrderSend itself. The caller sends the order with the returned lots/sl/tp/entryPrice. - Risk sizing assumes the account currency and symbol combination has a usable tick value at call time; if MODE_TICKVALUE has not been published yet for a just-added symbol, the risk cap is skipped entirely (treated as no cap) rather than silently sizing to zero - open the symbol in Market Watch first so the server has a chance to publish it. - currentOpenPositionsForSymbol is supplied by the caller, not counted internally - this header does not scan the order pool itself, so it works identically whether the caller's own counting logic includes pending orders, magic-number filtering, or neither. - The broker stop/freeze level check reflects the account's limits at the moment ShapeOrder is called; both can change with volatility on some brokers, so call this immediately before sending, not once at signal time.

Script · MT5 · v1.0 · Free

About Constraint Order Shaper

Constraint Order Shaper is a script for MetaTrader 5. It has 2 settings, such as Max risk percent and Max lots. MQL source code included.

Files in the download: ConstraintOrderShaper.mq5.mqh, ConstraintOrderShaper.txt, ConstraintOrderShaperDemo.ex5, ConstraintOrderShaperDemo.mq5.

Settings

SettingDefaultWhat it does
Max risk percent1.0Max % of account balance risked per trade
Max lots5.0Hard cap on lot size regardless of risk math

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 → Scripts.
  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 script onto a chart to run it once.

Download the PDF guide

Tags: Buy/Sell Arrows · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria