Loading…

Position Building Schedules

Most EAs that scale into a position bury the scaling logic inside the trading loop itself: a chain of if-price-moved-X-pips-add-Y-lots checks tangled up with the OrderSend calls that execute them. That makes the schedule itself impossible to unit test, and impossible to reuse between an EA that pyramids winners and one that averages into losers, even though the underlying question - "given where price is now, is a step due, and how big is it" - is identical in both cases. This library separates that question from the trading. A PositionBuildSchedule is a fixed sequence of steps, each carrying a trigger offset in pips from the initial entry price and a fraction of the schedule's total target lots. PositionBuildScheduleNextDueStep answers "which unfilled step, if any, has price already reached" given nothing but the schedule and the current price - no order pool access at all - and PositionBuildScheduleLotsForStep hands back that step's lot size already normalised to the symbol's lot step and clamped to its min/max, with the same epsilon-guarded rounding house style requires everywhere volume is computed. The caller (an EA) is responsible for actually sending the order and then calling PositionBuildScheduleMarkFilled; this library only ever computes, never trades. Two ready-made builders cover the common shapes: BuildPyramid produces N equal steps that arm as price moves favourably (adding to a winner), and BuildAveragingIn produces N geometrically front-loaded steps that arm as price moves against the initial entry (averaging into a drawdown, largest step first). Averaging into a loser carries real risk of its own - this library computes the schedule faithfully but takes no position on whether that is a sound strategy for the reader's account. Notes and limitations - A schedule holds at most 20 steps (POSITION_BUILD_MAX_STEPS). AddStep/the builder functions return false rather than overflow. - The library never touches the order pool and never calls OrderSend - it only computes trigger prices and lot sizes. All risk of averaging into a loser, exceeding margin, or violating a broker's max-lot rules is entirely the caller's to manage. - PositionBuildScheduleLotsForStep re-reads MODE_LOTSTEP/MODE_MINLOT/ MODE_MAXLOT from the broker on every call rather than caching them, since those can differ between symbols and change between brokers. - Trigger checking assumes the caller polls (e.g. once per tick); the library has no timer or event of its own to detect a due step. - If price gaps straight past more than one step's trigger in a single tick, PositionBuildScheduleNextDueStep still returns them one at a time in schedule order on repeated calls - loop it (as the demo does) until it returns -1 to catch every step a gap jumped over.

Script · MT5 · v1.0 · Free

About Position Building Schedules

Position Building Schedules is a script for MetaTrader 5. It has 2 settings, such as Total lots and Step offset pips. MQL source code included.

Files in the download: PositionBuildingSchedules.mq5.mqh, PositionBuildingSchedules.txt, PositionBuildingSchedulesDemo.ex5, PositionBuildingSchedulesDemo.mq5.

Settings

SettingDefaultWhat it does
Total lots0.9full target position size for both demo schedules
Step offset pips20.0pip spacing between steps

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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria