Strategy vault / Trend following
Closing Price Comparison Dual Moving Average Crossover Strategy
The Closing Price Comparison Dual Moving Average Crossover strategy is a relatively simple quantitative trading strategy. It calculates the recent 7 candle's average closing price and 20 candle's average closing…

Overview
The Closing Price Comparison Dual Moving Average Crossover strategy is a relatively simple quantitative trading strategy. It calculates the recent 7 candle's average closing price and 20 candle's average closing price. When the short-term moving average crosses over the long-term moving average from below, it signals a long position. When the short-term moving average crosses below the long-term moving average, it signals a short position. This allows the strategy to capture the inflection points in market's mid-term trends.
Strategy Logic
The core logic of this strategy is to calculate the average closing price of recent 7 candles (excluding the current candle) as the short-term moving average, and the average closing price of 20 candles (excluding recent 7 candles) as the long-term moving average. When the short-term moving average crosses over the long-term moving average from below, it indicates the market is turning from decline to rise, signaling a long position. When the short-term moving average crosses below the long-term moving average from above, it indicates the market is turning from rise to decline, signaling a short position.
Upon long signal, long position will be opened using the whole account capital. Upon short signal, the existing long position will be closed first before opening the short position using the same amount. Each opened position will be hold for 20-25 candles. During this period, if loss occurs, 50% of the position will be stopped loss. If sufficient profit occurs, 50% of the position will be taken profit.
Advantage Analysis
The advantages of this simple dual moving average crossover strategy are:
- Simple logic, easy to understand and implement;
- Using crossing of different period moving averages to determine inflection points in mid-term market trends, which is a widely used technical indicator;
- Can filter out market noise effectively and capture mid-term trends;
- Suitable for mid-long term trading, with 20-25 candle hold per position and good profit/loss ratio;
- Built-in stop loss and take profit mechanisms to control risks and lock in profits.
Risk Analysis
As a simple trend following strategy, it also faces some potential risks:
- Increased whipsaws and false signals when market enters consolidation;
- Price spikes during hold period may trigger stop loss;
- Hard to effectively determine true market reversal points, trading signal may lag.
The optimizations to address these risks are:
- Add filters, check if price breaks key support/resistance levels when MAs cross to remove false signals;
- Adjust hold period, shorten average hold time per position to control loss;
- Add other technical indicators to determine true market reversal points.
Optimization Directions
As a simple dual moving average crossover strategy, the main optimizations are:
Optimize MA parameters, test different short and long term MA combinations for best parameters;
Add other filter indicators like volume, volatility index etc. to avoid wrong signals in choppy markets;
Optimize stop loss and take profit strategies, test different ratios to find optimum;
Test effectiveness across different market cycles and optimize hold period;
Add machine learning algorithms, keep optimizing parameters through back-testing for more robustness.
Conclusion
In summary, this is a simple dual moving average crossover strategy, using MA crossovers across different periods to determine mid-term trend inflection points. It has high practicality and is easy to operate. But it also has limitations in effectively determining true market reversal points. Further optimizations on adding filters, parameter tuning, machine learning etc. are needed to make it more robust across varying market conditions for consistent alpha.
Source Pine Script
/*backtest
start: 2024-01-05 00:00:00
end: 2024-02-04 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nrathi2211
//@version=5
strategy("Closing Prices", overlay=true)
//variables
closingB7 = ta.highest(close, 7)[7]
closingB14 = ta.highest(close, 7)[20]
highB14 = ta.highest(low, 50)[7]
capital = 50000
//functions
qty_find(float price) => capital / int(price)
profit_take() =>
profit = strategy.opentrades.profit(strategy.opentrades - 1)
profit*.95
if(closingB7 < closingB14)
if(ta.crossover(close, closingB7))
strategy.entry("long_buy", strategy.long, qty_find(close))
current_profit = strategy.opentrades.profit(strategy.opentrades - 1)
if(current_profit < 0)
strategy.close("Exit long_buy SL", "long_buy", qty_percent = 50)
else if(current_profit < profit_take())
strategy.close("Exit long_buy TP", "long_buy", qty_percent = 50)
if(ta.crossunder(close, closingB7))
strategy.exit("long_sell", from_entry = "long_buy", stop = closingB7)
plot(closingB7, "cl", color.green, 2)
//plot(closingB14, "cl", color.red, 2)
plot(highB14, "cl", color.purple, 2)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-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/Closing-Price-Comparison-Dual-Moving-Average-Crossover-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
- Adaptive Bollinger Bands Trend Tracking StrategyPine Script · Trend following · 1h
- Automated Fibonacci Retracement Trading System StrategyPine Script · Trend following · 1d
- Bollinger Bands Precise Crossover Quantitative StrategyPine Script · Trend following · 1h
- Comprehensive Trading System Combining SMA Crossover Strategy with Fair Value Gap PullbackPine Script · Trend following · 1d
- Dual EMA Crossover StrategyPine Script · Trend following · 1d
- Dual Hull Moving Average Trading StrategyPine Script · Trend following · 1d