Strategy vault / Trend following
Adaptive EMA Crossover Dynamic Position Risk Management Strategy
This strategy is a trading system based on medium and long-term Exponential Moving Average (EMA) crossovers, incorporating dynamic position management and risk control mechanisms. The strategy identifies market…

Overview
This strategy is a trading system based on medium and long-term Exponential Moving Average (EMA) crossovers, incorporating dynamic position management and risk control mechanisms. The strategy identifies market trends through the crossover of 21-period and 55-period EMAs while dynamically adjusting position sizes based on user-defined risk-reward ratios and risk percentages, achieving precise risk control.
Strategy Principles
The core logic is built on EMA crossover signals from two time periods. When the 21-period EMA crosses above the 55-period EMA, the system identifies an uptrend and triggers a long signal; when the 21-period EMA crosses below the 55-period EMA, it identifies a downtrend and triggers a short signal. Stop-loss levels are set at the lowest point (for longs) or highest point (for shorts) of the past two candles, while take-profit levels are dynamically calculated based on the user-defined risk-reward ratio. Position sizing is dynamically calculated based on total account equity, risk percentage, and current stop-loss distance, ensuring risk control within preset parameters.
Strategy Advantages
- Dynamic Risk Management: Position sizes are dynamically calculated to ensure strict risk control within set percentage parameters.
- High Adaptability: EMA indicators adapt to market volatility, reducing false signals.
- Adjustable Risk-Reward Ratio: Users can set risk-reward ratios according to their risk preferences.
- Scientific Position Management: Positions are dynamically adjusted based on account size and risk distance, avoiding excessive leverage.
- Fully Automated Operation: Strategy can run 24/7 without manual intervention.
Strategy Risks
- Choppy Market Risk: EMA crossover signals may generate frequent false signals in ranging markets.
- Slippage Risk: Actual execution prices may significantly differ from signal prices in fast-moving markets.
- Money Management Risk: Despite risk controls, consecutive losses may still significantly impact the account.
- Systemic Risk: Sudden market events may render stop-losses ineffective.
Strategy Optimization Directions
- Add Trend Filters: Incorporate ADX or trend strength indicators to filter ranging market conditions.
- Optimize Stop-Loss Method: Consider using ATR for dynamic stop-loss adjustment to improve adaptability.
- Incorporate Volatility Adjustment: Dynamically adjust risk parameters based on market volatility.
- Time Filtering: Add trading time filters to avoid low liquidity periods.
- Include Volume Indicators: Integrate volume indicators to validate trend validity.
Summary
This strategy constructs a complete trading system by combining EMA trend signals with dynamic risk management. Its core advantages lie in scientific position management and risk control mechanisms, but it still requires appropriate parameter optimization based on market conditions and individual risk preferences. Through the suggested optimization directions, the strategy's stability and profitability can be further enhanced.
Source Pine Script
/*backtest
start: 2024-02-21 00:00:00
end: 2024-07-11 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Carlos Humberto Rodríguez Arias
//@version=5
strategy("EMA Crossover Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// EMA periods
MT_EMA = input.int(21, title="Medium Term EMA")
LT_EMA = input.int(55, title="Long Term EMA")
RR = input.float(2.0, title="Risk-Reward Ratio") // User-defined RR
RiskPercent = input.float(1.0, title="Risk Percentage") // User-defined risk percentage
// Calculate EMAs
Signal_MT_EMA = ta.ema(close, MT_EMA)
Signal_LT_EMA = ta.ema(close, LT_EMA)
// Plot EMAs
plot(Signal_MT_EMA, title="Medium Term EMA", color=color.orange, linewidth=2)
plot(Signal_LT_EMA, title="Long Term EMA", color=color.blue, linewidth=2)
// Determine trend conditions
uptrend = ta.crossover(Signal_MT_EMA, Signal_LT_EMA)
downtrend = ta.crossunder(Signal_MT_EMA, Signal_LT_EMA)
// Stop-Loss Calculations
longStopLoss = ta.lowest(low, 2) // SL for buy = lowest low of last 2 candles
shortStopLoss = ta.highest(high, 2) // SL for sell = highest high of last 2 candles
// Take-Profit Calculations
longTakeProfit = close + (close - longStopLoss) * RR
shortTakeProfit = close - (shortStopLoss - close) * RR
// Calculate Position Size based on Risk Percentage
capital = strategy.equity * (RiskPercent / 100)
longPositionSize = capital / (close - longStopLoss)
shortPositionSize = capital / (shortStopLoss - close)
// Execute Buy Order
if uptrend
strategy.entry("Long", strategy.long, qty=longPositionSize)
strategy.exit("Long Exit", from_entry="Long", stop=longStopLoss, limit=longTakeProfit)
// Execute Sell Order
if downtrend
strategy.entry("Short", strategy.short, qty=shortPositionSize)
strategy.exit("Short Exit", from_entry="Short", stop=shortStopLoss, limit=shortTakeProfit)
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/Adaptive-EMA-Crossover-Dynamic-Position-Risk-Management-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
- Dual EMA Filtered ATR Adaptive Trend Following StrategyPine Script · Trend following · 1h
- Dual Moving Average Trend Detection with Volatility Based Stop Loss StrategyPine Script · Trend following · 1d
- Dynamic EMA Trend Detection with ATR Threshold StrategyPine Script · Trend following · 1h
- Multi Exponential Moving Average Crossover Strategy with Volume Based ATR Dynamic Stop Loss OptimizationPine Script · Trend following · 2d
- Multi Factor Trend Price Action Strategy with Dynamic Risk Management SystemPine Script · Trend following · 2h
- Multi Indicator Dynamic Trend Following Strategy Based on EMA and SMAPine Script · Trend following · 1d