Strategy vault / Trend following
trend following strategy based on moving averages 430658
The dual moving average crossover strategy is a trend-following strategy based on moving averages. It generates trading signals by calculating moving averages of different periods and identifying crossovers. This…

Overview
The dual moving average crossover strategy is a trend-following strategy based on moving averages. It generates trading signals by calculating moving averages of different periods and identifying crossovers. This strategy uses a fast moving average and a slow moving average to form signals. When the fast MA crosses above the slow MA, it takes a bullish stance and buys. When the fast MA crosses below the slow MA, it takes a bearish stance and sells.
Strategy Logic
This strategy mainly relies on MA crossovers to generate trading signals. Specifically, it involves the following steps:
Calculate the fast MA and slow MA. The fast MA period is 10, and the slow MA period is 50.
Identify MA relationships. A buy signal is generated when the fast MA crosses above the slow MA. A sell signal is generated when the fast MA crosses below the slow MA.
Issue buy and sell signals. Go long when a buy signal occurs. Go short when a sell signal occurs.
Set stop loss and take profit. After entering a trade, set the stop loss based on the input percentage and take profit to manage risks.
By comparing price trend changes across different timeframes, this strategy determines whether the market is currently in an uptrend or downtrend. It is a typical trend-following strategy. MAs filter out market noise and generate more reliable trading signals.
Advantages
Effectively captures mid to long-term trends using the inherent trend-following nature of MAs.
Simple and clear crossover signals that are easy to implement.
Customizable fast and slow periods for parameter optimization.
Limits losses on individual trades through stop loss.
Risks
Prone to whipsaws and overtrading in range-bound markets.
MAs have lag and may miss short-term opportunities.
Does not account for sudden events like significant bearish news.
Lacks risk management mechanisms and could lead to losses beyond risk tolerance.
Risk Management:
Optimize MA periods to reduce false signals during consolidations.
Add other indicators as filters to address MA lag.
Supplement with news analytics.
Implement stop loss and position sizing to limit losses.
Enhancements
Combine with other analytical tools like channels and patterns to improve signal quality.
Optimize fast and slow MA parameters to find best combinations. 10-30 days for fast MA and 20-120 days for slow MA often work well.
Add position sizing rules. Fixed fractional position sizing can improve profits in trends.
Incorporate logic to handle sudden events like halting trading after major bearish news.
Backtest and paper trade to evaluate performance and continuously improve the system.
Summary
The dual moving average crossover strategy identifies trend direction by comparing fast and slow MA crossovers. It is a simple and practical trend-following approach. While effective, it has some limitations that can be addressed through optimizations like parameter tuning, adding filters, and incorporating other tools. With appropriate risk control, this strategy can provide good returns.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 10 | Fast MA Length |
| v_input_2 | 50 | Slow MA Length |
| v_input_3 | true | Stop Loss Percentage |
Source Pine Script
/*backtest
start: 2023-09-30 00:00:00
end: 2023-10-30 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Simple Moving Average Crossover", overlay=true)
// Input parameters
fast_length = input(10, title="Fast MA Length")
slow_length = input(50, title="Slow MA Length")
stop_loss_pct = input(1, title="Stop Loss Percentage", minval=0, maxval=5) / 100
// Calculate moving averages
fast_ma = sma(close, fast_length)
slow_ma = sma(close, slow_length)
// Plot moving averages
plot(fast_ma, color=color.blue, title="Fast MA")
plot(slow_ma, color=color.red, title="Slow MA")
// Strategy logic
long_condition = crossover(fast_ma, slow_ma)
short_condition = crossunder(fast_ma, slow_ma)
// Execute trades
if (long_condition)
strategy.entry("Long", strategy.long)
if (short_condition)
strategy.entry("Short", strategy.short)
// Set stop loss
long_stop_price = close * (1 - stop_loss_pct)
short_stop_price = close * (1 + stop_loss_pct)
strategy.exit("Stop Loss/Profit", from_entry="Long", stop=long_stop_price)
strategy.exit("Stop Loss/Profit", from_entry="Short", stop=short_stop_price)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-31. 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/trend-following-strategy-based-on-moving-averages-430658.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
- ADX MA and EMA Long Only Trend Tracking StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Adaptive FVG Detection and MA Trend Trading Strategy with Dynamic ResistancePine Script · Trend following · 1d
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m