💬 Forum

EI4_t4c0s_V2_2_Shorts

🏆 League #1887 / 1937

vaskosmihaylov/nfi-custom-strategies/user_data/strategies/El/EI4_t4c0s_V2_2_Shorts.py · ★3 · ⑂2 · first seen 2026-07-16 · repo updated 2026-07-27 · ⬇ 1 download

Basics mode: futures timeframe: 5m interface version: 3
Settings stoploss: -0.99 has minimal roi custom stoploss protections process only new candles startup candle count: 400 hyperopt hyperopt params: 24
Indicators ADX ATR EMA HMA RSI Stochastic talib technical
Concepts mean_reversion risk_management
11 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
"""
EI4_t4c0s_V2_2_Shorts Strategy

A shorts-only variant of the EI4_t4c0s_V2_2 strategy, designed to profit in bear markets
and during overbought conditions. This strategy mirrors the successful long-only approach
but inverts the logic for short positions.

Original EI4_t4c0s_V2_2 Strategy (Longs):
- Uses volatility-based dynamic stoploss with "moon mode"
- Multiple entry signals: lambo2, buy1ewo, buy2ewo, cofi
- EWO (Elliott Wave Oscillator) for momentum identification
- Unclog mechanism: -4% after 4 days

Strategy Concept:
Uses Elliott Wave Oscillator (EWO) and volatility indicators to identify overbought
conditions and mean reversion opportunities for short entries. Implements "crash mode"
stoploss that tightens during market crashes (inverse of moon mode).

Entry Conditions (OR logic - any can trigger entry):
1. Lambo Short: Parabolic pump (price > EMA, high RSI)
2. Sell1EWO Short: Rally during downtrend (EWO < MEAN_DN)
3. Sell2EWO Short: Extreme overbought (EWO > UP_FIB)
4. COFI Short: Stochastic overbought crossdown

Exit Conditions:
- Signal: Price falls below dynamic thresholds
- ROI: 0.99 (effectively disabled, let signals/stops control)
- Stop Loss: -0.99 base (custom stoploss manages risk)
- Crash Mode Stoploss: Dynamic trailing based on volatility

Key Differences from Long Strategy:
- Inverted entry/exit signals (overbought vs oversold)
- Crash mode stoploss: Tightens during crashes vs trailing during moons
- Shorter unclog window: 3 days vs 4 days (shorts are riskier)
- Uses min_l (distance from lows) vs max_l (distance from highs)
- Max 4 short positions via confirm_trade_entry()

Author: Derived from EI4_t4c0s_V2_2
Version: 1.0.0
"""

# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from typing import Dict, List
from functools import reduce
from typing import Optional
from pandas import DataFrame
# --------------------------------
import talib.abstract as ta
import numpy as np
import freqtrade.vendor.qtpylib.indicators as qtpylib
import datetime
from technical.util import resample_to_interval, resampled_merge
from datetime import datetime, timedelta
from freqtrade.persistence import Trade
from freqtrade.strategy import stoploss_from_open, merge_informative_pair, DecimalParameter, IntParameter, CategoricalParameter
import technical.indicators as ftt
import math
import logging

logger = logging.getLogger(__name__)


def EWO(dataframe, ema_length=5, ema2_length=3):
    df = dataframe.copy()
    ema1 = ta.EMA(df, timeperiod=ema_length)
    ema2 = ta.EMA(df, timeperiod=ema2_length)
    emadif = (ema1 - ema2) / df['close'] * 100
    return emadif


class EI4_t4c0s_V2_2_Shorts(IStrategy):
    """
    Shorts-only Elliott Wave Oscillator strategy with crash mode stoploss.

    This strategy is designed to run in PARALLEL with EI4_t4c0s_V2_2 (longs)
    in separate containers to evaluate short performance independently.
    """

    INTERFACE_VERSION = 3
    can_short = True

    # Hyperparameter configurations from long strategy
    # Will be optimized for shorts later
    buy_params = {
        "base_nb_candles_buy": 12,
        "rsi_buy": 58,
        "ewo_high": 3.001,
        "ewo_low": -10.289,
        "low_offset": 0.987,
        "lambo2_ema_14_factor": 0.981,
        "lambo2_enabled": True,
        "lambo2_rsi_14_limit": 39,
        "lambo2_rsi_4_limit": 44,
        "buy_adx": 20,
        "buy_fastd": 20,
        "buy_fastk": 22,
        "buy_ema_cofi": 0.98,
        "buy_ewo_high": 4.179
    }

    sell_params = {
        "base_nb_candles_sell": 22,
        "high_offset": 1.014,
        "high_offset_2": 1.01
    }

    @property
    def protections(self):
        return [
            {
                "method": "CooldownPeriod",
                "stop_duration_candles": 5
            },
            {
                "method": "MaxDrawdown",
                "lookback_period_candles": 48,
                "trade_limit": 20,
                "stop_duration_candles": 4,
                "max_allowed_drawdown": 0.2
            },
            {
                "method": "StoplossGuard",
                "lookback_period_candles": 24,
                "trade_limit": 4,
                "stop_duration_candles": 2,
                "only_per_pair": False
            },
            {
                "method": "LowProfitPairs",
                "lookback_period_candles": 6,
                "trade_limit": 2,
                "stop_duration_candles": 60,
                "required_profit": 0.02
            },
            {
                "method": "LowProfitPairs",
                "lookback_period_candles": 24,
                "trade_limit": 4,
                "stop_duration_candles": 2,
                "required_profit": 0.01
            }
        ]

    # ROI table (effectively disabled, let signals control)
    minimal_roi = {
        "0": 0.99,
    }

    # Stoploss (custom_stoploss manages risk)
    stoploss = -0.99
    sl1 = DecimalParameter(-0.013, -0.005, default=-0.013, space='sell', optimize=True)

    # SMAOffset
    base_nb_candles_buy = IntParameter(8, 30, default=buy_params['base_nb_candles_buy'], space='buy', optimize=False)
    base_nb_candles_sell = IntParameter(8, 30, default=sell_params['base_nb_candles_sell'], space='sell', optimize=False)
    low_offset = DecimalParameter(0.985, 0.995, default=buy_params['low_offset'], space='buy', optimize=True)
    high_offset = DecimalParameter(1.005, 1.015, default=sell_params['high_offset'], space='sell', optimize=True)
    high_offset_2 = DecimalParameter(1.010, 1.020, default=sell_params['high_offset_2'], space='sell', optimize=True)

    # lambo2
    lambo2_ema_14_factor = DecimalParameter(0.975, 0.995, decimals=3, default=buy_params['lambo2_ema_14_factor'], space='buy', optimize=True)
    lambo2_rsi_4_limit = IntParameter(30, 60, default=buy_params['lambo2_rsi_4_limit'], space='buy', optimize=True)
    lambo2_rsi_14_limit = IntParameter(30, 55, default=buy_params['lambo2_rsi_14_limit'], space='buy', optimize=True)

    # Protection
    fast_ewo = 50
    slow_ewo = 200

    rsi_buy = IntParameter(35, 60, default=buy_params['rsi_buy'], space='buy', optimize=True)
    move = IntParameter(35, 60, default=48, space='buy', optimize=True)
    mms = IntParameter(6, 20, default=12, space='buy', optimize=True)
    mml = IntParameter(300, 400, default=360, space='buy', optimize=True)

    # cofi
    is_optimize_cofi = False
    buy_ema_cofi = DecimalParameter(0.96, 0.98, default=0.97, optimize=is_optimize_cofi)
    buy_fastk = IntParameter(20, 30, default=20, optimize=is_optimize_cofi)
    buy_fastd = IntParameter(20, 30, default=20, optimize=is_optimize_cofi)
    buy_adx = IntParameter(20, 30, default=30, optimize=is_optimize_cofi)
    buy_ewo_high = DecimalParameter(2, 12, default=3.553, optimize=is_optimize_cofi)

    increment = DecimalParameter(low=1.0005, high=1.001, default=1.0007, decimals=4, space='buy', optimize=True, load=True)
    use_custom_stoploss = True
    process_only_new_candles = True

    # Custom Entry
    last_entry_price = None

    # Unclog (3 days for shorts vs 4 days for longs)
    unclog_days = IntParameter(1, 5, default=3, space='sell', optimize=True)
    unclog = DecimalParameter(0.01, 0.08, default=0.04, decimals=2, space='sell', optimize=True)

    # ATR-Based Dynamic Stop Loss
    atr_stop_multiplier = DecimalParameter(1.5, 3.0, default=2.0, space='sell', optimize=True)
    max_atr_stop = DecimalParameter(-0.20, -0.10, default=-0.15, space='sell', optimize=False)
    min_atr_stop = DecimalParameter(-0.05, -0.02, default=-0.03, space='sell', optimize=False)

    def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float,
                            time_in_force: str, current_time: datetime, entry_tag: Optional[str],
                            side: str, **kwargs) -> bool:
        """
        Enforce maximum short position limit.

        This callback is executed before every entry to ensure we don't exceed
        max_short_trades positions. Essential for risk management in crypto shorts.

        Args:
            side: Trade direction ('long' or 'short')

        Returns:
            bool: True to confirm entry, False to reject
        """
        # Only allow shorts in this strategy
        if side == "long":
            return False  # Reject any long signals

        # Count current open short positions
        short_count = 0
        trades = Trade.get_trades_proxy(is_open=True)
        for trade in trades:
            if trade.is_short:
                short_count += 1

        # Check if we can open another short
        if short_count >= self.max_short_trades:
            return False  # Already at max short positions

        return True  # Confirm entry

    ### Trailing Stop with Crash Mode ###
    def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
                        current_rate: float, current_profit: float, **kwargs) -> float:
        """
        Custom stoploss with 2-layer protection for shorts (mirrors long strategy):
        
        Layer 1: ATR-based dynamic stop for LOSING trades (prevents liquidations)
        Layer 2: Crash mode trailing for PROFITABLE shorts (immediate activation)
        
        Logic:
        - LOSING trades: Use ATR-based stop immediately
        - PROFITABLE trades: Apply crash mode immediately (no 48-hour delay)
          - Crash mode (min_l < 0.003): Making new lows = tighten stops (lock profits)
          - Rally mode (min_l >= 0.003): Not making new lows = allow trailing
        
        For shorts:
        - Crash mode (min_l < 0.003): Making new lows = profitable short → TIGHTEN stops
        - Rally mode (min_l >= 0.003): Not making new lows → Allow trailing
        
        This is the inverse of the long strategy's moon mode which loosens stops
        when making new highs. For shorts, we want to protect profit when the
        market crashes (our shorts are very profitable).
        
        Args:
            pair: Trading pair
            trade: Trade object
            current_time: Current timestamp
            current_rate: Current price
            current_profit: Current profit ratio
            **kwargs: Additional arguments
            
        Returns:
            float: Stoploss percentage (must be MORE NEGATIVE than base to tighten)
        """
        dataframe, _ = self.dp.get_analyzed_dataframe(pair=pair, timeframe=self.timeframe)
        current_candle = dataframe.iloc[-1].squeeze()
        
        # PHASE 1: ATR-Based Dynamic Stop Loss (for losing trades)
        # Prevents liquidations by using volatility-adjusted stops
        if current_profit < 0:
            # Calculate ATR-based stop: 2x ATR as percentage
            atr_stop = -1 * self.atr_stop_multiplier.value * current_candle['atr_pcnt']
            
            # Clamp between bounds to prevent too tight or too loose stops
            # max_atr_stop = -0.15 (maximum loss allowed)
            # min_atr_stop = -0.03 (minimum protection)
            atr_stop = max(atr_stop, self.max_atr_stop.value)
            atr_stop = min(atr_stop, self.min_atr_stop.value)
            
            # Return ATR-based stop for losing trades
            # This prevents trades from reaching -50%+ losses
            return atr_stop
        
        # PHASE 2: Crash Mode Trailing Stop (for profitable trades - IMMEDIATE activation)
        SLT1 = current_candle['move_mean']
        SL1 = self.sl1.value
        SLT2 = current_candle['move_mean_x']
        SL2 = current_candle['move_mean_x'] - current_candle['move_mean']
        display_profit = current_profit * 100
        slt1 = SLT1 * 100
        sl1 = SL1 * 100
        slt2 = SLT2 * 100
        sl2 = SL2 * 100
        
        # Crash mode: When making new lows (min_l < 0.003), tighten stops
        # Small min_l = price at/near lows = SHORT IS PROFITABLE
        if current_candle['min_l'] < .003:
            # Short is very profitable (market crashing to new lows), use conservative stop
            if SLT1 is not None and current_profit > SL1:
                self.dp.send_msg(f'*** {pair} *** SHORT Profit {display_profit:.2f}% - Crash Mode {slt1:.2f}/{sl1:.2f} activated')
                logger.info(f'*** {pair} *** SHORT Profit {display_profit:.2f}% - Crash Mode {slt1:.2f}/{sl1:.2f} activated')
                return SL1
        
        # Rally mode: Not making new lows (price rising away from lows), allow trailing
        else:
            if SLT2 is not None and current_profit > SLT2:
                self.dp.send_msg(f'*** {pair} *** SHORT Profit {display_profit:.2f}% - {slt2:.2f}/{sl2:.2f} activated')
                logger.info(f'*** {pair} *** SHORT Profit {display_profit:.2f}% - {slt2:.2f}/{sl2:.2f} activated')
                return SL2
            if SLT1 is not None and current_profit > SLT1:
                self.dp.send_msg(f'*** {pair} *** SHORT Profit {display_profit:.2f}% - {slt1:.2f}/{sl1:.2f} activated')
                logger.info(f'*** {pair} *** SHORT Profit {display_profit:.2f}% - {slt1:.2f}/{sl1:.2f} activated')
                return SL1
        
        # Default: Return base stoploss
        return self.stoploss

    def custom_entry_price(self, pair: str, trade: Optional['Trade'], current_time: datetime, proposed_rate: float,
                           entry_tag: Optional[str], side: str, **kwargs) -> float:
        """
        Custom entry price to avoid order clustering.

        For shorts: DECREMENT price (enter lower) if same as last entry.
        This is the inverse of longs which increment (enter higher).

        Args:
            pair: Trading pair
            trade: Trade object (None for new entries)
            current_time: Current timestamp
            proposed_rate: Proposed entry rate
            entry_tag: Entry signal tag
            side: Trade direction

        Returns:
            float: Adjusted entry price
        """
        dataframe, last_updated = self.dp.get_analyzed_dataframe(pair=pair, timeframe=self.timeframe)

        entry_price = (dataframe['close'].iat[-1] + dataframe['open'].iat[-1] + proposed_rate + proposed_rate) / 4
        logger.info(f"{pair} Using Entry Price: {entry_price} | close: {dataframe['close'].iat[-1]} open: {dataframe['open'].iat[-1]} proposed_rate: {proposed_rate}")

        # Check if there is a stored last entry price and if it matches the proposed entry price
        if self.last_entry_price is not None and abs(entry_price - self.last_entry_price) < 0.0001:
            # For shorts: INCREMENT to enter at a better (higher) price
            # When shorting, you SELL first, so entering at HIGHER price = better entry
            entry_price *= self.increment.value  # Multiply to enter higher
            logger.info(f"{pair} SHORT: Incremented entry price: {entry_price} based on previous entry price: {self.last_entry_price}.")

        # Update the last entry price
        self.last_entry_price = entry_price

        return entry_price

    def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float,
                           rate: float, time_in_force: str, exit_reason: str,
                           current_time: datetime, **kwargs) -> bool:
        """
        Confirm trade exit with crash mode logic (inverse of moon mode).

        For shorts:
        - Block ROI exit if NOT making new lows (min_l < 0.003)
        - Block exits below 0.3% profit

        Args:
            pair: Trading pair
            trade: Trade object
            order_type: Order type
            amount: Trade amount
            rate: Exit rate
            time_in_force: Time in force
            exit_reason: Reason for exit
            current_time: Current timestamp

        Returns:
            bool: True to confirm exit, False to block
        """
        dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
        last_candle = dataframe.iloc[-1].squeeze()

        # Block ROI exit if not making new lows (inverted from longs)
        # For shorts: min_l > 0.003 = price far from lows = not crashing = block exit
        if exit_reason == 'roi' and (last_candle['min_l'] > 0.003):
            return False

        # Handle freak events - block exits with low profit
        if exit_reason == 'Down Trend Soon' and trade.calc_profit_ratio(rate) < 0.003:
            logger.info(f"{trade.pair} Waiting for Profit")
            self.dp.send_msg(f'{trade.pair} Waiting for Profit')
            return False

        if exit_reason == 'roi' and trade.calc_profit_ratio(rate) < 0.003:
            logger.info(f"{trade.pair} ROI is below 0.003")
            self.dp.send_msg(f'{trade.pair} ROI is below 0.003')
            return False

        if exit_reason == 'partial_exit' and trade.calc_profit_ratio(rate) < 0:
            logger.info(f"{trade.pair} partial exit is below 0")
            self.dp.send_msg(f'{trade.pair} partial exit is below 0')
            return False

        if exit_reason == 'trailing_stop_loss' and trade.calc_profit_ratio(rate) < 0:
            logger.info(f"{trade.pair} trailing stop price is below 0")
            self.dp.send_msg(f'{trade.pair} trailing stop price is below 0')
            return False

        return True

    def custom_exit(self, pair: str, trade: 'Trade', current_time: 'datetime', current_rate: float, current_profit: float, **kwargs):
        """
        2-Layer Exit System for Shorts (SIMPLIFIED):
        
        Layer 1: Base stoploss (-0.99) = Safety net (almost never hit)
        Layer 2: custom_stoploss = ATR-based stops + Crash mode trailing
        Layer 3: custom_exit = Time-based cleanup only
        
        Logic:
        - REMOVED: Reversal detection (too aggressive, causes premature exits)
        - Profitable trades: Handled by custom_stoploss crash mode (activates immediately)
        - Losing trades: Handled by custom_stoploss ATR-based stops
        - After 48 hours: Unclog losing/zombie trades that ATR stop didn't catch
        
        Args:
            pair: Trading pair
            trade: Trade object
            current_time: Current timestamp
            current_rate: Current rate
            current_profit: Current profit
            
        Returns:
            str: Exit reason or None
        """
        # Calculate trade duration in hours
        trade_duration_hours = (current_time - trade.open_date_utc).total_seconds() / 3600
        
        # First 48 hours - Let ATR stops and crash mode handle everything
        if trade_duration_hours < 48:
            return None
        
        # After 48 hours - Unclog losing/zombie trades that stops didn't catch
        
        # Unclog: Force exit if losing > 4% (worst case scenario)
        if current_profit < -0.04:
            logger.info(f"*** {pair} *** SHORT Unclog: {current_profit*100:.2f}% loss after {trade_duration_hours:.1f} hours")
            return 'unclog_short'
        
        # Zombie: Force exit if stuck at breakeven after 48h
        if -0.005 <= current_profit <= 0.005:
            logger.info(f"*** {pair} *** SHORT Zombie: {current_profit*100:.2f}% (breakeven) after {trade_duration_hours:.1f} hours")
            return 'zombie_short'
        
        # Let custom_stoploss handle all other exits
        return None

    def leverage(self, pair: str, current_time: datetime, current_rate: float,
                 proposed_leverage: float, max_leverage: float, side: str, **kwargs) -> float:
        """
        Fixed 3x leverage for all short trades.

        Returns:
            float: Leverage multiplier (3.0 = 3x)
        """
        return 3.0

    # Exit signal
    use_exit_signal = True
    exit_profit_only = True
    exit_profit_offset = 0.01
    ignore_roi_if_entry_signal = False

    ## Optional order time in force.
    order_time_in_force = {
        'entry': 'gtc',
        'exit': 'gtc'
    }

    # Optimal timeframe for the strategy
    timeframe = '5m'

    position_adjustment_enable = False
    process_only_new_candles = True
    startup_candle_count = 400

    plot_config = {
        'main_plot': {
            'ma_buy': {'color': 'orange'},
            'ma_sell': {'color': 'orange'},
        },
    }

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Calculate technical indicators.

        Same indicators as long strategy - only the interpretation differs.
        """
        # Calculate all ma_buy values
        for val in self.base_nb_candles_buy.range:
            dataframe[f'ma_buy_{val}'] = ta.EMA(dataframe, timeperiod=val)

        # Calculate all ma_sell values
        for val in self.base_nb_candles_sell.range:
            dataframe[f'ma_sell_{val}'] = ta.EMA(dataframe, timeperiod=val)

        dataframe['ma_lo'] = dataframe[f'ma_buy_{self.base_nb_candles_buy.value}'] * (self.low_offset.value)
        dataframe['ma_hi'] = dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * (self.high_offset.value)
        dataframe['ma_hi_2'] = dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * (self.high_offset_2.value)

        dataframe['hma_50'] = qtpylib.hull_moving_average(dataframe['close'], window=50)
        # HMA-BUY SQUEEZE
        dataframe['HMA_SQZ'] = (((dataframe[f'ma_buy_{self.base_nb_candles_buy.value}'] - dataframe['hma_50'])
            / dataframe[f'ma_buy_{self.base_nb_candles_buy.value}']) * 100)

        dataframe['zero'] = 0
        # Elliot
        dataframe['EWO'] = EWO(dataframe, self.fast_ewo, self.slow_ewo)
        dataframe.loc[dataframe['EWO'] > 0, "EWO_UP"] = dataframe['EWO']
        dataframe.loc[dataframe['EWO'] < 0, "EWO_DN"] = dataframe['EWO']
        dataframe['EWO_UP'].ffill()
        dataframe['EWO_DN'].ffill()
        dataframe['EWO_MEAN_UP'] = dataframe['EWO_UP'].mean()
        dataframe['EWO_MEAN_DN'] = dataframe['EWO_DN'].mean()
        dataframe['EWO_UP_FIB'] = dataframe['EWO_MEAN_UP'] * 1.618
        dataframe['EWO_DN_FIB'] = dataframe['EWO_MEAN_DN'] * 1.618

        # RSI
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['rsi_fast'] = ta.RSI(dataframe, timeperiod=4)
        dataframe['rsi_slow'] = ta.RSI(dataframe, timeperiod=20)

        # lambo2
        dataframe['ema_14'] = ta.EMA(dataframe, timeperiod=14)
        dataframe['rsi_4'] = ta.RSI(dataframe, timeperiod=4)
        dataframe['rsi_14'] = ta.RSI(dataframe, timeperiod=14)

        # Cofi
        stoch_fast = ta.STOCHF(dataframe, 5, 3, 0, 3, 0)
        dataframe['fastd'] = stoch_fast['fastd']
        dataframe['fastk'] = stoch_fast['fastk']
        dataframe['adx'] = ta.ADX(dataframe)
        dataframe['ema_8'] = ta.EMA(dataframe, timeperiod=8)

        dataframe['OHLC4'] = (dataframe['open'] + dataframe['high'] + dataframe['low'] + dataframe['close']) / 4

        # Check how far we are from min and max
        dataframe['max'] = dataframe['OHLC4'].rolling(self.mms.value).max() / dataframe['OHLC4'] - 1
        dataframe['min'] = abs(dataframe['OHLC4'].rolling(self.mms.value).min() / dataframe['OHLC4'] - 1)

        dataframe['max_l'] = dataframe['OHLC4'].rolling(self.mml.value).max() / dataframe['OHLC4'] - 1
        dataframe['min_l'] = abs(dataframe['OHLC4'].rolling(self.mml.value).min() / dataframe['OHLC4'] - 1)

        # Apply rolling window operation to the 'OHLC4'column
        rolling_window = dataframe['OHLC4'].rolling(self.move.value)
        rolling_max = rolling_window.max()
        rolling_min = rolling_window.min()

        # Calculate the peak-to-peak value on the resulting rolling window data
        ptp_value = rolling_window.apply(lambda x: np.ptp(x))

        # Assign the calculated peak-to-peak value to the DataFrame column
        dataframe['move'] = ptp_value / dataframe['OHLC4']
        dataframe['move_mean'] = dataframe['move'].mean()
        dataframe['move_mean_x'] = dataframe['move'].mean() * 1.6
        dataframe['exit_mean'] = rolling_min * (1 + dataframe['move_mean'])
        dataframe['exit_mean_x'] = rolling_min * (1 + dataframe['move_mean_x'])
        dataframe['enter_mean'] = rolling_max * (1 - dataframe['move_mean'])
        dataframe['enter_mean_x'] = rolling_max * (1 - dataframe['move_mean_x'])
        dataframe['atr_pcnt'] = (ta.ATR(dataframe, timeperiod=5) / dataframe['OHLC4'])

        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Define SHORT entry conditions (inverted from long strategy).

        All 4 entry signals are inverted for overbought conditions:
        1. lambo_short: Parabolic pumps
        2. sell1ewo_short: Rallies during downtrend
        3. sell2ewo_short: Extreme overbought
        4. cofi_short: Stochastic overbought crossdown
        
        OPTIMIZATION (Jan 30, 2026):
        - Changed rsi_fast threshold from > 65 to > 60 (conservative loosening)
        - Expected: ~30% increase in short signals while maintaining quality
        - Rationale: Balances trade frequency with longs (rsi_fast < 35 equivalent)
        """
        # Lambo Short: Parabolic pump (inverted from lambo2)
        # Long: close < ema_14 * 0.981, rsi_4 < 44, rsi_14 < 39
        # Short: close > ema_14 * (2 - 0.981) = 1.019, rsi_4 > (100-44) = 56, rsi_14 > (100-39) = 61
        lambo_short = (
            (dataframe['close'] > (dataframe['ema_14'] * (2 - self.lambo2_ema_14_factor.value))) &
            (dataframe['rsi_4'] > (100 - int(self.lambo2_rsi_4_limit.value))) &
            (dataframe['rsi_14'] > (100 - int(self.lambo2_rsi_14_limit.value))) &
            (dataframe['atr_pcnt'] > dataframe['min_l']) &
            (dataframe['volume'] > 0)
        )
        dataframe.loc[lambo_short, 'enter_short'] = 1
        dataframe.loc[lambo_short, 'enter_tag'] = 'lambo_short'

        # Sell1EWO Short: Rally during downtrend (inverted from buy1ewo)
        # Long: rsi_fast < 35, close < ma_lo, EWO > EWO_MEAN_UP
        # Short: rsi_fast > 60 (OPTIMIZED from 65), close > ma_hi, EWO < EWO_MEAN_DN
        sell1ewo_short = (
                (dataframe['rsi_fast'] > 60) &  # CHANGED from 65 to 60
                (dataframe['close'] > dataframe['ma_hi']) &
                (dataframe['EWO'] < dataframe['EWO_MEAN_DN']) &
                (dataframe['close'] > dataframe['exit_mean_x']) &
                (dataframe['close'].shift() > dataframe['exit_mean_x'].shift()) &
                (dataframe['rsi'] > (100 - self.rsi_buy.value)) &
                (dataframe['atr_pcnt'] > dataframe['min']) &
                (dataframe['volume'] > 0)
        )
        dataframe.loc[sell1ewo_short, 'enter_short'] = 1
        dataframe.loc[sell1ewo_short, 'enter_tag'] = 'sell1ewo_short'

        # Sell2EWO Short: Extreme overbought (inverted from buy2ewo)
        # Long: rsi_fast < 35, close < ma_lo, EWO < EWO_DN_FIB
        # Short: rsi_fast > 60 (OPTIMIZED from 65), close > ma_hi, EWO > EWO_UP_FIB
        sell2ewo_short = (
                (dataframe['rsi_fast'] > 60) &  # CHANGED from 65 to 60
                (dataframe['close'] > dataframe['ma_hi']) &
                (dataframe['EWO'] > dataframe['EWO_UP_FIB']) &
                (dataframe['atr_pcnt'] > dataframe['min']) &
                (dataframe['volume'] > 0)
        )
        dataframe.loc[sell2ewo_short, 'enter_short'] = 1
        dataframe.loc[sell2ewo_short, 'enter_tag'] = 'sell2ewo_short'

        # COFI Short: Overbought crossdown (inverted from cofi)
        # Long: open < ema_8 * 0.98, crossed_above(fastk, fastd), fastk < 22, fastd < 20
        # Short: open > ema_8 * (2 - 0.98) = 1.02, crossed_below(fastk, fastd), fastk > 78, fastd > 80
        cofi_short = (
                (dataframe['open'] > dataframe['ema_8'] * (2 - self.buy_ema_cofi.value)) &
                (qtpylib.crossed_below(dataframe['fastk'], dataframe['fastd'])) &
                (dataframe['fastk'] > (100 - self.buy_fastk.value)) &
                (dataframe['fastd'] > (100 - self.buy_fastd.value)) &
                (dataframe['adx'] > self.buy_adx.value) &
                (dataframe['EWO'] < dataframe['EWO_MEAN_DN']) &
                (dataframe['atr_pcnt'] > dataframe['min']) &
                (dataframe['volume'] > 0)
            )
        dataframe.loc[cofi_short, 'enter_short'] = 1
        dataframe.loc[cofi_short, 'enter_tag'] = 'cofi_short'

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Define SHORT exit conditions (inverted from long strategy).

        Exit when price falls below thresholds (profit target reached for shorts).
        """
        # Exit Short Condition 1 (inverted from condition5)
        # Long: close > hma_50 AND close > ma_hi_2 AND close > exit_mean_x
        # Short: close < hma_50 AND close < ma_lo AND close < enter_mean_x
        condition_short_1 = (
                (dataframe['close'] < dataframe['hma_50']) &
                (dataframe['close'] < dataframe['ma_lo']) &
                (dataframe['close'] < dataframe['enter_mean_x']) &
                (dataframe['rsi'] < 50) &
                (dataframe['volume'] > 0) &
                (dataframe['rsi_fast'] < dataframe['rsi_slow'])
            )
        dataframe.loc[condition_short_1, 'exit_short'] = 1
        dataframe.loc[condition_short_1, 'exit_tag'] = 'Close < Offset Lo 1'

        # Exit Short Condition 2 (inverted from condition6)
        # Long: close < hma_50 AND close > ma_hi
        # Short: close > hma_50 AND close < ma_lo
        condition_short_2 = (
                (dataframe['close'] > dataframe['hma_50']) &
                (dataframe['close'] < dataframe['ma_lo']) &
                (dataframe['volume'] > 0) &
                (dataframe['rsi_fast'] < dataframe['rsi_slow'])
            )
        dataframe.loc[condition_short_2, 'exit_short'] = 1
        dataframe.loc[condition_short_2, 'exit_tag'] = 'Close < Offset Lo 2'

        return dataframe


def pct_change(a, b):
    return (b - a) / a