Automate a Trading Strategy (Part 3) — RSI, Session Filters, 5-Year Tests, and the First Step Toward Real Optimization

Lab Note (Part 3). In Part 1 we built a fast RSI prototype in TradingView to learn how to prototype and, more importantly, how to read backtests with the right skepticism.

In Part 2 we ported the same prototype to MT5 (MQL5) and tested it in OHLC modelling, introducing a more realistic research environment and deeper diagnostics.

Now in Part 3, we do what most traders skip entirely: we start a controlled research process. Not “curve fitting”. Not “parameter hunting”. A process designed to answer one question:

Does this strategy concept have any structural edge, or are we just mining noise?

This is where strategy research becomes engineering.

 

What we changed in Part 3 (overview)

Here are the modifications and tests we ran in this phase:

  • Switch from M1 to M5 to reduce noise
  • Expand the test window to 5 years (more regimes)
  • Since S&P 500 is structurally bullish, test long-only
  • Test a different risk/reward: 1:2 instead of 1:1
  • Apply a session filter aligned with NYSE open hours
  • Run a sweep optimization on RSI oversold (10 → 100)
  • Run a sweep optimization on SL/TP ratios
  • Introduce a context filter: buy only if price is X standard deviations below the open

Each step has a reason. And each step teaches something about what is (and isn’t) real.

 

1) Why we moved from M1 to M5

M1 is a brutal timeframe for strategy research. It contains:

  • microstructure noise
  • spread artifacts
  • random spikes
  • execution distortions

RSI is based on close-to-close price changes. On M1, those changes are often dominated by randomness rather than information.

By switching to M5 we reduce the noise floor and move toward a timeframe where:

  • signals are less frequent but more stable
  • indicator values are less “twitchy”
  • mean-reversion conditions can be evaluated more cleanly

Lab rule: if a strategy only works on M1 but collapses on M5, it’s often not edge — it’s noise exploitation.

RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500

2) Why we expanded the test to 5 years

One month is not a backtest. It’s a screenshot.

A 5-year window gives you exposure to multiple regimes:

  • strong bull phases
  • range periods
  • high-volatility shock events
  • low-volatility grinding markets

This matters because RSI mean-reversion strategies are extremely regime-sensitive. They can look decent in range markets and collapse in persistent trends.

Expanding the sample does not “make the strategy better”. It makes the measurement more truthful.

 

3) Long-only on S&P 500: a realistic constraint (with a hidden risk)

S&P 500 has a long-term upward drift. Shorting it systematically is structurally harder.

So we applied a pragmatic constraint: buy only, never sell.

This has two advantages:

  • It aligns the strategy with the instrument’s structural behavior.
  • It removes the most fragile side of RSI mean-reversion (shorting into bull regimes).

But it also introduces a subtle risk:

Long-only performance can be “borrowed” from the market drift. If results improve, you must still ask: is it RSI adding edge, or is the market doing the heavy lifting?

This is why professional research always compares a strategy against baselines (something we’ll formalize in a later part).

 

RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG

 

4) Changing RR from 1:1 to 1:2

It’s tempting to assume that higher reward-to-risk ratios automatically improve strategies. In reality:

  • If the entry has no edge, RR changes don’t fix it.
  • Mean-reversion strategies often produce short bounces, not long extensions.

So testing 1:2 is not about “making it profitable”. It’s about learning the strategy’s behavior.

In our later sweep (Step 7) we will see something important: the best RR tends to move back toward 1:1, which is consistent with a short-horizon mean-reversion mechanism.

 

RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG | RR 1:2
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG | RR 1:2

 

5) Session filtering: trading only when NYSE is open

This is where research becomes contextual.

When we look at the MT5 report (trade distribution by hour + volatility), we notice something consistent:

  • The highest trade activity and volatility cluster occurs between 16:00 and 19:00 (server time).
  • This matches the period when the US market is open.

Important detail: MT5 “server time” is not UTC

In this case, the broker is Darwinex MT4/MT5 and charts are set to “New York close”.

That means:

  • During US DST, server time behaves like GMT+3
  • Outside US DST, server time behaves like GMT+2

So the 16–19 server window maps roughly to:

  • 15–18 Rome/Madrid time
  • 09–13 New York time

For simplicity and operational convenience, we choose to trade only in the 10–12 NY window (aligned with NYSE open activity).

Engineering note: time filters can easily create false results if DST is not handled consistently. This is one of the most common sources of silent overfitting.

RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | SESSION FILTER
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | SESSION FILTER
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | SESSION FILTER
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | SESSION FILTER

 

6) RSI oversold optimization sweep (10 → 100): what we learned

We ran a parameter sweep on the oversold threshold with step 10:

  • 10, 20, 30, 40, … 100

The results were extremely informative:

  • At 100, the strategy produces no trades (correct and expected).
  • As we decrease oversold, the strategy enters more often.
  • As trade frequency increases, results get worse.

This is a strong diagnostic signal.

If “more signals” makes it worse, the underlying concept is likely not profitable. You are not filtering noise — you are letting more noise in.

The “oversold = 80” paradox (and why it screams overfitting)

At some point, we noticed something strange: oversold values around 80 could look profitable.

But that is conceptually wrong. RSI 80 is not oversold — it is typically overbought territory.

This is the moment where many traders make a catastrophic mistake:

  • They see a green curve.
  • They accept the parameter.
  • They convince themselves the strategy is “working”.

In reality, this is a classic case of parameter mining.

When an “absurd parameter” improves performance, it is rarely edge. It is a degree of freedom adapting to past noise.

We’ll go deeper into this in the next article, but it’s worth stating clearly:

Overfitting is the #1 enemy of quantitative traders.

 

RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG | RSI OPTIMIZATION
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG | RSI OPTIMIZATION

 

7) SL/TP optimization sweep: why 1:1 becomes “optimal”

We also ran a sweep optimization on stop and target values.

The result was consistent: the best-performing region tends to return to 1:1.

This is important for interpretation:

  • It suggests the system (if it has any signal at all) is not capturing large directional moves.
  • It is capturing short bounces (typical mean-reversion behavior).

But we must avoid a common trap:

“Optimal in-sample” is not a solution. It is a measurement of sensitivity.

Professional research does not search for the best number. It searches for stable regions that remain stable across time and regimes.

 

RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG | RSI OPTIMIZATION
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG | RR OPTIMIZATION

 

8) The missing piece: RSI doesn’t tell us where we are buying

At this point, we hit a key conceptual limitation.

RSI can tell you something about momentum exhaustion, but it does not explicitly tell you where price is relative to the day’s distribution.

In other words:

  • RSI can be oversold in a “normal” pullback.
  • RSI can be oversold in a violent dislocation.
  • Those two situations are not the same trade.

So we introduce a context filter.

 

Introducing a Standard Deviation (stdev) filter: buying only in statistical dislocations

We add a rule:

Only buy if price is X standard deviations below the open.

The value X is not guessed. It is provided via console input and pre-calculated using an external Python tool.

What “standard deviation from the open” means

Standard deviation is a measure of typical dispersion.

In trading terms, it answers:

  • How far does price usually move away from the open?
  • When is the move statistically “extreme”?

So if we say:

  • X = 1.0 → price is ~1 stdev below the open (uncommon move)
  • X = 2.0 → price is ~2 stdev below the open (rare move)

This turns the strategy into something more meaningful:

  • RSI provides a momentum exhaustion signal.
  • Stdev provides a location/context signal.

Instead of buying every RSI oversold, we buy only when the market is statistically stretched.

Why this filter is logically consistent

This is not a random extra condition. It is an attempt to transform a naive mean-reversion rule into a contextual mean-reversion rule:

  • not “buy oversold”
  • but “buy oversold when the move is extreme relative to its distribution”

This is exactly the kind of improvement that tends to reduce noise trades and increase robustness.

Lab takeaway: most profitable mean-reversion systems are not “indicator systems”. They are dislocation systems.

A warning: this can still overfit if done incorrectly

Even though the logic is sound, a stdev threshold can still be overfit if:

  • X is optimized too aggressively
  • the calculation is not stable across regimes
  • the distribution changes over time (which it does)

So the goal is not to find the perfect number. The goal is to define a robust region and validate it out-of-sample.

 

RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG | SIGMA FILTER
RSI SAMPLE STRATEGY | TRADING ENGINEERING LAB | 2021 TO 2026 | SP500 | ONLY LONG | SIGMA FILTER

 

What Part 3 teaches (and what it does not)

Part 3 teaches:

  • how to reduce noise (M5)
  • why longer samples matter (5 years)
  • how to align strategy direction with the asset (long-only)
  • how to use session filters logically (NYSE open)
  • how optimization reveals fragility and overfitting
  • why adding context (stdev) is a real conceptual upgrade

Part 3 does NOT prove:

  • that RSI works
  • that the system is profitable
  • that the optimized parameters are valid

It proves something more valuable:

It proves that we are now testing like engineers, not like gamblers.

 

Part 4 preview: continuing optimization (without falling into curve fitting)

In Part 4 we’ll continue with additional improvements and optimizations, but with one clear objective:

We want to improve structure and robustness — not chase the best backtest.

We will focus on:

  • refining the session and context filters
  • testing parameter sensitivity (stability regions vs single “best” values)
  • running deeper robustness checks across time and market conditions
  • starting a more controlled optimization workflow

The goal is to turn this into a research process that is repeatable, falsifiable, and closer to professional standards.

 

Final note: a necessary warning

Everything you’ve seen in this series so far is intentionally simple.

These are practical examples designed to teach the workflow: how to prototype, port, test, and interpret results like an engineer.

But we also need to be honest:

The strategy we are using (basic RSI mean-reversion) is a very weak strategy.

It is not meant to be traded as-is, and it is not meant to represent a “real edge”. It is simply a clean sandbox to demonstrate how research works.

If we see enough interest, we may decide to replicate this exact style of analysis again — but starting from a much more powerful strategy concept, so the process becomes not only educational, but also closer to what serious systematic traders actually build.

The Trading Engineer
The Trading Engineer
For over five years I’ve used trading as a real laboratory: I design rule-based strategies, test them on real data with code, bots, and tools I build myself, and deploy them on funded accounts with a verifiable track record. I hold three technical degrees, all completed with top grades, and I bring the same rigor into the markets. I founded Trading Engineering Lab to document this work transparently: no shortcuts, no easy promises, no “signals” or improvised courses—just numbers, journaling, rigorous analysis, and continuous system improvement. I write about systematic trading, risk management, statistical validation of strategies, and how discipline in trading and in life shapes performance. For me, trading is an experiment, not a lottery ticket. If you’re looking for hype, I’m probably not the right fit. If you’re interested in models, data, and process, welcome to the lab.

Artículos relacionados

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To stay up to date with the latest news, offers and special announcements.

I have read and agree to the terms of use page and privacy policy provided in accordance with Article 13 of EU Regulation 679/2016 and I consent to email marketing purposes.

Trending Now