Why TradingView doesn't follow pyramiding settings when you use `strategy.order()`
Why TradingView doesn’t follow pyramiding settings when you use strategy.order()
Section titled “Why TradingView doesn’t follow pyramiding settings when you use strategy.order()”TL;DR
strategy.entry()honourspyramidingandstrategy.risk.allow_entry_in.strategy.order()bypasses them entirely, so you can easily oversize positions unless you add your own guard.
At a Glance
Section titled “At a Glance”| Order function | Respects pyramiding? | Respects allow_entry_in? | Typical use |
|---|---|---|---|
strategy.entry | ✅ | ✅ | Standard entries where TradingView manages exposure. |
strategy.order | ❌ | ❌ | Custom sizing, scale-ins/scale-outs, bracket logic. |
strategy.exit, strategy.close | N/A | N/A | Exits only; they do not add exposure. |
Quick Actions
Section titled “Quick Actions”- Audit scripts that mix
strategy.entryandstrategy.order—ensure you cap exposure manually. - Use
strategy.opentradesorstrategy.position_sizeto implement your own guard if needed. - Prefer
strategy.entrywhen you want TradingView to enforce pyramiding automatically.
Quick Start
Section titled “Quick Start”//@version=5strategy("Pyramiding demo", overlay=true, pyramiding=1)
// This respects pyramidingif ta.crossover(close, ta.sma(close, 20)) strategy.entry("Long", strategy.long)
// This ignores pyramiding completelyif ta.crossover(close, ta.sma(close, 10)) strategy.order("Aggressive", strategy.long, qty=1)
Tip. If you must use `strategy.order`, guard it with your own counter: `if strategy.opentrades < maxAllowed`.
When to Use Each Order Function
Section titled “When to Use Each Order Function”| Scenario | Recommended function |
|---|---|
| Simple entries with pyramiding limits | strategy.entry |
| Adding to winning trades but still respecting risk limits | strategy.entry with higher pyramiding plus strategy.risk.allow_entry_in |
| Precise scale-in/scale-out sequence | strategy.order (add custom checks) |
| Bracket orders (entry + exit submitted together) | strategy.order with stop/limit parameters |
Adding Manual Guards Around strategy.order
Section titled “Adding Manual Guards Around strategy.order”maxManualTrades = input.int(2, "Max manual orders")
canAddManual = strategy.position_size >= 0 and strategy.opentrades < maxManualTrades
if canAddManual and ta.crossover(close, ta.sma(close, 10)) strategy.order("Manual add", strategy.long, qty=1)You’re responsible for maintaining these counters—the platform won’t do it for you.
Example Playbook
Section titled “Example Playbook”//@version=5strategy("Hybrid pyramiding", overlay=true, pyramiding=1)
fast = ta.ema(close, 21)slow = ta.ema(close, 55)atr = ta.atr(14)
riskPct = input.float(0.5, "Risk per add-on (%)", minval=0.1)maxAddOns = input.int(2, "Max manual add-ons", minval=0)useOrders = input.bool(true, "Use strategy.order for add-ons")
baseQty = strategy.equity * (riskPct * 0.01) / (atr * 2)
if ta.crossover(fast, slow) strategy.entry("Base", strategy.long, qty=baseQty)
if useOrders and strategy.position_size > 0 and strategy.opentrades < maxAddOns + 1 and close > strategy.position_avg_price + atr strategy.order("Add-on", strategy.long, qty=baseQty)
if strategy.position_size > 0 strategy.exit("Base Exit", from_entry="Base", stop=strategy.position_avg_price - atr * 2)
Why this works.
- The base entry uses `strategy.entry` and respects pyramiding=1.
- Manual add-ons use `strategy.order`, but a custom counter ensures only two additional legs.
- The exit continues to manage the entire position regardless of how it was built.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”strategy.orderis powerful but dangerous—use it only when you need custom fill logic or simultaneous stop/limit orders.- Combine manual exposure guards with risk checks (e.g.,
if strategy.position_size + qty <= maxSize). - Remember
strategy.ordercan submit entries even whenstrategy.position_sizeis zero and pyramiding = 0, effectively overriding your intent. - Consider using
strategy.entryplusstrategy.risk.allow_entry_infor most pyramiding use cases—it’s safer and easier to maintain.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”My position size exceeds pyramiding—why?
Section titled “My position size exceeds pyramiding—why?”You likely used strategy.order. Replace it with strategy.entry or add manual checks (strategy.opentrades, strategy.position_size).
Can I mix both functions?
Section titled “Can I mix both functions?”Yes. Use strategy.entry for base trades and strategy.order for exceptional cases, but always guard manual orders with counters.
Do risk functions apply to strategy.order?
Section titled “Do risk functions apply to strategy.order?”Most strategy.risk.* guards do not restrict strategy.order. Treat it as a raw API.
Key Takeaways
Section titled “Key Takeaways”strategy.entryrespects TradingView’s pyramiding/risk controls;strategy.orderdoes not.- If you rely on
strategy.order, build your own exposure safeguards. - Choose the function that matches your intent—simplicity and platform safeguards vs full manual control.
Keep Going
Section titled “Keep Going”- Review order phases for deeper insights
- Explore pyramiding strategies for structured scaling
- Connect to Live Trading once your position controls are bulletproof
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.