Tradingview: Pyramiding

Today we take a look at pyramiding on Tradingview. We will discuss what it is, how it affects your strategy and take a look at some common issues/misunderstandings that might arise when trying to use the feature.

What Is Pyramiding

To simplify strategy development, when using strategy.entry(), Tradingview limit the number of entries to one at any given time. This means that if you are long and your buy entry conditions are trueagain whilst you are still in a long position, it won’t buy again. The backtest engine will wait until you have exited (or reversed) your position before considering a long position again.

Pyramiding allows your strategy to enter multiple times in the same direction. This can be known as “Averaging Into” or “Scaling Into” a position. It can be especially useful for implementing complex entry and exit strategies. To access the Pyramiding setting, ho to the “strategy properties” tab of your strategy like so:

Pyramiding in the strategy properties tab

Note: The screenshot above shows the strategy properties of the default strategy script template. This is the template that Tradingview provides when you select “New –> Strategy”.  As you will see, Pyramiding is enabled by default but the order size is limited to one. This means that we are limited to one buy or sell in any given position. Perhaps confusingly, there is no difference between having Pyramiding on with 1 order and actually switching it off!

Changing the default

Should you wish to, you can change the default pyramiding setting in the code using the strategy()function call. Here we can use a variable called pyramiding. Set this variable to zero and pyramiding will be switched off. Feed any other number value to it and it will change how many orders are allowed.

strategy("My Strategy", overlay=true, pyramiding=100)

The example above will set your order limit to 100. A fully working example of setting this in the code will be provided later.

Pyramiding with strategy.order()

It is worth pointing out that Pyramiding does not affect the strategy.order()function. Tradingview’s wiki explains why succinctly:

this command places both entry and exit orders. It is not affected by pyramiding setting and by strategy.risk.allow_entry_in keyword. It allows you to create complex enter and exit order constructions when capabilities of the strategy.entry and strategy.exit are not enough.

As such, if you call strategy.order() it will be executed regardless of the Pyramiding setting you select. Pyramiding will only effect strategy.entry().


Code Example

Now we put the theory into practice! The following code example will create a simple script that allows us to average down whenever our portfolio is down x%. The idea will be to bring our average cost down so that we can still exit with a profit when conditions improve. With this in mind, the strategy shall also have a simple take profit exit at x% above our average price.

Code Commentary

Since this is not the first post in our getting started series, we won’t go through the code line by line. If you are new to pine script programming, you can start at our First Script post.

Before we discuss how to calcuate our PnL or how we determine when to enter in the code, we should give an overview of the main inputs and what they are supposed to do. This will allow readers to understand how to operate the script.

Our strategy has 4 main inputs:

  • target_perc: This is the target percentage level will trigger us to average down. In other words, if we have a close below this level from our average buying price, we will average down.
  • take_profit: A standard take profit percentage level. Use this to set how much profit you will target.
  • target_qty: Is the number of shares/contracts we will aim to buy when we average down. 50 will mean we buy 50% of our current holdings. So if we have 100 shares, then we buy 50 when we average down.
  • sma_period: Defines our SMA lookback period. Our strategy will enter the first/initial position when we have a close above our SMA level.


When we are averaging down, we must know what our current profit or loss is. Only then can we determine if we are ready to average down. Furthermore, we want to know our profit and loss in percentage terms rather than in monetory units (e.g. $).

The calculation for a percentage change is as follows:

(New Value – Old Value) / Old Value

(Close – Average Price) / (Average Price)

Which is then written in the code as:

pnl = (close - strategy.position_avg_price) / strategy.position_avg_price

Why not entry price? Well, during the first entry, the strategy.position_avg_priceis our entry price! On each subsequent entry, our average price lowers. At this point, we need to adjust both our take profit and the next average down target level from the strategy.position_avg_price. This is because the average price always represents the point which our entire combined  entries  will swing into profit or loss.


Our entries are nice and straightforward. For the first entry, we just check whether we have a crossover the SMA and place a market order. We create a separate condition for this as we only want this condition to be true if our position size is zero (i.e strategy.position_size == 0).

For the subsequent trades, we simply check if our PnL value is less than or equal to our target percentage. If it is, we place a market entry order. That simple.

if (pnl <= target_perc)

On the Charts

After running the script you should have something that looks like this. You can see that we plot both the SMA and the average price line in order to help with verifying the strategy is working as expected.

Showing the Average Down Script on the charts

Now go ahead and switch the number of pyramiding levels, you will see that the number of positions taken for some trades will increase and decrease with this number. (During trades when price moves significantly against us)

A word of caution

This strategy can look great on paper, but it can quickly eat up all of your capital in the real world. As such, it is recommended to start your initial position size at a small percentage of your account size. Also considering limiting the maximum number of pyramid trades or the subsequent position sizes of your averaged down trades using the target_qtyinput.

Finally, be aware of commissions. The more entries you put on, the more commissions you will end up paying unless you have a commission-free broker.

Potential Issues

Now we know how to use Pyramiding, it is worth looking at some examples of issues that can crop

Pyramiding doesn’t work!

There are certain types of strategies that Pyramiding will never work on. For example, if you take a simple moving average crossover strategy, it can only crossover one time before crossing back down and generating a short signal. As such, if you increase the number of pyramiding orders allowed, your results will not change at all.

Note the code snippet below is taken from Tradingview default Strategy template. You get this when selecting a “Blank strategy script” in the pine script editor.

Load up this code, enter the “strategy properties” tab and toggle the number of pyramiding orders allowed. You will see our returns don’t change.

Or there are too many entries!

Another potential cause of confusion might be that you are seeing too many entries on the chart. In this case check whether you are using strategy.order()calls in your code. To see why this might cause you to have more entries than your Pyramiding setting allows, take a look back up towards the start of the page.

Find This Post Useful?

If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.