D.SHOGBON
// CODE · SYSTEMS . MATH · LIFE NOTES
PUSHING INTELLIGENT AUTOMATION
ESSAY 1 MIN

Building fundamental mean-reversion trading signal

This is my approach to creating a mean reversion strategy using the 20 day Simple Moving Average Z-Score.

DATE 2026.09.19
CATEGORY ESSAY
READ TIME 1 MIN

Code: https://github.com/dashn9/learning_quant/blob/main/src/lessons/mean_reversion.rs

Under the principle that asset prices(though not guaranteed) will eventually snap back to their previous N(20 days in this case) average.

For example, say the SPY500, as a result of an outlier event, went up 20% from $700(Simple Moving Average) to $840, with a volatility(std dev) of 1.4. With a z-score of 100, an entry point with a trigger of -2 < z-score > 2 should trigger a short of this asset.

Formulas:

Standard Deviation / Volatility: σ = √[ Σ(return - average return)² / (n - 1) ] using Bessel’s correction (Closing prices were used in our case)

SMA: SMA₂₀ = (P₁ + P₂ + P₃ + ... + P₂₀) / 20

Z Score: Z = (P − SMA₂₀) / σ₂₀

Steps of operation:

You take the historical price of a given asset within a set duration N (In our example, we used 20).

You take the moving average, standard deviation, and then the z-score.

Compare the current close via the z-score with that baseline.

Compare the rule and decide whether to LONG, SHORT, or NEUTRAL.

Close the trade the following day(What I did)

Buy operations are the writer’s choice.

The strategy was executed on the SPY US500, and these were the results:

Evaluated days: 230Long entries: 13Short entries: 38Winning trades: 29Losing trades: 22Flat trades: 0Cumulative gross P&L (1 share per trade): +3.3299 USD

Note: These are notes from my learning journey; please do not implement the strategy with real money unless you know what you are doing.

// HAND-BUILT · NO TRACKING · WEIGHT < 80KB · LAST TOUCHED 2026-04-23