Strategy vault / Trend following
Dual Moving Average Crossover Dynamic Position Optimization Strategy with EMA Cross Signal System
This strategy is an automated trading system based on Exponential Moving Average (EMA) crossover signals. It utilizes the crossover relationship between 12-day and 25-day EMA lines to generate buy and sell signals…

Overview
This strategy is an automated trading system based on Exponential Moving Average (EMA) crossover signals. It utilizes the crossover relationship between 12-day and 25-day EMA lines to generate buy and sell signals, while automatically optimizing position switching based on current position status. This is an improved version of the traditional dual moving average strategy with enhanced dynamic position management capabilities.
Strategy Principles
The core logic of the strategy is based on the following key elements:
- Uses shorter-period (12-day) and longer-period (25-day) exponential moving averages as primary technical indicators
- Detects market trend reversal points through EMA line crossovers
- Generates long signals when 12-day EMA crosses above 25-day EMA (Golden Cross)
- Generates short signals when 12-day EMA crosses below 25-day EMA (Death Cross)
- Automatically checks current position status and optimizes position transitions based on new crossover signals
Strategy Advantages
- Stable and reliable signal system: EMA-based crossover signals respond more quickly to market changes compared to simple moving averages
- Intelligent position management: System automatically detects current position status and ensures optimal position transitions when signals appear
- Comprehensive risk control: Strategy includes complete stop-loss and position closing mechanisms
- Outstanding visualization: Clearly marks buy and sell signal points on charts for easy trader understanding and tracking
- Clear code structure: Facilitates subsequent strategy optimization and parameter adjustment
Strategy Risks
- Choppy market risk: May generate frequent false breakout signals in sideways markets
- Slippage risk: May face significant price execution deviation from signal prices in low-volume markets
- Trend delay risk: Due to moving average system, signals will have some lag relative to market tops and bottoms
- Capital management risk: Without proper position control, may result in significant account losses during consecutive losses
- Technical risk: Algorithmic trading may be affected by network latency, system failures, and other technical factors
Strategy Optimization Directions
- Introduce volatility indicators: Can add ATR or Bollinger Bands to filter false breakout signals
- Optimize parameter selection: Can optimize EMA periods through backtesting to better suit specific markets
- Enhance position management: Can dynamically adjust position sizes based on market volatility
- Add stop-loss mechanisms: Can set trailing stops to protect existing profits
- Improve signal filtering: Can add volume, trend strength, and other auxiliary indicators to improve signal quality
Summary
This is a well-designed automated trading strategy with clear logic. By combining EMA crossover signals with intelligent position management, the strategy can effectively capture market trends and make timely position adjustments. While there are some inherent risks, the strategy has good practical value and room for expansion through reasonable optimization and risk control measures.
Source Pine Script
/*backtest
start: 2024-07-01 00:00:00
end: 2025-01-01 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
// This Pine Script™ Code Compliance Mozilla Public License 2.0 Terms https://mozilla.org/MPL/2.0/
// © pyoungil0842
//@version=6
strategy("EMAGold/Death Cross Bands optimized position switching", overlay=true, calc_on_every_tick=true)
// EMASettings
ema12 = ta.ema(close, 12)
ema25 = ta.ema(close, 25)
// Golden cross and death cross conditions
goldenCross = ta.crossover(ema12, ema25) // whenEMA12Cross UpwardsEMA25hour
deathCross = ta.crossunder(ema12, ema25) // whenEMA12Cross DownwardsEMA25hour
// Check current position status
isLong = strategy.position_size > 0 // Whether holding a long position
isShort = strategy.position_size < 0 // Whether holding a short position
// Handling when a golden cross occurs
if (goldenCross)
if (isShort) // If you hold a short position, close short and open long
strategy.close("Short") // Close out short positions
strategy.entry("Long", strategy.long) // Enter a long position
else if (not isLong) // If there is no long position, open a new long position
strategy.entry("Long", strategy.long)
// Handling when a death cross occurs
if (deathCross)
if (isLong) // If holding a long position, close the long and open a short
strategy.close("Long") // Close long positions
strategy.entry("Short", strategy.short) // Enter short positions
else if (not isShort) // If there are no short positions, open a new short position
strategy.entry("Short", strategy.short)
// Display on chartEMALine
plot(ema12, title="EMA 12", color=color.blue)
plot(ema25, title="EMA 25", color=color.orange)
// Show signals on the chart
plotshape(series=goldenCross, title="Golden Cross", location=location.belowbar, color=color.green, style=shape.labelup, text="Buy")
plotshape(series=deathCross, title="Death Cross", location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-20. 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-Crossover-Dynamic-Position-Optimization-Strategy-with-EMA-Cross-Signal-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
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- EMA Cross Trend Following Strategy with SMA Stop Loss and Re Entry MechanismPine Script · Trend following · 1h
- EMA and RSI Trend Trading StrategyPine Script · Trend following · 1d
- Adaptive Trend Following StrategyPine Script · Trend following · 4h
- Dual Trend Lines Intelligent Tracking BTC Investment StrategyPine Script · Trend following