Strategy vault / Trend following
Dual Moving Average Crossover Adaptive Parameter Trading Strategy
This strategy is an adaptive parameter trading system based on dual moving average crossover signals. It generates trading signals through the crossover of fast and slow moving averages, combined with adjustable risk…

Overview
This strategy is an adaptive parameter trading system based on dual moving average crossover signals. It generates trading signals through the crossover of fast and slow moving averages, combined with adjustable risk management parameters including stop-loss, take-profit, and trailing stop, achieving flexible trading strategy management. The core of the strategy lies in dynamically adjusting various parameters through the control panel, enabling the strategy to adapt to different market environments.
Strategy Principles
The strategy employs two moving averages - fast and slow - as core indicators. A long position signal is generated when the fast moving average crosses above the slow moving average, while a position closure signal is generated when the fast moving average crosses below the slow moving average. Additionally, the strategy incorporates a triple risk control mechanism: fixed stop-loss, fixed take-profit, and trailing stop. These parameters can be adjusted in real-time through the control panel, ranging from 0.1% to larger percentages, providing traders with precise risk control capabilities.
Strategy Advantages
- Parameter Flexibility: The strategy allows traders to adjust key parameters such as moving average periods and stop-loss/take-profit ratios according to market conditions, enhancing adaptability.
- Comprehensive Risk Management: Effective downside risk control through triple protection mechanisms (stop-loss, take-profit, trailing stop).
- Clear Operating Logic: Trading signals based on moving average crossovers are simple and intuitive, easy to understand and execute.
- High Automation Level: The strategy can operate fully automatically, reducing emotional interference from manual intervention.
Strategy Risks
- Sideways Market Risk: In ranging markets, frequent moving average crossovers may lead to overtrading and consecutive losses.
- Slippage Risk: During severe market volatility, actual execution prices may significantly deviate from signal prices.
- Parameter Optimization Risk: Excessive parameter optimization may result in significant differences between live trading performance and backtesting results.
- Systemic Risk: Sudden major market events may cause price gaps that break through stop-loss levels.
Strategy Optimization Directions
- Add Market Trend Filter: Introduce additional trend identification indicators to avoid frequent trading in sideways markets.
- Optimize Stop-Loss Method: Consider incorporating volatility indicators to dynamically adjust stop-loss percentages.
- Introduce Volume Indicators: Use volume as auxiliary confirmation for trading signals.
- Add Time Filters: Set appropriate trading time windows to avoid highly volatile periods.
Summary
This strategy constructs an adaptive trading system through dual moving average crossovers combined with flexible risk management parameters. Its strengths lie in strong parameter adjustability and comprehensive risk control, while attention must be paid to risks from ranging markets and parameter optimization. The strategy has significant optimization potential through the addition of trend filters and stop-loss optimization methods. For traders, properly setting parameters and continuously monitoring strategy performance are key to ensuring strategy stability.
Source Pine Script
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-27 08:00:00
period: 2d
basePeriod: 2d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © traderhub
//@version=5
strategy("Two Moving Averages Strategy with Adjustable Parameters", overlay=true)
// Adjustable parameters for fast and slow moving averages
fastLength = input.int(10, title="Fast Moving Average Length", minval=1, maxval=100)
slowLength = input.int(30, title="Slow Moving Average Length", minval=1, maxval=100)
// Risk management parameters
stopLossPerc = input.float(1, title="Stop Loss (%)", step=0.1) // Stop-loss percentage
takeProfitPerc = input.float(2, title="Take Profit (%)", step=0.1) // Take-profit percentage
trailStopPerc = input.float(1.5, title="Trailing Stop (%)", step=0.1) // Trailing stop percentage
// Calculate fast and slow moving averages
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)
// Plot moving averages on the chart
plot(fastMA, color=color.blue, title="Fast Moving Average")
plot(slowMA, color=color.red, title="Slow Moving Average")
// Conditions for opening and closing positions
longCondition = ta.crossover(fastMA, slowMA) // Buy when fast moving average crosses above the slow moving average
shortCondition = ta.crossunder(fastMA, slowMA) // Sell when fast moving average crosses below the slow moving average
// Variables for stop-loss and take-profit levels
var float longStopLevel = na
var float longTakeProfitLevel = na
// Enter a long position
if (longCondition)
longStopLevel := strategy.position_avg_price * (1 - stopLossPerc / 100)
longTakeProfitLevel := strategy.position_avg_price * (1 + takeProfitPerc / 100)
strategy.entry("Long", strategy.long)
// Manage stop-loss, take-profit, and trailing stop for long positions
if (strategy.position_size > 0)
strategy.exit("Take Profit/Stop Loss", "Long", stop=longStopLevel, limit=longTakeProfitLevel, trail_offset=trailStopPerc)
// Close the long position and enter short when the condition is met
if (shortCondition)
strategy.close("Long")
strategy.entry("Short", strategy.short)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-11-29. 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-Adaptive-Parameter-Trading-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
- 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