# source: https://raw.githubusercontent.com/coylin-zy/binance-bot/11ec4dc08a4a2286dbad8bcdcc7e3342165d062f/user_data/strategies/SimpleSpot.py
# directory_url: https://github.com/coylin-zy/binance-bot/blob/main/user_data/strategies/
# User: coylin-zy
# Repository: binance-bot
# --------------------# --- Do not remove these imports ---
from pandas import DataFrame

from freqtrade.strategy import IStrategy
import talib.abstract as ta
from technical import qtpylib


class Github_coylin_zy_binance_bot__SimpleSpot__20260826_035707(IStrategy):
    """
    Simple RSI + EMA strategy for small-cap spot trading.
    Designed for ~20 USDT balance, 5m timeframe.

    Buy when:
      - RSI crosses above 30 (oversold bounce)
      - Price is above EMA50 (uptrend filter)

    Sell when:
      - RSI crosses above 70 (overbought)
      - Or ROI/stoploss/trailing triggers
    """

    INTERFACE_VERSION = 3
    can_short = False

    minimal_roi = {
        "60": 0.04,
        "30": 0.02,
        "15": 0.01,
        "0": 0.005,
    }

    stoploss = -0.08

    trailing_stop = True
    trailing_stop_positive = 0.01
    trailing_stop_positive_offset = 0.02
    trailing_only_offset_is_reached = True

    timeframe = "5m"
    process_only_new_candles = True
    use_exit_signal = True
    exit_profit_only = False
    ignore_roi_if_entry_signal = False
    startup_candle_count = 200

    order_types = {
        "entry": "limit",
        "exit": "limit",
        "stoploss": "market",
        "stoploss_on_exchange": False,
    }

    order_time_in_force = {
        "entry": "GTC",
        "exit": "GTC",
    }

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14)
        dataframe["ema50"] = ta.EMA(dataframe, timeperiod=50)
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                qtpylib.crossed_above(dataframe["rsi"], 30)
                & (dataframe["close"] > dataframe["ema50"])
                & (dataframe["volume"] > 0)
            ),
            "enter_long",
        ] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                qtpylib.crossed_above(dataframe["rsi"], 70)
                & (dataframe["volume"] > 0)
            ),
            "exit_long",
        ] = 1
        return dataframe
