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
- Pick one rule sheet of your own.
- 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.
- Write down any gap in the sheet the code shows you.
- Then take the course quiz.
Quick check
1. In this script, what closes a long on the opposite cross?
2. Why are ta.ema() and ta.crossover() at the left edge, not inside an if?
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
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. 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, 5-minute candles, 9:30 am to 4:00 pm New York time, 9 and 21 A moving average that gives the newest closes more weight, so it reacts sooner than a simple one of the same length.More in the glossary, enter on the close of the cross candle (the test fills at the next open), 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 one The smallest step a price can move. On many stock index futures that’s 0.25 of a point.More in the glossary 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 The difference between the price you expected and the price your order filled at.More in the glossary) and bought back at 5,016.25: −3.5 points, −$17.50, minus $2 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: −$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 numbersCheck one test trade by hand
Made-up trade · 1 MES contract · slippage 1 tick · $1 commission each side
- Short: next open 5,013.00, 1 tick worse when selling: 5,012.75
- Exit: next open 5,016.00, 1 tick worse when buying: 5,016.25
- −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
DrillMade-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.