Strategy vault / Trend following
Adaptive Dynamic Stop Loss and Take Profit Strategy with SMA Crossover and Volume Filter
This strategy is an automated trading system based on Simple Moving Average (SMA) crossovers and volume filtering. It utilizes the crossover of fast and slow SMAs to generate entry signals, while incorporating volume…

Overview
This strategy is an automated trading system based on Simple Moving Average (SMA) crossovers and volume filtering. It utilizes the crossover of fast and slow SMAs to generate entry signals, while incorporating volume indicators to confirm trend strength. The strategy also includes dynamic stop-loss and take-profit mechanisms, as well as time-based exit conditions, aimed at optimizing risk management and enhancing profitability.
Strategy Principles
The core principles of this strategy are based on the following key components:
SMA Crossover Signals:
- Uses two Simple Moving Averages of different periods (fast SMA and slow SMA)
- Generates a long signal when the fast SMA crosses above the slow SMA
- Generates a short signal when the fast SMA crosses below the slow SMA
Volume Filtering:
- Calculates a Simple Moving Average of volume
- Long signals require current volume to be above the volume SMA
- Short signals require current volume to be below the volume SMA
Dynamic Stop-Loss and Take-Profit:
- Sets stop-loss and take-profit levels based on a percentage of the entry price
- Stop-loss and take-profit levels can be adjusted through input parameters
Time-Based Exits:
- Sets a maximum holding time (in number of bars)
- Automatically closes positions after the maximum holding time to prevent long-term adverse positions
Backtest Period Setting:
- Allows users to define a specific backtest time range
- Ensures the strategy runs only within the specified historical period
Strategy Advantages
Trend Following and Momentum Combination: By combining SMA crossovers and volume filtering, the strategy can capture strong trend movements while avoiding frequent trades in weak markets.
Flexible Risk Management: The dynamic stop-loss and take-profit mechanisms allow the strategy to automatically adjust risk exposure based on market volatility, helping to protect profits and limit potential losses.
Prevention of Overholding: The maximum holding time limit helps prevent the strategy from holding losing positions for extended periods in adverse market conditions, promoting effective use of capital.
High Customizability: Multiple adjustable parameters (such as SMA periods, stop-loss and take-profit percentages, maximum holding time, etc.) allow the strategy to be optimized for different markets and trading styles.
Visual Support: The strategy plots SMA lines and trade signals on the chart, facilitating intuitive understanding and analysis of strategy performance.
Strategy Risks
Lagging Nature: SMA indicators are inherently lagging, which may lead to delayed entries or missed opportunities in rapidly reversing markets.
False Breakout Risk: In ranging markets, SMA crossovers may produce frequent false breakout signals, leading to overtrading and increased transaction costs.
Volume Dependency: Over-reliance on volume indicators may mislead the strategy under certain market conditions, especially during periods of low liquidity or abnormal trading volumes.
Fixed Percentage Stop-Loss/Take-Profit: Using fixed percentage stop-loss and take-profit may not be suitable for all market conditions, especially during periods of dramatic volatility changes.
Limitations of Time-Based Exits: Fixed maximum holding times may lead to premature exits when favorable trends have not yet concluded, impacting potential returns.
Strategy Optimization Directions
Dynamic Parameter Adjustment: Implement dynamic adjustment of SMA periods, stop-loss and take-profit percentages, and maximum holding times to adapt to different market cycles and volatility.
Incorporate Additional Filters: Introduce other technical indicators (such as RSI, MACD, etc.) as additional filtering conditions to improve the accuracy of trade signals.
Adaptive Volume Thresholds: Develop dynamically adjusting volume threshold mechanisms to better adapt to volume characteristics in different market phases.
Improved Exit Mechanisms: Explore intelligent exit mechanisms based on market structure or momentum indicators to replace fixed-time exits, enhancing strategy adaptability.
Volatility Adjustment: Implement dynamic stop-loss and take-profit level adjustments based on market volatility to better manage risk and capture profits.
Multi-Timeframe Analysis: Integrate data analysis from multiple timeframes to improve the strategy's ability to identify market trends and reversals.
Machine Learning Optimization: Utilize machine learning algorithms to dynamically optimize strategy parameters, improving performance across different market environments.
Conclusion
The "Adaptive Dynamic Stop-Loss and Take-Profit Strategy with SMA Crossover and Volume Filter" is a comprehensive trading system that combines trend following, volume analysis, and risk management. By leveraging SMA crossovers and volume filtering, the strategy aims to capture strong market trends, while its dynamic stop-loss and take-profit mechanisms and time-based exit features provide flexible risk control. Although there are some inherent limitations, such as signal lag and dependence on fixed parameters, the strategy offers multiple optimization directions, including dynamic parameter adjustment, introduction of additional technical indicators, and the use of machine learning techniques. Through continuous optimization and improvement, this strategy has the potential to become a powerful and flexible automated trading tool suitable for various market conditions and trading styles.
Source Pine Script
/*backtest
start: 2024-06-30 00:00:00
end: 2024-07-30 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Simple_CrossOver_Bot_V1_EBO", overlay=true)
// INPUTS
dateStart_Year = input.int(2018, title="Start Year", minval=2000)
dateStart_Month = input.int(1, title="Start Month", minval=1, maxval=12)
dateStart_Day = input.int(1, title="Start Day", minval=1, maxval=31)
dateEnd_Year = input.int(2019, title="End Year", minval=2000)
dateEnd_Month = input.int(1, title="End Month", minval=1, maxval=12)
dateEnd_Day = input.int(1, title="End Day", minval=1, maxval=31)
fast_SMA_input = input.int(7, title="SMA Fast")
slow_SMA_input = input.int(25, title="SMA Slow")
volume_SMA_input = input.int(20, title="Volume SMA")
stop_loss_percent = input.float(1.0, title="Stop Loss (%)", step=0.1) / 100
take_profit_percent = input.float(2.0, title="Take Profit (%)", step=0.1) / 100
max_bars_in_trade = input.int(50, title="Max Bars in Trade", minval=1)
// INDICATORS
fast_SMA = ta.sma(close, fast_SMA_input)
slow_SMA = ta.sma(close, slow_SMA_input)
volume_SMA = ta.sma(volume, volume_SMA_input)
// STRATEGY
LONG = ta.crossover(fast_SMA, slow_SMA) and fast_SMA > slow_SMA and volume > volume_SMA
SHORT = ta.crossunder(fast_SMA, slow_SMA) and fast_SMA < slow_SMA and volume < volume_SMA
// TRIGGERS
testPeriodStart = timestamp(dateStart_Year, dateStart_Month, dateStart_Day)
testPeriodEnd = timestamp(dateEnd_Year, dateEnd_Month, dateEnd_Day)
timecondition = true
// Track bar index for entries
var int long_entry_bar_index = na
var int short_entry_bar_index = na
if timecondition
if LONG
strategy.entry(id="LONG", direction=strategy.long)
long_entry_bar_index := bar_index
if SHORT
strategy.entry(id="SHORT", direction=strategy.short)
short_entry_bar_index := bar_index
// Exit conditions for LONG
if not na(long_entry_bar_index) and bar_index - long_entry_bar_index >= max_bars_in_trade
strategy.close("LONG")
long_entry_bar_index := na
// Exit conditions for SHORT
if not na(short_entry_bar_index) and bar_index - short_entry_bar_index >= max_bars_in_trade
strategy.close("SHORT")
short_entry_bar_index := na
// Standard exits
if LONG
strategy.exit("Exit LONG", from_entry="LONG", stop=close * (1 - stop_loss_percent), limit=close * (1 + take_profit_percent))
if SHORT
strategy.exit("Exit SHORT", from_entry="SHORT", stop=close * (1 + stop_loss_percent), limit=close * (1 - take_profit_percent))
// PLOTS
plot(fast_SMA, color=color.green, linewidth=1, title="Fast SMA")
plot(slow_SMA, color=color.yellow, linewidth=1, title="Slow SMA")
plot(volume_SMA, color=color.blue, linewidth=1, title="Volume SMA")
plotshape(series=LONG, location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", size=size.small)
plotshape(series=SHORT, location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", size=size.small)
// Uncomment the following lines for alerts
// alertcondition(LONG, title="LONG")
// alertcondition(SHORT, title="SHORT")
Published by ChaoZhang on the FMZ strategy square · last modified 2024-07-31. 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/Adaptive-Dynamic-Stop-Loss-and-Take-Profit-Strategy-with-SMA-Crossover-and-Volume-Filter.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
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Dual Moving Average Convergence Trend Tracking StrategyPine Script · Trend following · 1d
- Dual SMA Crossover StrategyPine Script · Trend following · 5m
- Dynamic Moving Average and Bollinger Bands Cross Strategy with Fixed Stop Loss Optimization ModelPine Script · Trend following · 3h
- Dynamic Signal Line Trend Following Strategy Combining ATR and VolumePine Script · Trend following · 1h
- EMA SMA Trend Following with Swing Trading Strategy Combined Volume Filter and Percentage Take Profit Stop Loss SystemPine Script · Trend following · 1d