Strategy vault / Trend following
Dynamic Moving Average Crossover Trend Following Strategy with Adaptive Risk Management
This strategy is a trend-following system based on dual moving average crossover signals, incorporating a dynamic take-profit and stop-loss mechanism. It utilizes 5-period and 12-period Simple Moving Averages (SMA)…

Overview
This strategy is a trend-following system based on dual moving average crossover signals, incorporating a dynamic take-profit and stop-loss mechanism. It utilizes 5-period and 12-period Simple Moving Averages (SMA) to generate trading signals, optimizing risk-reward ratio through dynamic adjustment of take-profit and stop-loss levels. Initial take-profit is set at 10% and stop-loss at 5%, with levels adjusting to 20% and 2.5% respectively when price moves favorably.
Strategy Principles
The core logic relies on the crossover relationship between fast (5-period) and slow (12-period) moving averages. A buy signal is generated when the fast MA crosses above the slow MA, while positions are closed when the fast MA crosses below the slow MA. The strategy's uniqueness lies in its dynamic risk management mechanism: after position entry, the system continuously monitors price movement and dynamically adjusts take-profit and stop-loss levels to maximize profits while controlling risk.
Strategy Advantages
- Employs classic dual MA crossover strategy with clear signals, easy to understand and execute
- Dynamic take-profit/stop-loss mechanism effectively protects realized profits and prevents drawdowns
- Strategy parameters can be flexibly adjusted for different market characteristics
- Comprehensive risk management mechanism effectively controls single-trade risk
- Clear code structure facilitates maintenance and optimization
Strategy Risks
- May generate false signals in ranging markets, leading to frequent trading
- Potential significant drawdowns in quick reversal scenarios
- Improper parameter settings may affect strategy performance
- Market liquidity issues may impact stop-loss execution Risk management recommendations:
- Add trend filters
- Optimize parameter selection
- Monitor market liquidity in real-time
- Establish comprehensive money management system
Optimization Directions
- Introduce trend strength indicators to filter ranging market signals
- Consider incorporating volume factors to improve signal reliability
- Optimize take-profit/stop-loss parameters to enhance risk-reward ratio
- Add market volatility adaptation mechanism
- Improve position sizing system
Summary
This strategy effectively captures trends and dynamically controls risk by combining classic moving average crossover signals with innovative dynamic risk management. The strategy design is clear, implementation is efficient, and it demonstrates good practicality and scalability. Through continuous optimization and improvement, this strategy shows promise for achieving stable returns in actual trading.
Source Pine Script
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-25 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("My Moving Average Crossover Strategy with Take Profit and Stop Loss", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
//risk_free_rate = float(request.security("IRUS", "D", close)/request.security("IRUS", "D", close[1]) - 1 ))
// MA periods
fastLength = input.int(5, title="Fast MA Length")
slowLength = input.int(12, title="Slow MA Length")
// Take Profit and Stop Loss
takeProfitLevel = input(10, title="Take Profit (пункты)") // Take profit % from the last price
stopLossLevel = input(5, title="Stop Loss (пункты)") // Stop loss % from the last price
takeProfitLevel_dyn = input(20, title="Dynamic Take Profit (пункты)") // Move TP if current_price higher buy_px
stopLossLevel_dyn = input(2.5, title="Dynamic Stop Loss (пункты)") // S Move SL if current_price higher buy_px
// Вычисление скользящих средних
fastMA = ta.sma(close, fastLength)
slowMA= ta.sma(close, slowLength)
// Conditions for Sell and Buy
longCondition = ta.crossover (fastMA, slowMA) // покупаем, если короткая MA персекает длинную снизу-вверх
shortCondition = ta.crossunder(fastMA, slowMA) // продаем, если короткая MA персекает длинную сверху-вниз
// Buy position condition
if (longCondition)
strategy.entry("Buy", strategy.long)
// Dynamic TP SL leveles
takeProfitPrice = strategy.position_avg_price * (1+ takeProfitLevel / 100)
stopLossPrice = strategy.position_avg_price * (1-stopLossLevel / 100)
entryPrice = strategy.position_avg_price
if (strategy.position_size > 0) // если есть открытая позиция
// takeProfitPrice := entryPrice * (1+ takeProfitLevel / 100)
// stopLossPrice := entryPrice * (1-stopLossLevel / 100)
// // Перемещение Stop Loss и Take Profit
if (close > entryPrice)
takeProfitPrice := close * (1+ takeProfitLevel_dyn / 100)
stopLossPrice := close * (1- stopLossLevel_dyn/ 100)
if (shortCondition)
strategy.close("Buy")
strategy.exit("Take Profit/Stop loss", "Buy", limit=takeProfitPrice, stop=stopLossPrice)
// Drawing MA lines
plot(fastMA, color=color.blue, title="Fast Moving Average")
plot(slowMA, color=color.orange, title="Slow Moving Average")
// Визуализация
plot(longCondition ? na : takeProfitPrice, title="Take Profit Level", color=color.green, linewidth=1, style=plot.style_line)
plot(longCondition ? na: stopLossPrice, title="Stop Loss Level", color=color.red, linewidth=1, style=plot.style_line)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-12-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-Moving-Average-Crossover-Trend-Following-Strategy-with-Adaptive-Risk-Management.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
- Adaptive FVG Detection and MA Trend Trading Strategy with Dynamic ResistancePine Script · Trend following · 1d
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m