0bps

Strategy vault / Breakout

Buying Strategy Based on Historical High Breakout

This strategy buys when the price breaks out above the historical n-day high in a bull market, with EMA stop loss. It belongs to trend following strategies.

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

Overview

This strategy buys when the price breaks out above the historical n-day high in a bull market, with EMA stop loss. It belongs to trend following strategies.

Strategy Logic

  1. Calculate the highest price over the past n days as the historical high price.

  2. Buy when the current close exceeds the historical high price.

  3. Use x-day EMA as the stop loss. Exit when price drops below EMA.

  4. Values of n and x adjustable via parameters, default to 200-day high and 90-day EMA.

  5. Simple and clear logic easy to implement.

Advantages

  1. Automatically follows trends formed by new highs.

  2. EMA trailing stop locks in most profits.

  3. No need to predict prices, just follow buy signals.

  4. Default parameters work well in bull markets.

  5. Concise code easy to understand and modify.

Risks

  1. Massive losses when bull market ends.

  2. Improper stop loss setups lead to premature or delayed stops.

  3. Unable to predict strength and pullback of new highs.

  4. Strong bias makes it unsuitable for other markets.

  5. Parameter optimization risks overfitting to historical data.

Enhancement

  1. Test different parameter combinations for optimum values.

  2. Evaluate other stop methods like fixed percentage stops.

  3. Optimize stops to balance frequency and risk control.

  4. Add filters to avoid buying on noise.

  5. Research ways to gauge buy signal strength.

  6. Can add profit taking exits to lock in gains.

Conclusion

This strategy automates trend following on new highs with EMA trailing stops. Though effective in some cases, it needs expansion and optimization to become robust across all markets.

Parameters

NameDefaultWhat it controls
v_input_int_1200ATH period
v_input_int_290ema line

Source Pine Script

/*backtest
start: 2023-08-20 00:00:00
end: 2023-09-19 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gmhfund

//@version=5
strategy("ATH 200d",overlay=1)
plot(close)

bars = input.int(200, "ATH period", minval=5, maxval=2000, step=1)
range_ema = input.int(90,"ema line",minval=100,maxval=400,step=1)

ath_price = ta.highest(bars)[1]
plot(ath_price,color=color.blue)

line_ema = ta.ema(close,range_ema)
exit_condition = ta.crossunder(close,line_ema)
plot(line_ema,color=color.orange)


strategy.entry("Buy", strategy.long, 1, when = close > ath_price) // enter long by market if current open great then previous high
//strategy.close("Buy",when = close < strategy.position_avg_price*0.9 )
strategy.close("Buy",when = exit_condition )

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-20. 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/Buying-Strategy-Based-on-Historical-High-Breakout.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