COUNTOFLONGSHARES
COUNTOFLONGSHARES returns the number of units held long by a ProBacktest or ProOrder strategy, used to cap position size when accumulating multiple orders.
Syntax
COUNTOFLONGSHARESHow it works
The function reads the open position and returns an integer: the quantity of units held on the long side at the moment of evaluation. It requires no parameters and no parentheses. When the strategy is flat or short, it returns 0.
The count is expressed in units, not in trades. A single order of 3 contracts and three cumulated orders of 1 contract both return 3. To reason about individual entries, combine it with TRADEINDEX and TRADEPRICE, which address orders by their index.
The typical context is a strategy with DEFPARAM CumulateOrders = true, where several entry orders can stack into one position. Comparing COUNTOFLONGSHARES against a ceiling before each additional BUY keeps total exposure within a planned limit. The short-side equivalent is COUNTOFSHORTSHARES, and COUNTOFPOSITION reports the signed size of whatever position is open.
Examples
Example 1, Pyramiding capped at three units (ProBacktest)
DEFPARAM CumulateOrders = true
myMACD = MACD[12,26,9](close)
long = myMACD crosses over 0
// First entry when flat
IF NOT LongOnMarket AND long THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Allow additions only while holding three units or fewer
MAXSHARES = COUNTOFLONGSHARES <= 3
// Add a unit if the last entry is seasoned and in profit
IF TRADEINDEX(1) > 5 AND Close - TRADEPRICE(1) > 10 AND LongOnMarket AND MAXSHARES THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
SET STOP TRAILING 50The strategy opens on a MACD cross, then adds one contract at a time while the running count stays at three or fewer, the last entry is at least five bars old, and price has moved 10 points beyond it.
Example 2, Hard exposure ceiling before every addition (ProOrder)
DEFPARAM CumulateOrders = true
trendUp = close > Average[100](close)
pullback = RSI[7](close) < 35
// Never hold more than 5 units long
IF trendUp AND pullback AND COUNTOFLONGSHARES < 5 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
SET STOP %TRAILING 2Each pullback in an uptrend adds one unit, but the condition on COUNTOFLONGSHARES guarantees the stacked position never exceeds five units.
Example 3, Scaling out once the position is large (ProOrder)
DEFPARAM CumulateOrders = true
IF close crosses over Average[50](close) AND COUNTOFLONGSHARES < 4 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// When fully loaded and momentum fades, shed one unit
IF COUNTOFLONGSHARES >= 4 AND RSI[14](close) crosses under 70 THEN
SELL 1 CONTRACT AT MARKET
ENDIF
SET STOP %LOSS 3The count drives both directions: it gates new entries below four units and triggers a partial exit once the position is fully built and momentum turns.
Common errors and gotchas
- Units, not trades. The return value counts shares, contracts or lots, not the number of entry orders. A strategy buying 2 contracts per signal reaches a cap of 6 after three signals, not after six.
- Pointless without cumulated orders. With the default
CumulateOrders = false, a strategy holds at most one entry at a time, so the function only ever returns that single order's quantity or 0. Pyramiding logic needsDEFPARAM CumulateOrders = trueto matter. - Zero when short or flat. The function ignores short exposure entirely and returns 0. Testing
COUNTOFLONGSHARES = 0does not mean the strategy is flat; combine withONMARKETorCOUNTOFSHORTSHARESfor the full picture. - Evaluated at bar time. The count reflects filled orders as of the current bar. An order sent this bar but not yet executed is not included, so back-to-back additions in one bar can overshoot a cap checked earlier in the code.
Related instructions
COUNTOFSHORTSHARES, number of units held short.COUNTOFPOSITION, signed size of the open position.CumulateOrders, DEFPARAM that allows stacking several entry orders.LONGONMARKET, true while a long position is open.ONMARKET, true while any position is open.TRADEINDEX, bar index of a given entry order.TRADEPRICE, entry price of a given order.BUY, opens or adds to a long position.
