# --- 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
from typing import Dict, List
from functools import reduce
from pandas import DataFrame, DatetimeIndex, merge
# --------------------------------
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy  # noqa
from freqtrade.strategy import DecimalParameter, IntParameter, CategoricalParameter


class SmoothScalp(IStrategy):
    """
        this strategy is based around the idea of generating a lot of potentatils buys and make tiny profits on each trade

        we recommend to have at least 60 parallel trades at any time to cover non avoidable losses
    """

    # Minimal ROI designed for the strategy.
    # This attribute will be overridden if the config file contains "minimal_roi"
    minimal_roi = {
        "0": 0.01
    }
    # Optimal stoploss designed for the strategy
    # This attribute will be overridden if the config file contains "stoploss"
    # should not be below 3% loss

    stoploss = -0.5
    # Optimal timeframe for the strategy
    # the shorter the better
    timeframe = '1m'
    
    # Hiperparámetros para optimización
    adx_value = IntParameter(10, 50, default=30, space='buy', optimize=True)
    mfi_value = IntParameter(10, 50, default=30, space='buy', optimize=True)
    fastk_value = IntParameter(10, 50, default=30, space='buy', optimize=True)
    fastd_value = IntParameter(10, 50, default=30, space='buy', optimize=True)
    cci_value_low = IntParameter(-200, -50, default=-150, space='buy', optimize=True)

    cci_value_high = IntParameter(50, 200, default=150, space='sell', optimize=True)
    fastk_sell_value = IntParameter(50, 100, default=70, space='sell', optimize=True)
    fastd_sell_value = IntParameter(50, 100, default=70, space='sell', optimize=True)

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe['ema_high'] = ta.EMA(dataframe, timeperiod=5, price='high')
        dataframe['ema_close'] = ta.EMA(dataframe, timeperiod=5, price='close')
        dataframe['ema_low'] = ta.EMA(dataframe, timeperiod=5, price='low')
        stoch_fast = ta.STOCHF(dataframe, 5, 3, 0, 3, 0)
        dataframe['fastd'] = stoch_fast['fastd']
        dataframe['fastk'] = stoch_fast['fastk']
        dataframe['adx'] = ta.ADX(dataframe)
        dataframe['cci'] = ta.CCI(dataframe, timeperiod=20)
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['mfi'] = ta.MFI(dataframe)

        # required for graphing
        bollinger = qtpylib.bollinger_bands(dataframe['close'], window=20, stds=2)
        dataframe['bb_lowerband'] = bollinger['lower']
        dataframe['bb_upperband'] = bollinger['upper']
        dataframe['bb_middleband'] = bollinger['mid']

        macd = ta.MACD(dataframe)
        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:
        dataframe.loc[
            (
                (dataframe['open'] < dataframe['ema_low']) &
                (dataframe['adx'] > self.adx_value.value) &
                (dataframe['mfi'] < self.mfi_value.value) &
                (
                    (dataframe['fastk'] < self.fastk_value.value) &
                    (dataframe['fastd'] < self.fastd_value.value) &
                    (qtpylib.crossed_above(dataframe['fastk'], dataframe['fastd']))
                ) &
                (dataframe['cci'] < self.cci_value_low.value)
            ),
            'buy'] = 1
        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                (
                    (dataframe['open'] >= dataframe['ema_high']) |
                    (qtpylib.crossed_above(dataframe['fastk'], self.fastk_sell_value.value)) |
                    (qtpylib.crossed_above(dataframe['fastd'], self.fastd_sell_value.value))
                ) & 
                (dataframe['cci'] > self.cci_value_high.value)
            ),
            'sell'] = 1
        return dataframe
