Strategy vault / Trend following
Dynamic Multi Period Trend Prediction with Moving Average Filter Strategy
This strategy is a trend following system that combines traditional technical analysis with modern artificial intelligence methods. It primarily uses Exponential Moving Average (EMA) and Simple Moving Average (SMA)…

Overview
This strategy is a trend following system that combines traditional technical analysis with modern artificial intelligence methods. It primarily uses Exponential Moving Average (EMA) and Simple Moving Average (SMA) as trend filters, while incorporating a prediction model to optimize entry timing. The strategy is specifically optimized for daily timeframes, aiming to capture medium to long-term market trends.
Strategy Principles
The core logic consists of three main components:
- Trend Determination System - Uses 200-period EMA and SMA as primary trend filters, determining current trend direction through price-to-moving average relationships
- Prediction Module - Employs an expandable prediction component, currently using simulated predictions, with the capability to be replaced by machine learning models
- Position Management - Sets a fixed 4-bar holding period to control position duration and risk
Trade signals require consistency between trend direction and prediction signals:
- Long signals: Price above both EMA and SMA, with positive prediction value
- Short signals: Price below both EMA and SMA, with negative prediction value
Strategy Advantages
- Clear Structure - Simple and intuitive strategy logic, easy to understand and maintain
- Controlled Risk - Effective risk control through fixed holding periods and dual moving average filters
- High Scalability - Flexible prediction module design, capable of integrating different prediction models
- Good Adaptability - Adjustable parameters to adapt to different market environments
- Moderate Trading Frequency - Daily timeframe operations reduce trading costs and psychological pressure
Strategy Risks
- Trend Reversal Risk - Potential consecutive losses at trend turning points
- Parameter Sensitivity - Moving average and holding period selections significantly impact strategy performance
- Model Dependency - Prediction module accuracy directly affects strategy effectiveness
- Slippage Impact - Daily timeframe operations may face significant slippage
- Market Environment Dependency - May underperform in ranging markets
Strategy Optimization Directions
- Prediction Model Upgrade - Introduce machine learning models to replace existing random predictions
- Dynamic Holding Period - Adjust holding time based on market volatility
- Stop-Loss Optimization - Add dynamic stop-loss mechanisms to improve risk control
- Position Management - Introduce volatility-based position management system
- Multi-dimensional Filtering - Add volume, volatility, and other auxiliary indicators
Summary
This strategy builds a robust trend following system by combining traditional technical analysis with modern prediction methods. Its main advantages lie in clear logic, controlled risk, and strong scalability. Through strategy optimization, particularly in prediction models and risk control improvements, it has the potential to further enhance stability and profitability. The strategy is suitable for investors seeking medium to long-term stable returns.
Source Pine Script
/*backtest
start: 2024-02-21 00:00:00
end: 2025-02-18 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("My Strategy", overlay=true)
// Parameters (adjust as needed)
neighborsCount = 8
maxBarsBack = 2000
featureCount = 5
useDynamicExits = true
useEmaFilter = true
emaPeriod = 200
useSmaFilter = true
smaPeriod = 200
// Moving Average Calculations
ema = ta.ema(close, emaPeriod)
sma = ta.sma(close, smaPeriod)
// Trend Conditions
isEmaUptrend = close > ema
isEmaDowntrend = close < ema
isSmaUptrend = close > sma
isSmaDowntrend = close < sma
// Model Prediction (Replace with your real model)
// Here a simulation is used, replace it with real predictions
prediction = math.random() * 2 - 1 // Random value between -1 and 1
// Entry Signals
isNewBuySignal = prediction > 0 and isEmaUptrend and isSmaUptrend
isNewSellSignal = prediction < 0 and isEmaDowntrend and isSmaDowntrend
// Exit Signals
var int barsHeld = 0
var bool in_position = false
var int entry_bar = 0
if isNewBuySignal and not in_position
in_position := true
entry_bar := bar_index
barsHeld := 1
else if isNewSellSignal and not in_position
in_position := true
entry_bar := bar_index
barsHeld := 1
else if in_position
barsHeld := barsHeld + 1
if barsHeld == 4
in_position := false
endLongTradeStrict = barsHeld == 4 and isNewBuySignal[1]
endShortTradeStrict = barsHeld == 4 and isNewSellSignal[1]
// Backtest Logic
var float totalProfit = 0
var float entryPrice = na
var int tradeDirection = 0
if isNewBuySignal and tradeDirection <= 0
entryPrice := close
tradeDirection := 1
strategy.entry("Long", strategy.long)
if isNewSellSignal and tradeDirection >= 0
entryPrice := close
tradeDirection := -1
strategy.entry("Short", strategy.short)
if (endLongTradeStrict and tradeDirection == 1) or (endShortTradeStrict and tradeDirection == -1)
exitPrice = close
profit = (exitPrice - entryPrice) / entryPrice
if tradeDirection == -1
profit := (entryPrice - exitPrice) / entryPrice
totalProfit := totalProfit + profit
tradeDirection := 0
strategy.close_all()
plot(close, color=color.blue)
plot(ema, color=color.orange)
plot(sma, color=color.purple)
Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-27. 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/Dynamic-Multi-Period-Trend-Prediction-with-Moving-Average-Filter-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
- ADX MA and EMA Long Only Trend Tracking StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Bollinger Band Moving Average Crossover StrategyPine Script · Trend following · 1h
- Bollinger Bands and EMA Trend Following StrategyPine Script · Trend following · 1h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Candlestick Pattern Trend Filter StrategyPine Script · Trend following · 1d