Pine Script

Advanced Programming with Pine Script: Implementing Multi-Level Stop/Limit Orders with Pyramiding

Advanced Programming with Pine Script: Implementing Multi-Level Stop/Limit Orders with Pyramiding

Trading automation has become increasingly popular among both novice and professional traders, with Pine Script at the forefront as one of the most accessible and powerful tools for creating custom trading strategies on TradingView. Pine Script allows users to design and backtest trading strategies directly within TradingView, making it a go-to choice for traders looking to automate their systems.

One advanced technique that traders often use to maximize profits during strong market trends is pyramiding. Pyramiding involves adding to an existing position as the trade moves in the trader’s favor, allowing them to scale into a larger position gradually. This can amplify gains in trending markets but requires careful planning and execution to manage risk effectively.

In this article, we will explore how to implement multi-level stop and limit orders using pyramiding in Pine Script. By placing orders at different price levels, traders can strategically enter or exit the market in stages, rather than relying on a single all-or-nothing approach. This method can be particularly powerful when combined with pyramiding, as it allows traders to capitalize on momentum while spreading their risk across multiple levels.

Let’s start with the following indicator:

//@version=5
indicator("Bollinger Bands Strategy", overlay=true)

source = close
length = input.int(20, minval=1)
m1 = input.float(1.5, minval=0.001, maxval=50)
m2 = input.float(2.0, minval=0.001, maxval=50)
m3 = input.float(2.5, minval=0.001, maxval=50)

[b1, u1, l1] = ta.bb(source, length, m1)
[b2, u2, l2] = ta.bb(source, length, m2)
[b3, u3, l3] = ta.bb(source, length, m3)

plot(u1, color = color.green, offset = 1)
plot(u2, color = color.green, offset = 1)
plot(u3, color = color.green, offset = 1)

plot(l1, color = color.red, offset = 1)
plot(l2, color = color.red, offset = 1)
plot(l3, color = color.red, offset = 1)

This is simple BB strategy where I display 3 BB levels for upper and lower with different multiplied: 1.5, 2 and 2.5. On the chart it looks like this:

Now let’s imaging that I want to create a pyramiding strategy that should go long on all 3 lower levels and long on all 3 higher levels. For that I need to use limit orders with additional conditions so that all 3 levels can trigger orders in orders correctly.

Let’s start by changing indicator to strategy and then adding pyramiding options with value 3 because we want to enter at 3 levels:

strategy("Bollinger Bands Strategy", overlay=true, pyramiding = 3)

Next let’s work on the limit orders on the first level. I’ll define these conditions with checks that before position are flat or opposite so that it will be triggered only once for the long positions:

if (strategy.position_size <= 0)
	strategy.entry("L1_long", strategy.long,  limit=l1)

if (strategy.position_size >= 0)
	strategy.entry("U1_short", strategy.short, limit=u1)

After saving it you can see that these orders works as we wanted:

Now let’s add limit orders for the second and third level. I’ll add a check the the number of open trades so that these levels will trigger a trade once once in a correct order.

if (strategy.position_size > 0 and strategy.opentrades == 1)
	strategy.entry("L1_long", strategy.long,  limit=l2)

if (strategy.position_size > 0 and strategy.opentrades == 2)
	strategy.entry("L1_long", strategy.long,  limit=l3)

if (strategy.position_size < 0 and strategy.opentrades == 1)
	strategy.entry("U1_short", strategy.short, limit=u2)

if (strategy.position_size < 0 and strategy.opentrades == 2)
	strategy.entry("U1_short", strategy.short, limit=u3)

After saving you’ll see that it works quite well for our needs, every level trigger only once orders:

Limitations / Future improvements

It’s a pretty simple implementation of stop limit orders with pyramiding. It’s really easy to implement it and understand, but it has some limitations. First is that it triggers only 1 order a bar and can’t execute 2 orders even for a big bar that crosses 2 levels at the same time. This can be solved with a bit more sophisticated logic of order conditions. Also, it’s possible to create a bit more dynamic version with levels in array for example and 1 order entry function that execute these trades.

Conclusion

Implementing multi-level stop and limit orders with pyramiding in Pine Script is a powerful way to enhance your trading strategy. In this article, we’ve walked through the fundamentals of placing multiple orders in Pine Script, explained how to use the pyramiding feature, and provided practical examples to help you get started. With the right balance of risk management and strategy optimization, this technique can significantly improve your trading performance.

As you continue to refine your trading strategy, remember that the key to success lies in continuous testing and optimization. The tools within Pine Script and TradingView make it easy to backtest and iterate, so take advantage of these features to fine-tune your approach.

In the ever-evolving world of trading, staying adaptable and using advanced techniques like multi-level order execution with pyramiding can provide you with a competitive edge. By mastering these concepts, you’re well on your way to building a more robust and profitable trading system.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news