Strategy vault / Trend following
Moving Average Trend Following Strategy
This strategy judges the market trend direction by calculating the fast moving average (Fast MA) and slow moving average (Slow MA) and making comparisons to implement long or short positions along the trend. When the…

Overview
This strategy judges the market trend direction by calculating the fast moving average (Fast MA) and slow moving average (Slow MA) and making comparisons to implement long or short positions along the trend. When the fast MA crosses over the slow MA, go long. When the fast MA crosses below the slow MA, go short. Meanwhile, stop loss and take profit are set to control risks.
Principles
The core logic of this strategy is based on the golden cross and dead cross of moving averages. Moving averages can reflect changes in the average market price very well. The fast average has a shorter period and can respond to price changes quickly. The slow average has a longer period and represents the broader market trend direction. When the fast MA crosses over the slow MA, it indicates the market is starting a bullish trend. When the fast MA crosses below the slow MA, it indicates the market is starting a bearish trend.
Specifically, this strategy calculates the 50-period fast MA and 200-period slow MA, respectively. On each candlestick's close, it judges if the fast MA has crossed over or below the slow MA. If there is a cross-over (the yellow line crossing over the red line), it enters a long position at next candlestick's open. If there is a cross-below (the yellow line crossing below the red line), it enters a short position at next candlestick's open.
After entering positions, TrailStop will be used to track the stop loss and lock in profits. In addition, the stop loss and take profit are set based on the ATR values.
Advantages
This is a typical trend-following strategy with the following advantages:
- Using moving averages to determine trend direction has high accuracy and good win rate
- Adopting fast and slow moving average combos can filter market noise effectively and capture major trends
- Setting stop loss and take profit can control single loss and increase profit probability
- Backtest results are good, with acceptable maximum drawdown and Sharpe ratio
- The strategy logic is simple and easy to understand, the parameters are flexible for adjustment, suitable for average traders
Risks
There are also some risks for this strategy:
- Signals generated by moving averages may lag and be impacted by false breakouts when extreme market volatility happens
- Improper setting of stop loss or take profit may lead to losses or missing profits
- Overly relying on parameter settings, improper parameters will greatly affect strategy performance
- It's unable to perfectly avoid small losses from price probes and pullbacks
- It does not consider the impact of fundamentals and significant news events on markets
Solutions:
- Reasonably evaluate and set the moving average cycle parameters
- Adopt adaptive stop loss and take profit to avoid manual setting errors
- Optimize parameters through complexity analysis and backtest
- Expand stop loss range appropriately and increase position sizing
- Incorporate fundamental analysis and major events to formulate response plans
Optimization Directions
There is room for further optimization of this strategy:
- Increase combinations of moving averages of various cycles to form multiple signal groups
- Add indicators like volume and volatility to confirm the accuracy of trend signals
- Use machine learning methods to dynamically optimize parameters
- Set up adaptive stop loss and take profit mechanisms
- Consider combining market sentiment, investor attention indicators
- Test versatility across different products
- Incorporate more complex breakout indicators or models
Summary
In summary, this strategy judges and follows market trends using simple moving average golden crosses and dead crosses, and controls risks with reasonable stop loss and take profit. It is an easy-to-implement trend following strategy for beginners. It deserves further research and optimization on aspects like parameters, stop loss mechanisms, optimization methods to improve strategy performance.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 50 | Fast MA Length |
| v_input_2 | 200 | Slow MA Length |
| v_input_3 | 2 | Risk-Reward Ratio |
Source Pine Script
/*backtest
start: 2024-01-24 00:00:00
end: 2024-01-31 00:00:00
period: 10m
basePeriod: 1m
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/
// © KasperKvist
//@version=4
strategy("EURCHF Smart Money Strategy", overlay=true)
// Input Parameters
fastLength = input(50, title="Fast MA Length")
slowLength = input(200, title="Slow MA Length")
riskRewardRatio = input(2, title="Risk-Reward Ratio")
// Calculate Moving Averages
fastMA = sma(close, fastLength)
slowMA = sma(close, slowLength)
// Strategy Conditions
longCondition = crossover(fastMA, slowMA)
shortCondition = crossunder(fastMA, slowMA)
// Execute Strategy
strategy.entry("Long", strategy.long, when = longCondition)
strategy.entry("Short", strategy.short, when = shortCondition)
// Set Stop Loss and Take Profit
atrValue = atr(14)
stopLoss = atrValue * 1
takeProfit = atrValue * riskRewardRatio
strategy.exit("ExitLong", from_entry="Long", loss=stopLoss, profit=takeProfit)
strategy.exit("ExitShort", from_entry="Short", loss=stopLoss, profit=takeProfit)
// Plot Moving Averages
plot(fastMA, color=color.green, title="Fast MA")
plot(slowMA, color=color.red, title="Slow MA")
Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-01. 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/Moving-Average-Trend-Following-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
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m
- BB Percentage Index Trend Fading StrategyPine Script · Trend following · 1h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m