Strategy vault / Trend following
Long Short Moving Average Crossover Trading Strategy
The long-short moving average crossover trading strategy is a typical trend-following strategy. It uses the golden cross and death cross of the fast and slow moving averages to determine market trends and make…

Overview
The long-short moving average crossover trading strategy is a typical trend-following strategy. It uses the golden cross and death cross of the fast and slow moving averages to determine market trends and make corresponding long and short trades. When the fast moving average crosses above the slow moving average, it indicates an upward trend, so go long. When the fast moving average crosses below the slow moving average, it indicates a downward trend, so go short. This strategy works well for markets with strong mid- to long-term trends.
Strategy Logic
The core logic of the long-short MA strategy is based on the golden cross and death cross of moving averages. Moving averages can effectively filter out market noise and reflect trend direction. The fast MA reacts more quickly to price changes and captures short-term trends. The slow MA responds more slowly and tracks long-term trends.
When the fast MA crosses above the slow MA, it shows that the short-term trend has more upward momentum than the long-term trend, so go long. When the fast MA crosses below the slow MA, it indicates stronger downward momentum in the short-term trend, so go short.
Specifically, this strategy defines a fast MA (length 9) and a slow MA (length 21). It then uses ta.crossover and ta.crossunder to detect golden crosses and death crosses between them. It goes long on golden crosses and goes short on death crosses.
Advantage Analysis
The long-short MA strategy has the following advantages:
- Simple logic, easy to understand and implement;
- Moving averages filter noise effectively and identify trends;
- Fast and slow MAs combined catch mid- to long-term trends;
- Customizable MA parameters work for different markets;
- Applicable to multiple timeframes, flexible.
Risk Analysis
The long-short MA strategy also has the following risks:
- Whipsaws and false signals may occur in ranging markets;
- Poor MA parameter tuning leads to bad signals;
- Unable to gauge trend strength, losses near reversals;
- Entry levels not clearly defined.
These risks can be reduced by optimizing MA parameters, adding filters, and setting stop losses.
Optimization Directions
The long-short MA strategy can be improved in the following aspects:
- Optimize MA parameters to find the best combination;
- Add other indicators as filters, e.g. MACD, KDJ to avoid bad signals;
- Add stop loss mechanisms to control loss per trade;
- Combine with volatility metrics to fine-tune entries.
Conclusion
In summary, the long-short MA crossover strategy is a simple and practical trend following system. By combining fast and slow moving averages, it can effectively identify trend direction. But it also has some flaws. After optimizations and enhancements, it can become a core quantitative trading strategy.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 9 | Fast MA Length |
| v_input_2 | 21 | Slow MA Length |
Source Pine Script
/*backtest
start: 2023-11-12 00:00:00
end: 2023-12-12 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("MA Strategy", overlay=true)
// Input parameters
fastLength = input(9, title="Fast MA Length")
slowLength = input(21, title="Slow MA Length")
// Calculate moving averages
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)
// Plot moving averages
plot(fastMA, color=color.blue, title="Fast MA")
plot(slowMA, color=color.red, title="Slow MA")
// Strategy conditions
longCondition = ta.crossover(fastMA, slowMA)
shortCondition = ta.crossunder(fastMA, slowMA)
// Strategy orders
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
// Plot entry signals
plotshape(series=longCondition, title="Buy Signal", color=color.green, style=shape.triangleup, size=size.small)
plotshape(series=shortCondition, title="Sell Signal", color=color.red, style=shape.triangledown, size=size.small)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-13. 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/Long-Short-Moving-Average-Crossover-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
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dual MA Crossover Trend Tracking StrategyPine Script · Trend following · 1h
- Dual Moving Average Convergence Trend Tracking StrategyPine Script · Trend following · 1d
- Dual Moving Average Crossover Strategy with Stop Loss and Take ProfitPine Script · Trend following · 4h
- Dual SMA Crossover StrategyPine Script · Trend following · 5m