Strategy vault / Trend following
Moving Average Crossover Trading Strategy 431280
This strategy is based on the crossover principle of moving averages to trade. It uses two moving averages, generating buy signals when the short term moving average crosses above the long term moving average from…

Overview
This strategy is based on the crossover principle of moving averages to trade. It uses two moving averages, generating buy signals when the short term moving average crosses above the long term moving average from below. Sell signals are generated when the price breaks below another moving average. This strategy is suitable for trending markets, able to effectively filter out some noise trades and capture the main trend.
Strategy Logic
This strategy uses user-customizable short term and long term moving average periods, exit moving average period, and moving average calculation methods.
When the short term moving average crosses above the long term moving average from below, a buy signal is generated. This indicates the short term trend has switched to an uptrend, and we can buy.
When the close price breaks below the exit moving average, a sell signal is generated. This indicates a trend reversal, so we should exit the position.
Therefore, the strategy's trading signals come from the crossover of the short term and long term moving averages, and the relationship between the closing price and the exit moving average.
Advantage Analysis
The advantages of this strategy are:
Simple and easy to implement.
Customizable parameters suit different market conditions.
Moving averages filter out noise and capture the main trend.
Can incorporate other technical indicators like trend, support/resistance to further optimize.
Controllable risk-reward ratio, has stop loss mechanism.
Risk Analysis
The risks are:
Prone to false signals in non-trending consolidation markets.
Improper parameter settings may cause missing trends or too many invalid trades.
Improper stop loss placement could increase losses.
Failed breakouts may cause losses.
Parameters need timely adjustment to suit market changes.
Solutions include optimizing parameters, incorporating other filters, adjusting stops, waiting for trend confirmation before trading, etc.
Optimization Directions
This strategy can be improved by:
Developing trend determination mechanisms and only trading after trend confirmation.
Adding filters like volume or volatility to filter signals.
Dynamic optimization of moving average periods.
Optimizing the stop loss mechanism to use a trailing stop.
Incorporating support/resistance and other indicators to further confirm signals.
Adjusting parameters based on different products and timeframes.
Conclusion
Overall this moving average crossover strategy is a simple and practical trend following system. It can be adjusted to market conditions by tweaking parameters and catch the main trend direction in trending markets. But risks like trend misidentification should be noted, and constant optimization is needed to adapt to changing markets. In general, this strategy has good viability.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | true | From Day |
| v_input_2 | true | From Month |
| v_input_3 | 2020 | From Year |
| v_input_4 | true | To Day |
| v_input_5 | true | To Month |
| v_input_6 | 2021 | To Year |
| v_input_7 | 13 | Enter Period for Short Moving Average |
| v_input_8 | 0 | Choose Smoothing Type for Short Moving Average: EMA | SMA | RMA | WMA | VWMA | SMMA | DEMA | TEMA | HullMA | LSMA |
| v_input_9 | 48 | Enter Period for Long Moving Average |
| v_input_10 | 0 | Choose Smoothing Type for Long Moving Average: EMA | SMA | RMA | WMA | VWMA | SMMA | DEMA | TEMA | HullMA | LSMA |
| v_input_11 | 30 | Enter Period for Exit Moving Average |
| v_input_12 | 0 | Choose Smoothing Type for Exit Moving Average: EMA | SMA | RMA | WMA | VWMA | SMMA | DEMA | TEMA | HullMA | LSMA |
Source Pine Script
/*backtest
start: 2022-10-30 00:00:00
end: 2023-11-05 00:00:00
period: 1d
basePeriod: 1h
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/
// © TwoChiefs
//@version=4
strategy("John EMA Crossover Strategy", overlay=true)
////////////////////////////////////////////////////////////////////////////////
// BACKTESTING RANGE
// From Date Inputs
fromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
fromYear = input(defval = 2020, title = "From Year", minval = 1970)
// To Date Inputs
toDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
toMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
toYear = input(defval = 2021, title = "To Year", minval = 1970)
// Calculate start/end date and time condition
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = true
////////////////////////////////////////////////////////////////////////////////
//CREATE USER-INPUT VARIABLES
periodShort = input(13, minval=1, title="Enter Period for Short Moving Average")
smoothingShort = input(title="Choose Smoothing Type for Short Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])
periodLong = input(48, minval=1, title="Enter Period for Long Moving Average")
smoothingLong = input(title="Choose Smoothing Type for Long Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])
periodExit = input(30, minval=1, title="Enter Period for Exit Moving Average")
smoothingExit = input(title="Choose Smoothing Type for Exit Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])
src1 = close
pivot = (high + low + close) / 3
//MA CALCULATION FUNCTION
ma(smoothing, src, length) =>
if smoothing == "RMA"
rma(src, length)
else
if smoothing == "SMA"
sma(src, length)
else
if smoothing == "EMA"
ema(src, length)
else
if smoothing == "WMA"
wma(src, length)
else
if smoothing == "VWMA"
vwma(src, length)
else
if smoothing == "SMMA"
na(src[1]) ? sma(src, length) : (src[1] * (length - 1) + src) / length
else
if smoothing == "HullMA"
wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length)))
//ASSIGN A MOVING AVERAGE RESULT TO A VARIABLE
shortMA=ma(smoothingShort, pivot, periodShort)
longMA=ma(smoothingLong, pivot, periodLong)
exitMA=ma(smoothingExit, pivot, periodExit)
//PLOT THOSE VARIABLES
plot(shortMA, linewidth=4, color=color.yellow,title="The Short Moving Average")
plot(longMA, linewidth=4, color=color.blue,title="The Long Moving Average")
plot(exitMA, linewidth=1, color=color.red,title="The Exit CrossUnder Moving Average")
//BUY STRATEGY
buy = crossover(shortMA,longMA) ? true : na
exit = crossunder(close,exitMA) ? true : na
strategy.entry("long",true,when=buy and time_cond)
strategy.close("long",when=exit and time_cond)
if (not time_cond)
strategy.close_all()
Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-06. 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-Trading-Strategy-431280.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
- Dual Moving Average Trend Following System with Adaptive Dynamic Filtering StrategyPine Script · Trend following · 30m
- EMA AROON ASHPine Script · Trend following · 3m
- EMA SMA Trend Following with Swing Trading Strategy Combined Volume Filter and Percentage Take Profit Stop Loss SystemPine Script · Trend following · 1d
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h
- Multi Factor Trend Price Action Strategy with Dynamic Risk Management SystemPine Script · Trend following · 2h
- Multi Indicator Dynamic Trend Following Strategy Based on EMA and SMAPine Script · Trend following · 1d