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

Free Trading School · Level 7 · Course 7.5

Splitting data into build and test

Lesson 12 of 15 in Backtesting in Python (optional)Module 3: Results and checks

Advanced · Backtesting in Python

Splitting data into build and test

Wildflower Trading School · Lesson 7.5.3.2

Wildflower

In Level 4 you met in-sample and out-of-sample data. You build your rules on one part of history, and judge them on a part you haven't touched. By the end of this lesson, you'll split a file by date in code and lock the test part away. You'll also know the rules that keep it honest.

1 of 9

What you'll learn

  • Split by date. Neighbouring bars are alike, so cut the data at one point in time.
  • Set each part's size. Use iloc to pick rows by position, like the first 75% for building.
  • Lock the test part away. Save it to its own file and work on the build part only.
  • Decide everything first. Set your rules and what counts as a pass before you look.
  • Run the test part once. Treat a poor result as an answer, and write it down.

Exercise

  1. Split your own file by date, save the test part to its own file, and keep df to the build part.
  2. Write down your rules and what would count as a pass before you go anywhere near the test file.

Next: Walk-forward testing in code.

Quick check

  1. 1. Why split by date rather than at random?

  2. 2. You ran the test part, didn't like it, and changed the rules. What is the test part now?

  3. 3. What does df.loc["2025-01-01":] give you?

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 “Walk-forward testing in code” is waiting next time.

Read the full notes

In code, you split the data by date with loc, save the test part to its own file, and keep working on the build part only. Decide everything first, then run the test part once.

  • Build on one part of history () and judge the finished rules once on a hidden part (), as in In-sample and out-of-sample data.
  • By date: build = df.loc[:"2024-12-31"]; test = df.loc["2025-01-01":]. Made-up file: 782 build bars, 261 test bars. Split by date, never at random: neighbouring bars are alike.
  • By share: cut = int(len(df) * 0.75); df.iloc[:cut] and df.iloc[cut:]. iloc picks rows by position.
  • Lock it away: save the test part with .to_csv("locked_test_2025.csv") and keep df to the build part. At the end, join it back, work out indicators across the joined data (they only look back), and keep trades that start in the test period.
  • Rules: decide everything first, including what counts as a pass; run the test part once; a poor result is an answer, not a reason to tune (); write it down (Keeping a version history). More: In-sample and out-of-sample testing.

Worked example

Made-up numbers

Where the cut lands

Made-up numbers · 1,043 bars · 75% for building

  1. 1,043 × 0.75 = 782.25
  2. int() drops the decimals: 782 build bars
  3. 1,043 − 782 = 261 test bars
  4. About one year of weekdays held back.

The cut lands at the turn of the year in this made-up file.

Your turn

Drill

Made-up numbers · 800 bars · 70% for building

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.