💬 Forum

ICTSmartMoneyStrategy

🏆 League #1929 / 1937

abror2-lab/crypto-auto/user_data/strategies/ICTSmartMoneyStrategy.py · first seen 2026-07-16 · repo updated 2026-06-12 · ⬇ 1 download

Basics mode: futures timeframe: 15m interface version: 3
Settings stoploss: -0.03 has minimal roi trailing process only new candles startup candle count: 100 hyperopt hyperopt params: 5
Indicators ATR RSI talib
Concepts mean_reversion 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
ICT Smart Money Strategy — Freqtrade Implementation

Rules:
1. MACRO TIME MATTERS — Trade only during high-reactivity windows
2. WAIT FOR STOP-HUNT — Liquidity sweep before entry
3. USE IFVG OR BREAKER ENTRY — IFVG = first, Breaker Block = second
4. TRADE ONLY PREMIUM/DISCOUNT ZONES — Sell premium, Buy discount
5. FOCUS ON 1-2 QUALITY TRADES — Clean 1:2 RR setups
"""

from freqtrade.strategy import IStrategy, IntParameter, DecimalParameter
from pandas import DataFrame
import talib.abstract as ta
import numpy as np


class ICTSmartMoneyStrategy(IStrategy):
    """
    ICT Smart Money Concepts Strategy
    
    Combines:
    - Fair Value Gaps (FVG) detection
    - Breaker Block detection
    - Liquidity sweep / stop-hunt detection
    - Premium/Discount zone filtering
    - Macro time filtering (London/NY killzones)
    """
    
    INTERFACE_VERSION = 3
    timeframe = '15m'
    can_short = True
    
    # 1:2 Risk-Reward minimum
    minimal_roi = {
        "0": 0.06,    # 6% immediate target (1:2 from 3% SL)
        "120": 0.04,  # After 2 hours, accept 4%
        "240": 0.02,  # After 4 hours, accept 2%
        "480": 0.01,  # After 8 hours, accept 1%
    }
    
    stoploss = -0.03  # 3% stop loss (for 1:2 RR with 6% target)
    trailing_stop = True
    trailing_stop_positive = 0.015
    trailing_stop_positive_offset = 0.035
    trailing_only_offset_is_reached = True
    
    process_only_new_candles = True
    startup_candle_count: int = 100
    
    # ── Parameters ──────────────────────────────────────────────────────
    
    # FVG detection
    fvg_lookback = IntParameter(3, 10, default=5, space="buy")
    fvg_min_size_pct = DecimalParameter(0.001, 0.01, default=0.003, decimals=3, space="buy")
    
    # Liquidity sweep detection
    swing_lookback = IntParameter(10, 30, default=20, space="buy")
    
    # Premium/Discount zone
    pd_lookback = IntParameter(30, 100, default=50, space="buy")
    
    # Volume confirmation
    vol_mult = DecimalParameter(1.0, 3.0, default=1.5, decimals=1, space="buy")
    
    # ── Macro Time Windows (UTC) ────────────────────────────────────────
    # London Open Killzone: 07:00-10:00 UTC
    # NY Open Killzone: 12:30-15:00 UTC  
    # London Close: 15:00-16:00 UTC
    
    LONDON_KZ = [7, 8, 9, 10]
    NY_KZ = [12, 13, 14, 15]
    LONDON_CLOSE = [16]
    
    # All allowed hours
    ALLOWED_HOURS = LONDON_KZ + NY_KZ + LONDON_CLOSE
    
    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """Calculate ICT/SMC indicators"""
        
        # ── Basic indicators ────────────────────────────────────────────
        dataframe['atr'] = ta.ATR(dataframe, timeperiod=14)
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['volume_mean'] = dataframe['volume'].rolling(20).mean()
        
        # ── Swing Highs/Lows (for liquidity levels) ────────────────────
        lb = self.swing_lookback.value
        dataframe['swing_high'] = dataframe['high'].rolling(lb*2+1, center=True).max()
        dataframe['swing_low'] = dataframe['low'].rolling(lb*2+1, center=True).min()
        
        # Swing high/low at specific candles (not rolling window)
        dataframe['is_swing_high'] = (
            (dataframe['high'] == dataframe['high'].rolling(lb*2+1, center=True).max()) &
            (dataframe['high'].shift(1) < dataframe['high']) &
            (dataframe['high'].shift(-1) < dataframe['high'])
        )
        dataframe['is_swing_low'] = (
            (dataframe['low'] == dataframe['low'].rolling(lb*2+1, center=True).min()) &
            (dataframe['low'].shift(1) > dataframe['low']) &
            (dataframe['low'].shift(-1) > dataframe['low'])
        )
        
        # Track recent swing levels
        dataframe['last_swing_high'] = np.nan
        dataframe['last_swing_low'] = np.nan
        for i in range(len(dataframe)):
            if dataframe['is_swing_high'].iloc[i]:
                dataframe.loc[dataframe.index[i], 'last_swing_high'] = dataframe['high'].iloc[i]
            if dataframe['is_swing_low'].iloc[i]:
                dataframe.loc[dataframe.index[i], 'last_swing_low'] = dataframe['low'].iloc[i]
        
        dataframe['last_swing_high'] = dataframe['last_swing_high'].ffill()
        dataframe['last_swing_low'] = dataframe['last_swing_low'].ffill()
        
        # ── Fair Value Gap (FVG) Detection ──────────────────────────────
        # Bullish FVG: candle[i-1].high < candle[i+1].low (gap up)
        # Bearish FVG: candle[i-1].low > candle[i+1].high (gap down)
        dataframe['fvg_bullish'] = (
            (dataframe['low'].shift(-1) > dataframe['high'].shift(1)) &
            ((dataframe['low'].shift(-1) - dataframe['high'].shift(1)) / dataframe['close'] > self.fvg_min_size_pct.value)
        )
        dataframe['fvg_bearish'] = (
            (dataframe['high'].shift(-1) < dataframe['low'].shift(1)) &
            ((dataframe['low'].shift(1) - dataframe['high'].shift(-1)) / dataframe['close'] > self.fvg_min_size_pct.value)
        )
        
        # FVG midpoint (inversion level)
        dataframe['fvg_bull_mid'] = np.where(
            dataframe['fvg_bullish'],
            (dataframe['low'].shift(-1) + dataframe['high'].shift(1)) / 2,
            np.nan
        )
        dataframe['fvg_bear_mid'] = np.where(
            dataframe['fvg_bearish'],
            (dataframe['low'].shift(1) + dataframe['high'].shift(-1)) / 2,
            np.nan
        )
        dataframe['fvg_bull_mid'] = dataframe['fvg_bull_mid'].ffill()
        dataframe['fvg_bear_mid'] = dataframe['fvg_bear_mid'].ffill()
        
        # ── Breaker Block Detection ─────────────────────────────────────
        # Breaker = failed FVG that gets reclaimed
        # Bullish breaker: bearish candle that caused break below, then price reclaims
        dataframe['breaker_bull'] = (
            (dataframe['close'] > dataframe['open']) &  # Current bullish
            (dataframe['close'].shift(1) < dataframe['open'].shift(1)) &  # Previous bearish
            (dataframe['low'] < dataframe['low'].shift(1)) &  # Swept previous low
            (dataframe['close'] > dataframe['high'].shift(1)) &  # Closed above previous high
            (dataframe['volume'] > dataframe['volume_mean'] * self.vol_mult.value)  # Volume confirm
        )
        
        dataframe['breaker_bear'] = (
            (dataframe['close'] < dataframe['open']) &  # Current bearish
            (dataframe['close'].shift(1) > dataframe['open'].shift(1)) &  # Previous bullish
            (dataframe['high'] > dataframe['high'].shift(1)) &  # Swept previous high
            (dataframe['close'] < dataframe['low'].shift(1)) &  # Closed below previous low
            (dataframe['volume'] > dataframe['volume_mean'] * self.vol_mult.value)  # Volume confirm
        )
        
        # ── Liquidity Sweep / Stop Hunt Detection ───────────────────────
        # Sweep = price takes out swing high/low then reverses
        dataframe['sweep_high'] = (
            (dataframe['high'] > dataframe['last_swing_high'].shift(1)) &  # Took out swing high
            (dataframe['close'] < dataframe['last_swing_high'].shift(1)) &  # But closed below (rejection)
            (dataframe['close'] < dataframe['open'])  # Bearish candle
        )
        
        dataframe['sweep_low'] = (
            (dataframe['low'] < dataframe['last_swing_low'].shift(1)) &  # Took out swing low
            (dataframe['close'] > dataframe['last_swing_low'].shift(1)) &  # But closed above (rejection)
            (dataframe['close'] > dataframe['open'])  # Bullish candle
        )
        
        # ── Premium/Discount Zone ───────────────────────────────────────
        pd_lb = self.pd_lookback.value
        range_high = dataframe['high'].rolling(pd_lb).max()
        range_low = dataframe['low'].rolling(pd_lb).min()
        range_mid = (range_high + range_low) / 2
        
        dataframe['range_mid'] = range_mid
        dataframe['range_high'] = range_high
        dataframe['range_low'] = range_low
        
        # Premium = above midpoint, Discount = below midpoint
        dataframe['in_premium'] = dataframe['close'] > range_mid
        dataframe['in_discount'] = dataframe['close'] < range_mid
        
        # Equilibrium zone (near midpoint ± 10% of range)
        range_size = range_high - range_low
        dataframe['in_equilibrium'] = (
            (dataframe['close'] > range_mid - range_size * 0.1) &
            (dataframe['close'] < range_mid + range_size * 0.1)
        )
        
        # ── Macro Time Filter ───────────────────────────────────────────
        dataframe['hour'] = dataframe['date'].dt.hour
        dataframe['macro_time'] = dataframe['hour'].isin(self.ALLOWED_HOURS)
        
        # Enhanced: weight by killzone importance
        dataframe['in_london_kz'] = dataframe['hour'].isin(self.LONDON_KZ)
        dataframe['in_ny_kz'] = dataframe['hour'].isin(self.NY_KZ)
        dataframe['in_killzone'] = dataframe['in_london_kz'] | dataframe['in_ny_kz']
        
        return dataframe
    
    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Entry Logic:
        
        LONG (Discount zone):
        1. Stop hunt / sweep low → IFVG entry OR
        2. Breaker block bullish in discount zone
        3. Must be in macro time window
        4. Volume confirmation
        
        SHORT (Premium zone):
        1. Stop hunt / sweep high → IFVG entry OR  
        2. Breaker block bearish in premium zone
        3. Must be in macro time window
        4. Volume confirmation
        """
        
        # ── LONG Entry ──────────────────────────────────────────────────
        # Condition 1: Sweep low + FVG bullish (IFVG entry)
        long_sweep_ifvg = (
            dataframe['sweep_low'] &
            (dataframe['close'] > dataframe['fvg_bull_mid']) &
            (dataframe['close'] < dataframe['range_mid'])  # In discount zone
        )
        
        # Condition 2: Breaker block bullish in discount
        long_breaker = (
            dataframe['breaker_bull'] &
            (dataframe['close'] < dataframe['range_mid'])  # In discount zone
        )
        
        # Condition 3: FVG bullish touch in discount zone
        long_fvg_touch = (
            (dataframe['low'] <= dataframe['fvg_bull_mid'] * 1.005) &  # Price touched FVG
            (dataframe['low'] >= dataframe['fvg_bull_mid'] * 0.995) &
            (dataframe['close'] > dataframe['open']) &  # Bullish candle
            (dataframe['close'] < dataframe['range_mid']) &  # Discount zone
            (dataframe['volume'] > dataframe['volume_mean'] * self.vol_mult.value)
        )
        
        # Combined long conditions
        long_entry = (
            (long_sweep_ifvg | long_breaker | long_fvg_touch) &
            dataframe['macro_time'] &
            (dataframe['volume'] > 0)
        )
        
        dataframe.loc[long_entry, 'enter_long'] = 1
        
        # ── SHORT Entry ─────────────────────────────────────────────────
        # Condition 1: Sweep high + FVG bearish (IFVG entry)
        short_sweep_ifvg = (
            dataframe['sweep_high'] &
            (dataframe['close'] < dataframe['fvg_bear_mid']) &
            (dataframe['close'] > dataframe['range_mid'])  # In premium zone
        )
        
        # Condition 2: Breaker block bearish in premium
        short_breaker = (
            dataframe['breaker_bear'] &
            (dataframe['close'] > dataframe['range_mid'])  # In premium zone
        )
        
        # Condition 3: FVG bearish touch in premium zone
        short_fvg_touch = (
            (dataframe['high'] >= dataframe['fvg_bear_mid'] * 0.995) &  # Price touched FVG
            (dataframe['high'] <= dataframe['fvg_bear_mid'] * 1.005) &
            (dataframe['close'] < dataframe['open']) &  # Bearish candle
            (dataframe['close'] > dataframe['range_mid']) &  # Premium zone
            (dataframe['volume'] > dataframe['volume_mean'] * self.vol_mult.value)
        )
        
        # Combined short conditions
        short_entry = (
            (short_sweep_ifvg | short_breaker | short_fvg_touch) &
            dataframe['macro_time'] &
            (dataframe['volume'] > 0)
        )
        
        dataframe.loc[short_entry, 'enter_short'] = 1
        
        return dataframe
    
    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Exit Logic:
        - Take profit at 1:2 RR (handled by ROI)
        - Exit on opposite zone breach
        - Exit on structure break
        """
        
        # LONG exit: price enters premium zone + bearish signal
        dataframe.loc[
            (
                (dataframe['close'] > dataframe['range_mid']) &  # Now in premium
                (
                    dataframe['sweep_high'] |  # Sweep high rejection
                    dataframe['breaker_bear'] |  # Breaker bearish
                    ((dataframe['rsi'] > 70) & (dataframe['close'] < dataframe['open']))  # RSI overbought + bearish
                )
            ),
            'exit_long'
        ] = 1
        
        # SHORT exit: price enters discount zone + bullish signal
        dataframe.loc[
            (
                (dataframe['close'] < dataframe['range_mid']) &  # Now in discount
                (
                    dataframe['sweep_low'] |  # Sweep low rejection
                    dataframe['breaker_bull'] |  # Breaker bullish
                    ((dataframe['rsi'] < 30) & (dataframe['close'] > dataframe['open']))  # RSI oversold + bullish
                )
            ),
            'exit_short'
        ] = 1
        
        return dataframe