Dynamic Trendline Breakout Reversal Trading Strategy
This strategy is a breakout trading system based on linear regression trendlines. It executes trades when price breaks through the trendline by a certain percentage, incorporating stop-loss, take-profit, and position…

Overview
This strategy is a breakout trading system based on linear regression trendlines. It executes trades when price breaks through the trendline by a certain percentage, incorporating stop-loss, take-profit, and position reversal mechanisms. The core concept is to capture sustained price movements following trendline breakouts while using position reversal to handle false signals.
Strategy Principle
The strategy uses the ta.linreg function to calculate a linear regression trendline over a specified period as the primary trend indicator. Long signals are generated when price breaks above the trendline by more than the set threshold, while short signals occur when price breaks below. The strategy employs a unidirectional position mechanism, allowing only long or short positions at any time. Risk management includes stop-loss and take-profit conditions, along with a position reversal mechanism that automatically opens a counter position with increased size when stops are hit.
Strategy Advantages
- Strong trend following capability: Linear regression trendlines effectively capture market trends and reduce false breakouts.
- Comprehensive risk control: Implements stop-loss and take-profit mechanisms to effectively control single trade risk.
- Position reversal mechanism: Quickly adjusts position direction during trend reversals with increased position size.
- Breakout confirmation: Uses threshold settings to filter minor fluctuations and improve signal reliability.
- Flexible position management: Controls overall position risk through maximum trade size limits and unidirectional positioning.
Strategy Risks
- Choppy market risk: May trigger frequent false breakout signals in ranging markets, leading to consecutive losses.
- Reversal trading risk: Position reversal mechanism may amplify losses during severe market volatility.
- Parameter sensitivity: Strategy performance heavily depends on parameter settings, which may lead to overtrading or missed opportunities.
- Slippage impact: Actual execution prices for stop and limit orders may significantly deviate from expected levels in fast markets.
- Money management risk: Inappropriate reversal multiplier settings may result in overly aggressive capital utilization.
Strategy Optimization Directions
- Incorporate volatility indicators: Dynamically adjust breakout thresholds based on market volatility to improve adaptability.
- Enhance reversal mechanism: Add conditional checks for reversals, such as trend strength indicators, to avoid unsuitable market conditions.
- Improve position sizing: Implement dynamic position management based on account equity and market volatility.
- Add market environment filters: Include trend strength and market state assessments to reduce trading frequency in unfavorable conditions.
- Optimize stop-loss methods: Introduce trailing stops or ATR-based dynamic stops for more flexible risk management.
Summary
This strategy constructs a complete trading system using linear regression trendlines and breakout trading concepts. It manages risk through stop-loss, take-profit, and position reversal mechanisms, demonstrating good trend-following capabilities. However, careful consideration is needed for parameter settings and market environment selection. Thorough parameter optimization and backtesting are recommended before live trading. Future improvements can focus on incorporating additional technical indicators and optimizing trading rules to enhance stability and adaptability.
Source Pine Script
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-25 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("BTC Trendline Strategy - 1min - One Direction", overlay=true)
// Input Settings
stop_loss_pct = input.float(10, title="Stop Loss Percentage", minval=0.1, step=0.1) / 100
take_profit_pct = input.float(10, title="Take profit percentage", minval=0.1, step=0.1) / 100
multiplier = input.int(2, title="Multiply when stop loss is triggered", minval=1)
length = input.int(20, title="Trendline Calculation Period", minval=1)
breakout_threshold = input.float(1, title="Breakout Percentage", minval=0.1) / 100 // Set the amplitude condition for breakthrough
max_qty = 1000000000000.0 // Set the maximum allowable trade volume
// Calculate linear regression trend line
regression = ta.linreg(close, length, 0) // Use linear regression to calculate the trend line of the price
// Draw trend line
plot(regression, color=color.blue, linewidth=2, title="Linear Regression Trendline")
// Determine breakthrough conditions: add a price deviation condition
long_condition = close > (regression * (1 + breakout_threshold)) // Go long when the current price is above the trend line and the breakout exceeds the set percentage
short_condition = close < (regression * (1 - breakout_threshold)) // Go short when the current price is below the trend line and the breakout exceeds the set percentage
// Ensure that only one directional position can be held at a time: avoid holding long and short simultaneously
if (strategy.position_size == 0) // When No Position Held
if (long_condition)
strategy.entry("Long", strategy.long)
if (short_condition)
strategy.entry("Short", strategy.short)
// Stop Loss and Take Profit Settings
long_stop_loss = strategy.position_avg_price * (1 - stop_loss_pct)
long_take_profit = strategy.position_avg_price * (1 + take_profit_pct)
short_stop_loss = strategy.position_avg_price * (1 + stop_loss_pct)
short_take_profit = strategy.position_avg_price * (1 - take_profit_pct)
// Draw stop-loss and take-profit lines for debugging
plot(long_stop_loss, color=color.red, linewidth=1, title="Long Stop Loss")
plot(long_take_profit, color=color.green, linewidth=1, title="Long Take Profit")
plot(short_stop_loss, color=color.red, linewidth=1, title="Short Stop Loss")
plot(short_take_profit, color=color.green, linewidth=1, title="Short Take Profit")
// Stop Loss and Take Profit Exit Strategies
strategy.exit("LongExit", from_entry="Long", stop=long_stop_loss, limit=long_take_profit)
strategy.exit("ShortExit", from_entry="Short", stop=short_stop_loss, limit=short_take_profit)
// Reverse trading logic
reverse_qty = math.min(math.abs(strategy.position_size) * multiplier, max_qty) // Limit Maximum Trade Volume
if (strategy.position_size < 0 and close > short_stop_loss) // When stopping a short position, reverse to go long and double your position
strategy.entry("Long Reverse", strategy.long, qty=reverse_qty)
if (strategy.position_size > 0 and close < long_stop_loss) // When stopping a long position, reverse to short and double your position
strategy.entry("Short Reverse", strategy.short, qty=reverse_qty)
// Print logs to help debug stop loss
if (strategy.position_size > 0)
label.new(bar_index, close, text="Long SL: " + str.tostring(long_stop_loss), color=color.green, style=label.style_label_up)
if (strategy.position_size < 0)
label.new(bar_index, close, text="Short SL: " + str.tostring(short_stop_loss), color=color.red, style=label.style_label_down)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-12-27. 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/Dynamic-Trendline-Breakout-Reversal-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
- 5 Day Moving Average Channel Breakout Strategy Combined With Mileage ConceptPine Script · Breakout · 1d
- ATR Trailing Stops StrategyPine Script · Breakout · 1h
- ATR Trend Breakout StrategyPine Script · Breakout · 1d
- Adaptive Bollinger Breakout with Moving Average Quantitative Strategy SystemPine Script · Breakout · 1h
- Adaptive Channel Breakout StrategyPine Script · Breakout · 1d
- Adaptive Multi Period Trading Strategy Based on Wave Trend Momentum BreakoutPine Script · Breakout · 2h