Pine Script strategy pyramiding controls how many same-direction entries a strategy.entry() call can add to an open position. Set the limit in strategy(), then check entry conditions, order IDs, reversals, and exits in the trade list.
What does pyramiding mean in a Pine Script strategy?
Pyramiding means adding to a position through additional entries in the same direction before the existing position is closed. For example, a strategy might enter long after an initial signal, then add again if price moves in its favor and another condition becomes true. The separate entries can affect the strategy’s total position size and trade history.
It is not the same as opening several unrelated trades while flat. The key question is whether the strategy already has a position in that direction when a new entry is submitted.
In Pine, strategy.entry() is the command most commonly used for entries. Repeated same-direction calls can add to a position when the strategy’s pyramiding setting allows it. If the limit does not allow an additional entry, the extra same-direction entry is not taken. That makes the setting a control on entry count, not a guarantee of a particular risk level or total account exposure.
Pyramiding also does not decide when an add should happen. Your conditions do that. A strategy can have a pyramiding limit above one and still never add if its entry condition does not become true again while a position is open. Before adding this behavior, write down what should trigger each add, how large each order should be, and which exit rule applies to each entry.
How do you enable pyramiding in Pine Script?
Set pyramiding in the strategy() declaration. Here is a minimal Pine Script v6 example with a simple moving-average condition:
//@version=6
strategy("Pyramiding example", overlay = true, pyramiding = 2)
average = ta.ema(close, 20)
longSignal = ta.crossover(close, average)
if longSignal
strategy.entry("Long", strategy.long)In this example, the strategy declaration allows up to two same-direction entries, while the code submits a long entry when the crossover condition is true. The number in the declaration is a limit; it does not make the script automatically submit that many orders. The condition and the strategy’s position state still matter.
Keep the declaration easy to find. If you are editing an existing strategy, check its current strategy() line before adding another parameter. The setting belongs in the declaration, not inside the if block containing strategy.entry().
You can also inspect the strategy’s Properties in TradingView. If you change a property there, compare that setting with the source declaration when results seem unexpected. A mismatch between what the script says and what you think is configured is a common debugging lead. For a broader look at how orders and fills are represented, see our guide to how TradingView backtesting works.
How do entry conditions and IDs affect added trades?
A pyramiding limit only matters if the script submits another qualifying entry. Review the condition and the entry ID together.
if longSignal
strategy.entry("Long", strategy.long)If this same call runs again while the strategy is long, it can add another entry when the pyramiding setting permits it. But a signal that happens only once will not create repeated adds. If you want a staged approach, define each stage clearly: for example, one rule for the initial entry and another for an add after a later condition. Avoid making the add condition a permanent true state unless repeated orders are actually intended.
The ID is not just a label for readability. Reusing an ID does not necessarily mean each call creates a separate independent entry in the way you might expect; Pine’s entry handling can modify an order associated with the same ID. The behavior of strategy.entry() also differs from strategy.order(): the latter is a basic order command and does not use the pyramiding limit in the same way. See this entry and exit command comparison for the distinction.
If you are debugging a strategy that appears not to add, first make the signal visible on the chart. Then check whether a position is already open, whether the condition becomes true again, whether the direction matches, and whether the entry is allowed by the limit. Our guide to why a Pine Script strategy may not take trades covers other order-path checks.
Does pyramiding affect strategy.order() and reversals?
Do not assume every order command follows the same entry rules. strategy.entry() has strategy-specific behavior: it can add to an open position in the same direction when permitted, and an entry in the opposite direction can reverse the existing position. strategy.order() behaves differently and is not constrained by the pyramiding setting in the same way. That distinction matters if you use it to change net position size. (Command behavior reference)
A reversal is not simply an add in the opposite direction. It changes the strategy’s position direction. If your rules intend to close a long before opening a short, make that intent explicit and inspect the resulting orders. If you instead intend a direct reversal, confirm that the resulting position matches your design.
This becomes especially important when alerts or external execution are involved. A chart strategy’s simulated order behavior and a receiving system’s interpretation of a webhook message are separate things. Make the intended action clear—open, add, close, or reverse—and verify that the receiving workflow tracks position state appropriately. The relevant question is not just “Did a sell signal fire?” but “What position did the strategy have, and what should the next action do to it?”
How should exits work with multiple entries?
Before enabling adds, decide whether your exit closes all entries, one entry, or only part of the position. Exit commands have their own IDs and entry associations; those choices affect what remains open after an exit is placed or filled.
For example, strategy.exit() can attach price-based exits to an entry ID through from_entry. If your strategy creates multiple entries, make sure the exit logic covers the entries you mean to manage. A stop or target that only refers to one entry may not protect every add. Conversely, an exit that applies broadly may close more exposure than you meant to close.
Check the strategy’s trade list and chart markers after each change. Confirm how many entries were opened, which ones were closed, and whether the remaining position behaves as intended. If you use a bar-count exit, multiple open entries add another detail: the start time for the exit may need to be tracked per trade rather than assumed to be the same for the whole position. Our guide to closing a Pine Script trade after a set number of bars discusses that issue.
Also review order sizing. A pyramiding limit counts entries; it does not cap the total quantity or dollar exposure by itself. If each add uses the default size, total exposure can grow as entries accumulate. Make sure your quantity rule accounts for the combined position. Our guide to position sizing in Pine Script explains how default and per-order sizing choices affect orders.
How do you test pyramiding without fooling yourself?
Test the feature as a change in order behavior, not as proof that the strategy is better. Compare the same strategy logic with pyramiding disabled and enabled, then inspect what changed: entry count, total position size, exits, and results. Keep the signal rules and other settings constant so you can identify the effect of the pyramiding change.
Use this checklist:
Confirm each add condition. Mark the bar where each intended entry should occur.
Check the position before the order. Determine whether the strategy is flat, already long, or already short when the condition runs.
Review the entry limit and order type. Verify the
strategy()setting and distinguishstrategy.entry()fromstrategy.order().Inspect IDs and exits. Make sure each entry is handled by the exits you intended.
Review sizing and costs. Account for combined quantity, commission, and slippage rather than looking only at the entry count.
Check the trade list. Confirm that fills and closures match the code’s intended sequence.
A historical simulation is a way to examine how the rules behaved on past bars, not a forecast. Results can also depend on order timing and fill assumptions. Our Pine Script strategy report provides a place to review simulated trades and performance details, while PineScripter can run a draft through its lint checks and repair compile errors before showing the script. Keep the logic review in your hands: compiling does not establish that the strategy’s orders match your intent.