0bps

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…

Author
ianzeng123
Complexity
Starter · 48 lines
Parameters
0
Backtest bar
1h · intraday
Tested on
ETH/USDT · spot

IMG IMG

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:

  1. Uses shorter-period (12-day) and longer-period (25-day) exponential moving averages as primary technical indicators
  2. Detects market trend reversal points through EMA line crossovers
  3. Generates long signals when 12-day EMA crosses above 25-day EMA (Golden Cross)
  4. Generates short signals when 12-day EMA crosses below 25-day EMA (Death Cross)
  5. Automatically checks current position status and optimizes position transitions based on new crossover signals

Strategy Advantages

  1. Stable and reliable signal system: EMA-based crossover signals respond more quickly to market changes compared to simple moving averages
  2. Intelligent position management: System automatically detects current position status and ensures optimal position transitions when signals appear
  3. Comprehensive risk control: Strategy includes complete stop-loss and position closing mechanisms
  4. Outstanding visualization: Clearly marks buy and sell signal points on charts for easy trader understanding and tracking
  5. Clear code structure: Facilitates subsequent strategy optimization and parameter adjustment

Strategy Risks

  1. Choppy market risk: May generate frequent false breakout signals in sideways markets
  2. Slippage risk: May face significant price execution deviation from signal prices in low-volume markets
  3. Trend delay risk: Due to moving average system, signals will have some lag relative to market tops and bottoms
  4. Capital management risk: Without proper position control, may result in significant account losses during consecutive losses
  5. Technical risk: Algorithmic trading may be affected by network latency, system failures, and other technical factors

Strategy Optimization Directions

  1. Introduce volatility indicators: Can add ATR or Bollinger Bands to filter false breakout signals
  2. Optimize parameter selection: Can optimize EMA periods through backtesting to better suit specific markets
  3. Enhance position management: Can dynamically adjust position sizes based on market volatility
  4. Add stop-loss mechanisms: Can set trailing stops to protect existing profits
  5. 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.

in Claude Code, Cursor, Codex & 4 more
/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
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open