Decision Trees: Teaching Computers to Analyze Markets
A step-by-step walkthrough of how decision trees split data into buy/sell decisions. More intuitive than you'd think.
MarketPulse Toronto Editorial Team
Editorial Team
Written by the MarketPulse Toronto editorial team, focused on clear, practical guidance for learning supervised learning in stock analysis.
What's a Decision Tree, Really?
Decision trees are surprisingly straightforward. They're basically a series of questions your computer asks about the data, and each answer narrows down what it thinks will happen next. It's like when you're deciding whether to buy a stock — you ask yourself: Is the price trending up? Is the volume increasing? Has the company missed earnings? Each yes or no leads you down a different path.
The magic is that computers can ask thousands of questions at once, finding patterns that humans would miss. But here's the real win: you can actually see why the computer made a decision. Unlike some other models that feel like a black box, decision trees show their work.
How the Tree Grows
Building a decision tree starts with your training data. Let's say you've got 500 days of TSX stock prices with labels: "good day" or "bad day" based on whether the stock went up or down. The algorithm doesn't just randomly split the data. It tests thousands of possible splits and picks the one that separates your data most cleanly.
First split might be: "Is the 50-day moving average above the current price?" That single question might separate your data into two groups that are much more homogeneous than before. Then it does it again on each group. And again. Until it either runs out of questions to ask or hits the depth limit you set.
Key Point: Each split reduces entropy — basically making your data groups more "pure." The algorithm greedily picks the split that reduces entropy the most.
The Practical Side: What Numbers Matter
When you're training a decision tree on market data, you'll see some key numbers. Tree depth typically ranges from 3 to 15 levels. Too shallow and you're missing patterns. Too deep and you're memorizing noise instead of learning trends. For Toronto stock data, we've found that depth 6–8 usually works well — not too simple, not overfit.
Then there's the minimum samples per leaf. If you set this to 5, your tree won't create a branch unless it can split at least 5 data points. This prevents the tree from getting too specific about individual trading days that won't repeat. It's one of your best defenses against overfitting.
The split criterion matters too. Most people use Gini impurity or entropy. For market data, they perform similarly, but Gini is slightly faster to compute. You'll rarely notice the difference, so pick whichever your library defaults to.
Why Trees Work for Market Data
Markets have thresholds. A stock doesn't gradually shift from "buy" to "sell" — it crosses levels. That's what decision trees are built for. They excel at finding those exact crossing points. If the 20-day moving average crosses above the 50-day moving average, that's a clear threshold. A decision tree will find it.
They also handle non-linear relationships well. Maybe price jumps up when volume exceeds 2 million shares AND the price is above $50, but only if it's also above the 200-day average. That's three conditions — non-linear, specific, the kind of thing that's hard to write manually but trees discover automatically.
The downside? Trees can overfit. They'll memorize the training data if you let them. That's why limiting depth and setting minimum samples per leaf matters so much.
Learning Note: Individual learning outcomes vary from person to person. This guide provides educational information about how decision trees work conceptually. Actual model performance depends heavily on your data quality, feature selection, and validation approach. Always test your model on unseen data before applying it to real trading decisions.
Building Your First Tree
The basic workflow is simple: gather data, split it into training and testing sets, train your tree, evaluate performance. Most people use Python with scikit-learn. You'll write maybe 10 lines of code to train a tree and get predictions.
Start conservative. Train a tree with depth 5, minimum samples per leaf of 10. See how it performs. If accuracy is too low, increase depth. If it seems to be overfitting (performs great on training data, poorly on test data), increase the minimum samples requirement.
You can visualize the tree to see what decisions it's making. That's huge — you'll actually understand which features matter most and what thresholds the tree picked. That kind of transparency is rare in machine learning.
Common Mistakes to Avoid
First mistake: not validating properly. You train on all your data, test on all your data, and you'll get amazing results that don't mean anything. Always hold out test data the tree hasn't seen.
Second: using raw prices instead of features. The tree needs meaningful inputs. Instead of raw closing price, give it moving averages, momentum indicators, volume ratios. The tree will find patterns faster with good features.
Third: forgetting about class imbalance. If 90% of your data is "hold" days and 10% is "buy" days, your tree will be biased. It can predict "hold" all day and still get 90% accuracy. Balance your classes or adjust class weights.
Pro Tip: Use cross-validation instead of a single train/test split. It gives you a more reliable picture of how your tree will perform on unseen data.
Moving Forward
Decision trees are one of the most approachable machine learning tools for market analysis. They're not perfect — no single model is — but they're intuitive, fast, and transparent. You can see exactly why they made a decision, which matters when you're trading real money.
Start by experimenting. Grab some Toronto stock data, train a tree with basic parameters, and see what happens. Don't expect perfection on your first try. The goal is understanding how the tree thinks about your data, what features matter, and where the natural thresholds are.
Once you're comfortable with decision trees, you'll be ready for ensemble methods like random forests — basically lots of trees voting together. But that's a story for another guide.