Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Costs and slippage settings

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

Advanced · Coding strategies in Pine Script

Costs and slippage settings

Wildflower Trading School · Lesson 7.4.3.4

Wildflower

In Level 4, you added fees and slippage to your hand tests, and watched small targets shrink. A strategy script needs the same honesty. By the end of this lesson, you'll know how to add commission and slippage in code, and which orders they touch. You'll also work out what each test trade pays.

1 of 8

What you'll learn

  • Add costs to the test. By default, every test fill is free and exact.
  • Set commission per contract. Use your own broker's fees for each side.
  • Add slippage in ticks. It fills market and stop orders worse. Limit orders don't slip.
  • Work out a trade's cost. A trade that ends at the stop costs more, as stops slip.
  • Rerun with more slippage. Keep it realistic, and note every change.

Exercise

  1. Add commission and slippage to your EMA cross strategy on a 5-minute MES1! chart, using your own broker's fees per side.
  2. Note the net result.
  3. Then change slippage to 2 and note how much it changes.

Next: Reading the strategy tester report.

Quick check

  1. 1. Which orders does slippage = 1 make worse?

  2. 2. What commission does a strategy charge if you set none?

  3. 3. commission_value = 1, cash per contract, 1 contract in and out. Commission?

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 “Reading the strategy tester report” is waiting next time.

Read the full notes

Add costs to a Pine Script strategy with commission_type = strategy.commission.cash_per_contract, commission_value (per contract, per fill) and slippage (ticks on market and stop orders), because by default the test charges nothing.

  • By default, a strategy charges no and 0 ticks of , so every test fill is free and exact.
  • commission_type = strategy.commission.cash_per_contract with commission_value = 1 charges a made-up $1 per contract on each fill: $2 a . Use your own broker's fees per side.
  • slippage = 1 fills and one tick worse; aren't touched. backtest_fill_limits_assumption = 1 makes a limit fill only if price passes it by a tick (Verify price for limit orders in the Properties tab).
  • Made-up, 1 contract: a trade ending at the pays $1.25 + $1.25 + $2 = $4.50; one ending at the pays $1.25 + $2 = $3.25.
  • Then rerun with more slippage, but not an unrealistic amount. Note any change made in the Properties tab in your version history. See Commission: the fee on every trade, Spread and slippage, Adding worse costs on purpose , Slippage and commissions in backtests and Pine Script strategy settings.

Worked example

Made-up numbers

What a stopped-out trade pays

Made-up trade · 1 MES contract · $1 commission each side · slippage = 1

  1. Market entry slips 1 tick: $1.25
  2. Stop exit slips 1 tick: $1.25
  3. Commission in and out: $2.00
  4. Total: $4.50

A trade ending at a limit target pays $3.25, because limits don't slip.

Your turn

Drill

Made-up example · commission_value = 1 · cash per contract · 3 contracts · one entry and one exit

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.