Strategy vault / Trend following
Multi Moving Average Trend Trading Strategy
This article introduces a trend trading strategy based on multiple moving averages called the "Multi-Moving Average Trend Trading Strategy". The strategy is primarily applied to the Nasdaq futures market and captures…

Overview
This article introduces a trend trading strategy based on multiple moving averages called the "Multi-Moving Average Trend Trading Strategy". The strategy is primarily applied to the Nasdaq futures market and captures upward market trends by analyzing the price position relative to long, medium, and short-term moving averages. It also closes all positions at a specific time each day.
The strategy employs three simple moving averages (SMAs): long-term (default 200 periods), medium-term (default 21 periods), and short-term (default 9 periods). A buy signal is triggered when the price is above the long-term and medium-term moving averages and crosses above the short-term moving average, provided there are no open positions. The strategy also sets fixed-point stop-gain and stop-loss levels to manage risk. Additionally, all positions are closed at 17:00 each trading day.
Strategy Principle
Calculate the long-term (default 200 periods), medium-term (default 21 periods), and short-term (default 9 periods) simple moving averages.
Determine if the current price is above the long-term and medium-term moving averages.
Check if the current price crosses above the short-term moving average.
When conditions 2 and 3 are both satisfied, and there are no open positions, a buy signal is triggered.
After buying, set fixed-point stop-gain and stop-loss levels. Close the position when the price reaches either level.
Close all positions at 17:00 each trading day.
Strategy Advantages
Simple and easy to understand: The strategy is based on moving averages, making it straightforward to comprehend and implement.
Trend following: By analyzing the price position relative to moving averages of different periods, the strategy effectively captures upward market trends.
Risk control: The strategy incorporates fixed-point stop-gain and stop-loss levels, helping to manage risk for individual trades.
Automatic position closing: The strategy automatically closes all positions at a specific time each trading day, avoiding overnight risks.
Strategy Risks
Parameter optimization: The strategy's performance may be sensitive to the moving average period parameters, requiring optimization for different markets and instruments.
Choppy markets: In choppy market conditions, frequent crossover signals may lead to suboptimal strategy performance.
Slippage risk: During high market volatility, fixed-point stop-gain and stop-loss levels may not execute as intended, resulting in slippage risk.
Strategy Optimization Directions
Dynamic stop-gain and stop-loss: Adjust stop-gain and stop-loss levels dynamically based on market volatility or price trends to optimize the risk-reward ratio.
Trend filters: Incorporate additional technical indicators, such as the ADX, to confirm trend strength and filter out false signals in choppy markets.
Multi-instrument adaptation: Refine the strategy to adapt to different futures instruments and market characteristics.
Money management: Introduce more sophisticated money management rules, such as position sizing and risk control, to enhance the strategy's robustness.
Summary
The "Multi-Moving Average Trend Trading Strategy" is a simple and easy-to-understand trend-following strategy that captures upward market trends by analyzing the price position relative to moving averages of different periods. The strategy incorporates fixed-point stop-gain and stop-loss levels and automatically closes all positions at a specific time each day to manage risk. However, the strategy may underperform in choppy markets and faces challenges such as parameter optimization and slippage risk. Future optimizations can focus on dynamic stop-gain and stop-loss levels, trend filters, multi-instrument adaptation, and money management to further improve the strategy's robustness and adaptability.
Source Pine Script
/*backtest
start: 2024-04-01 00:00:00
end: 2024-04-30 23:59:59
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Médias Móveis de MarcosJR", overlay=true)
// Inputs para data inicial e final
start_year = input.int(2020, title="Ano Inicial")
start_month = input.int(1, title="Mês Inicial")
start_day = input.int(1, title="Dia Inicial")
end_year = input.int(2020, title="Ano Final")
end_month = input.int(12, title="Mês Final")
end_day = input.int(31, title="Dia Final")
// Convertendo dia, mês e ano para timestamp
start_date = timestamp(start_year, start_month, start_day, 00, 00)
end_date = timestamp(end_year, end_month, end_day, 23, 59)
// Condição para verificar se a data está dentro do intervalo especificado
date_within_range = true
// Parâmetros para os períodos das médias móveis
ma_short_period = input.int(9, title="MA Curta")
ma_medium_period = input.int(21, title="MA Média")
ma_long_period = input.int(200, title="MA Longa")
// Definindo médias móveis
ma_short = ta.sma(close, ma_short_period)
ma_medium = ta.sma(close, ma_medium_period)
ma_long = ta.sma(close, ma_long_period)
// Plotando as médias móveis no gráfico com espessura aumentada
plot(ma_short, color=color.blue, title="MA Curta", linewidth=2)
plot(ma_medium, color=color.orange, title="MA Média", linewidth=2)
plot(ma_long, color=color.red, title="MA Longa", linewidth=2)
// Verificando se o preço está acima das médias móveis
above_ma_long = close > ma_long
above_ma_medium = close > ma_medium
// Verificando se o preço tocou na média móvel curta
touch_ma_short = ta.crossover(close, ma_short)
// Condições de compra
buy_condition = date_within_range and above_ma_long and above_ma_medium and touch_ma_short
// Sinais de entrada e saída de compra
var float entry_price = na
if (buy_condition and strategy.opentrades == 0) // Verifica se não há operações em andamento
entry_price := close // Define o preço de entrada ao comprar
// Parâmetros para o tamanho do stop gain e stop loss em pontos
stop_gain_points = input.int(100, title="Stop Gain (pontos)", minval=1)
stop_loss_points = input.int(100, title="Stop Loss (pontos)", minval=1)
// Calcular o preço de saída alvo (Stop Gain) e de stop loss
target_price = entry_price + stop_gain_points * syminfo.mintick
stop_loss_price = entry_price - stop_loss_points * syminfo.mintick
// Sair da operação de compra quando o preço atingir o stop gain ou stop loss
if (strategy.position_size > 0)
strategy.exit("Venda", "Compra", limit=target_price, stop=stop_loss_price)
// Sinais de entrada de compra
if (buy_condition and strategy.opentrades == 0) // Verifica se não há operações em andamento
strategy.entry("Compra", strategy.long)
// Plotando setas de compra
plotshape(series=buy_condition, title="Sinal de Compra", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
// Função para verificar se é 17:00 do mesmo dia
is_17_oclock_same_day = hour == 17 and minute == 0 and hour[1] < 17
// Sair de todas as operações às 17:00 do mesmo dia
if (is_17_oclock_same_day)
strategy.close_all()
Published by ChaoZhang on the FMZ strategy square · last modified 2024-05-11. 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/Multi-Moving-Average-Trend-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