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
- Add commission and slippage to your EMA cross strategy on a 5-minute MES1! chart, using your own broker's fees per side.
- Note the net result.
- Then change slippage to 2 and note how much it changes.
Quick check
1. Which orders does slippage = 1 make worse?
2. What commission does a strategy charge if you set none?
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
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 What your broker and the exchange charge for each contract you trade, often quoted per contract for the round trip: in and out.More in the glossary and 0 ticks of The difference between the price you expected and the price your order filled at.More in the glossary, 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 Both sides of a trade together: the order that opens it and the order that closes it. A fee per side is charged twice in a round trip.More in the glossary. Use your own broker's fees per side.
- slippage = 1 fills An order to buy or sell straight away at the best price available. It fills, but the price isn’t promised.More in the glossary and An order that waits until price reaches a trigger you set, then becomes a market order (or a limit order, for a stop-limit).More in the glossary one tick worse; An order that only fills at a price you set or better. The price is promised, but a fill isn’t.More in the glossary 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 A small futures contract on the S&P 500, traded at CME Group. Each point is worth $5, and its smallest step of 0.25 is worth $1.25.More in the glossary contract: a trade ending at the An order that closes a trade if price reaches a level you set against it. It aims to limit the loss, but in a fast market it can fill at a worse price.More in the glossary pays $1.25 + $1.25 + $2 = $4.50; one ending at the An exit order that closes a trade when price reaches a level you choose in your favour.More in the glossary 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 numbersWhat a stopped-out trade pays
Made-up trade · 1 MES contract · $1 commission each side · slippage = 1
- Market entry slips 1 tick: $1.25
- Stop exit slips 1 tick: $1.25
- Commission in and out: $2.00
- Total: $4.50
A trade ending at a limit target pays $3.25, because limits don't slip.
Your turn
DrillMade-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.