Strategy vault / Trend following
SMA Trend Following Strategy with Trailing Stop Loss and Disciplined Re Entry
This strategy identifies upward trends based on the slope of the Simple Moving Average (SMA) and enters long positions when specific conditions are met. It incorporates an optional trailing stop-loss mechanism to…

Overview
This strategy identifies upward trends based on the slope of the Simple Moving Average (SMA) and enters long positions when specific conditions are met. It incorporates an optional trailing stop-loss mechanism to protect profits by dynamically adjusting the stop-loss price. Furthermore, the strategy sets a condition for re-entry after a stop-loss event to prevent entering positions at excessively high prices. With these features, the strategy effectively captures upward trends, manages risk, and ensures disciplined trading.
Strategy Logic
- Calculate the SMA over the specified period and determine if its slope within a given window size is greater than the minimum slope threshold to identify an upward trend.
- When the SMA slope is positive and the current price is above the SMA, the strategy enters a long position.
- If the trailing stop-loss is enabled, the trailing stop price is calculated based on the current market price and the specified trailing stop percentage. The trailing stop price adjusts upwards as the price moves in favor of the position, protecting profits.
- The strategy exits the position when the price crosses below the SMA or when the trailing stop-loss is triggered.
- After a stop-loss exit, if the price is above the SMA by a specified percentage, the strategy will not re-enter the position to avoid buying at excessively high prices.
Strategy Advantages
- Trend Following: By utilizing the SMA slope to identify upward trends, the strategy effectively captures trending opportunities.
- Risk Management: The optional trailing stop-loss feature dynamically protects profits and limits potential losses.
- Disciplined Re-Entry: The re-entry condition after a stop-loss prevents buying at overextended prices, ensuring trading discipline.
- Parameter Flexibility: The strategy provides multiple adjustable parameters, such as SMA length, minimum slope, trailing stop percentage, etc., allowing for optimization based on different markets and trading styles.
Strategy Risks
- Parameter Sensitivity: The strategy's performance is sensitive to parameter selection, and suboptimal parameter settings may lead to subpar results.
- Choppy Markets: In choppy market conditions, frequent trades may result in high transaction costs and potential losses.
- Unforeseen Events: Unexpected market events and abnormal price movements can cause the strategy to fail or incur unexpected losses.
Strategy Optimization Directions
- Dynamic Parameter Optimization: Introduce adaptive mechanisms to dynamically adjust parameters such as SMA length, minimum slope, etc., based on market conditions to adapt to different market environments.
- Enhanced Risk Control: Incorporate additional risk management techniques, such as volatility-based position sizing, dynamic stop-loss, etc., to further control risk exposure.
- Long-Short Trading: Extend the strategy to support short selling, allowing for profiting from downward trends as well.
- Multi-Timeframe Confirmation: Combine signals from multiple timeframes to improve the reliability and robustness of trend identification.
Summary
This strategy leverages SMA trend following, trailing stop-loss, and disciplined re-entry mechanisms to capture upward trends while managing risk. By optimizing parameter settings, enhancing risk management, supporting long-short trading, and incorporating multi-timeframe confirmation, the strategy's adaptability and robustness can be further improved.
Source Pine Script
/*backtest
start: 2023-05-28 00:00:00
end: 2024-06-02 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("MA Incline Strategy with Optional Trailing Stop-Loss", overlay=true, calc_on_every_tick=true)
// Input parameters
windowSize = input.int(20, title="Window Size")
maLength = input.int(150, title="Moving Average Length")
minSlope = input.float(0.1, title="Minimum Slope")
useTrailingStop = input.bool(true, title="Use Trailing Stop-Loss")
trailingStopPercentage = input.float(2.8, title="Trailing Stop Percentage (%)") / 100
// Calculate the moving average
ma = ta.sma(close, maLength)
// Calculate the slope of the moving average over the window size
previousMa = ta.sma(close[windowSize], maLength)
slopeMa = (ma - previousMa) / windowSize
// Check conditions
isAboveMinSlope = slopeMa > minSlope
isAboveMa = close > ma
// Buy condition
buyCondition = isAboveMinSlope and isAboveMa
// Execute strategy
if (buyCondition and strategy.opentrades == 0)
strategy.entry("Long", strategy.long)
// Trailing stop-loss (optional)
if (strategy.opentrades == 1 and useTrailingStop and isAboveMa)
// Calculate the trailing stop price
trailPrice = close * (1 - trailingStopPercentage)
// Use the built-in strategy.exit function with the trailing stop
strategy.exit("Trail Stop", "Long", stop=trailPrice)
// Exit condition
sellCondition = ta.crossover(ma, close)
if (sellCondition and strategy.opentrades == 1)
strategy.close("Long")
Published by ChaoZhang on the FMZ strategy square · last modified 2024-06-03. 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/SMA-Trend-Following-Strategy-with-Trailing-Stop-Loss-and-Disciplined-Re-Entry.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
- Adaptive FVG Detection and MA Trend Trading Strategy with Dynamic ResistancePine Script · Trend following · 1d
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m