CumulateOrders
CumulateOrders is a DEFPARAM parameter that controls whether a ProOrder strategy can stack multiple entries in the same direction. Syntax and examples.
Syntax
DEFPARAM CumulateOrders = trueDEFPARAM CumulateOrders = falseParameters
| Name | Type | Default | Description |
|---|---|---|---|
CumulateOrders | boolean | true | When true, entry orders in the direction of an open position add to it. When false, such orders are ignored until the position is closed. |
How it works
CumulateOrders is not an executable instruction but a strategy-level parameter declared with DEFPARAM. Like all DEFPARAM lines, it must appear at the top of the code, before any trading logic, and it is evaluated once when the strategy starts.
With CumulateOrders = false, the strategy holds at most one position slice per direction. If a BUY condition fires while a long is already open, the extra order is discarded. This keeps position size fixed and is the usual choice for strategies where each signal represents one complete trade.
With CumulateOrders = true, each time an entry condition is met another order is sent in the same direction, so the position grows. This enables pyramiding and averaging-in behavior, but it also means a condition that stays true bar after bar will keep buying until margin or platform limits intervene. Cumulative strategies therefore almost always pair the parameter with a size guard built on COUNTOFPOSITION, COUNTOFLONGSHARES or COUNTOFSHORTSHARES.
The parameter does not affect exits. SELL, EXITSHORT and stop or target instructions behave the same in both modes.
Examples
Example 1, Restricting the strategy to one entry per direction (ProOrder)
DEFPARAM CumulateOrders = false
// One position slice at a time. While a long is open,
// further BUY orders in the same direction are ignored.The declaration preserved from the official reference. Placed at the top of the program, it prevents the strategy from adding to an existing position.
Example 2, Pyramiding with an explicit size cap (ProBacktest)
DEFPARAM CumulateOrders = true
fastMA = Average[20](close)
slowMA = Average[50](close)
// Add one contract on each new bullish crossover,
// but never hold more than 4 contracts in total
IF fastMA crosses over slowMA AND abs(COUNTOFPOSITION) < 4 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Flatten everything on a bearish crossover
IF LongOnMarket AND fastMA crosses under slowMA THEN
SELL AT MARKET
ENDIFEnabling CumulateOrders lets each crossover add a contract, while the COUNTOFPOSITION guard bounds total exposure at four contracts.
Example 3, Averaging into a short on rallies (ProOrder)
DEFPARAM CumulateOrders = true
rsiVal = RSI[14](close)
// Open or add to the short each time RSI pushes above 70,
// capped at three lots
IF rsiVal crosses over 70 AND COUNTOFSHORTSHARES < 3 THEN
SELLSHORT 1 LOT AT MARKET
ENDIF
SET STOP %TRAILING 1Each overbought push adds a lot to the short. Because the entry uses crosses over, the order fires once per RSI excursion rather than on every bar the level holds.
Common errors and gotchas
- DEFPARAM placed too late. All
DEFPARAMdeclarations, includingCumulateOrders, must come before any other instruction. A declaration after trading logic causes a compile error. - Silent pyramiding failure. When add-on entries never execute despite conditions being met, the usual cause is the parameter left at
falseor omitted in code copied from a single-entry strategy. The orders are dropped without any warning. - Runaway accumulation. With
CumulateOrders = true, a condition written as a state (rsiVal > 70) instead of an event (rsiVal crosses over 70) sends an order on every bar it stays true. Combine event-style conditions with aCOUNTOFPOSITIONcap. - Expecting it to merge opposite orders. The parameter only governs same-direction stacking. An order against an open position still reduces or reverses it according to normal order handling, regardless of this setting.
Related instructions
DEFPARAM, declares strategy parameters at the top of the code.COUNTOFPOSITION, signed size of the open position, used to cap pyramiding.COUNTOFLONGSHARES, unsigned count of shares held long.COUNTOFSHORTSHARES, unsigned count of shares held short.BUY, opens or adds to a long position.SELLSHORT, opens or adds to a short position.ONMARKET, true while any position is open.FLATAFTER, DEFPARAM parameter that forces the strategy flat after a set time.FLATBEFORE, DEFPARAM parameter that keeps the strategy flat before a set time.
