Strategy vault / Trend following
Moving Average Crossover Trend Following Strategy 483523
This strategy is a trading system based on moving average crossovers, supporting both EMA and SMA types of moving averages and provides optimized preset parameters for multiple timeframes including 1-hour, 4-hour…

Overview
This strategy is a trading system based on moving average crossovers, supporting both EMA and SMA types of moving averages and provides optimized preset parameters for multiple timeframes including 1-hour, 4-hour, daily, weekly, and bi-weekly. The system generates trading signals through the crossover of fast and slow moving averages and offers visualized price range filling effects.
Strategy Principle
The core of the strategy is to identify potential trend changes by monitoring crossovers between fast and slow moving averages. A long signal is generated when the fast moving average crosses above the slow moving average, while a short signal is generated when the fast moving average crosses below the slow moving average. The strategy offers three trading modes: long-only, short-only, and bi-directional trading. The optimal parameter combinations show that different timeframes require different moving average parameters and types.
Strategy Advantages
- Scientific Parameter Optimization: Parameters optimized through historical data analysis for different timeframes
- High Flexibility: Supports custom parameter settings, allowing adjustment of moving average lengths and types based on market conditions
- Visual Intuitiveness: Clear trend visualization through color-filled areas distinguishing bullish and bearish trends
- Multi-timeframe Applicability: Provides specially optimized parameters for different timeframes
- Complete Information Display: Real-time display of current strategy settings and parameters through an information panel
Strategy Risks
- Lag Risk: Moving averages are inherently lagging indicators, potentially causing delays in fast-moving markets
- Ineffective in Ranging Markets: Frequent crossover signals in sideways markets may lead to consecutive losses
- Parameter Dependency: Although optimized parameters are provided, adjustments may be needed based on specific market conditions
- Market Environment Changes: Parameters optimized based on historical data may become ineffective when future market conditions change
Strategy Optimization Directions
- Add Trend Filters: Incorporate trend indicators like ADX to execute trades only during strong trends
- Introduce Volatility Adjustment: Dynamically adjust moving average parameters based on market volatility
- Optimize Stop Loss Mechanism: Implement dynamic stop loss positions using ATR
- Add Volume Confirmation: Incorporate volume analysis when generating signals to improve reliability
- Develop Adaptive Parameters: Research and develop a parameter system that automatically adjusts based on market conditions
Summary
This is a rigorously optimized moving average crossover strategy applicable to multiple timeframes. Through scientific parameter optimization and flexible configuration options, the strategy provides traders with a reliable trend-following tool. While there are some inherent risks, the suggested optimization directions can further enhance the strategy's stability and reliability. The strategy's design philosophy combines classical technical analysis methods with modern quantitative analysis tools to provide traders with a trading system that is both simple to use and rigorously validated.
Source Pine Script
/*backtest
start: 2024-07-12 00:00:00
end: 2025-02-22 08:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/
//@version=5
strategy("MA Crossover [ClémentCrypto]", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000,process_orders_on_close=true)
// Groupe pour le choix entre preset et personnalisé
usePreset = input.bool(title="Utiliser Preset", defval=true, group="Mode Selection")
// Inputs pour la stratégie
timeframeChoice = input.string(title="Timeframe Preset", defval="1H", options=["1H", "4H", "1D", "1W", "2W"], group="Preset Settings")
tradeDirection = input.string(title="Trading Direction", defval="Long Only", options=["Long Only", "Short Only", "Both Directions"], group="Strategy Settings")
// Paramètres personnalisés MA
customFastLength = input.int(title="Custom Fast MA Length", defval=23, minval=1, group="Custom MA Settings")
customSlowLength = input.int(title="Custom Slow MA Length", defval=395, minval=1, group="Custom MA Settings")
customMAType = input.string(title="Custom MA Type", defval="EMA", options=["SMA", "EMA"], group="Custom MA Settings")
// Paramètres MA optimisés pour chaque timeframe
var int fastLength = 0
var int slowLength = 0
var string maType = ""
if usePreset
if timeframeChoice == "1H"
fastLength := 23
slowLength := 395
maType := "EMA"
else if timeframeChoice == "4H"
fastLength := 41
slowLength := 263
maType := "SMA"
else if timeframeChoice == "1D"
fastLength := 8
slowLength := 44
maType := "SMA"
else if timeframeChoice == "1W"
fastLength := 32
slowLength := 38
maType := "SMA"
else if timeframeChoice == "2W"
fastLength := 17
slowLength := 20
maType := "SMA"
else
fastLength := customFastLength
slowLength := customSlowLength
maType := customMAType
// Calcul des moyennes mobiles
fastMA = maType == "SMA" ? ta.sma(close, fastLength) : ta.ema(close, fastLength)
slowMA = maType == "SMA" ? ta.sma(close, slowLength) : ta.ema(close, slowLength)
// Conditions de trading simplifiées
longEntier = ta.crossover(fastMA, slowMA)
longExit = ta.crossunder(fastMA, slowMA)
shortEntier = ta.crossunder(fastMA, slowMA)
shortExit = ta.crossover(fastMA, slowMA)
// Définition des couleurs
var BULL_COLOR = color.new(#00ff9f, 20)
var BEAR_COLOR = color.new(#ff0062, 20)
var BULL_COLOR_LIGHT = color.new(#00ff9f, 90)
var BEAR_COLOR_LIGHT = color.new(#ff0062, 90)
// Couleurs des lignes MA
fastMAColor = fastMA > slowMA ? BULL_COLOR : BEAR_COLOR
slowMAColor = color.new(#FF6D00, 60)
// Gestion des positions
if tradeDirection == "Long Only"
if (longEntier)
strategy.entry("Long", strategy.long)
if (longExit)
strategy.close("Long")
else if tradeDirection == "Short Only"
if (shortEntier)
strategy.entry("Short", strategy.short)
if (shortExit)
strategy.close("Short")
else if tradeDirection == "Both Directions"
if (longEntier)
strategy.entry("Long", strategy.long)
if (longExit)
strategy.close("Long")
if (shortEntier)
strategy.entry("Short", strategy.short)
if (shortExit)
strategy.close("Short")
// Plots
var fastMAplot = plot(fastMA, "Fast MA", color=fastMAColor, linewidth=2)
var slowMAplot = plot(slowMA, "Slow MA", color=slowMAColor, linewidth=1)
fill(fastMAplot, slowMAplot, color=fastMA > slowMA ? BULL_COLOR_LIGHT : BEAR_COLOR_LIGHT)
// Barres colorées
barcolor(fastMA > slowMA ? color.new(BULL_COLOR, 90) : color.new(BEAR_COLOR, 90))Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-24. 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/Moving-Average-Crossover-Trend-Following-Strategy-483523.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
- Multi Factor Trend Price Action Strategy with Dynamic Risk Management SystemPine Script · Trend following · 2h
- DPO EMA Trend Crossover Quantitative Strategy ResearchPine Script · Trend following · 1d
- Dual EMA Crossover Trend Following Strategy with ATR and ADX FiltersPine Script · Trend following · 1d
- Advanced Multi Indicator Trend Confirmation Trading StrategyPine Script · Trend following · 1d
- Advanced Quantitative Trading Multi Moving Average Crossover System with Volume Filter StrategyPine Script · Trend following · 1d
- Multi Factor Trend Tracking Dynamic Risk Management Stock Trading StrategyPine Script · Trend following · 1d