Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Strategy versus indicator

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

Advanced · Coding strategies in Pine Script

Strategy versus indicator

Wildflower Trading School · Lesson 7.4.3.1

Wildflower

So far, every script has drawn on the chart. Today a script starts placing test trades. By the end of this lesson, you'll know the difference between an indicator script and a strategy script. You'll also turn one into the other, and see when TradingView fills a test order.

1 of 8

What you'll learn

  • Tell a strategy from an indicator. A strategy draws too, and also places test trades.
  • Place a test order. strategy.entry buys or sells at market.
  • Read the tester. The Strategy Tester lists every test trade and adds them up.
  • Fill at the next open. A fill at the signal close uses a price you couldn't get.
  • Remember test trades are made up. They're worked out on past candles. No orders are sent.

Exercise

  1. Copy the EMA cross script into a new strategy, add it to a 5-minute MES1! chart, and open the List of trades.
  2. Pick three trades and check on the chart that each one filled at the open of the candle after the cross.

Next: Entries, exits, stops and targets in code.

Quick check

  1. 1. What can a strategy() script do that an indicator() script can't?

  2. 2. By default, when does a test order made at a candle's close fill?

  3. 3. You're long and strategy.entry("Short", strategy.short) runs. What happens?

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 “Entries, exits, stops and targets in code” is waiting next time.

Read the full notes

An indicator script only draws on the chart, while a strategy script also places test trades that TradingView's Strategy Tester lists and adds up; by default each order fills at the next candle's open.

  • An indicator() script draws on the chart. A strategy() script can draw the same things and also places test trades with a pretend broker. The (newer TradingView pages call it the Strategy Report) lists and adds them up.
  • strategy.entry("Long", strategy.long) places a to buy; strategy.short sells. An entry in the opposite direction closes the open and turns it round. The EMA cross test is a made-up teaching example, not a strategy to use.
  • On old , the script runs at each candle's close, and by default an order fills at the next candle's open. A fill at the signal close would use a price you couldn't get: see Look-ahead bias.
  • Test trades are worked out on candles already drawn. No real orders are sent. It isn't : see Setting up a paper account and What Pine Script is.

Worked example

Made-up numbers

Where does the order fill?

Made-up candles · MES1! · tick size 0.25

  1. Signal candle: the 9 EMA crosses above the 21 EMA, close 5,012.00
  2. The script places strategy.entry("Long", strategy.long) at that close
  3. Next candle opens at 5,012.50: the order fills there, 2 ticks above the close

By default, a test order fills at the next open, the first price you could actually get.

Your turn

Drill

Made-up example · MES1! · signal candle closes at 5,012.00 · next candle opens at 5,012.50 · tick size 0.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.