Strategy vault / Trend following
Moving Average Crossover Strategy 434304
The moving average crossover strategy is a quantitative trading strategy based on technical indicators. It judges the trend direction of the market by calculating the crossover relationship between two moving average…

Overview
The moving average crossover strategy is a quantitative trading strategy based on technical indicators. It judges the trend direction of the market by calculating the crossover relationship between two moving average lines, and generates corresponding trading signals.
Strategy Logic
The core indicators of this strategy are two moving average lines: a longer 40-period simple moving average (SMA) and the closing price of the stock. When the closing price breaks through the 40-period SMA from below, it indicates that the market trend may reverse and the stock may enter a new uptrend. At this point, the strategy will generate a long signal. When the closing price falls below the 40-period SMA, it indicates that the uptrend of the stock has ended and it may enter a downtrend channel. The strategy will close long positions at this time.
By comparing the breakthrough relationship between closing price and SMA, the turning points of the price trend can be captured to make trading decisions according to the trend direction.
Advantages of the Strategy
The strategy has the following advantages:
- Simple and clear rules that are easy to understand and implement.
- Can effectively capture the reversal of stock mid-to-long term trends and timely adjust positions.
- The SMA indicator has a certain filtering effect on abnormal price movements and can reduce false signals.
- Customizable SMA parameters, applicable to different trading varieties and cycles.
Risks of the Strategy
There are also the following risks:
- As a trend tracking tool, the SMA indicator lags in responding to sudden events.
- Frequent trading and whipsaw may increase trading costs and locked profit risks.
- Improper parameter settings may lead to overtrading or missing opportunities.
Risks can be controlled by adjusting SMA parameters, setting stop loss lines, etc.
Optimization Directions
The strategy can also be optimized in the following aspects:
- Add comparisons of multiple moving averages to form a filter to reduce false signals.
- Incorporate other indicators such as trading volume and graph rector to enhance reliability.
- Dynamically optimize SMA parameters to automatically adapt them to market changes.
- Set stop loss mechanisms with compound conditions to control single transaction loss.
Summary
The moving average crossover strategy judges trend reversal by comparing price changes with the SMA relationship. It is a relatively classic rule-based trading strategy. The strategy is simple to implement, easy to follow mid-to-long term trends for profit, while there are also certain risks of profit retracement and lagging identification. Risks can be controlled and decision effectiveness improved by parameter settings and incorporating other indicators.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 40 | SMA Length |
Source Pine Script
/*backtest
start: 2023-11-04 00:00:00
end: 2023-12-04 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy(title="MA Crossover (40)", overlay=true)
// Input for the SMA length (24)
sma_length = input(40, title="SMA Length")
sma = ta.sma(close, sma_length)
// Determine if the current candle crosses above the 24-period SMA
longCondition = ta.crossover(close, sma)
// Determine if the current candle crosses and closes below the 24-period SMA
closeLongCondition = ta.crossunder(close, sma)
// Plot the 24-period SMA
plot(sma, color=color.blue, title="24-period SMA")
// Long entry signal
if (longCondition)
strategy.entry("Long", strategy.long)
// Close long position when the current candle crosses and closes below the 24-period SMA
if (closeLongCondition)
strategy.close("Long")
// Create alerts
alertcondition(longCondition, title="Candle Crosses Above SMA 40", message="Candle has crossed above SMA 40.")
alertcondition(longCondition, title="Candle Closes Above SMA 40", message="Candle has closed above SMA 40.")
Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-05. 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-Crossover-Strategy-434304.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