Trend Following with Breakout and Frequency Filter Long Only
This strategy is a trend following strategy based on breakout and frequency filtering, only taking long positions. The main idea of the strategy is to use the EMA indicator to determine the current trend direction…

Overview
This strategy is a trend following strategy based on breakout and frequency filtering, only taking long positions. The main idea of the strategy is to use the EMA indicator to determine the current trend direction, generate a long signal when the price breaks out of the highest price within a certain range, and use a frequency filter to control the trading frequency to avoid opening positions too frequently. The strategy also sets a stop loss point to control risk and closes positions when the trend ends.
Strategy Principle
- Calculate the EMA indicator to determine the current trend direction. When the closing price is above the EMA, it is considered a bullish trend.
- Calculate the highest price within a certain range as the breakout condition. When the closing price breaks out of the highest price within the shortest or longest lookback period and the current trend is bullish, a long signal is generated.
- Introduce a frequency filter to control the minimum interval time between consecutive position openings to avoid excessive trading frequency.
- Set a stop loss point. When the price falls below the stop loss price, close the position to control risk.
- Define the trend end signal. When the closing price falls below the EMA, the trend is considered to have ended. If a long position is held at this time, close the position.
Strategy Advantages
- Trend following: By using the EMA indicator to determine the trend direction and trading in line with the trend, it helps to improve strategy returns.
- Breakout confirmation: Using price breakout as the entry signal allows for timely entry at the beginning of the trend, capturing more profit potential.
- Frequency control: Introducing a frequency filter to control the time interval between consecutive position openings avoids excessive trading and reduces trading costs and risks.
- Stop loss protection: Setting a stop loss point to promptly stop loss when the price moves in the opposite direction by a certain magnitude effectively controls downside risk.
- Dynamic position closing: Dynamically closing positions based on the trend end signal allows for timely locking in of existing profits and avoids losses caused by trend reversal.
Strategy Risks
- Parameter sensitivity: The performance of the strategy is relatively sensitive to parameter selection, and different parameter settings may lead to significant differences in strategy performance. Sufficient backtesting and optimization of parameters are required.
- Breakout failure: Price breakouts do not guarantee that the trend will definitely continue, and there may be cases of breakout failure, resulting in consecutive losses for the strategy.
- Trend recognition: The strategy relies on the EMA indicator to judge the trend, but the EMA indicator may experience lag or misjudgment, affecting the accuracy of the strategy.
- Frequent trading: Although the strategy introduces a frequency filter, frequent position opening and closing may still occur when market volatility is high, increasing trading costs.
- Stop loss risk: The setting of the stop loss point may not completely avoid the maximum drawdown of the strategy, and large losses may still occur in extreme market conditions.
Strategy Optimization Directions
- Parameter optimization: Optimize key parameters of the strategy, such as EMA length, lookback period length, stop loss percentage, etc., to find the optimal parameter combination and improve strategy stability and profitability.
- Signal filtering: After the breakout signal is generated, other technical indicators or conditions can be introduced to confirm the signal a second time, improving signal quality and reducing misjudgments and false signals.
- Trend judgment: Try using other trend judgment indicators such as MACD, DMI, etc., or combine multiple indicators to jointly judge the trend and improve the accuracy of trend recognition.
- Dynamic stop loss: Dynamically adjust the stop loss point according to market volatility conditions, such as using the ATR indicator to calculate the dynamic stop loss price or introducing a trailing stop loss strategy to better control risk.
- Position management: Optimize the position management strategy, dynamically adjust the position size according to market volatility and account capital conditions, control the risk exposure of a single transaction, and improve capital utilization efficiency.
Summary
This strategy is a trend following strategy based on breakout and frequency filtering. It uses the EMA indicator to determine the trend direction, uses price breakout as the entry signal, introduces a frequency filter to control the trading frequency, and sets a stop loss point to control risk. The advantages of the strategy lie in trend following, breakout confirmation, frequency control, stop loss protection, and dynamic position closing, but it also has potential risks such as parameter sensitivity, breakout failure, trend recognition, frequent trading, and stop loss risk. To further optimize the strategy, we can start from aspects such as parameter optimization, signal filtering, trend judgment, dynamic stop loss, and position management to improve the stability and profitability of the strategy.
Source Pine Script
/*backtest
start: 2023-05-22 00:00:00
end: 2024-05-27 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Trend Following with Breakout and Frequency Filter (Long Only)", overlay=true)
// Input Parameters
emaLength = input.int(50, title="EMALength")
lookbackPeriodMin = input.int(80, title="Shortest look-back period")
lookbackPeriodMax = input.int(120, title="Longest look-back period")
stopLossPct = input.float(2, title="Stop Loss Percentage") / 100 // Stop Loss Percentage
minHoldBars = input.int(10, title="Minimum PositionKNumber of lines") // Minimum PositionKNumber of lines
// CalculationEMA
ema = ta.ema(close, emaLength)
// Calculate highest and lowest price
highestHigh = ta.highest(high, lookbackPeriodMax)
lowestLow = ta.lowest(low, lookbackPeriodMax)
// Define trend direction
isBullish = close > ema
// Define breakout signal
breakoutCondition = (ta.crossover(close, highestHigh[lookbackPeriodMin]) or ta.crossover(close, highestHigh[lookbackPeriodMax])) and isBullish
// Calculate stop-loss point
stopLossLevelLong = close * (1 - stopLossPct)
// DrawEMA
plot(ema, title="EMA", color=color.blue)
// Record the last opening time
var float lastEntryTime = na
// Execute the strategy and label the signal
if (breakoutCondition and (na(lastEntryTime) or (time - lastEntryTime) > minHoldBars * timeframe.multiplier))
strategy.entry("go long", strategy.long)
label.new(bar_index, high, text="buy", style=label.style_label_up, color=color.green, textcolor=color.white)
strategy.exit("stop loss", from_entry="go long", stop=stopLossLevelLong)
lastEntryTime := time
// Define trend end signal
exitCondition = close < ema
if (exitCondition and (strategy.position_size > 0) and (time - lastEntryTime) > minHoldBars * timeframe.multiplier)
strategy.close("go long")
label.new(bar_index, low, text="sell", style=label.style_label_down, color=color.red, textcolor=color.white)Published by ChaoZhang on the FMZ strategy square · last modified 2024-05-28. 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-with-Breakout-and-Frequency-Filter-Long-Only.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 Multi Period Trading Strategy Based on Wave Trend Momentum BreakoutPine Script · Breakout · 2h
- BB Breakout StrategyPine Script · Breakout · 1d
- BMSB Breakout Strategy BMSBPine Script · Breakout · 1h
- Bollinger Band T3 Moving Average StrategyPine Script · Breakout · 10m
- Bollinger Bands Dual track Breakthrough StrategyPine Script · Breakout · 30m
- Bollinger Breakout StrategyPine Script · Breakout · 30m