Strategy vault / Trend following
Moving Average Crossover Strategy 428084
This strategy utilizes the golden cross and death cross of moving averages to determine trends and identify potential buying and selling opportunities. It uses both fast and slow moving averages and generates trading…
Overview
This strategy utilizes the golden cross and death cross of moving averages to determine trends and identify potential buying and selling opportunities. It uses both fast and slow moving averages and generates trading signals based on their crossover.
Strategy Logic
The strategy employs two moving averages with different timeframes. The first MA has a shorter timeframe, set to 20 days, to capture short-term price moves. The second MA has a longer timeframe, set to 120 days, to gauge the long-term trend.
When the faster MA crosses above the slower MA, a golden cross occurs, signaling an upward trend in the short term, and a buy signal is generated. When the faster MA crosses below the slower MA, a death cross occurs, signaling a downward trend in the short term, and a sell signal is generated.
The strategy uses ta.crossover and ta.crossunder to detect the crossover of the MAs. Once a crossover is identified, a corresponding buy or sell signal is triggered.
Advantage Analysis
The biggest advantage of this strategy is its simplicity. Moving averages are among the most common technical analysis tools and easy to understand even for non-professionals. MAs also effectively filter out market noise and identify trend direction.
Compared to more complex indicators, MAs are relatively straightforward to implement in a strategy. It only requires optimizing the MA periods to create a robust system.
Moreover, the MA strategy offers flexibility. The parameters can be adjusted for different products and timeframes, from long term to short term.
Risk Analysis
The major risk is whipsaws generating frequent false signals when the trend oscillates. In this case, the MA periods should be properly tuned to filter out noise.
Another potential risk is the lagging nature of MAs. It takes time for MAs to reflect new trends, which may cause slippage.
Also, the strategy does not consider the impact of sudden events like major news. These could invalidate the effectiveness of MAs. Stops should be implemented to control risks.
Optimization Directions
The strategy can be further enhanced through:
Adding filters like volume to avoid false signals in range-bound markets.
Using adaptive MAs that adjust periods based on volatility.
Combining other indicators like MACD and Stochastics to confirm signals.
Establishing price channels and only considering signals on breakouts.
Implementing stop loss and take profit to increase robustness.
Conclusion
In summary, the MA crossover strategy generates signals by crossing fast and slow MAs. It is easy to use and identifies trends, but also carries risks of false signals and lags. With optimized parameters, added filters, and indicator combinations, it can greatly improve viability. Overall, the MA strategy is a practical trend following system worth studying and applying for traders.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1_close | 0 | Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4 |
| v_input_2 | 20 | 1st MA Length |
| v_input_string_1 | 0 | 1st MA Type: EMA |
| v_input_3 | 120 | 2nd MA Length |
| v_input_string_2 | 0 | 2nd MA Type: EMA |
Source Pine Script
/*backtest
start: 2022-09-21 00:00:00
end: 2023-09-27 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/
// © brandlabng
//@version=5
//study(title="Holly Grail FX", overlay = true)
strategy('HG|E30m', overlay=true)
src = input(close, title='Source')
price = request.security(syminfo.tickerid, timeframe.period, src)
ma1 = input(20, title='1st MA Length')
type1 = input.string('EMA', '1st MA Type', options=['EMA'])
ma2 = input(120, title='2nd MA Length')
type2 = input.string('EMA', '2nd MA Type', options=['EMA'])
price1 = if type1 == 'EMA'
ta.ema(price, ma1)
price2 = if type2 == 'EMA'
ta.ema(price, ma2)
//plot(series=price, style=line, title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=plot.style_line, title='1st MA', color=color.new(#219ff3, 0), linewidth=2)
plot(series=price2, style=plot.style_line, title='2nd MA', color=color.new(color.purple, 0), linewidth=2)
longCondition = ta.crossover(price1, price2)
if longCondition
strategy.entry('Long', strategy.long)
shortCondition = ta.crossunder(price1, price2)
if shortCondition
strategy.entry('Short', strategy.short)Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-28. 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-428084.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
- EMA SMA Multi Indicator Comprehensive Trend Following StrategyPine Script · Trend following · 1h
- Multifactor Driven Trend Trading StrategyPine Script · Trend following · 1m
- Trading Strategy Following Dual Timeframe TrendsPine Script · Trend following · 1h
- Trend Tracking Strategy Based on Multi factor Model with Adaptive Trailing StoplossPine Script · Trend following · 1d
- BlackBit Trader XO Macro Trend Scanner StrategyPine Script · Trend following · 1h
- MACD Trend Following Trading Strategy Based on MACD IndicatorPine Script · Trend following · 10m