Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Position size and futures contract values

Lesson 12 of 16 in Coding strategies in Pine ScriptModule 3: The strategy tester

Advanced · Coding strategies in Pine Script

Position size and futures contract values

Wildflower Trading School · Lesson 7.4.3.3

Wildflower

A test result is only as real as the number of contracts behind it. Today we make sure the test trades what you mean. By the end of this lesson, you'll know how TradingView sizes a test trade if you don't tell it. You'll also fix the size at one contract, and size by the dollars you'd risk.

1 of 9

What you'll learn

  • Set a fixed size. Use strategy.fixed and 1 so each test trade is one contract.
  • Work out the tick value. Multiply the tick size by the dollars per point.
  • Set enough starting money. With the default settings, too little can mean no trades.
  • Size by risk. Divide your risk dollars by the stop's dollar cost, then round down.
  • Keep risk within your limit. Rounding down keeps it at or under. Too wide a stop gives 0.

Exercise

  1. Add a strategy to a 5-minute MES1! chart without setting a size, and look at the Size column in the List of trades.
  2. Then add default_qty_type = strategy.fixed and default_qty_value = 1, save, and check that every trade is now 1 contract.

Next: Costs and slippage settings.

Quick check

  1. 1. If you set no size, how big is each test order?

  2. 2. On MES, what is syminfo.mintick * syminfo.pointvalue?

  3. 3. Risk $100, stop 24 ticks, tick value $1.25. How many contracts after math.floor?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Costs and slippage settings” is waiting next time.

Read the full notes

Set a futures strategy's size in the code with default_qty_type = strategy.fixed and default_qty_value = 1, or size by risk with math.floor(risk dollars / (stop ticks × syminfo.mintick × syminfo.pointvalue)).

  • By default, a strategy's orders use 100% of its pretend money (default_qty_type = strategy.percent_of_equity, default_qty_value = 100), which on futures isn't a set number of . Set the size in the code: default_qty_type = strategy.fixed, default_qty_value = 1.
  • syminfo.mintick is the size (0.25 on ) and syminfo.pointvalue the ($5 on MES). Their product is the : $1.25.
  • TradingView counts one contract as price × point value: at a made-up 5,000, $25,000. With the default 100% setting, the test needs that much starting money (Initial capital, in the Properties tab), or it may place no trades.
  • Sizing by : contracts = math.floor(riskDollars / (stopTicks * tickValue)). With a made-up $100 and a 16-tick stop: 5 contracts. Rounding down keeps risk at or under the limit; a stop too wide for the limit gives 0. Pass the size as the third part of strategy.entry().
  • More on these settings: Pine Script strategy settings. See Risk per trade in dollars and Sizing the strategy to your account.

Worked example

Made-up numbers

Size a made-up trade by risk

Made-up numbers · $100 risk · 24-tick stop · MES tick value $1.25

  1. Risk per contract: 24 × $1.25 = $30
  2. $100 ÷ $30 = 3.33
  3. math.floor rounds down: 3 contracts, $90 at risk

Rounding down keeps the risk at or under the limit.

Your turn

Drill

Made-up example · risk $60 · stop 12 ticks · MES tick value $1.25

Trading is risky. You can lose money. Test results are hypothetical, and a good test doesn't mean future profits. This lesson explains how Pine Script works. It isn't advice.