Strategy vault / Machine learning
Neural Moving Average Crossover Trend Following Strategy with EMA Filtering System
This strategy is a trading system based on moving average crossover signals and trend filtering. It combines short-term SMA crossover signals (9-period and 15-period) with a long-term EMA (200-period) as a trend…

Overview
This strategy is a trading system based on moving average crossover signals and trend filtering. It combines short-term SMA crossover signals (9-period and 15-period) with a long-term EMA (200-period) as a trend filter to capture market trends across different timeframes. The system also includes a re-entry mechanism that allows for position rebuilding during trend continuation.
Strategy Principles
The strategy employs a triple moving average system for trading decisions:
- Uses 9-period and 15-period Simple Moving Averages (SMA) for generating trading signals
- Employs 200-period Exponential Moving Average (EMA) as a trend filter
- Generates long signals when the short-term SMA (9-period) crosses above the 15-period SMA and price is above the 200-period EMA
- Generates short signals when the short-term SMA (9-period) crosses below the 15-period SMA and price is below the 200-period EMA
- Includes re-entry logic allowing new positions after initial crossover signals as long as price maintains position relative to the 200 EMA
Strategy Advantages
- Multiple timeframe analysis: Combines short-term and long-term moving averages for a comprehensive market perspective
- Trend filtering: Uses 200 EMA to filter false signals and improve trade quality
- Re-entry mechanism: Allows multiple entries during strong trends to maximize profit potential
- Clear entry/exit rules: Based on objective technical indicators, reducing subjective judgment
- Bi-directional trading: Profits from both bullish and bearish markets
- Integrated risk management: Automatic risk control through the moving average system
Strategy Risks
- Choppy market risk: May generate frequent false signals in sideways markets
- Lag risk: Moving averages are inherently lagging indicators, potentially missing optimal entry points
- Trend reversal risk: May suffer significant losses during sharp market reversals
- Re-entry risk: Excessive position building may lead to overexposure Mitigation measures:
- Add additional volatility filters
- Set maximum position limits
- Implement dynamic stop-loss mechanisms
- Deploy position sizing system
Strategy Optimization Directions
- Dynamic Period Optimization:
- Automatically adjust moving average periods based on market volatility
- Introduce Adaptive Moving Averages (AMA) to replace fixed-period ones
- Entry Optimization:
- Add volume confirmation
- Include momentum indicator validation
- Implement price pattern confirmation
- Risk Management Optimization:
- Implement dynamic position sizing
- Add trailing stops
- Develop volatility-based stop-loss settings
- Re-entry Logic Optimization:
- Add trend strength confirmation
- Design graduated position building system
- Incorporate market environment recognition
Summary
This strategy builds a comprehensive trend-following trading system by combining multiple moving average systems with trend filters. Its main advantage lies in capturing significant profits in trending markets while improving stability through moving average filtering and re-entry mechanisms. Although inherent risks exist, implementing the optimization directions can further enhance strategy performance. The strategy is suitable for tracking medium to long-term market trends and serves as a reliable trading tool for patient traders.
Source Pine Script
/*backtest
start: 2024-02-19 00:00:00
end: 2025-02-17 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("SMA Crossover with EMA Filter", overlay=true)
// Define indicators
sma9 = ta.sma(close, 9)
sma15 = ta.sma(close, 15)
ema200 = ta.ema(close, 200)
// Crossover conditions
bullish_crossover = ta.crossover(sma9, sma15) // Buy signal
bearish_crossover = ta.crossunder(sma9, sma15) // Sell signal
// Filters
above_ema200 = close > ema200
below_ema200 = close < ema200
// Buy condition (only above 200 EMA)
buy_signal = bullish_crossover and above_ema200
if buy_signal
strategy.entry("Buy", strategy.long)
// Sell condition (only below 200 EMA)
sell_signal = bearish_crossover and below_ema200
if sell_signal
strategy.entry("Sell", strategy.short)
// Exit condition if the signal reverses
exit_long = bearish_crossover
exit_short = bullish_crossover
if exit_long
strategy.close("Buy")
if exit_short
strategy.close("Sell")
// Re-entry condition when price crosses EMA 200 after a prior crossover
buy_reentry = ta.barssince(bullish_crossover) > 0 and above_ema200
sell_reentry = ta.barssince(bearish_crossover) > 0 and below_ema200
if buy_reentry
strategy.entry("Buy", strategy.long)
if sell_reentry
strategy.entry("Sell", strategy.short)
// Plot indicators
plot(sma9, color=color.blue, title="SMA 9")
plot(sma15, color=color.red, title="SMA 15")
plot(ema200, color=color.orange, title="EMA 200")
Published by ChaoZhang on the FMZ strategy square · last modified 2025-02-18. 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/Neural-Moving-Average-Crossover-Trend-Following-Strategy-with-EMA-Filtering-System.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
- Bull Bear Power Trading Strategy with Volume Percentile Based Dynamic Take Profit SystemPine Script · Machine learning · 1d
- Multi indicator Quant Trading StrategyPine Script · Machine learning · 1h
- Multi Dynamic Indicator Combination StrategyPine Script · Machine learning · 2d
- Dual Pressure Quantitative Trading StrategyPine Script · Machine learning · 1h
- Multi indicator Combination StrategyPine Script · Machine learning · 1d
- Liquidity Dynamic Risk Management Multi Factor Trading StrategyPine Script · Machine learning · 1h