# source: https://raw.githubusercontent.com/xathail/freqtrade/dfe86b64a3488a47924efb807abbf5ad29b929dc/freqtrade-bot/user_data/strategies/sample.py
import numpy as np
import pandas as pd
from pandas import DataFrame
from freqtrade.strategy import IStrategy, IntParameter, BooleanParameter
import talib.abstract as ta
 
class Github_xathail_freqtrade__sample__20250223_124108(IStrategy):
    INTERFACE_VERSION = 3
 
    can_short: bool = False
 
    minimal_roi = {
        "0": 0.1,
    }
 
    stoploss = -0.1
 
    timeframe = '30m'
 
    process_only_new_candles = True
 
    use_exit_signal = True
    exit_profit_only = False
    ignore_roi_if_entry_signal = False
 
    startup_candle_count: int = 200
 
    buy_rsi = IntParameter(20, 80, default=30, space='buy', optimize=True)
    sell_rsi = IntParameter(20, 80, default=70, space='sell', optimize=True)
    short_rsi = IntParameter(20, 80, default=80, space='sell', optimize=True)
    exit_short_rsi = IntParameter(20, 80, default=40, space='buy', optimize=True)
 
    use_rsi = BooleanParameter(default=True, space='buy', optimize=True)
    use_macd = BooleanParameter(default=True, space='buy', optimize=True)
 
    max_open_trades = 3
 
    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        if self.use_rsi.value:
            dataframe['rsi'] = ta.RSI(dataframe)
        if self.use_macd.value:
            macd = ta.MACD(dataframe)
            dataframe['macd'] = macd['macd']
            dataframe['macdsignal'] = macd['macdsignal']
            dataframe['macdhist'] = macd['macdhist']
        return dataframe
 
    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = []
 
        if self.use_rsi.value:
            conditions.append(dataframe['rsi'] < self.buy_rsi.value)
        if self.use_macd.value and 'macd' in dataframe and 'macdsignal' in dataframe:
            conditions.append(dataframe['macd'] > dataframe['macdsignal'])
 
        dataframe.loc[
            np.all(conditions, axis=0),
            'enter_long'
        ] = 1
 
        conditions = []
 
        if self.use_rsi.value:
            conditions.append(dataframe['rsi'] > self.short_rsi.value)
        if self.use_macd.value and 'macd' in dataframe and 'macdsignal' in dataframe:
            conditions.append(dataframe['macd'] < dataframe['macdsignal'])
 
        dataframe.loc[
            np.all(conditions, axis=0),
            'enter_short'
        ] = 1
 
        return dataframe
 
    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            dataframe['rsi'] > self.sell_rsi.value,
            'exit_long'
        ] = 1
 
        dataframe.loc[
            dataframe['rsi'] < self.exit_short_rsi.value,
            'exit_short'
        ] = 1
 
        return dataframe