Defining Rules Before Backtesting
How Do You Define Trading Rules Before Backtesting?
The difference between a backtest that teaches you something real and one that misleads you starts before you write a single line of code. Most traders jump into backtesting with vague ideas: "buy when the price looks good" or "sell when I think it's too high." Those aren't rules—they're hunches. A trading rules definition is a written, unambiguous set of conditions that tell you exactly when to enter a position, when to exit it, and how much money to risk. Before you test anything, you must write down what your strategy actually is. This article walks you through the process of defining rules clearly enough that a computer could execute them, and clearly enough that you could explain them to another trader without confusion.
Quick definition: Trading rules are explicit, testable conditions—written in plain language or code—that specify exactly when to enter a trade, when to exit it, and how much to risk on each trade.
Key takeaways
- Write rules in plain language first; only then translate to code or a backtest engine.
- Your rules must be testable by historical data alone; no emotion, no hindsight, no "I would have seen that coming."
- Define entry rules, exit rules, and position-sizing rules separately; each is its own decision point.
- Ambiguous rules produce ambiguous backtests; specificity is not weakness—it is the foundation of honest testing.
- Revisit your rules after every backtest to catch logic gaps, circular reasoning, and lookahead bias.
Why Writing Rules Matters Before You Backtest
Many traders feel rushed to test their ideas. The urge is understandable: you want to see if your strategy would have worked. But backtesting without written rules is like building a house without a blueprint—you'll fill in the gaps as you go, and those gaps will accommodate whatever result you want to see.
When you write rules first, you're forced to make decisions while you're still thinking clearly, before you've seen the historical results. You commit to a specific entry price, a specific exit condition, and a specific position size. Those commitments are binding when the backtest runs. You can't change your mind halfway through because the price "looks wrong" or because you "feel like" stopping out early.
The written rules also serve as a communication document. If you talk to another trader, your rules are right there. If you come back to your strategy six months later, you'll know exactly what you were testing. That clarity prevents the silent drift that kills most trading systems—the gradual relaxation of rules after they stop working.
Plain-Language Rule Definition: The Foundation
Start by writing your rules in plain English (or your native language). Not code, not a software specification—plain sentences that describe what happens when.
A rule should answer these questions:
- When do I enter? State the price, volume, or indicator condition.
- When do I exit? State the exact exit trigger: target price, time, loss limit, or indicator signal.
- How much do I risk per trade? State the dollar amount, percentage of account, or formula.
Example of a weak rule: "Buy stocks that are breaking out and strong."
Example of a clear rule: "Buy when the closing price exceeds the highest close in the last 20 days, and the 5-day average volume is above 1 million shares."
The second rule can be tested. The first cannot. When you write clearly, you also discover gaps in your own thinking. A new trader might write "sell when my profit reaches 5%," then realize they haven't said whether that's 5% from the entry price or 5% from the intraday low. That ambiguity must be resolved before the backtest runs.
Entry Rules: Be Specific
An entry rule is the trigger that says "now is the time to buy (or short)." It can be based on price, time, volume, an indicator, or a combination.
Price-based entry: "Buy the open if the previous day's close was above the 50-day moving average."
Time-based entry: "Buy the open on the first Monday of each month."
Volume-based entry: "Buy only if the day's volume is above the 20-day average volume."
Indicator-based entry: "Buy when the RSI is below 30 and the MACD histogram turns positive."
Combination entry: "Buy if the price breaks above the previous week's high AND the 5-day average volume exceeds 1 million AND the 14-day RSI is above 40."
The more specific you are, the more testable the rule becomes. "Price breaks above the previous week's high" can be tested objectively. "The price looks strong" cannot.
Exit Rules: Be Explicit
An exit rule tells you when to close the position. Most traders are clear on entries but vague on exits. That vagueness is deadly because it lets you cherry-pick the exits that look good in hindsight.
Profit-target exit: "Sell when the price reaches 2% above the entry price."
Time-based exit: "Sell if the trade hasn't reached the profit target within 10 trading days."
Stop-loss exit: "Sell if the price falls 1% below the entry price."
Indicator-based exit: "Sell when the RSI exceeds 70."
Trailing stop exit: "Sell if the price pulls back 0.5% from its intraday high since entry."
The key word is "when." Not "if you think," not "if the price looks weak," not "if I feel comfortable." When. The moment the condition is true, you exit. No exceptions, no discretion. That discipline is what makes a backtest meaningful.
Decision tree
Position Sizing Rules: The Third Dimension
A position-sizing rule tells you how much money (or how many shares) to risk on each trade. This is separate from your entry and exit rules, but equally important.
Fixed dollar amount: "Buy exactly $10,000 of stock per trade."
Percentage of account: "Risk 1% of the account balance per trade."
Fixed share count: "Buy exactly 100 shares per trade."
Kelly formula or optimization: "Risk the amount suggested by the Kelly criterion based on your win rate and average win/loss."
Volatility-adjusted: "Risk $X per trade, then adjust the share count based on the current ATR (Average True Range)."
Beginners often treat position size as an afterthought. It isn't. Your position size interacts with your stops and targets to determine your risk and reward on every single trade. A rule that works at 1% risk per trade may blow up at 5% risk per trade because of drawdown, margin calls, or psychological pressure.
Backtesting Parameters: Hidden Rules
When you set up a backtest, you're also defining rules implicitly. These matter:
Start date and end date: Your rules might work great in 2020–2021 (bull market) but fail in 2022 (bear market). Do you backtest only bull markets or the full range?
Commission and slippage: Real trading has both. Do you include realistic costs or test without them?
Data quality: Are you using dividend-adjusted prices? Do you account for stock splits?
Time of day: Do your entries and exits happen at the open, close, or intraday?
Write these down as part of your rule definition. Your backtest should document all of them.
Avoiding Lookahead Bias When Defining Rules
Lookahead bias is the biggest hidden pitfall in rule definition. It happens when your rule uses information that wouldn't have been available at the time you're testing.
Lookahead bias example: "Buy if the next day's close is above the current close." You can't know the next day's close when you're entering today. Your rule is rigged.
Another example: "Buy if the indicator reaches a 5-year low." If you're backtesting in 2020, you can't know whether a reading is a 5-year low until you've seen the next five years of data.
When you write rules, imagine yourself as a trader with a calendar. Today is January 10, 2020. You can use data up to and including January 9. You cannot look ahead. Any rule that requires looking ahead is broken.
Testing Rule Clarity: Walk Through a Trade
Once you've written your rules, run through a real historical trade manually. Pick a random date in your backtest period.
Example: Your rules say "buy when the close is above the 20-day moving average."
Walk through:
- January 15, 2019: Is today's close above the 20-day MA? Yes. Enter.
- January 20, 2019: Is the exit condition met? Yes, the price has hit the target. Exit.
If you can't walk through a trade without stopping to guess what the rule means, the rule isn't clear enough. Rewrite it.
Common Mistakes in Rule Definition
Mistake 1: Circular rules. A rule that uses the exit signal as the entry signal. Example: "Buy when the RSI is below 30 and sell when the RSI is above 70." If RSI is above 70, it's already above 30, so the entry signal is always true. You've defined a rule that will never work as intended.
Mistake 2: Vague time references. "Buy when the price is near support." Near is not a number. Is it 1% near? 5% near? Use exact prices or exact distances.
Mistake 3: Emotion-based exit rules. "Sell if I'm nervous" or "hold if the uptrend feels strong." These aren't rules; they're invitations to disaster. Stick to objective conditions.
Mistake 4: Mixing different time frames. "Buy on the daily chart break, but use the 15-minute chart to exit." This can work, but you must be explicit about every time frame in every rule.
Mistake 5: Undefined edge. A rule with no reason to work. Example: "Buy every time the date ends in 7." That's not a trading rule; that's superstition. Every rule should have a logical or statistical basis.
FAQ
What if I don't know my exact entry price until the market opens?
Use an order type rule: "Place a buy limit order at [specific price] at the market open. If filled during that bar, I'm in the trade." This is testable: did the price reach that level during the bar?
Can I have multiple entry conditions, any one of which triggers a trade?
Yes, but write them clearly. "Buy if condition A OR condition B OR condition C" is fine. Just list all three. Avoid "buy if any strong signal appears"—that's not a rule.
What if my strategy needs human judgment?
Then it can't be backtested reliably. Backtest only the objective parts. Use judgment as a filter outside the backtest: test the rules, then apply judgment to the results.
Should I change my rules if the backtest shows they don't work?
You can change them, but not because of the backtest result you want. Change them if you discover a logical flaw or a lookahead bias. Document every change and retest from the beginning.
How many rules is too many?
There's no magic number. Some of the best strategies have 3 rules. Some have 15. The issue is complexity: the more rules you add, the more likely you are to overfit (optimize the rules to past data rather than future data). Aim for simplicity with precision.
Do I need rules for every position I trade?
Yes. Even if you trade different symbols, your entry, exit, and sizing rules should apply to all of them. That consistency is how you avoid mental accounting—the trap of applying different standards to different trades.
Related concepts
- Backtesting Overview — Why backtesting matters and what it can and can't tell you.
- Entry Rule: A Clear Definition — Deep dive into entry rule structure and examples.
- Exit Rule: A Clear Definition — How to define exits without lookahead bias or discretion.
- Position Sizing in a Backtest — How to define and test position-sizing rules.
Summary
Trading rules definition is the foundation of honest backtesting. Before you code anything, write plain-language rules that answer three questions: when do I enter, when do I exit, and how much do I risk. Be specific enough that a computer could execute your rules automatically. Check for lookahead bias—rules must use only information available at the time of the trade. Walk through a historical trade manually to verify your rules are clear. Avoid vague language, circular logic, and emotion-based conditions. A clear rule definition takes an afternoon but saves months of wasted backtesting. The traders who take this step seriously are the ones who catch their flaws before the market does.