💬 Forum

NFIRefactorStrategy

wangc-joker/nfi-alpha-strategy/strategies/NFIRefactorStrategy.py · first seen 2026-07-16 · repo updated 2026-05-10 · ⬇ 1 download

Basics mode: spot
Settings dca
Concepts dca
Methods _handle_grind_mode _handle_scalp_mode _handle_top_coins_mode _remove_profit_target _set_profit_target _should_hold_trade adjust_trade_position base_tf_5m_indicators btc_info_15m_indicators btc_info_1d_indicators btc_info_1h_indicators btc_info_4h_indicators btc_info_5m_indicators btc_info_switcher calc_total_profit confirm_trade_entry confirm_trade_exit correct_min_stake custom_exit custom_stake_amount exit_profit_target get_hold_trades_config_file get_ticker_indicator has_valid_entry_conditions info_switcher informative_15m_indicators informative_1d_indicators informative_1h_indicators informative_4h_indicators is_backtest_mode is_system_v3 is_system_v3_1 is_system_v3_2 leverage load_hold_trades_config long_buyback_entry_v2 long_buyback_entry_v3 long_buyback_exit_v2 long_exit_btc long_exit_dec long_exit_grind long_exit_high_profit long_exit_main long_exit_normal long_exit_pump long_exit_quick long_exit_rapid long_exit_rebuy long_exit_scalp long_exit_signals long_exit_stoploss long_exit_top_coins long_exit_williams_r long_grind_entry long_grind_entry_v2 long_grind_entry_v3 long_grind_exit_v2 long_rebuy_adjust_trade_position long_rebuy_adjust_trade_position_v3 long_rebuy_entry_v3 mark_profit_target order_filled short_buyback_entry_v2 short_buyback_exit_v2 short_exit_dec short_exit_grind short_exit_high_profit short_exit_main short_exit_normal short_exit_pump short_exit_quick short_exit_rapid short_exit_rebuy short_exit_scalp short_exit_signals short_exit_stoploss short_exit_top_coins short_exit_williams_r short_grind_entry short_grind_entry_v2 short_grind_entry_v3 short_grind_exit_v2 short_rebuy_adjust_trade_position short_rebuy_adjust_trade_position_v3 short_rebuy_entry_v3 update_signals_from_config version
Other NostalgiaForInfinityX7 nfi_refactor
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 →

Source

Download Raw
  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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
from NostalgiaForInfinityX7 import NostalgiaForInfinityX7
from nfi_refactor.entries import confirm_entry
from nfi_refactor.entries import trend as entry_trend
from nfi_refactor.exits import confirm_exit
from nfi_refactor.exits import custom_exit as custom_exit_module
from nfi_refactor.exits import dec as exit_dec
from nfi_refactor.exits import main as exit_main
from nfi_refactor.exits import mode_advanced as exit_mode_advanced
from nfi_refactor.exits import mode_scalp as exit_mode_scalp
from nfi_refactor.exits import mode_simple as exit_mode_simple
from nfi_refactor.exits import mode_top_coins as exit_mode_top_coins
from nfi_refactor.exits import profit_target as profit_target_module
from nfi_refactor.exits import signals as exit_signals
from nfi_refactor.exits import stoploss as exit_stoploss
from nfi_refactor.exits import trend as exit_trend
from nfi_refactor.exits import williams as exit_williams
from nfi_refactor.indicators import base_timeframe
from nfi_refactor.indicators import pipeline
from nfi_refactor.indicators.btc_informative import btc_info_indicators, btc_info_switcher
from nfi_refactor.indicators import pair_informative
from nfi_refactor.market_context.informative_pairs import build_informative_pairs
from nfi_refactor.position import adjustment as adjustment_module
from nfi_refactor.position import adjustment_helpers
from nfi_refactor.position import leverage as leverage_module
from nfi_refactor.position import order_events
from nfi_refactor.position import profit as profit_module
from nfi_refactor.position import rebuy_adjustment
from nfi_refactor.position import stake as stake_module
from nfi_refactor.state import hold_trades
from nfi_refactor.state import initialization
from nfi_refactor.state import runtime as runtime_state


class NFIRefactorStrategy(NostalgiaForInfinityX7):
    """
    Parity adapter for the NFI modular refactor.

    This first version intentionally inherits the original strategy behavior.
    We will move logic into modules step by step, with backtest parity checks
    after each extraction.
    """

    def __init__(self, config: dict) -> None:
        initialization.initialize_strategy(self, config)

    def version(self) -> str:
        return "nfi-refactor-parity-adapter-upstream-v17.4.43-0.2.0"

    def informative_pairs(self):
        return build_informative_pairs(
            pairs=self.dp.current_whitelist(),
            config=self.config,
            info_timeframes=self.info_timeframes,
            btc_info_timeframes=self.btc_info_timeframes,
        )

    def btc_info_1d_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict):
        return btc_info_indicators(self, btc_info_pair, btc_info_timeframe, metadata)

    def btc_info_4h_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict):
        return btc_info_indicators(self, btc_info_pair, btc_info_timeframe, metadata)

    def btc_info_1h_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict):
        return btc_info_indicators(self, btc_info_pair, btc_info_timeframe, metadata)

    def btc_info_15m_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict):
        return btc_info_indicators(self, btc_info_pair, btc_info_timeframe, metadata)

    def btc_info_5m_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict):
        return btc_info_indicators(self, btc_info_pair, btc_info_timeframe, metadata)

    def btc_info_switcher(self, btc_info_pair, btc_info_timeframe, metadata: dict):
        return btc_info_switcher(self, btc_info_pair, btc_info_timeframe, metadata)

    def informative_1d_indicators(self, metadata: dict, info_timeframe):
        return pair_informative.informative_1d_indicators(self, metadata, info_timeframe)

    def informative_4h_indicators(self, metadata: dict, info_timeframe):
        return pair_informative.informative_4h_indicators(self, metadata, info_timeframe)

    def informative_1h_indicators(self, metadata: dict, info_timeframe):
        return pair_informative.informative_1h_indicators(self, metadata, info_timeframe)

    def informative_15m_indicators(self, metadata: dict, info_timeframe):
        return pair_informative.informative_15m_indicators(self, metadata, info_timeframe)

    def info_switcher(self, metadata: dict, info_timeframe):
        return pair_informative.info_switcher(self, metadata, info_timeframe)

    def base_tf_5m_indicators(self, metadata: dict, df):
        return base_timeframe.base_tf_5m_indicators(self, metadata, df)

    def populate_indicators(self, df, metadata: dict):
        return pipeline.populate_indicators(self, df, metadata)

    def populate_entry_trend(self, df, metadata: dict):
        # Upstream X7 v17.4.15 -> v17.4.43 changed many entry protections.
        # Delegate this surface to the parent until those changes are re-split.
        return super().populate_entry_trend(df, metadata)

    def confirm_trade_entry(
        self,
        pair: str,
        order_type: str,
        amount: float,
        rate: float,
        time_in_force: str,
        current_time,
        entry_tag,
        side: str,
        **kwargs,
    ) -> bool:
        return confirm_entry.confirm_trade_entry(
            self,
            pair,
            order_type,
            amount,
            rate,
            time_in_force,
            current_time,
            entry_tag,
            side,
            **kwargs,
        )

    def _handle_grind_mode(self, pair: str, config: dict, current_time) -> bool:
        return confirm_entry.handle_grind_mode(pair, config, current_time)

    def _handle_top_coins_mode(self, pair: str, config: dict, current_time) -> bool:
        return confirm_entry.handle_top_coins_mode(pair, config, current_time)

    def _handle_scalp_mode(self, pair: str, config: dict, current_time) -> bool:
        return confirm_entry.handle_scalp_mode(self, pair, config, current_time)

    def confirm_trade_exit(
        self,
        pair: str,
        trade,
        order_type: str,
        amount: float,
        rate: float,
        time_in_force: str,
        exit_reason: str,
        current_time,
        **kwargs,
    ) -> bool:
        return confirm_exit.confirm_trade_exit(
            self,
            pair,
            trade,
            order_type,
            amount,
            rate,
            time_in_force,
            exit_reason,
            current_time,
            **kwargs,
        )

    def leverage(
        self,
        pair: str,
        current_time,
        current_rate: float,
        proposed_leverage: float,
        max_leverage: float,
        entry_tag,
        side: str,
        **kwargs,
    ) -> float:
        return leverage_module.leverage(
            self,
            pair,
            current_time,
            current_rate,
            proposed_leverage,
            max_leverage,
            entry_tag,
            side,
            **kwargs,
        )

    def custom_stake_amount(
        self,
        pair: str,
        current_time,
        current_rate: float,
        proposed_stake: float,
        min_stake,
        max_stake: float,
        leverage: float,
        entry_tag,
        side: str,
        **kwargs,
    ) -> float:
        return stake_module.custom_stake_amount(
            self,
            pair,
            current_time,
            current_rate,
            proposed_stake,
            min_stake,
            max_stake,
            leverage,
            entry_tag,
            side,
            **kwargs,
        )

    def correct_min_stake(self, min_stake: float) -> float:
        return stake_module.correct_min_stake(self, min_stake)

    def get_ticker_indicator(self):
        return runtime_state.get_ticker_indicator(self)

    def is_backtest_mode(self) -> bool:
        return runtime_state.is_backtest_mode(self)

    def is_system_v3(self, trade) -> bool:
        return runtime_state.is_system_v3(self, trade)

    def is_system_v3_1(self, trade) -> bool:
        return runtime_state.is_system_v3_1(self, trade)

    def is_system_v3_2(self, trade) -> bool:
        return runtime_state.is_system_v3_2(self, trade)

    def has_valid_entry_conditions(self, trade, exit_rate: float, last_candle, previous_candle) -> bool:
        return runtime_state.has_valid_entry_conditions(
            self, trade, exit_rate, last_candle, previous_candle
        )

    def update_signals_from_config(self, config):
        return runtime_state.update_signals_from_config(self, config)

    def _set_profit_target(
        self, pair: str, sell_reason: str, rate: float, current_profit: float, current_time
    ):
        return runtime_state.set_profit_target(
            self, pair, sell_reason, rate, current_profit, current_time
        )

    def _remove_profit_target(self, pair: str):
        return runtime_state.remove_profit_target(self, pair)

    def mark_profit_target(
        self,
        mode_name: str,
        pair: str,
        sell: bool,
        signal_name: str,
        trade,
        current_time,
        current_rate: float,
        current_profit: float,
        last_candle,
        previous_candle_1,
    ) -> tuple:
        return profit_target_module.mark_profit_target(
            self,
            mode_name,
            pair,
            sell,
            signal_name,
            trade,
            current_time,
            current_rate,
            current_profit,
            last_candle,
            previous_candle_1,
        )

    def exit_profit_target(
        self,
        mode_name: str,
        pair: str,
        trade,
        current_time,
        current_rate: float,
        profit_stake: float,
        profit_ratio: float,
        profit_current_stake_ratio: float,
        profit_init_ratio: float,
        last_candle,
        previous_candle_1,
        previous_rate,
        previous_profit,
        previous_sell_reason,
        previous_time_profit_reached,
        enter_tags,
    ) -> tuple:
        return profit_target_module.exit_profit_target(
            self,
            mode_name,
            pair,
            trade,
            current_time,
            current_rate,
            profit_stake,
            profit_ratio,
            profit_current_stake_ratio,
            profit_init_ratio,
            last_candle,
            previous_candle_1,
            previous_rate,
            previous_profit,
            previous_sell_reason,
            previous_time_profit_reached,
            enter_tags,
        )

    def calc_total_profit(self, trade, filled_entries, filled_exits, exit_rate: float) -> tuple:
        return profit_module.calc_total_profit(
            self, trade, filled_entries, filled_exits, exit_rate
        )

    def get_hold_trades_config_file(self):
        return hold_trades.get_hold_trades_config_file(self)

    def load_hold_trades_config(self):
        return hold_trades.load_hold_trades_config(self)

    def _should_hold_trade(self, trade, rate: float, sell_reason: str) -> bool:
        return hold_trades.should_hold_trade(self, trade, rate, sell_reason)

    def bot_loop_start(self, current_time, **kwargs) -> None:
        return initialization.bot_loop_start(self, current_time, **kwargs)

    def custom_exit(
        self,
        pair: str,
        trade,
        current_time,
        current_rate: float,
        current_profit: float,
        **kwargs,
    ):
        return custom_exit_module.custom_exit(
            self,
            pair,
            trade,
            current_time,
            current_rate,
            current_profit,
            **kwargs,
        )

    def populate_exit_trend(self, df, metadata: dict):
        return exit_trend.populate_exit_trend(self, df, metadata)

    def long_exit_grind(self, *args, **kwargs):
        return exit_mode_simple.long_exit_grind(self, *args, **kwargs)

    def long_exit_btc(self, *args, **kwargs):
        return exit_mode_simple.long_exit_btc(self, *args, **kwargs)

    def short_exit_grind(self, *args, **kwargs):
        return exit_mode_simple.short_exit_grind(self, *args, **kwargs)

    def long_exit_signals(self, *args, **kwargs):
        return exit_signals.long_exit_signals(self, *args, **kwargs)

    def short_exit_signals(self, *args, **kwargs):
        return exit_signals.short_exit_signals(self, *args, **kwargs)

    def long_exit_main(self, *args, **kwargs):
        return exit_main.long_exit_main(self, *args, **kwargs)

    def short_exit_main(self, *args, **kwargs):
        return exit_main.short_exit_main(self, *args, **kwargs)

    def long_exit_stoploss(self, *args, **kwargs):
        return exit_stoploss.long_exit_stoploss(self, *args, **kwargs)

    def short_exit_stoploss(self, *args, **kwargs):
        return exit_stoploss.short_exit_stoploss(self, *args, **kwargs)

    def long_exit_top_coins(self, *args, **kwargs):
        return exit_mode_top_coins.long_exit_top_coins(self, *args, **kwargs)

    def short_exit_top_coins(self, *args, **kwargs):
        return exit_mode_top_coins.short_exit_top_coins(self, *args, **kwargs)

    def long_exit_scalp(self, *args, **kwargs):
        return exit_mode_scalp.long_exit_scalp(self, *args, **kwargs)

    def short_exit_scalp(self, *args, **kwargs):
        return exit_mode_scalp.short_exit_scalp(self, *args, **kwargs)

    def long_exit_williams_r(self, *args, **kwargs):
        return exit_williams.long_exit_williams_r(self, *args, **kwargs)

    def short_exit_williams_r(self, *args, **kwargs):
        return exit_williams.short_exit_williams_r(self, *args, **kwargs)

    def long_exit_dec(self, *args, **kwargs):
        return exit_dec.long_exit_dec(self, *args, **kwargs)

    def short_exit_dec(self, *args, **kwargs):
        return exit_dec.short_exit_dec(self, *args, **kwargs)

    def long_exit_high_profit(self, *args, **kwargs):
        return exit_mode_advanced.long_exit_high_profit(self, *args, **kwargs)

    def short_exit_high_profit(self, *args, **kwargs):
        return exit_mode_advanced.short_exit_high_profit(self, *args, **kwargs)

    def long_exit_normal(self, *args, **kwargs):
        return exit_mode_advanced.long_exit_normal(self, *args, **kwargs)

    def short_exit_normal(self, *args, **kwargs):
        return exit_mode_advanced.short_exit_normal(self, *args, **kwargs)

    def long_exit_pump(self, *args, **kwargs):
        return exit_mode_advanced.long_exit_pump(self, *args, **kwargs)

    def short_exit_pump(self, *args, **kwargs):
        return exit_mode_advanced.short_exit_pump(self, *args, **kwargs)

    def long_exit_quick(self, *args, **kwargs):
        return exit_mode_advanced.long_exit_quick(self, *args, **kwargs)

    def short_exit_quick(self, *args, **kwargs):
        return exit_mode_advanced.short_exit_quick(self, *args, **kwargs)

    def long_exit_rapid(self, *args, **kwargs):
        return exit_mode_advanced.long_exit_rapid(self, *args, **kwargs)

    def short_exit_rapid(self, *args, **kwargs):
        return exit_mode_advanced.short_exit_rapid(self, *args, **kwargs)

    def long_exit_rebuy(self, *args, **kwargs):
        return super().long_exit_rebuy(*args, **kwargs)

    def short_exit_rebuy(self, *args, **kwargs):
        return exit_mode_advanced.short_exit_rebuy(self, *args, **kwargs)

    def order_filled(self, pair: str, trade, order, current_time, **kwargs) -> None:
        return order_events.order_filled(self, pair, trade, order, current_time, **kwargs)

    def adjust_trade_position(
        self,
        trade,
        current_time,
        current_rate: float,
        current_profit: float,
        min_stake,
        max_stake: float,
        current_entry_rate: float,
        current_exit_rate: float,
        current_entry_profit: float,
        current_exit_profit: float,
        **kwargs,
    ):
        # Upstream changed v3 rebuy/grind de-risk routing; keep parity first.
        return super().adjust_trade_position(
            trade,
            current_time,
            current_rate,
            current_profit,
            min_stake,
            max_stake,
            current_entry_rate,
            current_exit_rate,
            current_entry_profit,
            current_exit_profit,
            **kwargs,
        )

    def long_rebuy_adjust_trade_position(self, *args, **kwargs):
        return rebuy_adjustment.long_rebuy_adjust_trade_position(self, *args, **kwargs)

    def long_rebuy_adjust_trade_position_v3(self, *args, **kwargs):
        return super().long_rebuy_adjust_trade_position_v3(*args, **kwargs)

    def short_rebuy_adjust_trade_position(self, *args, **kwargs):
        return rebuy_adjustment.short_rebuy_adjust_trade_position(self, *args, **kwargs)

    def short_rebuy_adjust_trade_position_v3(self, *args, **kwargs):
        return super().short_rebuy_adjust_trade_position_v3(*args, **kwargs)

    def long_buyback_entry_v2(self, *args, **kwargs):
        return adjustment_helpers.long_buyback_entry_v2(self, *args, **kwargs)

    def long_grind_entry_v2(self, *args, **kwargs):
        return adjustment_helpers.long_grind_entry_v2(self, *args, **kwargs)

    def long_buyback_exit_v2(self, *args, **kwargs):
        return adjustment_helpers.long_buyback_exit_v2(self, *args, **kwargs)

    def long_grind_exit_v2(self, *args, **kwargs):
        return adjustment_helpers.long_grind_exit_v2(self, *args, **kwargs)

    def long_grind_entry_v3(self, *args, **kwargs):
        return super().long_grind_entry_v3(*args, **kwargs)

    def long_buyback_entry_v3(self, *args, **kwargs):
        return adjustment_helpers.long_buyback_entry_v3(self, *args, **kwargs)

    def long_rebuy_entry_v3(self, *args, **kwargs):
        return adjustment_helpers.long_rebuy_entry_v3(self, *args, **kwargs)

    def long_grind_entry(self, *args, **kwargs):
        return adjustment_helpers.long_grind_entry(self, *args, **kwargs)

    def short_buyback_entry_v2(self, *args, **kwargs):
        return adjustment_helpers.short_buyback_entry_v2(self, *args, **kwargs)

    def short_grind_entry_v2(self, *args, **kwargs):
        return adjustment_helpers.short_grind_entry_v2(self, *args, **kwargs)

    def short_buyback_exit_v2(self, *args, **kwargs):
        return adjustment_helpers.short_buyback_exit_v2(self, *args, **kwargs)

    def short_grind_exit_v2(self, *args, **kwargs):
        return adjustment_helpers.short_grind_exit_v2(self, *args, **kwargs)

    def short_grind_entry_v3(self, *args, **kwargs):
        return super().short_grind_entry_v3(*args, **kwargs)

    def short_rebuy_entry_v3(self, *args, **kwargs):
        return adjustment_helpers.short_rebuy_entry_v3(self, *args, **kwargs)

    def short_grind_entry(self, *args, **kwargs):
        return adjustment_helpers.short_grind_entry(self, *args, **kwargs)