Strategy vault / Mean reversion
Dual Moving Average Strategy
This strategy employs dual moving averages, specifically 8-period and 21-period ones. It generates long signals when the shorter MA crosses over the longer one, and short signals when the shorter MA crosses below the…

Overview
This strategy employs dual moving averages, specifically 8-period and 21-period ones. It generates long signals when the shorter MA crosses over the longer one, and short signals when the shorter MA crosses below the longer one.
The strategy also incorporates the slope of the moving average line to filter out some non-trending periods and only produce signals when a trend is more apparent.
Principles
The core of this strategy lies in the crossover of the short-term and long-term moving averages. The shorter MA can capture trend changes faster, while the longer MA has better noise filtering effects. The establishment of an uptrend is suggested when the shorter MA crosses over the longer MA, leading to a long signal; the establishment of a downtrend is suggested when the shorter MA crosses below the longer MA, leading to a short signal.
The strategy also sets a slope threshold. Only when the slope is greater than the positive threshold value will a long signal be generated. Only when the slope is less than the negative threshold value will a short signal be generated. This helps filter out zones where no pronounced trend exists, resulting in trading signals of higher quality.
Specifically, the logic for generating trading signals is:
- Calculate the 8-period and 21-period simple moving averages
- Detect crossover signals between the two
- Calculate the slope of the 21-period moving average line using the arctangent function atan
- Only generate long signals when the slope exceeds a preset positive threshold
- Only generate short signals when the slope falls below a preset negative threshold
Advantage Analysis
The advantages of this strategy include:
- The strategy idea is simple and easy to understand/implement
- Incorporating slope index helps filter out non-trending periods and improves signal quality
- Employing dual moving averages allows both to play to their strengths, improving robustness
- Parameters can be adjusted to suit different trading instruments
- Simple program implementation facilitates further optimization
Risk Analysis
Some risks also exist with this strategy:
- More false signals may occur during violent market fluctuations
- Crossover itself tends to produce some false signals
- There is some degree of lag, unable to instantly capture trend reversals
Some ways to optimize based on these risks:
- Adjust MA parameters to suit market characteristics
- Optimize slope threshold to improve robustness
- Add stop loss mechanisms to control single loss
- Incorporate other indicators to filter signals
- Employ adaptive parameter setting to improve robustness
Optimization Directions
Some directions for optimizing the strategy:
- Employ adaptive MAs, adjusting parameters based on volatility
- Incorporate volume analysis to avoid errors during consolidation
- Add volatility index to enhance quality and timeliness
- Add machine learning for automatic parameter optimization
- Leverage deep learning to uncover more complex nonlinear patterns
Conclusion
In summary, this dual MA strategy is simple and practical. By capturing different trend characteristics through the two period parameters and combining them to generate trading signals. Meanwhile, incorporating the slope threshold improves signal quality. This strategy can serve as a basic one for extensions, with ample optimization space and potential.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 7 | Angle |
| v_input_2 | 2 | Angle Period |
| v_input_3 | 10 | ATR Period |
| v_input_4 | 6 | Angle Level |
| v_input_5_close | 0 | MA Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4 |
Source Pine Script
/*backtest
start: 2024-01-09 00:00:00
end: 2024-01-16 00:00:00
period: 10m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//written by sixpathssenin
//@version=4
strategy(title="Dual Moving Average",initial_capital=10000,overlay=true)
ma1= sma(close,8)
ma2= sma(close,21)
angleCriteria = input(title="Angle", type=input.integer, defval=7, minval=1, maxval=13)
i_lookback = input(2, "Angle Period", input.integer, minval = 1)
i_atrPeriod = input(10, "ATR Period", input.integer, minval = 1)
i_angleLevel = input(6, "Angle Level", input.integer, minval = 1)
i_maSource = input(close, "MA Source", input.source)
f_angle(_src, _lookback, _atrPeriod) =>
rad2degree = 180 / 3.141592653589793238462643 //pi
ang = rad2degree * atan((_src[0] - _src[_lookback]) / atr(_atrPeriod)/_lookback)
ang
_angle = f_angle(ma2, i_lookback, i_atrPeriod)
plot(ma1,color=#FF0000)
plot(ma2,color=#00FF00)
crosso=crossover(ma1,ma2)
crossu=crossunder(ma1,ma2)
_lookback = 15
f_somethingHappened(_cond, _lookback) =>
bool _crossed = false
for i = 1 to _lookback
if _cond[i]
_crossed := true
_crossed
longcrossed = f_somethingHappened(crosso,_lookback)
shortcrossed = f_somethingHappened(crossu,_lookback)
long = longcrossed and _angle > angleCriteria
short= shortcrossed and _angle < -(angleCriteria)
if(long)
strategy.entry("Long",strategy.long)
if(short)
strategy.entry("short",strategy.short)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-17. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Dual-Moving-Average-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Adaptive Trading Strategy Combining Dynamic Trend Reversal and Stochastic IndicatorPine Script · Mean reversion · 1d
- Advanced Momentum EMA Trend Following with RSI Overbought Oversold Trading StrategyPine Script · Mean reversion · 1d
- Advanced Quantitative Trend Following and Cloud Reversal Composite Trading StrategyPine Script · Mean reversion · 1d
- Advanced Strategy with Volume and Price Pullback Multiple Take ProfitPine Script · Mean reversion · 1h
- Bidirectional Moving Average Reversion Trading StrategyPine Script · Mean reversion · 1h
- Bollinger Bands ATR Risk Reward Trading StrategyPine Script · Mean reversion · 1d