# source: https://raw.githubusercontent.com/remiotore/freqtrade/44beaeb6a420cd8e9f2e4ea93e11d6cfa192ee03/strategies/Discord_MACD.py
# https://github.com/mrjbq7/ta-lib/tree/master/docs/func_groups
# https://github.com/freqtrade/freqtrade-strategies/tree/master/user_data
# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from typing import Dict, List
from functools import reduce
from pandas import DataFrame
# --------------------------------

import talib.abstract as ta


class Github_remiotore_freqtrade__Discord_MACD__20260111_210550(IStrategy):
    """
    author@: Gert Wohlgemuth
    idea:
        uptrend definition:
            Github_remiotore_freqtrade__Discord_MACD__20260111_210550 above Github_remiotore_freqtrade__Discord_MACD__20260111_210550 signal
            and CCI < -50
        downtrend definition:
            Github_remiotore_freqtrade__Discord_MACD__20260111_210550 below Github_remiotore_freqtrade__Discord_MACD__20260111_210550 signal
            and CCI > 100
    """

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

    # ROI table:
    minimal_roi = {
        "0": 0.1641,
        "40": 0.05223,
        "87": 0.01553,
        "139": 0
    }

    # Stoploss:
    stoploss = -0.1529

    # Trailing stop:
    trailing_stop = True
    trailing_stop_positive = 0.13261
    trailing_stop_positive_offset = 0.1939
    trailing_only_offset_is_reached = True

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

        macd = ta.Github_remiotore_freqtrade__Discord_MACD__20260111_210550(dataframe, fastperiod=24, slowperiod=56, signalperiod=11)
        dataframe['macd'] = macd['macd']
        dataframe['macdsignal'] = macd['macdsignal']
        dataframe['macdhist'] = macd['macdhist']
        dataframe['cci'] = ta.CCI(dataframe)

        return dataframe

    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Based on TA indicators, populates the buy signal for the given dataframe
        :param dataframe: DataFrame
        :return: DataFrame with buy column
        """
        dataframe.loc[
            (
                    (dataframe['macd'] > dataframe['macdsignal'])
                    & (dataframe['cci'] <= -183)
            ),
            'buy'] = 1

        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Based on TA indicators, populates the sell signal for the given dataframe
        :param dataframe: DataFrame
        :return: DataFrame with buy column
        """
        dataframe.loc[
            (
                    (dataframe['macd'] < dataframe['macdsignal'])
                    & (dataframe['cci'] >= 325)
            ),
            'sell'] = 1

        return dataframe
