Configure how TradingView strategies calculate order size
Configure how TradingView strategies calculate order size
Section titled “Configure how TradingView strategies calculate order size”TL;DR
Choose between fixed contracts, cash amount, or equity percentages so your strategy orders match your risk model automatically.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Strategy Basics |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5strategy( title = "Sizing demo", default_qty_type = strategy.percent_of_equity, default_qty_value = 2.5)
if ta.crossover(ta.ema(close, 20), ta.ema(close, 50)) strategy.entry("Long", strategy.long)Why It Matters
Section titled “Why It Matters”TradingView strategies submit orders based on one default sizing rule unless you override it. Picking the wrong mode (for example fixed contracts on a high-priced asset) can invalidate a backtest or blow past risk tolerances. Setting the appropriate default_qty_type keeps results consistent with your account model—critical when sharing strategies or automating them.
What You’ll Learn
Section titled “What You’ll Learn”- Differences between
fixed,cash, andpercent_of_equitysizing - How to switch the default order type at strategy declaration
- When to override per-order sizing
- Guardrails for testing with realistic buying power
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
strategy() | Declares the strategy and sets global sizing defaults. |
strategy.default_qty_type | Enum values (fixed, cash, percent_of_equity) passed to default_qty_type. |
strategy.default_qty_value | Numeric value interpreted according to the selected type. |
strategy.position_size | Series exposing the current position size for sanity checks. |
strategy.order() / strategy.entry() | Accept per-order sizing overrides when needed. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Declare the strategy with the desired sizing mode
Pick adefault_qty_typethat matches your trading plan.strategy("Sizing demo",default_qty_type = strategy.cash,default_qty_value = 10000) -
Verify how TradingView interprets the value
The same numeric input means different things depending on the type.var string sizingInfo = switch strategy.default_qty_typestrategy.fixed => "Fixed contracts: " + str.tostring(strategy.default_qty_value)strategy.cash => "Cash allocation: $" + str.tostring(strategy.default_qty_value)strategy.percent_of_equity => "Percent of equity: " + str.tostring(strategy.default_qty_value) + "%"label.new(bar_index, high, sizingInfo, style=label.style_label_upper, textcolor=color.white, bgcolor=color.new(color.blue, 75)) -
Override individual orders when necessary
Use theqty/qty_percentarguments onstrategy.entry()orstrategy.order()for exceptions without changing the global default.if ta.crossover(close, ta.ema(close, 20))strategy.entry("Breakout", strategy.long) // uses the default sizingif ta.crossunder(close, ta.ema(close, 50))strategy.entry("Hedge", strategy.short, qty_percent = 1) // overrides with 1% of equity
Example Playbook
Section titled “Example Playbook”//@version=5strategy( title = "Sizing showcase", overlay = true, calc_on_every_tick = false, initial_capital = 50000, default_qty_type = strategy.percent_of_equity, default_qty_value = 2.5)
fastLen = input.int(20, "Fast EMA")slowLen = input.int(50, "Slow EMA")riskPct = input.float(1.0, "Hedge percent", step=0.1)
fast = ta.ema(close, fastLen)slow = ta.ema(close, slowLen)
plot(fast, "Fast EMA", color=color.green)plot(slow, "Slow EMA", color=color.orange)
longSignal = ta.crossover(fast, slow)shortSignal = ta.crossunder(fast, slow)
if longSignal strategy.entry("Trend long", strategy.long)
if shortSignal strategy.entry("Trend hedge", strategy.short, qty_percent = riskPct)
plot(strategy.position_size, "Position size", color=color.new(color.blue, 0), style=plot.style_columns, display=display.data_window)- The default 2.5% equity sizing keeps trend entries proportional to account growth.
- Per-trade overrides handle hedges without touching the global default.
- Plotting
strategy.position_sizeconfirms that bar-by-bar sizing matches expectations.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Percent-of-equity sizing recalculates each bar—expect shares/contracts to change as equity fluctuates.
- Cash sizing uses account currency; remember to factor leverage or broker margin requirements when testing.
- Fixed sizing ignores price levels; use it for futures contracts or instruments quoted in ticks.
- Document the sizing model in your strategy description so users know how to tune the script.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”My backtest fails because orders are “too small”
Section titled “My backtest fails because orders are “too small””Some brokers enforce minimum contract sizes. If your cash or percent sizing results in fractional contracts, TradingView rounds down to the nearest whole unit. Adjust the amount or switch to fixed sizing for that market.
Percent sizing produces different shares than expected
Section titled “Percent sizing produces different shares than expected”TradingView uses current equity (initial capital + strategy profits) when calculating percent sizing. Use initial_capital plus pyramiding settings to keep results realistic.
Key Takeaways
Section titled “Key Takeaways”default_qty_typedefines howdefault_qty_valueis interpreted for every order.- Pick the sizing model that mirrors your real-world trading plan before backtesting.
- Override per order with
qty/qty_percentwhen special sizing rules are required. - Monitor
strategy.position_sizein the data window to validate the number of contracts or shares submitted.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.