Just The Markets

Rules First, Money Later: Test a Trading Strategy · Lesson 1 of the course

Turning a Trading Idea Into Rules a Backtest Can Run

Trading rules for a backtest have to be exact enough that anyone applying them gets the same trades. Here is how a loose pullback idea becomes rules a spreadsheet can follow.

AI-assisted, reviewed by the Just The Markets human editor: Lovely Oryza → About 13 minutes Published

  1. 01Turning a Trading Idea Into Rules a Backtest Can Run
  2. 02Backtest Costs and Biases: Slippage, Survivorship, Look-Ahead
  3. 03Reading Backtest Results: Expectancy, Drawdown and Streaks
  4. 04From Backtest to Live: Paper Trading and Small Size

In this lesson you will learn to

  • Rewrite a loose trading idea as exact entry, exit, stop, size and filter rules
  • Find the gaps in a rule set that would let two people take different trades
  • Size a position from a fixed dollar risk and the distance to the stop

“Buy strong stocks on pullbacks.” Hand that sentence to two traders. You get two trade lists. Strong by what measure? How far is a pullback, and when does it end? Where is the stop? When do you sell? A backtest can only run a rule a spreadsheet can follow, so every one of those questions needs an answer written down before the first test.

The test for a rule

A rule set is ready when two people get identical trades. Same entry days, same prices, same share counts, same exits. If they differ anywhere, some rule has a gap.

The parts to cover are entry, exit, stop, size and filters. Leave one out and you will fill it in by eye as the test runs, choosing on each chart whatever looks sensible at the time, and that is precisely the kind of judgment, shaped by knowing what happened next, that a backtest exists to take out of the result.

The worked rewrite

Here is the pullback idea turned into rules:

  • Trend: the close is above its 50-day average.
  • Pullback: the stock has fallen three days in a row.
  • Entry: buy at the close on the day both are true.
  • Stop: the 20-day low.
  • Exit: sell after ten days.

Closer. Still not exact. Put each line through the two-person test and the gaps show.

Is the “50-day average” simple or exponential? Say simple, on closing prices. “Fallen three days in a row” could mean three lower closes, or it could mean three red candles where each close sits below that day’s open, and on a volatile stock those two readings pick different days often enough to change the whole trade list. Pick three consecutive lower closes.

“The 20-day low” moves every day. Does the stop trail it, or is it fixed at entry? Fix it at entry. Use the lowest low of the last 20 sessions, entry day included. A stop that trails the 20-day low each morning behaves like another strategy altogether, locking in gains on some trades and cutting others short, so if trailing is what you want, it needs its own written rule and its own test.

“After ten days” could mean calendar days or trading days. Make it trading days, selling at the close of the tenth session after entry.

Then the collisions. A gap below the stop fills at the open. Write that down. When the stop and the ten-day exit land on one day, the stop wins, since it is hit during the session. A fresh signal while you already hold the stock is ignored.

Size and filters

Size needs its own rule. Fixed dollar risk is easiest to test. Suppose the hypothetical rule risks $500 a trade.

A wider stop means fewer shares and the same $500 at risk. Losses stay comparable across trades. The lesson on reading results measures everything in multiples of that risk.

Filters decide which stocks can trade at all. Typical ones set a floor on price and on average daily volume, which keeps thinly traded shares out of the test, where the spreadsheet would otherwise assume fills at the closing price that a real order in those shares would rarely get. The exact levels are your choice. Write them down with the rest.

The finished set:

  • Universe: stocks above your minimum price and volume.
  • Entry: buy at the close when the close is above its 50-day simple average of closes and the close has been lower than the prior close on three consecutive sessions.
  • Stop: the lowest low of the 20 sessions up to and including the entry day, fixed at entry; if the stock opens below it, exit at the open.
  • Exit: sell at the tenth session’s close, counting from entry.
  • Size: shares equal $500 divided by the distance from entry to stop.

Write it before you look

The last rule is about order. Write the rules, date them, and only then run the test. The moment you see a chart of past trades, every setting starts to look adjustable: the 50-day could be 40, the ten-day exit could be twelve. Each tweak fits the past better. A setting chosen because it drew the best curve on the data you just looked at has been fitted to the noise in that particular stretch of prices, and nothing says the same noise will turn up in the months you actually trade. The drift is called overfitting, and a walk-forward test is one way to catch it. Entry styles like this one are covered on the stock trading hub and the swing trading hub.

With the rules fixed, the next step is running them against history honestly. Costs and biases come first. They can sink a good-looking result.

Check your understanding

Lesson quiz

  1. 1Which of these entry rules is exact enough to backtest?
    Show the answer

    B: Buy at the close when the close is above the 50-day simple average of closes. It names the price field, the lookback, the type of average and the time of the order, so anyone running it on the same data gets the same entries.

  2. 2Your entry is $40, your stop is $38, and your rule risks $300 a trade. How many shares do you buy?
    Show the answer

    A: 150. The risk per share is 40 less 38, or $2.00, and $300 divided by $2.00 is 150 shares.

  3. 3Why write the rules before looking at any results?
    Show the answer

    C: Seeing results first bends the rules toward the past data. Once you have seen what would have worked, the rules drift toward fitting that history, and the test stops telling you anything about the future.

People also ask

What rules does a trading strategy need before I can backtest it?

At minimum, an entry condition, an exit condition, a stop, a position size and any filters on which stocks qualify. Each has to be stated with a price field, a lookback length and a time of day, so the same data always produces the same trades no matter who runs the test.

Can I change my rules after I see the backtest?

You can, but each change fitted to the same history makes the result less trustworthy. Keep a dated log of every change and the reason, and test the final version on data the earlier runs never touched before trusting it.