Strategy vault / Trend following
Moving Average Crossover Strategy 427436
This is a typical moving average crossover trading strategy. It uses the crossover points of fast and slow moving averages as trading signals. When the fast moving average crosses above the slow moving average from…
Overview
This is a typical moving average crossover trading strategy. It uses the crossover points of fast and slow moving averages as trading signals. When the fast moving average crosses above the slow moving average from below, it is considered as a buy signal. When the fast moving average crosses below the slow moving average from above, it is considered as a sell signal. This strategy combines two moving averages and can effectively filter market noise and identify trends.
Strategy Principles
The main steps of this strategy are:
Set the fast moving average period fastMA and slow moving average period slowMA.
Calculate the fast moving average fast and slow moving average slow based on input type Type. Type=1 is simple moving average, Type=2 is exponential moving average.
Set backtest time range start and finish.
Define crossover function: when fast crosses above slow, generate buy signal; when fast crosses below slow, generate sell signal.
When crossover function is triggered, if within backtest time range, issue open long or close short order.
When backtest window ends or crossover function crosses below, issue close long order.
Plot the fast moving average fast and slow moving average slow.
This strategy uses the crossover of fast and slow moving averages to determine the trend within the holding period and generate trading signals accordingly. The backtest time window simulates real trading.
Advantage Analysis
The advantages of this strategy:
Moving averages are effective in determining trends and filtering random fluctuations.
The combination of fast and slow moving averages can identify trend changes.
The parameters of moving averages can be adjusted to adapt to different period trends.
Flexible choice between simple and exponential moving averages.
Backtest functionality allows testing and optimizing strategy parameters.
Simple and clear logic, easy to understand and implement.
Drawing moving average charts allows visual determination of trends and effects.
Risk Analysis
Some risks of this strategy:
May generate false signals during range-bound periods.
Moving averages have lagging effect, may miss turn points.
Relies solely on moving average crossover, no other indicators or filters.
Does not consider trading costs.
No stop loss strategy.
Unreasonable parameter settings may affect strategy performance.
Improper selection of backtest time range may cause overfitting.
Optimization Directions
The strategy can be optimized in the following aspects:
Add other indicators like MACD, RSI to confirm signals and improve accuracy.
Add stop loss strategy to control single loss.
Optimize moving average parameters for different periods.
Add position sizing based on market conditions.
Consider trading costs, adjust entry and exit points.
Test longer timeframe to avoid overfitting.
Continuously optimize parameters using walks forward analysis.
Summary
The moving average crossover strategy is a simple and practical trend following strategy. It can filter random fluctuations and identify trend directions. But it also has some problems like lagging effect, and should be combined with other indicators. Continuous optimization and testing can improve strategy performance and make it more reliable for live trading. Overall, this strategy suits investors with relatively low requirements for trend determination.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 13 | FastMA |
| v_input_2 | 144 | SlowMA |
| v_input_3 | true | Type (1 = SMA, 2 = EMA) |
| v_input_4 | false | SlowMAIsFactor |
| v_input_5 | true | From Day |
| v_input_6 | true | From Month |
| v_input_7 | 2018 | From Year |
| v_input_8 | true | To Day |
| v_input_9 | true | To Month |
| v_input_10 | 2020 | To Year |
Source Pine Script
/*backtest
start: 2023-09-13 00:00:00
end: 2023-09-20 00:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=2
// strategy("MavCrossover v2", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)
// Revision: 1
// Author: @ToS_MavericK
// === INPUT SMA ===
fastMA = input(defval = 13, title = "FastMA", minval = 1, step = 1)
slowMA = input(defval = 144, title = "SlowMA", minval = 1, step = 1)
Type = input(defval = 1, title = "Type (1 = SMA, 2 = EMA)", minval = 1, maxval = 2, step = 1)
SlowMAIsFactor = input(false)
slowMA := SlowMAIsFactor == true ? round(fastMA * slowMA) : slowMA
// === INPUT BACKTEST RANGE ===
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 = 2018, title = "From Year", minval = 2012)
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 = 2020, title = "To Year", minval = 2012)
// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => true // create function "within window of time"
// === MA SETUP ===
fast = Type == 1 ? sma(close, fastMA) : ema(close, fastMA)
slow = Type == 1 ? sma(close, slowMA) : ema(close, slowMA)
// === EXECUTION ===
strategy.entry("L", strategy.long, when = crossover(fast, slow) and window()) // buy long when "within window of time" AND crossover
strategy.close("L", when = crossunder(fast, slow) or time > finish) // sell long when window ends OR crossunder
plot(fast, title = 'FastMA', color = yellow, linewidth = 2, style = line) // plot FastMA
plot(slow, title = 'SlowMA', color = aqua, linewidth = 2, style = line) // plot SlowMAPublished by ChaoZhang on the FMZ strategy square · last modified 2023-09-21. 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-427436.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
- Bollinger Band Moving Average Crossover StrategyPine Script · Trend following · 1h
- Bollinger Bands and EMA Trend Following StrategyPine Script · Trend following · 1h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Candlestick Pattern Trend Filter StrategyPine Script · Trend following · 1d