Strategy vault / Mean reversion
Reversal Candlestick Backtesting Strategy
This strategy identifies candlestick patterns to track trading signals and incorporates take profit and stop loss logic for automated trading. It goes long or short when reversal patterns are identified and closes…

Overview
This strategy identifies candlestick patterns to track trading signals and incorporates take profit and stop loss logic for automated trading. It goes long or short when reversal patterns are identified and closes positions when take profit or stop loss is triggered.
Strategy Logic
Identify candlestick patterns: When candle body size is smaller than threshold and open equals close, it is identified as tracking signal.
Long/short: When reversal candlestick pattern identified, go long if previous close higher than 2 days ago, go short if previous close lower than 2 days ago.
Take profit/Stop loss: When price reaches entry price + take profit points when long, take profit; When price reaches entry price - take profit points when short, take profit; When price triggers stop loss point after long/short, stop loss.
Advantages
Candlestick reversal patterns effectively capture turning points of price, enhancing validity of trading signals.
Take profit/Stop loss mechanism effectively controls risks, locks in profits, avoids enlarging losses.
Automated trading without manual intervention reduces trading costs and improves efficiency.
Risks
Subjectivity in candlestick pattern identification may lead to misjudgements.
Improper take profit/stop loss point settings may miss larger trends or stop loss prematurely.
Strategy parameters need constant testing and optimization, otherwise overfitting.
Optimization Directions
Optimize candlestick identification conditions with more indicators to improve accuracy.
Test on different trading instruments, adjust take profit/stop loss points, optimize parameters.
Enrich strategy logic by adding algorithms to identify more trading signals.
Add position sizing module to dynamically adjust positions based on reference indicators.
Conclusion
This strategy identifies reversal signals through candlestick patterns and sets take profit/stop loss rules for automated trading. The strategy is simple and practical to a certain extent. But there is room for improvement in terms of identification accuracy and parameter optimization. Further testing and optimization are recommended before applying in live trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 10 | Take Profit pip |
| v_input_2 | 10 | Stop Loss pip |
| v_input_3 | 0.5 | Min. Size Body pip |
Source Pine Script
/*backtest
start: 2023-12-26 00:00:00
end: 2024-01-25 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
////////////////////////////////////////////////////////////
// Copyright by HPotter v1.0 30/01/2019
// This is a candlestick where the open and close are the same.
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title = "Doji Backtest", overlay = true)
input_takeprofit = input(10, title="Take Profit pip", step=0.01)
input_stoploss = input(10, title="Stop Loss pip", step=0.01)
input_minsizebody = input(0.5, title="Min. Size Body pip", step=0.01)
barcolor(abs(close - open) <= input_minsizebody ? open == close ? yellow : na : na)
possell = 0.0
posbuy = 0.0
pospricebuy = 0.0
pospricesell = 0.0
barcolornow = blue
pospricesell := close< close[2] ? abs(close - open) <= input_minsizebody ? open == close ? close : nz(pospricesell[1], 0) : nz(pospricesell[1], 0) : nz(pospricesell[1], 0)
possell := iff(pospricesell > 0 , -1, 0)
barcolornow := possell == -1 ? red: posbuy == 1 ? green : blue
pospricesell := iff(low <= pospricesell - input_takeprofit and pospricesell > 0, 0 , nz(pospricesell, 0))
pospricesell := iff(high >= pospricesell + input_stoploss and pospricesell > 0, 0 , nz(pospricesell, 0))
pospricebuy := close > close[2] ? abs(close - open) <= input_minsizebody ? open == close ? close : nz(pospricebuy[1], 0) : nz(pospricebuy[1], 0) : nz(pospricebuy[1], 0)
posbuy := iff(pospricebuy > 0 , 1, 0)
barcolornow := posbuy == 1 ? green: barcolornow
pospricebuy := iff(high >= pospricebuy + input_takeprofit and pospricebuy > 0, 0 , nz(pospricebuy, 0))
pospricebuy := iff(low <= pospricebuy - input_stoploss and pospricebuy > 0, 0 , nz(pospricebuy, 0))
barcolor(barcolornow)
if (posbuy == 0 and possell == 0)
strategy.close_all()
if (posbuy == 1)
strategy.entry("Long", strategy.long)
if (possell == -1)
strategy.entry("Short", strategy.short)
pospricebuy := iff(high <= pospricebuy + input_takeprofit and pospricebuy > 0, 0 , nz(pospricebuy, 0))
pospricebuy := iff(low >= pospricebuy - input_stoploss and pospricebuy > 0, 0 , nz(pospricebuy, 0))
pospricesell := iff(low <= pospricesell - input_takeprofit and pospricesell > 0, 0 , nz(pospricesell, 0))
pospricesell := iff(high >= pospricesell + input_stoploss and pospricesell > 0, 0 , nz(pospricesell, 0))
Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-26. 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/Reversal-Candlestick-Backtesting-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
- A Trend Reversal Strategy Based on Moving Averages Price Patterns and VolumePine Script · Mean reversion · 1d
- ATR and EMA based Dynamic Take Profit and Stop Loss Adaptive StrategyPine Script · Mean reversion · 1h
- Adaptive Mean Reversion Trading Strategy Based on Chande Momentum OscillatorPine Script · Mean reversion · 1d
- Adaptive Moving Average Tracking StrategyPine Script · Mean reversion · 1d