Pine Script default_qty_value: Read Order Size in Code
Pine Script default_qty_value cannot be read at runtime: no built-in variable exposes it or the Properties tab Order size. Create your own inputs for size mode and value, compute qty in code, and pass it to strategy.entry(qty=...).
- No built-in variable returns default_qty_value, default_qty_type, or the Properties tab Order size, so a script cannot read them.
- default_qty_value in strategy() must be a compile-time constant, so an input cannot feed it.
- Compute qty yourself from an input and pass it to strategy.entry() or strategy.order(); an explicit qty overrides the default entirely.
- Percent of equity qty is strategy.equity * pct / 100 / (price * syminfo.pointvalue), rounded down to the symbol's contract step.
- strategy.position_size is 0 and strategy.position_avg_price is na before entry, so neither can drive pre-entry sizing.
Can Pine Script read default_qty_value or the Order size setting?
No. Pine Script has no built-in variable that returns default_qty_value, default_qty_type, or the Order size field in the Properties tab. The declaration arguments and the dialog values are consumed by the strategy emulator and are never exposed to your code.
The Properties tab is also one-way. If a user edits Order size there, the script is not told, so any risk or target math you wrote before an entry cannot see the change.
The reliable pattern is to make your own input the single source of truth. Read it for calculations, then pass the resulting quantity to the order call. The rest of this page shows how.
Why can't an input feed default_qty_value in strategy()?
The strategy() declaration is evaluated when the script is compiled and added to the chart, before any bar runs. Its sizing arguments, including default_qty_value and default_qty_type, must be compile-time constants. An input.float() result is an input-qualified value, which is resolved later, so the compiler rejects it there.
This is why the common idea of writing default_qty_value = myInput does not work. You also cannot go the other way and extract the declaration constant into a variable, because the declaration does not create one.
What you can do is write a plain constant in strategy() as a fallback and ignore it by always passing an explicit qty.
What units does Order size use for each default_qty_type?
The number in default_qty_value means different things depending on default_qty_type. Mixing these up is the main reason sizing math disagrees with the Strategy Tester.
| default_qty_type | Unit of the value | Quantity sent to the order | Notes |
|---|---|---|---|
| strategy.fixed | Contracts, shares, or lots | value | Default is 1. Fractional values only make sense on symbols that allow them. |
| strategy.cash | Account currency amount | value / (price * pointvalue) | Price is the expected fill price, which is not known when the order is placed. |
| strategy.percent_of_equity | Percent, 0 to 100 | equity * value / 100 / (price * pointvalue) | Equity is evaluated by the emulator, so your own estimate can differ slightly. |
How do I calculate position size before entry?
Compute the quantity from your own inputs on the bar where the signal fires. The formulas below cover all three types. For stocks and crypto, syminfo.pointvalue is 1. For futures it is the currency value of one full price point per contract, so it must be in the denominator or the quantity will be off by the contract multiplier.
Equity choice matters. strategy.equity is initial capital plus net profit plus open profit, so it moves with unrealized PnL. strategy.initial_capital is constant and gives non-compounding sizing. A realized-only equity is strategy.initial_capital + strategy.netprofit, which compounds on closed trades but ignores floating PnL. Pick one deliberately and expose it as an input.
Do not use strategy.position_size for this. It is 0 while flat, which is exactly when you are sizing an entry. strategy.position_avg_price is na when flat, so it cannot supply an entry price either. Use close or a planned entry level such as a stop or limit price.
For risk-based sizing, the same idea applies: qty = riskCash / (stopDistance * syminfo.pointvalue), where stopDistance is in price units.
fixed: qty = value
cash: qty = value / (price * pointvalue)
percent_of_equity: qty = equity * value / 100 / (price * pointvalue)
risk based: qty = riskCash / (abs(entry - stop) * pointvalue)
Example: equity 10000, 10 percent, price 50, pointvalue 1
qty = 10000 * 10 / 100 / 50 = 20 sharesCopy-paste strategy with a calc_qty function
This minimal strategy has an input for the sizing mode, an input for the size value, and a calc_qty function. The strategy() fallback is only used if some order call omits qty.
The function floors to the symbol's minimum contract step. syminfo.mincontract is used when available, with a fallback of 1 if it is na. Flooring rather than rounding keeps the notional at or below the intended amount.
//@version=6
strategy("Explicit sizing", overlay=true, initial_capital=10000,
default_qty_type=strategy.fixed, default_qty_value=1)
sizeMode = input.string("Percent of equity", "Order size type",
options=["Fixed", "Cash", "Percent of equity"])
sizeVal = input.float(10.0, "Order size value", minval=0.0, step=1.0)
realized = input.bool(false, "Use realized equity only")
calc_qty(string mode, float value, float price, float eq) =>
float unitCost = price * syminfo.pointvalue
float raw = switch mode
"Fixed" => value
"Cash" => value / unitCost
"Percent of equity" => eq * value / 100.0 / unitCost
=> 0.0
float step = na(syminfo.mincontract) ? 1.0 : syminfo.mincontract
math.floor(raw / step) * step
eq = realized ? strategy.initial_capital + strategy.netprofit : strategy.equity
qty = calc_qty(sizeMode, sizeVal, close, eq)
longSignal = ta.crossover(ta.sma(close, 10), ta.sma(close, 30))
exitSignal = ta.crossunder(ta.sma(close, 10), ta.sma(close, 30))
if longSignal and qty > 0
strategy.entry("Long", strategy.long, qty = qty)
if exitSignal
strategy.close("Long")
plot(qty, "Planned qty", display = display.data_window)Why does my calculated qty differ from the Properties tab result?
Once you pass qty to strategy.entry() or strategy.order(), default_qty_type and default_qty_value are ignored for that order. The default applies only when qty is omitted, so an explicit qty removes the mismatch between declaration and calculation, but your own formula now owns the result.
Remaining differences have specific causes:
| Symptom | Cause | Fix |
|---|---|---|
| Filled notional slightly off target | Market orders fill at the next bar open by default, but the formula used close | Use the same price basis, or set process_orders_on_close=true so fills happen at close |
| Qty changes between historical and realtime | calc_on_every_tick=true recalculates on each tick with a moving close | Size on bar close, or use a stable price such as the previous close |
| Order missing or smaller than expected | Qty rounded down to 0, or exceeds available funds | Guard with qty > 0 and cap the percent value |
| Futures size is wrong by a large factor | syminfo.pointvalue left out | Divide by price * syminfo.pointvalue |
| Fractional qty not honored | Symbol or emulator rounds to its minimum step | Floor to syminfo.mincontract yourself |
| Qty keeps growing or shrinking | strategy.equity includes open profit | Use initial_capital or a realized-only equity |
How do I keep one source of truth for order size?
Duplicating the number in strategy() and in an input is how the two drift apart. A user changes the input to 25, the declaration still says 10, and any order without qty quietly uses 10.
Follow three rules. Always pass qty on every strategy.entry(), strategy.order() and any reversal call. Treat the strategy() values as an unused fallback with a harmless constant. Put every sizing formula behind the single input pair so the Inputs tab is the only place users change size.
Because the script cannot read Properties tab edits, an input is also the only way to let users adjust size and have your risk and target math react to it.
Common questions
What is the default order size in Pine Script if I omit qty?
If you do not set default_qty_value, the default is 1 unit with strategy.fixed, meaning one contract, share, or lot. If you did set the declaration or edited the Properties tab, orders without qty use that value, interpreted by default_qty_type.
Can I change default_qty_value from the Inputs tab?
No. default_qty_value must be a compile-time constant in strategy(), and inputs are not constants. Create an input.float and pass the computed quantity as qty to the order call instead.
Should I use strategy.equity or strategy.initial_capital for position size?
Use strategy.equity for compounding that includes open profit, strategy.initial_capital for constant sizing, or initial_capital plus strategy.netprofit for realized-only compounding. The choice changes how fast size grows and how sensitive it is to floating PnL.
Does strategy.entry still use default_qty_value when I pass qty?
No. An explicit qty overrides default_qty_type and default_qty_value for that order. The defaults apply only when qty is omitted.
Why is strategy.position_size 0 when I calculate my entry quantity?
strategy.position_size reports the current open position, which is 0 when flat. It holds the filled size only after an entry executes, so pre-entry sizing must come from inputs and price, not from position variables.