Skip to content
Wildflower
Join free
Backtesting in Python (optional)

Free Trading School · Level 7 · Course 7.5

Python basics for traders

Lesson 3 of 15 in Backtesting in Python (optional)Module 1: Getting started

Advanced · Backtesting in Python

Python basics for traders

Wildflower Trading School · Lesson 7.5.1.3

Wildflower

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

  1. In your notebook, type a list of five made-up closes.
  2. Write a loop that prints each change from the close before, then check two of the changes by hand.

Next: Loading price data with pandas.

Quick check

  1. 1. closes = [10, 20, 30]. What is closes[0]?

  2. 2. What does the spacing at the start of a line do in Python?

  3. 3. What does entry = 5000.25 do?

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 “Loading price data with pandas” is waiting next time.

Read the full notes

A needs only a little : 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 , × 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 numbers

Checking the loop by hand

Made-up numbers · closes: 5001.00, 5003.50, 5002.25, 5006.00

  1. 5003.50 − 5001.00 = +2.50
  2. 5002.25 − 5003.50 = −1.25
  3. 5006.00 − 5002.25 = +3.75
  4. 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

Drill

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