Strategy vault / Trend following
Trend Following Buy Dip Sell Peak Strategy
This strategy implements automated trend following trading by calculating Bollinger Bands to identify dips and peaks and using long-term and short-term moving averages to determine the overall trend direction. The…

Overview
This strategy implements automated trend following trading by calculating Bollinger Bands to identify dips and peaks and using long-term and short-term moving averages to determine the overall trend direction. The core idea is to buy dips and sell peaks according to the prevailing trend.
Strategy Logic
The key components of the strategy are:
Calculate Bollinger Bands with upper and lower bands based on close price and standard deviation.
Determine long-term and short-term trend using 300-period and 20-period SMA.
Generate buy signal when close breaks below lower band while long SMA is above and short SMA turns up.
Generate sell signal when close breaks above upper band while long SMA is below and short SMA turns down.
Use OCO orders to set stop loss and take profit.
With this design, the strategy can automatically identify dip buying and peak selling opportunities along the major trend direction.
Advantage Analysis
The advantages of this strategy include:
Automated trend detection without manual judgment.
Systematically capture dips for buying opportunities.
Systematically identify peak selling opportunities for profit taking.
Effective risk control using stop loss and take profit.
Filter out invalid signals to improve win rate.
Flexible trend following by position adjustment.
Clear logic and easy to understand and optimize.
Risk Analysis
The main risks to consider:
Inappropriate security selection could fail the trend tracking.
Improper parameter tuning may cause overtrading or missed trades.
Trend reversal from sudden events may lead to larger losses.
Stop loss too tight may cause excessive stops.
Insufficient liquidity may prevent full execution.
Overfitting with insufficient backtesting period.
The solutions include: select liquid stocks with clear trends; optimize parameters; watch out for news; relax stop loss; evaluate real trading volume; expand backtest period.
Optimization Directions
Some ways to optimize the strategy:
Optimize parameters like Bollinger period, standard deviation multiplier and moving average periods.
Add stop loss methods like trailing stop or moving average stop to better control risks.
Incorporate position sizing based on key levels to improve capital utilization efficiency.
Add volume filter to avoid invalid breakouts with low volume.
Add relative strength indicator to determine buy/sell bias.
Introduce machine learning for automatic parameter tuning and strategy evaluation.
Combine with other strategies to create multi-strategy portfolio for greater robustness.
These optimizations can further enhance the strategy's performance and stability.
Summary
The strategy offers a clear and understandable approach to systematically buy dips and sell peaks along the trend. With proper risk control, it has good profit potential. Further improvements can be made via parameter tuning, stop loss modification, position sizing, etc. The strategy serves as a solid foundation for automated trend following trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 15 | length |
| v_input_2 | 1.25 | mult |
| v_input_3 | 300 | longMAPeriod |
| v_input_4 | 20 | shortMAPeriod |
Source Pine Script
/*backtest
start: 2023-09-23 00:00:00
end: 2023-10-23 00:00:00
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("Buy Dip Sell Rip Strategy", overlay=true)
source = close
length = input(15, minval=1)
mult = input(1.25, minval=0.001, maxval=50)
longMAPeriod = input(300, minval=5)
shortMAPeriod = input(20, minval=5)
basis = sma(source, length)
longMA = sma(source, longMAPeriod)
prevLongMA = sma(close[1],longMAPeriod)
shortMA = sma(source, shortMAPeriod)
dev = mult * stdev(source, length)
upper = basis + dev
lower = basis - dev
buyEntry = crossover(source, lower)
sellEntry = crossunder(source, upper)
if (source > lower and source[1] < lower)
if (longMA < source and shortMA>source)
strategy.entry("BBandLE", strategy.long, stop=lower, oca_name="BollingerBands", comment="BBandLE")
else
strategy.close("BBandSE")
else
strategy.cancel(id="BBandLE")
if (source > upper and source[1] < upper)
if (longMA > source and shortMA < source)
strategy.entry("BBandSE", strategy.short, stop=upper, oca_name="BollingerBands", comment="BBandSE")
else
strategy.close("BBandLE")
else
strategy.cancel(id="BBandSE")
//plot(strategy.equity, title="equity", color=red, linewidth=2, style=areabr)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-24. 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/Trend-Following-Buy-Dip-Sell-Peak-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
- Dynamic Moving Average and Bollinger Bands Cross Strategy with Fixed Stop Loss Optimization ModelPine Script · Trend following · 3h
- Relative Volume Trend Following Trading StrategyPine Script · Trend following · 1d
- AlphaTrend Dual Tracking StrategyPine Script · Trend following · 1h
- Moving Average Crossover Strategy 430371Pine Script · Trend following · 1h
- Multi Moving Average and Bollinger Bands Crossover Options Quantitative Trading StrategyPine Script · Trend following · 5m
- Three RSI Moving Average Bands StrategyPine Script · Trend following · 1h