💬 Forum

ICT_SMC_Strategy

🏆 League #1923 / 1937

win-boom/BTCquant/user_data/strategies/ict_smc_strategy.py · first seen 2026-07-16 · repo updated 2026-06-20 · ⬇ 1 download

Basics mode: futures timeframe: 15m interface version: 3
Settings stoploss: -0.02 has minimal roi trailing process only new candles startup candle count: 300
Indicators ATR EMA RSI SMA talib
Concepts trailing
15 related strategies ( identical code, similar name)

Each tile is a different kind of check — from an instant code lint to full sandboxed backtests and forward tests on recent data. Not sure what a check actually proves? See the FAQ →

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
ICT/SMC Strategy — Smart Money Concepts for Freqtrade
Efficient implementation using vectorized operations
15m timeframe, Binance Futures

Core ICT Concepts Implemented:
1. Market Structure (HH/HL/BOS/CHoCH)
2. Liquidity Sweeps
3. Order Blocks (simplified)
4. Fair Value Gaps (simplified)
5. Displacement / Momentum
6. Session Killzones
"""
from pandas import DataFrame
import pandas as pd
import talib.abstract as ta
import numpy as np
from freqtrade.strategy import IStrategy


class ICT_SMC_Strategy(IStrategy):
    INTERFACE_VERSION = 3
    timeframe = '15m'
    can_short = True

    stoploss = -0.02
    trailing_stop = True
    trailing_stop_positive = 0.005
    trailing_stop_positive_offset = 0.012
    trailing_only_offset_is_reached = True

    minimal_roi = {"0": 0.06, "480": 0.04, "1440": 0.02, "4320": 0}

    max_open_trades = 6
    startup_candle_count = 300
    process_only_new_candles = True
    use_exit_signal = False

    def populate_indicators(self, d: DataFrame, m: dict) -> DataFrame:
        # ============================================================
        # 1. SWING HIGHS/LOWS (vectorized)
        # ============================================================
        # Swing high: candle high > 3 before and 3 after
        d['swing_high'] = (
            (d['high'] > d['high'].shift(1)) &
            (d['high'] > d['high'].shift(2)) &
            (d['high'] > d['high'].shift(3)) &
            (d['high'] >= d['high'].shift(-1)) &
            (d['high'] >= d['high'].shift(-2)) &
            (d['high'] >= d['high'].shift(-3))
        )
        # Swing low: candle low < 3 before and 3 after
        d['swing_low'] = (
            (d['low'] < d['low'].shift(1)) &
            (d['low'] < d['low'].shift(2)) &
            (d['low'] < d['low'].shift(3)) &
            (d['low'] <= d['low'].shift(-1)) &
            (d['low'] <= d['low'].shift(-2)) &
            (d['low'] <= d['low'].shift(-3))
        )

        # Forward-fill last swing high/low values
        d['last_sh'] = d['high'].where(d['swing_high']).ffill()
        d['last_sl'] = d['low'].where(d['swing_low']).ffill()

        # ============================================================
        # 2. BREAK OF STRUCTURE (BOS)
        # ============================================================
        # Bull BOS: close breaks above last swing high
        d['bos_bull'] = d['close'] > d['last_sh'].shift(1)
        # Bear BOS: close breaks below last swing low
        d['bos_bear'] = d['close'] < d['last_sl'].shift(1)

        # CHoCH: BOS after opposite trend (simplified: 2+ BOS in one direction, then opposite BOS)
        d['bull_count'] = d['bos_bull'].rolling(24).sum()   # ~6 hours
        d['bear_count'] = d['bos_bear'].rolling(24).sum()
        d['choch_bear'] = d['bos_bear'] & (d['bull_count'].shift(1) >= 2)  # bear BOS after bull run
        d['choch_bull'] = d['bos_bull'] & (d['bear_count'].shift(1) >= 2)  # bull BOS after bear run

        # ============================================================
        # 3. LIQUIDITY SWEEPS
        # ============================================================
        # Sweep of 20-bar highs/lows
        d['hh_20'] = d['high'].rolling(20).max()
        d['ll_20'] = d['low'].rolling(20).min()
        # High sweep: price hits 20-bar high then closes back below
        d['sweep_high'] = (d['high'] >= d['hh_20'].shift(1) * 0.998) & (d['close'] < d['hh_20'].shift(1) * 0.997)
        # Low sweep: price hits 20-bar low then closes back above
        d['sweep_low'] = (d['low'] <= d['ll_20'].shift(1) * 1.002) & (d['close'] > d['ll_20'].shift(1) * 1.003)

        # Sweep of previous candle extremes
        d['sweep_prev_high'] = (d['high'] > d['high'].shift(1)) & (d['close'] < d['high'].shift(1))
        d['sweep_prev_low'] = (d['low'] < d['low'].shift(1)) & (d['close'] > d['low'].shift(1))

        # ============================================================
        # 4. ORDER BLOCKS (simplified: last opposite candle before displacement)
        # ============================================================
        # Displacement: candle body > 2x 20-period average body
        d['body'] = (d['close'] - d['open']).abs()
        d['avg_body'] = d['body'].rolling(20).mean()
        d['displacement_up'] = (d['close'] > d['open']) & (d['body'] > d['avg_body'] * 1.8)
        d['displacement_down'] = (d['close'] < d['open']) & (d['body'] > d['avg_body'] * 1.8)
        d['displacement'] = d['displacement_up'] | d['displacement_down']

        # Bull OB: bear candle before displacement up = reversal zone
        d['ob_bull'] = d['displacement_up'] & (d['close'].shift(1) < d['open'].shift(1))
        # Bear OB: bull candle before displacement down
        d['ob_bear'] = d['displacement_down'] & (d['close'].shift(1) > d['open'].shift(1))

        # OB candle range becomes potential entry zone
        d['ob_bull_high'] = np.where(d['ob_bull'], d['high'].shift(1), np.nan)
        d['ob_bull_low'] = np.where(d['ob_bull'], d['low'].shift(1), np.nan)
        d['ob_bear_high'] = np.where(d['ob_bear'], d['high'].shift(1), np.nan)
        d['ob_bear_low'] = np.where(d['ob_bear'], d['low'].shift(1), np.nan)

        # OB retest: price returns to OB zone within 12 candles of OB formation
        d['ob_bull_retest'] = False
        d['ob_bear_retest'] = False
        for lookback in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]:
            d['ob_bull_retest'] = d['ob_bull_retest'] | (
                d['ob_bull'].shift(lookback) &
                (d['low'] <= d['ob_bull_high'].shift(lookback)) &
                (d['close'] >= d['ob_bull_low'].shift(lookback))
            )
            d['ob_bear_retest'] = d['ob_bear_retest'] | (
                d['ob_bear'].shift(lookback) &
                (d['high'] >= d['ob_bear_low'].shift(lookback)) &
                (d['close'] <= d['ob_bear_high'].shift(lookback))
            )

        # ============================================================
        # 5. FAIR VALUE GAP (FVG) — simplified vectorized
        # ============================================================
        # Bull FVG: C[2] low > C[0] high → unfilled gap
        d['fvg_bull'] = d['low'].shift(2) > d['high'].shift(0) * 1.002
        d['fvg_bear'] = d['high'].shift(2) < d['low'].shift(0) * 0.998

        # FVG retest within 12 candles
        d['fvg_bull_retest'] = False
        d['fvg_bear_retest'] = False
        for lb in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]:
            d['fvg_bull_retest'] = d['fvg_bull_retest'] | (
                d['fvg_bull'].shift(lb) & (d['low'] <= d['high'].shift(lb - 2))
            )
            d['fvg_bear_retest'] = d['fvg_bear_retest'] | (
                d['fvg_bear'].shift(lb) & (d['high'] >= d['low'].shift(lb - 2))
            )

        # ============================================================
        # 6. FIBONACCI / OTE ZONE
        # ============================================================
        # Use rolling 96-period (1 day) swing high/low for fib levels
        d['swing_high_96'] = d['high'].rolling(96).max()
        d['swing_low_96'] = d['low'].rolling(96).min()
        swing_range = d['swing_high_96'] - d['swing_low_96']

        d['fib_062'] = d['swing_high_96'] - swing_range * 0.618  # OTE entry
        d['fib_079'] = d['swing_high_96'] - swing_range * 0.786  # OTE boundary
        d['fib_062_s'] = d['swing_low_96'] + swing_range * 0.618  # Short OTE
        d['fib_079_s'] = d['swing_low_96'] + swing_range * 0.786

        # In OTE zone for long: price between 0.62 and 0.79 retrace
        d['in_ote_long'] = (d['close'] >= d['fib_079']) & (d['close'] <= d['fib_062'])
        d['in_ote_short'] = (d['close'] >= d['fib_062_s']) & (d['close'] <= d['fib_079_s'])

        # ============================================================
        # 7. TRADITIONAL INDICATORS
        # ============================================================
        d['e20'] = ta.EMA(d, timeperiod=20)
        d['e50'] = ta.EMA(d, timeperiod=50)
        d['rsi'] = ta.RSI(d, timeperiod=14)
        d['atr'] = ta.ATR(d, timeperiod=14)
        d['vr'] = d['volume'] / ta.SMA(d['volume'], timeperiod=20)

        # ============================================================
        # 8. KILLZONE — London/NY session
        # ============================================================
        d['hour'] = pd.to_datetime(d['date']).dt.hour
        d['in_killzone'] = d['hour'].between(7, 9) | d['hour'].between(12, 14)

        return d

    def populate_entry_trend(self, d: DataFrame, m: dict) -> DataFrame:
        # ===== ICT LONG ENTRY =====
        # Structure: channel_h/bos_bull/choch_bull confirming uptrend
        structure_bull = d['bos_bull'] | d['close'] > d['e50']

        # Setup: liquidity sweep below recent low (stop hunt) + OB/FVG entry
        entry_setup_long = (
            d['sweep_low'] | d['sweep_prev_low']
        ) & (
            d['ob_bull_retest'] | d['fvg_bull_retest'] | d['in_ote_long']
        )

        # Confirmation: displacement + RSI
        confirm_long = (
            d['displacement_up'] &
            d['rsi'].between(30, 70) &
            d['vr'] > 0.6
        )

        d.loc[
            structure_bull & entry_setup_long & confirm_long & (d['volume'] > 0),
            ['enter_long', 'enter_tag']
        ] = (1, 'ICT_L')

        # ===== ICT SHORT ENTRY =====
        structure_bear = d['bos_bear'] | d['close'] < d['e50']

        entry_setup_short = (
            d['sweep_high'] | d['sweep_prev_high']
        ) & (
            d['ob_bear_retest'] | d['fvg_bear_retest'] | d['in_ote_short']
        )

        confirm_short = (
            d['displacement_down'] &
            d['rsi'].between(30, 70) &
            d['vr'] > 0.6
        )

        d.loc[
            structure_bear & entry_setup_short & confirm_short & (d['volume'] > 0),
            ['enter_short', 'enter_tag']
        ] = (1, 'ICT_S')

        return d

    def populate_exit_trend(self, d: DataFrame, m: dict) -> DataFrame:
        return d