# source: https://raw.githubusercontent.com/camster91/crypto-bots/7c4d387f5795cc50312412e1cc9d7ee39a871eec/bot-instance/user_data/strategies/UltraSimpleShort.py
"""
Github_camster91_crypto_bots__UltraSimpleShort__20260519_145234 — The simplest possible bear market strategy

Just three rules:
1. Check if we're below 200 EMA (bear market)
2. Short on red candles with high volume when RSI is above 55 (overbought bounce)
3. Exit: 4% profit target OR 2% stoploss

That's it. No multi-indicator complexity.
"""

import talib.abstract as ta
import pandas as pd
from pandas import DataFrame
from freqtrade.strategy import IStrategy


class Github_camster91_crypto_bots__UltraSimpleShort__20260519_145234(IStrategy):
    INTERFACE_VERSION = 3

    timeframe = '1h'
    startup_candle_count = 50
    can_short = True
    process_only_new_candles = True

    stoploss = -0.03
    trailing_stop = True
    trailing_stop_positive = 0.01
    trailing_stop_positive_offset = 0.04
    trailing_only_offset_is_reached = True
    use_custom_stoploss = False

    minimal_roi = {
        "0": 0.06,
        "240": 0.02,
        "720": 0,
    }

    protections = [
        {"method": "CooldownPeriod", "stop_duration": 8},
        {"method": "StoplossGuard", "lookback_period": 24, "trade_limit": 1, "stop_duration": 12, "only_per_pair": False},
    ]

    def informative_pairs(self):
        return []

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe['ema_200'] = ta.EMA(dataframe, timeperiod=200)
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['volume_sma'] = dataframe['volume'].rolling(20).mean()
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        bear = dataframe['close'] < dataframe['ema_200']

        short_entry = (
            bear &
            (dataframe['rsi'] > 55) &
            (dataframe['rsi'] < 70) &  # Not extremely overbought
            (dataframe['close'] < dataframe['open']) &
            (dataframe['volume'] > dataframe['volume_sma'] * 1.5)  # 50% above avg volume
        )

        dataframe.loc[short_entry, 'enter_short'] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        return dataframe
