0bps

Strategy vault / Breakout

Dynamic Breakout Trend Following Strategy

This is a dynamic trend following breakout strategy. It tracks stock's highest and lowest price in real time. When price breaks through the highest price in recent period, it will go long. When price breaks through…

Author
ChaoZhang
Complexity
Starter · 38 lines
Parameters
2
Backtest bar
2h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This is a dynamic trend following breakout strategy. It tracks stock's highest and lowest price in real time. When price breaks through the highest price in recent period, it will go long. When price breaks through the lowest price in recent period, it will go short. Meanwhile, stop loss and take profit are set to control risks and ensure fixed risk reward ratio.

Strategy Logic

The core logic of this strategy is to track and trade price breakout points of trends. Specifically, the strategy calculates the highest high and lowest low in recent 20 days. When today's close price breaks through yesterday's highestHigh, it is considered as an upward trend breakout signal and will go long. When today's close price breaks through yesterday's lowestLow, it is considered as a downward trend breakout signal and will go short.

After going long or short, stop loss of 1% and take profit of 2% are set. This ensures fixed risk reward ratio of 2:1 for each trade. It effectively controls the risk of single trade.

Advantages

The biggest advantage of this strategy is quickly capturing reversal points of price trends, while controlling risks of each single trade. Main advantages are:

  1. Dynamic calculation of highest and lowest price, real time tracking of price trend changes, which can quickly capture price reversal signals.

  2. Taking breakout method for entries improves quality of entries.

  3. Setting stop loss and take profit to control risk reward ratio of single trade effectively manages trade risk.

  4. Simple and easy to understand logic, suitable for quant beginners.

  5. Less code which is easy for testing and optimization.

Risks

There are also some risks of this strategy:

  1. Following trends for entries may miss the best turning points of price reversal.

  2. Fixed stop loss and take profit cannot adapt to market change, may stop out or profit target prematurely.

  3. No pyramiding logic for later additional entries, cannot keep following trends.

  4. No consideration of big cycles, may conflict with major trend causing losses.

  5. No position sizing module, cannot control overall position management.

Optimization Directions

There is still large room for optimization, mainly in below directions:

  1. Add dynamic stop loss and take profit based on market volatility.

  2. Add trend direction filter based on moving average to avoid conflict major trends.

  3. Add trend strength indicator to ensure entering on strong trends only.

  4. Add pyramiding logic to maximize profits by keep following trends.

  5. Combine with position sizing module to dynamically adjust position size and control overall risk.

  6. Optimize parameters to find optimum parameter sets.

Summary

In summary, this strategy is suitable for quant beginners to learn and practice overall. Its advantage lies in simplicity and easy understanding, also with stop loss and take profit logic to control risk. But still has many aspects to optimize, can serve as chance for further learning. Generally speaking, this strategy is suitable to master from principle to application for beginners.

Parameters

NameDefaultWhat it controls
v_input_120Length
v_input_2trueStop Loss %

Source Pine Script

/*backtest
start: 2023-11-28 00:00:00
end: 2023-12-28 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Trend Following Breakout Strategy with 2:1 RRR", overlay=true)

// Define calculation of previous high and low
length = input(20, minval=1, title="Length")
highestHigh = highest(high, length)
lowestLow = lowest(low, length)

// Define conditions for buying and selling
longCondition = close > highestHigh[1] // The current closing price is higher than the previous period's high
shortCondition = close < lowestLow[1] // The current closing price is lower than the previous period's low

// To ensure a profit-loss ratio of2:1,We need to define stop-loss and target price
stopLoss = input(1, title="Stop Loss %") / 100
takeProfit = stopLoss * 2

// If buy conditions are met, enter long
if (longCondition)
    strategy.entry("Long", strategy.long)
    strategy.exit("Long TP", "Long", profit=takeProfit * close, loss=stopLoss * close)

// If sell conditions are met, enter short
if (shortCondition)
    strategy.entry("Short", strategy.short)
    strategy.exit("Short TP", "Short", profit=takeProfit * close, loss=stopLoss * close)

// Drawing shows previous high and previous low
plot(highestHigh, color=color.green, title="Previous High")
plot(lowestLow, color=color.red, title="Previous Low")

Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Dynamic-Breakout-Trend-Following-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open