Disclaimer: This article is for educational and informational purposes only. It does not constitute financial or investment advice. Trading forex and CFDs carries significant risk of loss. Past performance of any strategy — including backtests — does not guarantee future results. Never trade with money you cannot afford to lose.
What Is This Strategy?
The Damped Oscillator Regime Shift is an adaptive, physics-inspired trading strategy for MetaTrader 5 that treats price as a moving physical body rather than as a chart of candlestick patterns. Its primary analytical tool is a least-squares regression that fits a damped linear oscillator — the same equation of motion that describes a weight on a spring — to recent log-price data every single bar. This places its trading style somewhere between mean-reversion and trend-following, because the strategy is designed to switch between the two automatically based on what the data measures.
At the heart of the approach is a simple idea from first principles: over any short horizon, price acceleration can be modelled as a combination of two forces — a position-feedback force (labelled A, a stiffness or "spring constant") and a velocity-feedback force (labelled B, a damping term). These two coefficients are not fixed by the trader. Instead, they are measured on every bar using ordinary least squares (a standard statistical method for fitting a line to data) over a rolling window of recent price moves. The sign of the fitted stiffness A tells the strategy whether the market is currently behaving like a stable spring that pulls price back to its centre (a mean-reverting regime) or an unstable one that pushes price further away (a trending regime).
As a learning tool, this strategy suits intermediate and advanced students who are curious about regime detection, adaptive systems, and quantitative modelling. It is a compelling case study in how the same market condition — price stretched far from its average — can justify opposite trades depending on the measured dynamics. It is not a "set and forget" template; it rewards those who want to understand why a system chooses to fade a move in one context and ride it in another.
How It Works
The strategy acts once per completed bar. On each new bar it rebuilds its statistics, re-fits the oscillator equation, and then decides whether to enter, hold, or exit. Here is the logic in plain English:
- Fit the equation of motion. Using the last
Windowprice samples, the strategy runs a two-variable regression on log-price to estimate coefficient A (position feedback / stiffness) and coefficient B (velocity feedback / damping). Log-price is simply the natural logarithm of the close, which keeps percentage moves comparable across price levels. - Measure standardised displacement. It calculates d, how far the current price sits from its rolling mean, expressed in standard-deviation units (the same units used in a z-score). A d of +1.0 means price is one standard deviation above its centre.
- Classify the regime. If A is below
−DeadZone, the market is in a restoring (mean-reverting) regime. If A is above+DeadZone, it is in an explosive (trending) regime. If A sits inside the dead zone, the regime is treated as indecisive and no new trade is armed. - Restoring regime — fade the stretch. The strategy signals a long when d falls to or below
−EntryBand(price stretched below centre, spring expected to pull it up) and a short when d rises to or above+EntryBand(price stretched above centre). - Explosive regime — ride the stretch. The logic inverts. The strategy signals a long when d reaches
+EntryBand(price running up and away from centre) and a short when d reaches−EntryBand. - Damping gate. Before arming a fresh entry, the strategy checks coefficient B. If velocity feedback is strongly working against the intended direction, the signal is vetoed. B is used as a conviction filter, not as a trade trigger on its own.
- Stop-loss logic. When a position opens, the stop is placed at
AtrSlMult × ATRaway from entry. ATR (Average True Range) is a volatility measure, so the stop distance automatically widens in fast markets and tightens in calm ones. - Take-profit logic. The target is set at
AtrTpMult × ATRaway from entry, again scaling with live volatility. - Trailing and regime-flip exit. Open trades trail using a chandelier-style ATR stop that only ever tightens. Critically, if the fitted regime flips against an open position, the strategy closes it immediately — the physics changing sign is itself treated as an exit signal. Only one position per magic number is held at a time; the system never stacks trades.

Strategy Parameters
| Parameter | Default | Min | Max | Description |
|---|---|---|---|---|
| Window | 50 | 20 | 150 | Number of recent motion samples used to fit the equation of motion each bar. Larger values smooth the regime estimate but react more slowly. |
| EntryBand | 1.0 | 0.3 | 2.5 | Standardised displacement (in standard-deviation units) required to arm an entry. Higher values demand a more extreme stretch before trading. |
| DeadZone | 0.02 | 0.0 | 0.20 | Minimum absolute value of the fitted stiffness A for a regime to be considered decisive. Also gates the damping veto. |
| AtrPeriod | 14 | 7 | 30 | Number of bars used to calculate the ATR for stop, target, and trailing distances. |
| AtrSlMult | 2.0 | 1.0 | 4.0 | Stop-loss distance as a multiple of ATR. |
| AtrTpMult | 3.0 | 1.0 | 6.0 | Take-profit distance as a multiple of ATR. |
| Lots | 0.10 | 0.01 | 1.0 | Fixed position size in lots for each trade. |

Recommended Chart Settings
The Damped Oscillator Regime Shift is written to run on a single timeframe — whichever chart you attach it to becomes the timeframe it models, because every calculation uses the chart's own bars. A sensible starting point for study is a liquid major forex pair such as EUR/USD on the H1 (1-hour) timeframe, where there are enough bars per day to keep the rolling regression well-fed and spreads are typically tight.
That said, the whole premise of this strategy is adaptation, so it is worth observing across several symbols and timeframes to see how the measured regime behaves in different conditions. Trending currency pairs, ranging pairs, and higher-volatility instruments will each produce different mixes of restoring and explosive regimes. Remember that results will vary considerably across market conditions, and any settings that appear favourable in one period may not carry over to another.
How to Install on MetaTrader 5
- Download the .ex5 file from the link below
- Copy it to your MT5
MQL5\Expertsfolder - Restart MetaTrader 5 or refresh the Navigator panel
- Drag the EA onto a chart matching the recommended symbol and timeframe
- Configure the input parameters and enable Algo Trading
What to Consider Before Using This EA
Strengths. The most appealing quality of this approach is that it is structurally incapable of fighting the regime it has just measured. Because the same displacement produces opposite trades depending on the sign of the fitted stiffness, the strategy avoids one of the most common systematic errors: applying a mean-reversion rule inside a trend, or a breakout rule inside a range. It uses no indicator library and carries no fixed directional bias — it re-derives its behaviour from raw price on every bar. The ATR-based, volatility-adaptive risk framework and the regime-flip exit are also thoughtful touches that let positions breathe and cut them when the underlying dynamics change.
Limitations. A regression fitted to a short window is inherently noisy. The coefficients A and B can flicker around the dead-zone boundary, which may cause the strategy to hesitate or to flip regimes frequently in choppy, directionless markets — potentially producing whipsaw entries and exits. The model assumes local linear dynamics, an approximation that can break down around news events, gaps, or sudden volatility spikes. It also holds only one position at a time, so it will sit out other opportunities while a trade is open. As with any strategy that adapts to recent data, it may lag when the market's true character shifts faster than the window can capture. There are no assurances about how it will perform in live conditions; it should be treated as a framework for study and testing, not a finished solution.
Risk Management Tips
Sound risk management matters more than any single entry rule. Consider the following general principles as you study this or any automated strategy:
- Size positions to your account, not to the default. The built-in
Lotsvalue of 0.10 is a placeholder. Many educators suggest risking no more than 1–2% of account equity on any single trade, and adjusting lot size accordingly. - Test on a demo account first. Run the EA on a demo or simulated account for an extended period before considering any real capital, so you can observe how the regime logic behaves in conditions you have not backtested.
- Understand drawdown. Every strategy experiences losing streaks. Know the largest peak-to-trough decline you are willing to tolerate, and stop to reassess if it is reached.
- Respect the stop-loss. The ATR-based stops are integral to the design. Widening or removing them can dramatically change the risk profile.
- Diversify your study, not just your trades. Treat this EA as one data point in a broader learning process rather than a complete trading plan.
Risk Warning
Trading foreign exchange, CFDs, and other leveraged financial instruments involves substantial risk of loss and is not suitable for all investors. The strategies and tools discussed on this page are provided for educational purposes only and do not constitute financial advice, investment recommendations, or solicitation to trade. Always consult a qualified financial adviser before making trading decisions. Past backtest performance is not indicative of future results.
Downloads
- Expert Advisor: DampedOscillatorRegimeShift.ex5 (0 downloads)
- Source Code: DampedOscillatorRegimeShift.mq5 (0 downloads)
- Documentation: DampedOscillatorRegimeShift.pdf (0 downloads)