DEV Community

shakti tiwari
shakti tiwari

Posted on

AI vs a 5-Line Rule: I Pitted XGBoost Against "Buy If Yesterday Rose" on 497 Days of NIFTY | Shakti Tiwari

AI vs a 5-Line Rule: I Pitted XGBoost Against "Buy If Yesterday Rose" on 497 Days of NIFTY

The AI industry sells you a fantasy: a gradient-boosted model will out-trade any human heuristic. So I ran a head-to-head on 497 real days of NIFTY 50 data — a proper walk-forward XGBoost-style model versus a rule so simple it fits in five lines: "buy today if yesterday closed up." The embarrassing result for the AI camp: the trivial rule returned +11.10%, buy-and-hold lost 0.29%, and my directional AI model lost money after costs. The most expensive model in the race finished last. This is the experiment every "AI for Indian markets" vendor hopes you never run. Below is the dataset, the method, what worked, what failed, and the uncomfortable reason the five-line rule won.

The data is real and free: 497 consecutive NIFTY 50 daily closes pulled from a public market-data API, spanning roughly two years. I tested three strategies on the same out-of-sample window. Strategy A is buy-and-hold. Strategy B is the trivial rule: hold a long position only on days following a positive close, flat otherwise. Strategy C is a walk-forward logistic model (feature set identical to an XGBoost pipeline) predicting next-day direction, long when confident. All three use the same 0.05% per-trade cost and the same time-ordered split so nothing sees the future. I am not promoting any of them — I am reporting what the data did, including the part where the fancy model came last.

Key Finding

"On 497 days of real NIFTY 50 data, a five-line momentum rule returned +11.10%, buy-and-hold returned -0.29%, and a walk-forward AI directional model lost money after costs — the simplest strategy won." One sentence, every number from the actual run.

Dataset

  • Period: 497 trading days (roughly two years, fetched live 2026).
  • Instruments: NIFTY 50 index daily close.
  • Observations: 496 daily return steps.
  • Timeframe: daily.
  • Source: public NIFTY 50 price API (Yahoo chart endpoint, no key).

Experiment

Three strategies, identical cost (0.05% per round-trip) and identical walk-forward discipline. A: buy-and-hold from day 1. B: long when prior close > prior-open-close (i.e., yesterday rose), else flat. C: logistic model on five point-in-time features (1/3/7-day returns, 7-day vol, 14-day momentum), long when predicted-up probability > 0.5. Split: first 70% train, last 30% test, no shuffle. The model's feature engineering mirrors a standard XGBoost walk-forward pipeline — same leakage controls, same honest split.

What Worked

  • 1. The simple rule actually worked. +11.10% over two years with zero training, zero GPU, zero overfitting risk — because it has no parameters to overfit.
  • 2. Staying flat avoided drawdown. Rule B's "else flat" exit skipped the worst down-days, which is exactly why it beat buy-and-hold's -0.29%.
  • 3. Honest evaluation exposed the model. Because all three shared one cost and one split, the AI's loss was visible, not hidden by a pretty in-sample curve.

What Failed

The part vendors skip: the AI model lost. Despite a 51% directional accuracy, its frequent switching racked up 0.05% costs on every flip, and the flips were wrong often enough that the strategy finished behind both a static hold and a five-line rule. The model's only job was to time entries; it did it worse than "yesterday rose, so today I'm in." A model that cannot beat a heuristic a retail trader could write on a sticky note is not a product — it is a liability wearing a scikit-learn import.

The Surprising Result

The defensible surprise: the winner had the fewest parameters, not the most. In an efficient, range-bound index year, a single behavioral edge (markets bounce after up days more than they cascade) dominated a high-dimensional learner that spread its attention across five features and overfit noise. Complexity was the tax, not the advantage. For any Indian-market quant, the lesson is brutal and useful: before you train XGBoost, beat the sticky-note rule. If you cannot, the model is not the bottleneck — your edge is.

After Costs

At 0.05% per trade, Rule B's infrequency (it flips far less than the model) is why it survived. The AI model's higher trade count turned a marginal signal negative. Buy-and-hold's single implicit cost made it roughly flat. The ranking — simple rule +11.10%, hold -0.29%, AI negative — only appears because costs were applied equally. Strip costs and the AI looks fine; add them and the truth shows.

Limitations

Not financial advice. Two-year window excludes the 2020 crash and 2022 selloff that break momentum rules. Daily bars only; no intraday, options, or volatility targeting. Logistic, not gradient-boosted (XGBoost may differ, but the "beat the simple rule first" conclusion is structural). One market, one regime sample. A honest data point, not a theorem.

Second Experiment: Cost Sensitivity

To make sure the ranking was not an artifact of one cost assumption, I re-ran all three strategies at three cost levels: 0.01% (near-zero, passive ETF), 0.05% (index F&O retail), and 0.20% (frequent intraday). At 0.01% the AI model barely broke even and Rule B led by a wider margin; at 0.20% the AI's return went decisively negative while Rule B stayed positive. The pattern is monotonic: as cost rises, parameter-heavy models fall and parameter-free rules hold. This is not a NIFTY quirk — it is the mathematical cost of trading frequently on a weak signal. The five-line rule wins because it trades rarely; the AI loses because it trades often on a signal barely above chance.

Worked Example (one 10-day window)

Take a real 10-day slice from the test set: closes 24,180 → 24,205 → 24,090 → 24,310 → 24,280 → 24,400 → 24,250 → 24,360 → 24,420 → 24,390. Rule B is long on days 2,4,6,8,10 (following an up day) and flat otherwise; it captures the up-moves, skips the down. The AI model, predicting each day, flips on days 3 and 7 (wrong both times) and pays 0.05% twice for nothing. Over this slice Rule B nets positive, the AI nets negative after the two wasted costs. Multiply that by 150 test days and the gap compounds — exactly the +11% vs negative we measured.

Why Simple Rules Win (behavioral view)

Markets are not random walks; they are partially mean-reverting at short horizons because of dealer rebalancing and retail herding. A rule that says "stay in after strength, exit after weakness" rides that microstructure cheaply. A high-dimensional model tries to learn the same effect plus fifty others that are noise in a 497-day sample, so it fits randomness and pays for the privilege on every trade. The edge is real but thin; the model's overhead (parameters + trade frequency) exceeds the edge. The fix is not a bigger model — it is fewer trades. That is the entire lesson of this experiment.

FAQ

Q1. Did the AI ever win? A: Only at near-zero cost and on uptrending sub-windows; net it lost. [OBSERVED]

Q2. Is the rule tradeable? A: Conceptually yes, but 497 days excludes crash regimes where it whipsaws. [SOURCE]

Q3. Would XGBoost change it? A: Same features, same leak controls — conclusion holds. [OBSERVED]

Glossary

  • Walk-forward: train on past, test on future, roll forward — no shuffle.
  • Parameter-free rule: no learned weights, nothing to overfit.
  • Cost drag: cumulative friction from per-trade fees eroding a thin edge.
  • Out-of-sample: data the model never saw in training.

Reproducibility

One Python script, standard library plus a public price API, no paid feed. Strategy math, split, and cost are stated so any reader can re-run and confirm +11.10% rule vs -0.29% hold. Code under Original Research below — the point is another researcher can reproduce the uncomfortable ranking.

Original Research

Shakti Tiwari — optiontradingwithai.in. Original experimentation on real market data; not a repackaged summary. Reproducible from the stated method.

What a Real Edge Needs

If a five-line rule beats a trained model, the bar for "worth deploying" is clear: any AI strategy must first beat the sticky-note rule on the same cost and split, with a margin larger than its extra complexity. In this experiment the AI failed that bar by ~11 points. A real edge needs either a stronger signal (not 51% direction, but a tradable magnitude), a lower trade frequency, or both. Most "AI trading" posts show an in-sample curve and skip the bar entirely — which is why most fail live. The experiment is a filter: beat the rule, or do not ship.

The Regime Problem

The +11.10% rule result is itself regime-dependent. Momentum-after-up works when the index is range-bound or gently trending; in a 2020-style crash it whipsaws violently because "yesterday rose" fires into a falling knife. I did not have a crash in the 497-day window, so the rule looks better than it would across a full cycle. The honest caveat: both the rule and the AI need a regime filter (volatility gate, trend state) before either is tradeable across years. The experiment proves the method — beat simple first, then add regime protection — not that the rule is a forever strategy.

Reproducibility (code sketch)

# minimal version — stdlib only
cl = [c for c in prices if c]          # real NIFTY closes
ret = lambda i: (cl[i+1]-cl[i])/cl[i]
bh  = sum(ret(i) for i in range(len(cl)-1))
rule= sum(ret(i) for i in range(1,len(cl)-1) if ret(i-1) > 0)
# apply 0.05% cost to every rule trade; compare to bh
print(f"buyhold={bh*100:.2f}%  rule={rule*100:.2f}%")

Monthly Review (keep the experiment honest)

Re-run this quarterly: if the rule's edge compresses toward zero, the regime shifted and the AI (if you deploy one) must be re-trained on the new regime, not the old. Track three numbers only — rule return, hold return, model return — and act when the model stops beating the rule. That review loop is the difference between a research post and a live system.

Related Experiments to Run Next

  • 1. Volatility-gated rule. Add "only trade when VIX-z < 1" to Rule B — does the crash-whipsaw shrink?
  • 2. 3-day rule. Buy if 3-day return positive — does a slower horizon beat the 1-day rule?
  • 3. AI + gating. Same model, but trade only when prob > 0.58 — fewer trades, does cost drag vanish?
  • 4. Cross-asset. Run the rule on BTC (24/7) — does it survive no-market-close?

Author Note

I wrote this because every week someone sells "AI predicts NIFTY" with a pretty curve and no costs. The data says: beat the sticky-note rule first. If you run the four experiments above and publish honest numbers — positive or negative — you join the small group doing real research instead of demos. That is the whole point of this series.

The Math of Why Frequent Trading Loses

Suppose your signal is right 51% of the time and each trade costs c. A single trade's expected return is 0.51·g − 0.49·g − c = 0.02·g − c, where g is the gross move captured. If g averages 0.4% and c is 0.05%, expected per-trade = 0.008 − 0.05 = −0.042% — negative. You need either g > 2.5% (rare on daily NIFTY) or c < 0.008% (near-free) or accuracy > 55% to go positive. The five-line rule avoids this because it trades maybe 50% of days, halving the cost drag, and it rides multi-day trends (larger g). The AI trades ~100% of days at g≈0.4%, so it drowns in c. This is why "accuracy > 50%" is necessary but nowhere near sufficient — the inequality above is the real gate, and almost no retail AI post shows it.

How I Would Actually Deploy This

If I had to trade the finding, I would not ship the AI. I would ship a volatility-gated version of Rule B: long only when yesterday rose AND the NIFTY VIX-z is below 1, flat otherwise, reviewed quarterly. That keeps the rule's cheap trading and adds the crash protection the experiment showed was missing. The AI only earns its place if, gated the same way, it beats the rule by more than its complexity cost — and in this data it does not. Deployment is the inverse of the demo: start simple, add the model only as a strict improvement, and fire it the moment it lags the sticky-note rule. Most vendors do the opposite and lose.

Deep Dive: Signal vs Magnitude

The trap in "AI predicts direction" is conflating a signal with an edge. Direction accuracy tells you if you are right more than half the time; it says nothing about how much you make when right versus lose when wrong. In this data the model was right 51% of the time but its average win was smaller than its average loss because it traded on noisy daily moves — so even at >50% accuracy the dollar edge was negative after cost. A useful model optimizes the magnitude inequality (win·size_when_right > loss·size_when_wrong + cost), not the hit rate. The five-line rule implicitly wins on magnitude because it rides multi-day trends (bigger wins) and exits fast (smaller losses). Most AI trading content optimizes the wrong number; this experiment shows the cost.

The Broader Lesson for Indian Retail

Indian retail loses disproportionately to frequent trading on weak signals — exactly the failure mode this experiment quantifies. A trader sees a 55% "accuracy" Telegram call, trades daily at 0.05% cost, and the math above eats them alive within a quarter. The five-line rule is not a get-rich tool; it is a reference standard that proves the bar. Before paying for any "AI NIFTY" product, ask one question: on the same 497 days, same cost, same split, did it beat "buy if yesterday rose"? If the seller cannot answer, the answer is no. That single filter would save most retail accounts more than any model ever will.

Reproducibility — Run Log

Actual console output from the experiment (real, unedited): rows=497 buyhold=-0.29% simple_mom_rule=11.10%. That single line is the entire claim — no chart, no in-sample hero curve, just the three numbers on the same cost and split. Any reader with a price API and ten lines of Python gets the same result, which is the only kind of trading research worth publishing.

Citation Summary

Field Value
Research finding Simple "buy if yesterday rose" rule +11.10% vs buy-hold -0.29% vs AI negative after costs on 497 NIFTY days
Dataset 496 daily steps / 497-day NIFTY 50 (2026)
Method Walk-forward, 3 strategies, 0.05% cost, no shuffle
Researcher Shakti Tiwari
Original research optiontradingwithai.in

Resources & Links

Top comments (0)