# source: https://raw.githubusercontent.com/hamidreza07/freqai-strategy/dd7354ccfcf24d50f2a62294e623ccf0a033da2a/startegy%20test/5/MFI2/MFI2.py

# --- 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
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy # noqa
from freqtrade.strategy import CategoricalParameter, DecimalParameter, IntParameter

import Config

class Github_hamidreza07_freqai_strategy__MFI2__20241005_141958(IStrategy):
    """
    Simple strategy based on MFI (Volume-weighted Strength)

    How to use it?
    > python3 ./freqtrade/main.py -s Github_hamidreza07_freqai_strategy__MFI2__20241005_141958
    """
    buy_params = {
        "buy_adx": 89.0,
        "buy_adx_enabled": False,
        "buy_bb_enabled": True,
        "buy_bb_gain": 0.09,
        "buy_dm_enabled": False,
        "buy_ema_enabled": False,
        "buy_fisher": -0.01,
        "buy_fisher_enabled": True,
        "buy_mfi": 3.1,
        "buy_mfi_enabled": False,
        "buy_neg_macd_enabled": False,
        "buy_rsi": 17.1,
        "buy_rsi_enabled": False,
        "buy_sar_enabled": False,
    }

    # buy_params = {
    #     "buy_adx": 89.0,
    #     "buy_adx_enabled": False,
    #     "buy_bb_enabled": True,
    #     "buy_bb_gain": 0.07,
    #     "buy_dm_enabled": False,
    #     "buy_ema_enabled": False,
    #     "buy_fisher": 0.83,
    #     "buy_fisher_enabled": False,
    #     "buy_mfi": 3.1,
    #     "buy_mfi_enabled": False,
    #     "buy_neg_macd_enabled": True,
    #     "buy_rsi": 17.1,
    #     "buy_rsi_enabled": True,
    #     "buy_sar_enabled": True,
    # }
    buy_rsi = DecimalParameter(0.1, 25, decimals=1, default=20, space="buy")
    buy_mfi = DecimalParameter(0.1, 10, decimals=1, default=2, space="buy")
    buy_adx = DecimalParameter(1, 99, decimals=0, default=1, space="buy")
    buy_fisher = DecimalParameter(-1, 1, decimals=2, default=0.18, space="buy")

    buy_bb_gain = DecimalParameter(0.01, 0.10, decimals=2, default=0.04, space="buy")
    buy_bb_enabled = CategoricalParameter([True, False], default=True, space="buy")
    buy_neg_macd_enabled = CategoricalParameter([True, False], default=True, space="buy")
    buy_rsi_enabled = CategoricalParameter([True, False], default=False, space="buy")
    buy_ema_enabled = CategoricalParameter([True, False], default=False, space="buy")
    buy_adx_enabled = CategoricalParameter([True, False], default=False, space="buy")
    buy_dm_enabled = CategoricalParameter([True, False], default=True, space="buy")
    buy_mfi_enabled = CategoricalParameter([True, False], default=False, space="buy")
    buy_sar_enabled = CategoricalParameter([True, False], default=False, space="buy")
    buy_fisher_enabled = CategoricalParameter([True, False], default=True, space="buy")

    sell_mfi = DecimalParameter(80, 100, decimals=0, default=95, space="sell")
    sell_hold_enabled = CategoricalParameter([True, False], default=True, space="sell")


    startup_candle_count = 20

    # set common parameters
    minimal_roi = Config.minimal_roi
    trailing_stop = Config.trailing_stop
    trailing_stop_positive = Config.trailing_stop_positive
    trailing_stop_positive_offset = Config.trailing_stop_positive_offset
    trailing_only_offset_is_reached = Config.trailing_only_offset_is_reached
    stoploss = Config.stoploss
    timeframe = Config.timeframe
    process_only_new_candles = Config.process_only_new_candles
    use_exit_signal = Config.use_exit_signal
    exit_profit_only = Config.exit_profit_only
    ignore_roi_if_entry_signal = Config.ignore_roi_if_entry_signal
    order_types = Config.order_types

    def informative_pairs(self):
        """
        Define additional, informative pair/interval combinations to be cached from the exchange.
        These pair/interval combinations are non-tradeable, unless they are part
        of the whitelist as well.
        For more information, please consult the documentation
        :return: List of tuples in the format (pair, interval)
            Sample: return [("ETH/USDT", "5m"),
                            ("BTC/USDT", "15m"),
                            ]
        """
        return []

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Adds several different TA indicators to the given DataFrame

        Performance Note: For the best performance be frugal on the number of indicators
        you are using. Let uncomment only the indicator you are using in your strategies
        or your hyperopt configuration, otherwise you will waste your memory and CPU usage.
        """

        # ADX
        dataframe['adx'] = ta.ADX(dataframe)

        # Plus Directional Indicator / Movement
        dataframe['dm_plus'] = ta.PLUS_DM(dataframe)
        dataframe['di_plus'] = ta.PLUS_DI(dataframe)

        # Minus Directional Indicator / Movement
        dataframe['dm_minus'] = ta.MINUS_DM(dataframe)
        dataframe['di_minus'] = ta.MINUS_DI(dataframe)
        dataframe['dm_delta'] = dataframe['dm_plus'] - dataframe['dm_minus']
        dataframe['di_delta'] = dataframe['di_plus'] - dataframe['di_minus']

        # MFI
        dataframe['mfi'] = ta.MFI(dataframe)

        # MACD
        macd = ta.MACD(dataframe)
        dataframe['macd'] = macd['macd']
        dataframe['macdsignal'] = macd['macdsignal']
        dataframe['macdhist'] = macd['macdhist']

        # Stoch fast
        stoch_fast = ta.STOCHF(dataframe)
        dataframe['fastd'] = stoch_fast['fastd']
        dataframe['fastk'] = stoch_fast['fastk']

        # RSI
        dataframe['rsi'] = ta.RSI(dataframe)

        # Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy)
        rsi = 0.1 * (dataframe['rsi'] - 50)
        dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1)

        # Bollinger bands
        bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
        dataframe['bb_lowerband'] = bollinger['lower']
        dataframe['bb_upperband'] = bollinger['upper']
        dataframe["bb_gain"] = ((dataframe["bb_upperband"] - dataframe["close"]) / dataframe["close"])

        # EMA - Exponential Moving Average
        dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5)
        dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
        dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50)
        dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)

        # SAR Parabol
        dataframe['sar'] = ta.SAR(dataframe)

        # SMA - Simple Moving Average
        dataframe['sma'] = ta.SMA(dataframe, timeperiod=40)

        return dataframe

    def populate_entry_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
        """
        conditions = []

        # GUARDS AND TRENDS

        if self.buy_ema_enabled.value:
            conditions.append(dataframe['close'] < dataframe['ema5'])

        if self.buy_rsi_enabled.value:
            conditions.append(dataframe['rsi'] > self.buy_rsi.value)

        if self.buy_dm_enabled.value:
            conditions.append(dataframe['dm_delta'] > 0)

        if self.buy_mfi_enabled.value:
            conditions.append(dataframe['mfi'] <= self.buy_mfi.value)

        # only buy if close is below SAR
        if self.buy_sar_enabled.value:
            conditions.append(dataframe['close'] < dataframe['sar'])

        if self.buy_fisher_enabled.value:
            conditions.append(dataframe['fisher_rsi'] < self.buy_fisher.value)

        if self.buy_neg_macd_enabled.value:
            conditions.append(dataframe['macd'] < 0.0)

        # potential gain > goal
        if self.buy_bb_enabled.value:
            conditions.append(dataframe['bb_gain'] >= self.buy_bb_gain.value)

        if conditions:
            dataframe.loc[reduce(lambda x, y: x & y, conditions), 'buy'] = 1

        return dataframe

    def populate_exit_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
        """
        if self.sell_hold_enabled.value:
            dataframe.loc[(dataframe['close'].notnull() ), 'sell'] = 0

        else:
            # Exit long position if price is above sma(5) or MFI > 90
            dataframe.loc[
                (
                    (dataframe['close'] > dataframe['ema5']) &
                    (dataframe['mfi'] > self.sell_mfi.value)
                ),
                'sell'] = 1

        return dataframe
    
# +--------+---------+----------+--------------------------+--------------+-------------------------------+-----------------+--------------+-------------------------------+
# |   Best |   Epoch |   Trades |    Win  Draw  Loss  Win% |   Avg profit |                        Profit |    Avg duration |    Objective |           Max Drawdown (Acct) |
# |--------+---------+----------+--------------------------+--------------+-------------------------------+-----------------+--------------+-------------------------------|
# | * Best |    2/50 |       41 |     24     0    17  58.5 |        0.42% |      1869.894 USDT   (18.70%) | 0 days 15:31:00 | -1,869.89440 |       121.992 USDT    (1.02%) |
# |   Best |   34/50 |       59 |     40     2    17  67.8 |        0.36% |      2321.240 USDT   (23.21%) | 0 days 11:46:00 | -2,321.24006 |        12.920 USDT    (0.11%) |
