# source: https://raw.githubusercontent.com/mpicard/lucy/34591dd9f5365682e709091c567051d63d749552/user_data/strategies/CofiBitStrategy.py
# --- Do not remove these libs ---
import freqtrade.vendor.qtpylib.indicators as qtpylib
import talib.abstract as ta
from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame


# --------------------------------


class github_mpicard_lucy__CofiBitStrategy__20210225_202849(IStrategy):
    """
    taken from slack by user CofiBit
    """

    # Minimal ROI designed for the strategy.
    # This attribute will be overridden if the config file contains "minimal_roi"
    minimal_roi = {"40": 0.05, "30": 0.06, "20": 0.07, "0": 0.10}

    # Optimal stoploss designed for the strategy
    # This attribute will be overridden if the config file contains "stoploss"
    stoploss = -0.25

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

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        stoch_fast = ta.STOCHF(dataframe, 5, 3, 0, 3, 0)
        dataframe["fastd"] = stoch_fast["fastd"]
        dataframe["fastk"] = stoch_fast["fastk"]
        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")
        dataframe["adx"] = ta.ADX(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["open"] < dataframe["ema_low"])
                & (qtpylib.crossed_above(dataframe["fastk"], dataframe["fastd"]))
                & (dataframe["fastk"] < 30)
                & (dataframe["fastd"] < 30)
                & (dataframe["adx"] > 30)
            ),
            "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["open"] >= dataframe["ema_high"]))
            | (dataframe["fastk"] > 70)
            | (qtpylib.crossed_above(dataframe["fastd"], 70)),
            "sell",
        ] = 1

        return dataframe
