Strategy vault / Trend following
Configurable Moving Average Crossover Strategy
This article introduces a flexible and powerful moving average crossover trading strategy that allows traders to customize moving average parameters and types based on different market conditions. The core of the…

Overview
This article introduces a flexible and powerful moving average crossover trading strategy that allows traders to customize moving average parameters and types based on different market conditions. The core of the strategy is to use moving averages of different periods and types for trend tracking and signal generation.
Strategy Principles
The strategy generates trading signals by calculating three moving averages of different periods (fast, slow, and exit lines). The main principles include:
- Moving Average Type Selection: Supports Simple Moving Average (SMA), Exponential Moving Average (EMA), Weighted Moving Average (WMA), and Hull Moving Average (HMA).
- Entry Conditions:
- Long Entry: Close price above fast MA, fast MA above slow MA, and close price above exit MA
- Short Entry: Close price below fast MA, fast MA below slow MA, and close price below exit MA
- Exit Conditions:
- Long Exit: After at least two bars, close price below exit MA
- Short Exit: After at least two bars, close price above exit MA
Strategy Advantages
- High Configurability: Traders can flexibly adjust moving average periods and types
- Multi-Market Adaptability: Applicable to different liquidity instruments by adjusting parameters
- Strong Trend Tracking Capability: Uses multiple moving averages to filter false signals
- Risk Control: Default position management of 10% of account equity
- Flexible Trading Direction: Option to enable or disable short trading
Strategy Risks
- Parameter Sensitivity: Different markets may require different moving average parameters
- Performance in Trending Markets: May generate more invalid signals in range-bound markets
- Transaction Costs: Default setting of 0.06% commission needs consideration in actual trading
- Backtesting Limitations: Currently validated only on a few instruments (e.g., BTCUSD and NIFTY)
Strategy Optimization Directions
- Dynamic Parameter Adjustment: Introduce adaptive moving average periods
- Combine with Other Technical Indicators: Add RSI, MACD for signal filtering
- Stop Loss Mechanism: Add volatility-based stop loss strategy
- Multi-Timeframe Verification: Comprehensive backtesting across different time periods
- Machine Learning Optimization: Use algorithms to automatically find optimal parameter combinations
Summary
The Configurable Moving Average Crossover Strategy (MA-X) provides a flexible trend-tracking framework. Through reasonable configuration and continuous optimization, this strategy can become a powerful tool in the quantitative trading toolbox. Traders need to make personalized adjustments based on specific market characteristics and conduct thorough backtesting and verification.
Source Pine Script
/*backtest
start: 2024-04-03 00:00:00
end: 2025-04-02 00:00:00
period: 2d
basePeriod: 2d
exchanges: [{"eid":"Futures_Binance","currency":"BNB_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © YetAnotherTA
//@version=6
strategy("Configurable MA Cross (MA-X) Strategy", "MA-X", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type = strategy.commission.percent, commission_value = 0.06)
// === Inputs ===
// Moving Average Periods
maPeriodA = input.int(13, title="Fast MA")
maPeriodB = input.int(55, title="Slow MA")
maPeriodC = input.int(34, title="Exit MA")
// MA Type Selection
maType = input.string("EMA", title="MA Type", options=["SMA", "EMA", "WMA", "HMA"])
// Toggle for Short Trades (Disabled by Default)
enableShorts = input.bool(false, title="Enable Short Trades", tooltip="Enable or disable short positions")
// === Function to Select MA Type ===
getMA(src, length) =>
maType == "SMA" ? ta.sma(src, length) : maType == "EMA" ? ta.ema(src, length) : maType == "WMA" ? ta.wma(src, length) : ta.hma(src, length)
// === MA Calculation ===
maA = getMA(close, maPeriodA)
maB = getMA(close, maPeriodB)
maC = getMA(close, maPeriodC)
// === Global Variables for Crossover Signals ===
var bool crossAboveA = false
var bool crossBelowA = false
crossAboveA := ta.crossover(close, maA)
crossBelowA := ta.crossunder(close, maA)
// === Bar Counter for Exit Control ===
var int barSinceEntry = na
// Reset the counter on new entries
if (strategy.opentrades == 0)
barSinceEntry := na
// Increment the counter on each bar
if (strategy.opentrades > 0)
barSinceEntry := (na(barSinceEntry) ? 1 : barSinceEntry + 1)
// === Entry Conditions ===
goLong = close > maA and maA > maB and close > maC and crossAboveA
goShort = enableShorts and close < maA and maA < maB and close < maC and crossBelowA // Shorts only when toggle is enabled
// === Exit Conditions (only after 1+ bar since entry) ===
exitLong = (strategy.position_size > 0) and (barSinceEntry >= 2) and (close < maC)
exitShort = enableShorts and (strategy.position_size < 0) and (barSinceEntry >= 2) and (close > maC)
// === Strategy Execution ===
// Long entry logic
if (goLong)
strategy.close("Short") // Close any short position
strategy.entry("Long", strategy.long)
alert("[MA-X] Go Long")
barSinceEntry := 1 // Reset the bar counter
// Short entry logic (only if enabled)
if (enableShorts and goShort)
strategy.close("Long") // Close any long position
strategy.entry("Short", strategy.short)
alert("[MA-X] Go Short")
barSinceEntry := 1 // Reset the bar counter
// Exit logic (only after at least 1 bar has passed)
if (exitLong)
strategy.close("Long")
alert("[MA-X] Exit Long")
if (enableShorts and exitShort)
strategy.close("Short")
alert("[MA-X] Exit Short")
// === Plotting ===
plot(maA, color=color.green, linewidth=2, title="Fast MA")
plot(maB, color=color.blue, linewidth=2, title="Slow MA")
plot(maC, color=color.red, linewidth=2, title="Exit MA")Published by ianzeng123 on the FMZ strategy square · last modified 2025-04-03. 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/Configurable-Moving-Average-Crossover-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
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dynamic Dual EMA Crossover Strategy with Adaptive Profit Loss ControlPine Script · Trend following · 1d
- EMA SMA Multi Indicator Comprehensive Trend Following StrategyPine Script · Trend following · 1h
- EMA and RSI Trend Trading StrategyPine Script · Trend following · 1d
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h