# source: https://raw.githubusercontent.com/1amcord/freqtrade_settings/b078a9d66f5e88f6ad18d35066241aaf91c31032/strategies/KeltnerChannel.py
# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame

# Add your lib to import here
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
import pandas_ta as pta
import numpy as np  # noqa
import pandas as pd  # noqa

# These libs are for hyperopt
from functools import reduce
from freqtrade.strategy import (
    BooleanParameter, CategoricalParameter, DecimalParameter, IStrategy, IntParameter)


class github_1amcord_freqtrade_settings__KeltnerChannel__20220814_101522(IStrategy):
    timeframe = "15m"

    {
        "params": {
            "trailing": {
                "trailing_stop": False,
                "trailing_stop_positive": False,
                "trailing_stop_positive_offset": 0.0,
                "trailing_only_offset_is_reached": False
            },
            "buy": {
                "atrs_range": 1,
                "rsi_buy_hline": 61,
                "window_range": 16
            },
            "sell": {},
            "protection": {},
            "roi": {
                "0": 0.696,
                "10216": 0.40800000000000003,
                "26070": 0.143,
                "41881": 0
            },
            "stoploss": {
                "stoploss": -0.254
            }
        }
    }
    # Both stoploss and roi are set to 100 to prevent them to give a sell signal.
    stoploss = -100
    minimal_roi = {"0": 100}

    # Hyperopt spaces
    window_range = IntParameter(13, 56, default=16, space="buy")
    atrs_range = IntParameter(1, 8, default=1, space="buy")
    rsi_buy_hline = IntParameter(30, 70, default=61, space="buy")

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Keltner Channel
        for windows in self.window_range.range:
            for atrss in self.atrs_range.range:
                dataframe[f"kc_upperband_{windows}_{atrss}"] = qtpylib.keltner_channel(
                    dataframe, window=windows, atrs=atrss)["upper"]
                dataframe[f"kc_middleband_{windows}_{atrss}"] = qtpylib.keltner_channel(
                    dataframe, window=windows, atrs=atrss)["mid"]

        # Rsi
        dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14)

        # Print stuff for debugging dataframe
        # #print(metadata)
        # #print(dataframe.tail(20)
        return dataframe

    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = []
        conditions.append(
            (qtpylib.crossed_above(
                dataframe['close'], dataframe[f"kc_upperband_{self.window_range.value}_{self.atrs_range.value}"]))
            & (dataframe['rsi'] > self.rsi_buy_hline.value)
        )

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

        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = []
        conditions.append(
            (qtpylib.crossed_below(
                dataframe['close'], dataframe[f"kc_middleband_{self.window_range.value}_{self.atrs_range.value}"]))
        )

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

        return dataframe
