Loading…

TWAP Execution Slicer

Sending a large order in one shot on a thin symbol moves the price against itself before the order even finishes filling. A time-weighted average price slicer spreads that same size across a window of smaller child orders instead, so no single clip carries the full market impact of the parent size. This library only decides what to send and when - it never sends a trade itself. That split matters here more than almost anywhere else in this catalogue: an execution library that trades on your behalf hides its own retry policy, slippage handling and error recovery inside itself, and every caller inherits whatever choices it silently made. TwapShouldSendNextSlice returns a plain true/false for "is it time to send the next slice"; the calling EA stays fully responsible for the actual trade call, its retry behaviour, and what happens if one slice fails to fill. That is a deliberate design choice, not a missing feature. Slicing respects the broker's lot step and minimum from the start: every slice is floored to the step - never rounded up, because a slice that rounds up over-executes the parent size - and if an even split would put any slice below the broker minimum, the child count is silently reduced until every slice clears it. The function returns the count it actually used, and the caller must use that count from then on, not the one it requested. The schedule between slices is randomised within a min/max interval range, so the clip pattern is not perfectly regular and mechanically predictable to whatever is watching the tape on the other side. The randomisation compresses automatically as the window runs low, so a run of large random gaps early cannot leave the final slices unable to fit before the deadline - the schedule always finishes on time. Notes and limitations - This is a decision engine, not an execution engine. It never sends, modifies or cancels an order - the caller owns every side effect. - The demo simulates the clock in memory and never calls Sleep or waits in real time; a real EA drives TwapShouldSendNextSlice from its own OnTick using the current time, tick by tick, for as long as the chart is running. - Reducing the child count to respect the broker minimum lot means the actual slice count can differ from what was requested for a small parent size on a symbol with a coarse lot step - read the return value, do not assume it. - The randomised schedule is a simple uniform draw between the configured bounds, not a model of real market microstructure or a volume-aware pace. See the Volume Participation Scheduler in this catalogue for pacing against an actual intraday volume profile instead of the clock. - State (the slice arrays and the next-index counter) is entirely the caller's responsibility to persist across ticks; this header holds none of it itself.

Script · MT4 · v1.0 · Free

About TWAP Execution Slicer

TWAP Execution Slicer is a script for MetaTrader 4. It has 6 settings, such as Total lots, Child count and Window minutes. MQL source code included.

Files in the download: TwapExecutionSlicer.mqh, TwapExecutionSlicer.txt, TwapExecutionSlicerDemo.mq4.

Settings

SettingDefaultWhat it does
Total lots1.00Parent order size to slice
Child count8Requested number of child slices
Window minutes20Total execution window, minutes
Min interval sec60Minimum seconds between slices
Max interval sec240Maximum seconds between slices
Clock step sec15Simulated clock granularity for the walk-through

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria