A backtest only needs a small part of Python. By the end of this lesson, you'll know how Python stores a number and keeps a list of prices. You'll also see how a loop goes through that list, one price at a time. That loop is exactly how a backtest walks through a chart. Every price in this lesson is made up.
1 of 10
What you'll learn
- Store values in names. One = saves a value, and == checks if two things match.
- Do sums in code. Points times 5 gives dollars per contract on the Micro S&P 500.
- Hold prices in lists. Python counts from 0, so closes[0] is the first price.
- Walk through prices with a loop. The spacing tells Python which lines belong inside.
- Look back one bar. Subtract the close before from each one. Tests use past bars only.
Exercise
- In your notebook, type a list of five made-up closes.
- Write a loop that prints each change from the close before, then check two of the changes by hand.
Quick check
1. closes = [10, 20, 30]. What is closes[0]?
2. What does the spacing at the start of a line do in Python?
3. What does entry = 5000.25 do?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
A Running a strategy’s exact rules over past prices to see which trades they would have taken.More in the glossary needs only a little A free programming language. Traders use it to write their own tests of trading rules on files of past prices.More in the glossary: names that store numbers, lists that hold prices in order, and loops that visit each price one at a time, looking back at the bar before.
- Names store values: entry = 5000.25 stores the number under the name entry. One = stores; == asks whether two things are the same.
- Sums: + add, - take away, * times, / divide. On the 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, One whole unit of a price. On the S&P 500, a move from 5,000 to 5,001 is one point, and each futures contract sets what a point is worth in dollars.More in the glossary × 5 gives dollars per contract (Points, ticks and dollars by hand).
- Lists: closes = [5001.0, 5003.5, 5002.25, 5006.0]. Python counts from 0, so closes[0] is the first item and closes[-1] the last. len counts items; max and sum do what they say.
- Loops: for close in closes: runs the indented lines once for each close. if checks a condition. The spacing tells Python what belongs inside.
- Looking back: for i in range(1, len(closes)) with closes[i] - closes[i - 1] gives each change from the bar before. A backtest walks through bars the same way, and must only look back.
- Errors are normal: the last line of the message names the problem.
Worked example
Made-up numbersChecking the loop by hand
Made-up numbers · closes: 5001.00, 5003.50, 5002.25, 5006.00
- 5003.50 − 5001.00 = +2.50
- 5002.25 − 5003.50 = −1.25
- 5006.00 − 5002.25 = +3.75
- 2.50 − 1.25 + 3.75 = 5.00, the last close minus the first.
The loop's output matches the sums by hand.
Your turn
DrillMade-up numbers · closes = [4990.0, 4994.5, 4992.0]
Trading is risky. You can lose money. Test results are hypothetical, and a test can't tell you what will happen next. This lesson explains how testing works. It isn't advice.