A script can look right, run without errors, and still test trades you could never have taken. Today we look at three mistakes that do exactly that. By the end of this lesson, you'll be able to spot three things. A signal that only exists halfway through a candle, a fill at a price you couldn't get, and a script that peeks at the future.
1 of 10
What you'll learn
- Wait for the candle to close. Live signals can appear and vanish. That's repainting.
- Leave live updates off. With calc_on_every_tick on, live trades may not match the test.
- Fill at the next open. process_orders_on_close fills at a price you couldn't get.
- Stop peeking at future data. Use [1] when lookahead is on in request.security.
- Search and recheck. Find the three settings, reload the chart and forward test.
Exercise
- Open one of your strategies and search it for calc_on_every_tick, process_orders_on_close and lookahead.
- For each one you find, write one line on why it's there.
- Then add process_orders_on_close = true to a copy and compare the Total P&L with the original.
Quick check
1. Which line reads yesterday's daily high safely?
2. What does process_orders_on_close = true do?
3. A mark appears halfway through a candle, then vanishes at the close. What is this?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
Three common Pine Script mistakes make tests look better than real trading: acting on the live candle before it closes, filling at the signal close with process_orders_on_close, and reading future data with request.security lookahead_on and no [1] offset.
- On the live One bar on a price chart. It shows where price opened and closed, and the highest and lowest it reached, in one stretch of time.More in the glossary, a signal can appear and vanish before the close: When an indicator’s mark or value changes after it was first drawn, from reading a candle that hadn’t closed yet.More in the glossary. For indicators, add and barstate.isconfirmed. Strategies wait for the close by default; calc_on_every_tick = true makes them act on live updates, so live trades may not match the test. See Repainting indicators and hindsight.
- process_orders_on_close = true fills orders at the close that created them. TradingView calls same-tick fills typically unrealistic. The default, false, fills at the next open. Made-up: close 5,012.00, next open 5,012.75: 3 ticks the test skipped.
- request.security() fetches data from another timeframe, such as a A bigger candle size than the one you’re looking at. On a 5-minute chart, the 1-hour chart is a higher timeframe.More in the glossary. request.security(syminfo.tickerid, "D", high, lookahead = barmerge.lookahead_on) gives the day's final high from its first candle: A backtest mistake: using a price the strategy couldn’t have known yet at the moment it made its decision.More in the glossary. TradingView's recommended form uses high[1] with lookahead on. See Look-ahead bias and Repainting.
- Search for the three settings (see Pine Script strategy settings), reload the chart to check marks don't move, and Running a finished strategy, unchanged, on new prices as they arrive, and recording every trade.More in the glossary: see What forward testing is.
Worked example
Made-up numbersWhat a fill at the close hides
Made-up candles · MES1! · tick 0.25 = $1.25
- Long signal candle closes at 5,012.00: process_orders_on_close fills here
- Next candle opens at 5,012.75: the first price you could get
- 0.75 points = 3 ticks = $3.75 on one contract the test never paid
Leave process_orders_on_close at its default, false, for signals that come from the close.
Your turn
DrillMade-up example · 20 test trades · each entry filled 2 ticks better at the close than the next open · 1 MES contract · tick $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.