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
- Split your own file by date, save the test part to its own file, and keep df to the build part.
- Write down your rules and what would count as a pass before you go anywhere near the test file.
Quick check
1. Why split by date rather than at random?
2. You ran the test part, didn't like it, and changed the rules. What is the test part now?
3. What does df.loc["2025-01-01":] give you?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
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 (The part of the price history used to build and adjust a strategy’s rules. The part kept back to test them is out-of-sample.More in the glossary) and judge the finished rules once on a hidden part (History kept back while a strategy’s rules are built, then used once to test the finished rules unchanged. The part used to build them is in-sample.More in the glossary), 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 (Tuning a strategy’s rules so closely to one stretch of past prices that they describe that stretch’s quirks more than anything that carries on.More in the glossary); write it down (Keeping a version history). More: In-sample and out-of-sample testing.
Worked example
Made-up numbersWhere the cut lands
Made-up numbers · 1,043 bars · 75% for building
- 1,043 × 0.75 = 782.25
- int() drops the decimals: 782 build bars
- 1,043 − 782 = 261 test bars
- About one year of weekdays held back.
The cut lands at the turn of the year in this made-up file.
Your turn
DrillMade-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.