Strategy vault / Trend following
Moving Average Crossover with Trailing Stop Loss Strategy
This strategy uses two Simple Moving Averages (SMAs) with different periods to capture price trends and incorporates the Relative Strength Index (RSI) and Average True Range (ATR) indicators to optimize trade signals…

Overview
This strategy uses two Simple Moving Averages (SMAs) with different periods to capture price trends and incorporates the Relative Strength Index (RSI) and Average True Range (ATR) indicators to optimize trade signals and risk management. A buy signal is generated when the short-term SMA crosses above the long-term SMA, and a sell signal is generated when the opposite occurs. The strategy employs a trailing stop loss method, dynamically adjusting the take profit and stop loss levels based on price movements to better protect profits and control risks.
Strategy Principle
- Calculate two SMAs with different periods, defaulting to 10 and 30.
- Generate a buy signal when the short-term SMA crosses above the long-term SMA, and a sell signal when the short-term SMA crosses below the long-term SMA.
- Upon buying, set the stop loss and take profit levels based on the current closing price, defaulting to 2 units below and 6 units above the closing price, respectively.
- Dynamically adjust the take profit level during the holding period to better protect profits based on price movements.
- Use the 14-period RSI and ATR indicators to assist in assessing market trends and volatility, optimizing trade signals.
Strategy Advantages
- Simplicity: The strategy is based on the classic moving average crossover principle, with clear logic and easy to understand and implement.
- Trend following: By using two SMAs with different periods, the strategy effectively captures medium to long-term market trends and adapts to various market environments.
- Dynamic stop loss and take profit: The trailing stop loss method dynamically adjusts the take profit and stop loss levels based on price movements, protecting profits while controlling risks.
- Multi-indicator synergy: Combining RSI and ATR indicators provides a more comprehensive assessment of market trends and volatility, improving the reliability of trade signals.
Strategy Risks
- Parameter optimization risk: SMA periods, take profit and stop loss levels, and other parameters need to be optimized for different markets and instruments. Improper parameter settings may lead to poor strategy performance.
- Choppy market risk: In choppy market conditions, frequent trade signals may result in overtrading and rapid capital depletion.
- Trend reversal risk: When market trends reverse, the strategy may experience consecutive losses.
Strategy Optimization Directions
- Dynamic parameter optimization: Dynamically adjust key parameters such as SMA periods and take profit/stop loss levels based on market changes to improve the strategy's adaptability.
- Signal filtering: Introduce additional technical indicators or market sentiment indicators for secondary confirmation of trade signals, reducing misjudgments and overtrading.
- Position sizing: Dynamically adjust position sizes based on market volatility and account risk tolerance to control single trade risk.
- Multi-instrument synergy: Apply the strategy to multiple related instruments and use inter-instrument correlations for hedging to reduce overall portfolio risk.
Summary
The Moving Average Crossover with Trailing Stop Loss Strategy is a quantitative trading strategy based on classic technical analysis principles. It captures market trends using two SMAs with different periods and dynamically controls risk using a trailing stop loss method. The strategy also incorporates RSI and ATR indicators for a more comprehensive assessment of market conditions. Although the strategy has clear logic and is easy to implement, it is essential to consider issues such as parameter optimization, choppy market risk, and trend reversal risk in practical applications. Future optimizations can focus on dynamic parameter optimization, signal filtering, position sizing, and multi-instrument synergy to improve the strategy's stability and profitability.
Source Pine Script
/*backtest
start: 2023-05-23 00:00:00
end: 2024-05-28 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
// suitable for : AMZN - 30 minutes, MSFT - 30 minutes, NVDA -15 minutes
strategy("AAPL-SIMPLE_SMA", overlay=true)
// Create Indicator's
// Create Indicator's
shortSMA = ta.sma(close, 10)
longSMA = ta.sma(close, 30)
rsi = ta.rsi(close, 14)
atr = ta.atr(14)
qty = 1
// Specify crossover conditions
longCondition = ta.crossover(shortSMA, longSMA)
shortCondition = ta.crossunder(shortSMA, longSMA)
// // Execute trade if condition is True
if (longCondition)
stopLoss = close -2
// stopLoss=1
takeProfit = close +6
action = "buy"
strategy.entry("long", strategy.long, qty=qty)
// strategy.exit("exit", "long", stop=stopLoss, limit=takeProfit)
strategy.exit("exit", "long", limit=takeProfit)
alert('{"TICKER":"'+syminfo.ticker+'","ACTION":"'+action+'","PRICE":"'+str.tostring(close)+'","STOPLOSS":"'+str.tostring(stopLoss)+'","TAKEPROFIT":"'+str.tostring(takeProfit)+'","QTY":"'+str.tostring(qty)+'"}')
plot(shortSMA)
plot(longSMA, color=color.purple)Published by ChaoZhang on the FMZ strategy square · last modified 2024-05-29. 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-with-Trailing-Stop-Loss-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
- BB Percentage Index Trend Fading StrategyPine Script · Trend following · 1h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dual Mode Adaptive Trend Trading Strategy EMA Crossover with ATR Volatility Based Risk Management SystemPine Script · Trend following · 1d
- Dynamic Dual EMA Crossover Strategy with Adaptive Profit Loss ControlPine Script · Trend following · 1d
- Dynamic Signal Line Trend Following and Volatility Filtering StrategyPine Script · Trend following · 1h