Strategy vault / Mean reversion
Bearish Harami Reversal Backtest Strategy
The Bearish Harami Reversal Backtest Strategy identifies bearish Harami reversal patterns in candlestick charts and automatically trades them. It goes short when detecting a bearish Harami pattern and closes the…

Overview
The Bearish Harami Reversal Backtest Strategy identifies bearish Harami reversal patterns in candlestick charts and automatically trades them. It goes short when detecting a bearish Harami pattern and closes the position when the stop loss or take profit is triggered.
Strategy Logic
The core pattern recognition indicator of this strategy is: the close of the first candle is a long bullish candle and the second candle's close is inside the first candle's body, forming a bearish candle. This indicates a potential Bearish Harami reversal pattern. When this pattern forms, the strategy goes short.
The specific logic is:
- Calculate if the body size of the first candle ABS(Close1 - Open1) is greater than the set minimum body size
- Check if the first candle is bullish Close1 > Open1
- Check if the current candle is bearish Open > Close
- Check if current candle's open is less than or equal to previous close Open <= Close1
- Check if previous candle's open is less than or equal to current candle's close Open1 <= Close
- Check if current candle's body is less than previous body Open - Close < Close1 - Open1
- If all conditions pass, a Bearish Harami has formed and the strategy goes short.
Advantage Analysis
The advantages of this strategy are:
- Utilizes the strong bearish reversal signal of Harami for higher profit probability
- Extensive backtest data results are positive
- Simple clear logic that is easy to understand and optimize
- Customizable stop loss and take profit for risk control
Risk Analysis
There are also some risks:
- Market may have false breakouts leading to losing positions. Can widen stop loss or add filters.
- High volatility may trigger stop loss prematurely. Should choose lower volatility products.
- Insufficient backtest data may not reflect real market conditions. Should increase test data size and verify in live trading.
Optimization Directions
The strategy can be further optimized in the following areas:
- Add Volume, MACD and other filters to improve signal quality
- Optimize stop loss and take profit strategies, adjust levels dynamically
- Increase position holding efficiency, combine with trend and other factors to reduce ineffective trades
- Test different trading products to find lower volatility alternatives
Conclusion
The Bearish Harami Reversal Backtest Strategy has clear, easy to understand logic, good backtest results and controllable risks. It has room for live trading adjustments and optimizations. Overall the trading signals are reliable and worth further optimizations and verification in live trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 20 | Take Profit pip |
| v_input_2 | 10 | Stop Loss pip |
| v_input_3 | 3 | Min. Size Body pip |
Source Pine Script
/*backtest
start: 2023-11-15 00:00:00
end: 2023-11-19 23:00:00
period: 15m
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
////////////////////////////////////////////////////////////
// Copyright by HPotter v1.0 16/01/2019
// This is a bearish reversal pattern formed by two candlesticks in which a short
// real body is contained within the prior session's long real body. Usually the
// second real body is the opposite color of the first real body. The Harami pattern
// is the reverse of the Engulfing pattern.
//
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title = "Bearish Harami Backtest", overlay = true)
input_takeprofit = input(20, title="Take Profit pip")
input_stoploss = input(10, title="Stop Loss pip")
input_minsizebody = input(3, title="Min. Size Body pip")
barcolor(abs(close- open) >= input_minsizebody ? close[1] > open[1] ? open > close ? open <= close[1] ? open[1] <= close ? open - close < close[1] - open[1] ? yellow :na :na : na : na : na : na)
pos = 0.0
barcolor(nz(pos[1], 0) == -1 ? red: nz(pos[1], 0) == 1 ? green : blue )
posprice = 0.0
posprice := abs( close - open) >= input_minsizebody? close[1] > open[1] ? open > close ? open <= close[1] ? open[1] <= close ? open - close < close[1] - open[1] ? close :nz(posprice[1], 0) :nz(posprice[1], 0) : nz(posprice[1], 0) : nz(posprice[1], 0) : nz(posprice[1], 0): nz(posprice[1], 0)
pos := iff(posprice > 0, -1, 0)
if (pos == 0)
strategy.close_all()
if (pos == -1)
strategy.entry("Short", strategy.short)
posprice := iff(low <= posprice - input_takeprofit and posprice > 0, 0 , nz(posprice, 0))
posprice := iff(high >= posprice + input_stoploss and posprice > 0, 0 , nz(posprice, 0))
Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-23. 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/Bearish-Harami-Reversal-Backtest-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
- 1 2 3 Pattern Quantitative Trading Strategy with EMAs MACD and 4th Candle ExtensionPine Script · Mean reversion · 1h
- 3 10 Oscillator Profile Flagging StrategyPine Script · Mean reversion · 1d
- ATR and EMA based Dynamic Take Profit and Stop Loss Adaptive StrategyPine Script · Mean reversion · 1h
- Advanced 15 Minute Chart Trading Signal StrategyPine Script · Mean reversion · 3h
- BONK Multi Factor Trading StrategyPine Script · Mean reversion · 1d
- Bitcoin Futures Position Trading StrategyPine Script · Mean reversion · 1h