Strategy vault / Trend following
Golden Cross Death Cross Moving Average Trading Strategy
Strategy Logic: 1. Define three MAs - short period MA, long period MA and trend MA. The periods are 20, 200 and 50 respectively. 2. A buy signal is generated when the short period MA crosses above the long period MA…

Overview: This strategy implements golden cross and death cross trading based on three moving averages with different periods. It goes long when the short period MA crosses above the long period MA, and goes short when the short period MA crosses below the long period MA. The trend direction is determined by the trend MA line.
Strategy Logic:
Define three MAs - short period MA, long period MA and trend MA. The periods are 20, 200 and 50 respectively.
A buy signal is generated when the short period MA crosses above the long period MA. A sell signal is generated when the short period MA crosses below the long period MA.
Check if both the short and long MAs are above the trend MA. If not, the signal is filtered out to avoid trading against the trend.
Set stop loss and take profit as a percentage of the entry price. Optimize parameters based on backtesting.
Plot the MA crossover points to visualize entry signals.
Advantages:
Simple and intuitive strategy logic, easy to understand and implement.
Can effectively capture mid-term trends and trade with the momentum.
Filtering with trend MA avoids trading against the trend.
MA periods can be adjusted for different market conditions.
Customizable stop loss and take profit to control risks.
Risks:
Sharp volatile moves may trigger stop loss.
Larger losses when trend reverses.
Improper parameter tuning may lead to overtrading or missing opportunities.
Transaction costs need to be considered.
Enhancements:
Add volatility filter like ATR to avoid false signals.
Use machine learning to dynamically optimize parameters.
Add more indicators like MACD to determine trend.
Implement trailing stop loss to lock in profits.
Backtest to find optimal stop loss and take profit levels.
Conclusion:
The strategy captures trends effectively with clear logic and easy execution. Controlling risks with trend filter, stop loss and take profit. Parameter tuning requires optimization for market conditions. More indicators can improve performance. Suitable for mid-term trend trading. Performed well in backtest and demo trading. In live trading, beware of whipsaws and trend reversal risks. Has practical value overall.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_int_1 | 64 | Long MA Length |
| v_input_int_2 | true | Short MA Length |
| v_input_int_3 | 200 | Trend MA Length |
| v_input_1 | true | Long Stop Loss (%) |
| v_input_2 | 3 | Long Take Profit (%) |
| v_input_3 | true | Short Stop Loss (%) |
| v_input_4 | 3 | Short Take Profit (%) |
Source Pine Script
/*backtest
start: 2022-10-23 00:00:00
end: 2023-10-29 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("XAU M15", overlay=true)
// Define input parameters
long_length = input.int(64, title="Long MA Length")
short_length = input.int(1, title="Short MA Length")
trend_length = input.int(200, title="Trend MA Length")
// Calculate moving averages
long_ma = ta.sma(close, long_length)
short_ma = ta.sma(close, short_length)
trend_ma = ta.sma(close, trend_length)
// Plot moving averages on chart
plot(long_ma, color=color.blue, title="Long MA")
plot(short_ma, color=color.red, title="Short MA")
plot(trend_ma, color=color.green, title="Trend MA")
// Entry conditions
enterLong = ta.crossover(long_ma, short_ma) and long_ma > trend_ma and short_ma > trend_ma
enterShort = ta.crossunder(long_ma, short_ma) and long_ma < trend_ma and short_ma < trend_ma
if (enterLong)
strategy.entry("Long", strategy.long)
if (enterShort)
strategy.entry("Short", strategy.short)
// Exit conditions
exitLong = ta.crossunder(long_ma, short_ma)
exitShort = ta.crossover(long_ma, short_ma)
if (exitLong)
strategy.close("Long")
if (exitShort)
strategy.close("Short")
// Set stop loss and take profit levels
long_stop_loss_percentage = input(1, title="Long Stop Loss (%)") / 100
long_take_profit_percentage = input(3, title="Long Take Profit (%)") / 100
short_stop_loss_percentage = input(1, title="Short Stop Loss (%)") / 100
short_take_profit_percentage = input(3, title="Short Take Profit (%)") / 100
strategy.exit("Take Profit/Stop Loss", "Long", stop=close * (1 - long_stop_loss_percentage), limit=close * (1 + long_take_profit_percentage))
strategy.exit("Take Profit/Stop Loss", "Short", stop=close * (1 + short_stop_loss_percentage), limit=close * (1 - short_take_profit_percentage))
plotshape(series=enterLong, title="Buy Entry", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.tiny)
plotshape(series=enterShort, title="Sell Entry", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.tiny)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-30. 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/Golden-Cross-Death-Cross-Moving-Average-Trading-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
- 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