
This is a mean reversion trading strategy that captures short-term price reversal opportunities by identifying consecutive bearish and bullish candle patterns. The core logic enters a long position after three consecutive bearish candles and exits after three consecutive bullish candles. The strategy can optionally incorporate an EMA filter to enhance trade quality.
The strategy is based on the following core elements: 1. Consecutive candle counter: Tracks the number of consecutive bullish and bearish candles 2. Entry condition: Triggers a long signal when specified number (default 3) of consecutive lower closing prices occurs 3. Exit condition: Triggers a closing signal when specified number (default 3) of consecutive higher closing prices occurs 4. EMA filter: Optional 200-period exponential moving average as trend filter 5. Trading time window: Can set specific trading start and end times to limit trading periods
This is a well-designed mean reversion strategy that generates returns by capturing short-term oversold bounce opportunities. The strategy’s main advantages are simple logic and high adaptability, but risk control needs attention in practical application. It is recommended to enhance strategy stability through adding stop loss mechanisms, optimizing filter conditions and other improvements.
/*backtest
start: 2025-01-19 00:00:00
end: 2025-02-18 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("3 Down, 3 Up Strategy", overlay=true, initial_capital = 1000000, default_qty_value = 200, default_qty_type = strategy.percent_of_equity, process_orders_on_close = true, margin_long = 5, margin_short = 5, calc_on_every_tick = true)
//#region INPUTS SECTION
// ============================================
// Time Settings
// ============================================
startTimeInput = input(timestamp("1 Jan 2014"), "Start Time", group = "Time Settings")
endTimeInput = input(timestamp("1 Jan 2099"), "End Time", group = "Time Settings")
isWithinTradingWindow = true
// ============================================
// Strategy Settings
// ============================================
buyTriggerInput = input.int(3, "Consecutive Down Closes for Entry", minval = 1, group = "Strategy Settings")
sellTriggerInput = input.int(3, "Consecutive Up Closes for Exit", minval = 1, group = "Strategy Settings")
// ============================================
// EMA Filter Settings
// ============================================
useEmaFilter = input.bool(false, "Use EMA Filter", group = "Trend Filter")
emaPeriodInput = input.int(200, "EMA Period", minval = 1, group = "Trend Filter")
//#endregion
//#region INDICATOR CALCULATIONS
// ============================================
// Consecutive Close Counter
// ============================================
var int aboveCount = na
var int belowCount = na
aboveCount := close > close[1] ? (na(aboveCount) ? 1 : aboveCount + 1) : 0
belowCount := close < close[1] ? (na(belowCount) ? 1 : belowCount + 1) : 0
// ============================================
// Trend Filter Calculation
// ============================================
emaValue = ta.ema(close, emaPeriodInput)
//#endregion
//#region TRADING CONDITIONS
// ============================================
// Entry/Exit Logic
// ============================================
longCondition = belowCount >= buyTriggerInput and isWithinTradingWindow
exitCondition = aboveCount >= sellTriggerInput
// Apply EMA Filter if enabled
if useEmaFilter
longCondition := longCondition and close > emaValue
//#endregion
//#region STRATEGY EXECUTION
// ============================================
// Order Management
// ============================================
if longCondition
strategy.entry("Long", strategy.long)
if exitCondition
strategy.close_all()
//#endregion