Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Coding one strategy from start to finish

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

Advanced · Coding strategies in Pine Script

Coding one strategy from start to finish

Wildflower Trading School · Lesson 7.4.3.7

Wildflower

This is the last lesson of the course, and it puts everything together. We'll take one written rule sheet from Level 3 and turn it into a complete strategy script. By the end of this lesson, you'll have seen every rule become a line of code, and checked one test trade by hand. You'll also find a gap in the written rules that only code would show.

1 of 11

What you'll learn

  • Turn each rule into a line. Write the rule sheet next to the code it becomes.
  • Lay out the script in order. Settings go first. Entries and stops go inside if blocks.
  • Check trades by hand. Work out one fill and result from the List of trades.
  • Spot gaps in your rules. Code shows where the sheet doesn't say what happens.
  • Fix the words first. Decide the rule in words, then change the code and version name.

Exercise

  1. Pick one rule sheet of your own.
  2. Write each rule next to the line of code it becomes, then code it, add it to a chart, and check three trades by hand.
  3. Write down any gap in the sheet the code shows you.
  4. Then take the course quiz.

Quick check

  1. 1. In this script, what closes a long on the opposite cross?

  2. 2. Why are ta.ema() and ta.crossover() at the left edge, not inside an if?

  3. 3. The code shows a trade can open at 4:00 pm, when your sheet says everything is closed. What first?

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 “Why use Python for testing” is waiting next time.

Read the full notes

To code a strategy from a rule sheet, turn each rule into a line: settings in the strategy line, indicators and the time window at the top, entries and stops inside if blocks, then check trades by hand and fix gaps in the written rules first.

  • The sheet: setting A from Test it yourself: moving average crossover. , 5-minute candles, 9:30 am to 4:00 pm New York time, 9 and 21 , enter on the close of the cross candle (the test fills at the next open), one beyond the last swing, exit on the opposite cross, all closed at 4:00. A made-up teaching example, not a strategy to trade.
  • The script (22 lines): fixed 1 contract and made-up costs in the strategy line (see Pine Script strategy settings); averages, crosses, the time window and swing stops at the left edge; entries with stops inside if blocks; strategy.close_all() outside the hours.
  • Check by hand: a made-up short sold at 5,012.75 (next open with 1 tick of ) and bought back at 5,016.25: −3.5 points, −$17.50, minus $2 : −$19.50. One trade isn't a result.
  • Code shows gaps in the sheet: a 3:55 pm cross fills at 4:00 pm, when the sheet says everything is closed, and the close-all fills at 4:05 pm. Decide the rule in words, then change the code and its version name. See Testing the rules as written and Keeping a version history.

Worked example

Made-up numbers

Check one test trade by hand

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

  1. Short: next open 5,013.00, 1 tick worse when selling: 5,012.75
  2. Exit: next open 5,016.00, 1 tick worse when buying: 5,016.25
  3. −3.50 points = −$17.50; minus $2.00 commission: −$19.50

It matches the List of trades. One trade is a check, not a result.

Your turn

Drill

Made-up example · long signal · lowest low of the last 5 candles is 5,031.25 · 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.