Skip to content

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.

DifficultyBeginner
Time to implement10-15 min
CategoryStrategy Basics
//@version=5
strategy(
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)
Tip. `default_qty_type` controls how TradingView interprets `default_qty_value`. Changing the type without adjusting the value leads to wildly different position sizes.

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.

  • Differences between fixed, cash, and percent_of_equity sizing
  • How to switch the default order type at strategy declaration
  • When to override per-order sizing
  • Guardrails for testing with realistic buying power
CallPurpose
strategy()Declares the strategy and sets global sizing defaults.
strategy.default_qty_typeEnum values (fixed, cash, percent_of_equity) passed to default_qty_type.
strategy.default_qty_valueNumeric value interpreted according to the selected type.
strategy.position_sizeSeries exposing the current position size for sanity checks.
strategy.order() / strategy.entry()Accept per-order sizing overrides when needed.
  1. Declare the strategy with the desired sizing mode
    Pick a default_qty_type that matches your trading plan.

    strategy(
    "Sizing demo",
    default_qty_type = strategy.cash,
    default_qty_value = 10000
    )
  2. Verify how TradingView interprets the value
    The same numeric input means different things depending on the type.

    var string sizingInfo = switch strategy.default_qty_type
    strategy.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))
  3. Override individual orders when necessary
    Use the qty/qty_percent arguments on strategy.entry() or strategy.order() for exceptions without changing the global default.

    if ta.crossover(close, ta.ema(close, 20))
    strategy.entry("Breakout", strategy.long) // uses the default sizing
    if ta.crossunder(close, ta.ema(close, 50))
    strategy.entry("Hedge", strategy.short, qty_percent = 1) // overrides with 1% of equity
//@version=5
strategy(
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)
Why this works.
  • 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_size confirms that bar-by-bar sizing matches expectations.
  • 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.

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.

  • default_qty_type defines how default_qty_value is interpreted for every order.
  • Pick the sizing model that mirrors your real-world trading plan before backtesting.
  • Override per order with qty/qty_percent when special sizing rules are required.
  • Monitor strategy.position_size in the data window to validate the number of contracts or shares submitted.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.