Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Coding mistakes to look for

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

Advanced · Coding strategies in Pine Script

Coding mistakes to look for

Wildflower Trading School · Lesson 7.4.3.6

Wildflower

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

  1. Open one of your strategies and search it for calc_on_every_tick, process_orders_on_close and lookahead.
  2. For each one you find, write one line on why it's there.
  3. Then add process_orders_on_close = true to a copy and compare the Total P&L with the original.

Next: Coding one strategy from start to finish.

Quick check

  1. 1. Which line reads yesterday's daily high safely?

  2. 2. What does process_orders_on_close = true do?

  3. 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

Make a free account and we'll remember where you are, so “Coding one strategy from start to finish” is waiting next time.

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 , a signal can appear and vanish before the close: . 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 . request.security(syminfo.tickerid, "D", high, lookahead = barmerge.lookahead_on) gives the day's final high from its first candle: . 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 : see What forward testing is.

Worked example

Made-up numbers

What a fill at the close hides

Made-up candles · MES1! · tick 0.25 = $1.25

  1. Long signal candle closes at 5,012.00: process_orders_on_close fills here
  2. Next candle opens at 5,012.75: the first price you could get
  3. 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

Drill

Made-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.