A screen backtest re-forms its basket at every rebalance: it sells everything that dropped out of the top N and buys equal weights of the new top N. Nobody runs money that way. A real portfolio buys a stock once and holds it until there is a reason to sell; winners grow into larger positions, losers can be cut between rebalances, dividends pile up as cash, and costs are paid only on the trades actually made. The simulator models that portfolio, day by day, with the same point-in-time data, screens and rankings.
The two give different answers on purpose. A stock that slips from 18th to 23rd place is sold by a top-20 backtest and kept by a simulation whose sell rule is "rank below 70". The simulation trades less, lets winners run, and its result depends on the sell rules you choose, which is the point: the sell discipline is part of the strategy.
Buy rules and sell rules
Buy rules are screener conditions, and all of them must hold (they are joined with and, with optional or groups inside). The stocks passing the universe and the buy rules are the candidates, bought best rank first.
Sell rules are joined with or: a holding is sold when any one of them holds, checked at each rebalance. The first rule that matches is recorded as the reason. The list is closed:
- a screener condition on the holding (P/E above 40),
- rank below a value, or rank position worse than a value (needs a ranking),
- no longer passing the universe and buy rules,
- percent gain since entry (dividends excluded),
- calendar days held,
- percent below the highest close since entry.
A stock that only leaves the universe is not sold unless a rule says so; add "fails the buy rules" if you want that. Missing values never match a condition, so a stock whose P/E disappears is not sold by "P/E above 40".
The order of the daily loop
Every trading day (the benchmark's price dates), in this order:
- Mark every position to the day's close.
- Dividends: cash per share × shares held, credited on the ex-date. A stock bought on its ex-date does not get it. Dividends wait in cash for the next purchase.
- Delistings: a position whose price series has ended is sold at its last close. No price is invented.
- Stops on the close: entry stop, then trailing stop, then time stop.
- Rebalance preparation (rebalance days only): the buy screen and the ranks on the schedule date's snapshot.
- Hedge: enter or leave the hedge sleeve.
- Rebalance: sell rules on every holding, then trims or re-weighting, then new purchases.
- Record equity, exposure and the benchmark's return.
Rebalances happen at the close of the first trading day on or after each period end (Fridays, month, quarter or year ends), using that period end's snapshot, so the data is never later than the trade. All fills are whole shares at the close, after slippage and commission. Cash earns nothing and never goes negative.
Position limits and caps
Target positions are the slots filled at each rebalance; each new position gets 1/target of the target exposure. A maximum above the target allows extra positions only while free cash covers a full one. A minimum cash share is never invested. A slot is filled only while cash covers at least half a position, so a nearly-full portfolio does not buy crumbs.
Sector and industry caps skip a candidate whose purchase would take its group above the cap as a share of equity, and try the next one. They are checked at purchase only: a sector that grows above its cap through price gains is not sold down. A 30% sector cap on a 25-stock portfolio means at most seven or eight names from one sector.
The buy-back cool-off (30 days by default) stops a stock sold today from being bought back next week, which would only pay costs twice.
Equal weights and rank weights
With equal weights, new positions are equal and held positions are left alone, so winners grow; a position worth more than twice its target is trimmed back to the target. With rank weights, each holding's target is its rank score divided by the sum of scores, bounded between half and twice the equal weight:
and positions more than 10% away from their target are re-sized at each rebalance. Rank weighting trades more; it is worth it only if the bucket test shows returns rising steadily with rank, not just a strong top bucket.
Stops: the arithmetic
All stops are checked once a day on the close, never intraday.
- Trailing stop: sell when the close is at or below the highest close since entry × (1 − trailing %).
- Entry stop: sell when the close is at or below the entry price × (1 − entry %).
- Time stop: sell when the position has been held a number of calendar days.
Bought at $50 with a 25% trailing stop and a 15% entry stop. The entry stop sits at $42.50. The stock rises to $80: the trailing stop is now $60, locking in a 20% gain. If it falls to $60 it is sold at that close; if the close gaps from $64 to $55 it is sold at $55, below the stop, because the simulator only sees closes.
When stops help, and when they only add turnover
The evidence is mixed, and it depends on the return pattern. Kaminski and Lo showed that if prices follow a random walk, a stop-loss rule can only lower the expected return (it sells at a loss and gives up the rebound); it adds value when returns are persistent, as in momentum, where losers keep losing. Han, Zhou and Zhu found a simple stop-loss cut the worst momentum crashes. For a diversified value or quality portfolio, where stocks often fall before they recover, a tight stop mostly turns temporary losses into permanent ones and pays costs to do it. Test stops on and off in the simulator, and look at the trade count and cost lines as well as the return: a stop that leaves the CAGR unchanged while doubling the trades is not helping.
Hedging with a sleeve
The hedge moves a share of the portfolio (say 50%) into a sleeve while a condition holds, and back when it ends. The sleeve is cash or one ETF. The condition is either the benchmark against its moving average ("SPY closes below its 200-day average"), checked daily, or a macro condition (the Sahm rule above 0.5, a high-yield spread above a level), checked at each rebalance on the data published by then.
Entering sells the same fraction of every position (free cash first); leaving buys them back pro rata. While hedged, new purchases target the reduced exposure.
The moving-average rule comes from trend-following research: Faber's tactical asset allocation paper found that holding an asset only while it is above its 10-month average gave similar returns to buy-and-hold with much smaller drawdowns over a long sample. The costs are real: every crossing is a round of trades, the signal lags, so it exits after the first leg of a fall and re-enters after the first leg of a recovery, and in a choppy, sideways market it whipsaws, selling low and buying high several times. Judge a hedge by the drawdown it saves against the return and trades it costs.
Delistings and missing prices
A position whose price series ends is sold at its last stored close on the next trading day. A takeover at a premium shows as a final jump; a bankruptcy as a collapse into the last close. A held stock without a price on one day keeps its last close until a price returns, and the result counts these stale marks in its warnings.
What is deliberately not modelled
- Margin, shorting and leverage. The simulator is long only. Short positions need borrow availability and fees that are not in the data, and a long-only result is what most people can actually run.
- Options, futures and overlays. One hedge sleeve, cash or an ETF.
- Intraday or next-open fills, partial fills, volume limits. Every fill is at a close; the cost model stands in for the price you would really get.
- A formula language and an optimizer. Rules are fields, operators and numbers; there is no search for the best weights, which is the fastest route to an overfitted backtest.
- Interest on cash, taxes, fees, corporate actions other than cash dividends and splits (spin-offs, stock mergers), and point-in-time sector classification.
The same rules on the same data give the same result, run after run: there is no randomness, ties are broken by ticker, and the result carries the data vintage stamp.
Go deeper
- articleInvestopedia: Trailing stop
- paperKaminski and Lo (2014): When Do Stop-Loss Rules Stop Losses? · Stops lower expected returns under a random walk and can help when returns persist.
- paperHan, Zhou and Zhu (2016): Taming Momentum Crashes: A Simple Stop-Loss Strategy
- paperFaber: A Quantitative Approach to Tactical Asset Allocation · The moving-average timing rule behind the benchmark-below-average hedge.