# source: https://raw.githubusercontent.com/ikonclast/bob_der_botmeister/da05d31149b2f7577580f600e6bb591310274fe0/strategies/BRE_MV_REPAIR_HYPER.py
# BRE_MV Hyperopt-Ready Repair
# Base: BRE_MV_E1X1_G0_66c6 (302 trades, -27.65%)
# Goal: Optimize to Positive Profit

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

class Github_ikonclast_bob_der_botmeister__BRE_MV_REPAIR_HYPER__20260716_095920(IStrategy):
    """
    BRE_MV Repair with Hyperopt Parameters
    Target: Fix the -28% profit while keeping 200+ trades
    """
    timeframe = '1h'
    
    # HYPEROPT PARAMS
    # Entry
    ema_period = IntParameter(10, 30, default=20, space='buy')
    ema_threshold = DecimalParameter(0.980, 0.998, default=0.995, space='buy', decimals=3)
    
    # Exit 
    exit_threshold = DecimalParameter(0.970, 0.990, default=0.985, space='sell', decimals=3)
    
    # Risk
    stoploss_pct = DecimalParameter(-0.08, -0.02, default=-0.03, space='sell', decimals=2)
    
    # Trailing
    trailing = DecimalParameter(0.005, 0.030, default=0.015, space='sell', decimals=3)
    
    @property
    def stoploss(self):
        return float(self.stoploss_pct.value)
    
    @property
    def trailing_stop_positive(self):
        return float(self.trailing.value)
    
    minimal_roi = {
        "0": 0.03,
        "30": 0.015,
        "60": 0.005,
    }
    
    trailing_stop = True
    
    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe['ema'] = ta.EMA(dataframe, timeperiod=int(self.ema_period.value))
        return dataframe
    
    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[:, 'buy'] = 0
        threshold = float(self.ema_threshold.value)
        dataframe.loc[dataframe['close'] > dataframe['ema'] * threshold, 'buy'] = 1
        return dataframe
    
    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[:, 'sell'] = 0
        exit_thresh = float(self.exit_threshold.value)
        dataframe.loc[dataframe['close'] < dataframe['ema'] * exit_thresh, 'sell'] = 1
        return dataframe
