Strategy vault / Trend following
Moving Average Crossover Strategy 439756
This is a trading strategy based on moving average crossover signals. It uses a 45-day moving average line as the major technical indicator and generates buy and sell signals when the price breaks through the moving…

Overview
This is a trading strategy based on moving average crossover signals. It uses a 45-day moving average line as the major technical indicator and generates buy and sell signals when the price breaks through the moving average line.
Strategy Logic
When the price rallies and breaks above the 45-day moving average line, a buy signal is generated. After holding the position for 8 days, a sell signal is generated. Afterwards, if the price rallies and breaks above the 45-day moving average line again, a new buy signal will be triggered, and so on so forth.
The specific logic principles are:
- Calculate the 45-day moving average line.
- When the closing price breaks from below to above the moving average line, a buy signal is generated to go long.
- Hold the position for 8 trading days after entering the market.
- Close the long position after 8 days and generate a sell signal.
- If later on the closing price breaks from below to above the moving average line again, regenerate a buy signal to reopen a long position.
The above constitutes the core trading logic of this strategy.
Advantages
This strategy has the following advantages:
- The trading rules are simple and clear, easy to understand and implement.
- Utilizes the trend tracking feature of moving averages to effectively capture medium-to-long term trends.
- The 8-day holding period is appropriately long enough to track trends and short enough to cut losses in time.
- The rules for re-entering the market are clear, which helps restrict trading frequency.
Risks
There are a few risks with this strategy:
- The lagging nature of moving averages could lead to late entries and premature exits.
- The fixed holding period and MA parameters may fail to adapt to changing market conditions.
- Trading frequency might be too high, increasing costs and slippage.
- Breakout signals may produce false signals resulting in some whipsaws.
Solutions:
- Optimize MA parameters to reduce lag.
- Increase holding period or use trailing stops to better track trends.
- Add filters using other indicators like MACD or KDJ to confirm signals.
- Refine re-entry rules to control frequency.
Enhancement Areas
The main enhancement areas are:
Optimize MA parameters to find best combinations, e.g. 15-day, 30-day, 60-day MAs.
Optimize holding period to determine optimal duration, e.g. 5 days, 10 days, 15 days.
Add trailing stops to track trends and control risks, e.g. trialing stops or ATR stops.
Add filters using other indicators like MACD, KDJ to reduce false signals.
Refine re-entry rules to prevent over-trading, e.g. enforce cooling-off periods.
Test effectiveness across different markets and instruments. Parameters need to be tuned for different markets.
Summary
In summary, this MA crossover strategy is a simple and practical trend following system. It takes advantage of the trend tracking ability of MAs and combines price breakouts to generate trade signals. The pros are it's easy to implement while the cons are occasional whipsaws. The strategy can be further enhanced through parameter optimization and adding other indicators as filters.
Source Pine Script
/*backtest
start: 2023-01-16 00:00:00
end: 2024-01-22 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Moving Average Crossover Strategy", overlay=true)
// Calculate the 45-day moving average
ma_length = 45
ma = ta.sma(close, ma_length)
// Track position entry and entry bar
var bool in_long_position = na
var int entry_bar = na
var int exit_bar = na
// Entry condition: Close price crosses above the 45-day moving average to enter the position
if (not in_long_position and ta.crossover(close, ma) and not na(ma[1]) and close > ma and close[1] < ma[1])
in_long_position := true
entry_bar := bar_index
// Exit condition: Close the position after holding for 8 trading days
if (in_long_position and bar_index - entry_bar >= 8)
in_long_position := false
exit_bar := bar_index
// Re-entry condition: Wait for price to cross over the 45-day moving average again
if (not in_long_position and ta.crossover(close, ma) and not na(ma[1]) and close > ma and close[1] > ma[1] and (na(exit_bar) or bar_index - exit_bar >= 8))
in_long_position := true
entry_bar := bar_index
// Execute long entry and exit
if (in_long_position)
strategy.entry("Long", strategy.long)
if (not in_long_position)
strategy.close("Long")Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-23. 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-439756.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 Crossover Strategy with Stop Loss and Take ProfitPine Script · Trend following · 4h
- Dynamic Dual EMA Crossover Strategy with Adaptive Profit Loss ControlPine Script · Trend following · 1d
- EMA and RSI Trend Trading StrategyPine Script · Trend following · 1d