# pr#agma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
# isort: skip_file
# --- Do not remove these libs ---
from datetime import datetime

import numpy as np  # noqa
import pandas as pd  # noqa
from freqtrade.strategy.parameters import  DecimalParameter, BooleanParameter, IntParameter
from pandas import DataFrame
import math
from functools import reduce


from freqtrade.strategy.interface import IStrategy

# --------------------------------
# Add your lib to import here
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
from freqtrade.strategy.strategy_helper import merge_informative_pair


# This class is a sample. Feel free to customize it.
class StrategyJD_5_9(IStrategy):
    # Strategy interface version - allow new iterations of the strategy interface.
    # Check the documentation or the Sample strategy to get the latest version.
    INTERFACE_VERSION = 2

    buy_decalage4 = IntParameter(1, 10, default=5, space="buy")

    min_percent4 = IntParameter(1, 20, default=10, space='buy')
    # buy_mrsi3 = DecimalParameter(-0.1, 0.1, decimals=2, default=0, space="buy")

    rapport_min_n = IntParameter(1, 20, default=10, space='buy')

    min_n = 16

    max_percent = DecimalParameter(0, 0.05, decimals=3, default=0.005, space='sell')
    max_percent2 = DecimalParameter(0, 0.05, decimals=3, default=0.005, space='sell')
    max_percent3 = DecimalParameter(0, 0.05, decimals=3, default=0.005, space='sell')

    max_profit = DecimalParameter(0, 0.1, decimals=2, default=0.01, space='sell')
    max_profit2 = DecimalParameter(0, 0.1, decimals=2, default=0.01, space='sell')
    max_profit3 = DecimalParameter(0, 0.1, decimals=2, default=0.01, space='sell')

    sell_h_RSI = IntParameter(70, 98, default=88, space='sell')
    sell_h_RSI2 = IntParameter(70, 98, default=88, space='sell')
    sell_h_RSI3 = IntParameter(70, 98, default=80, space='sell')

    sell_h_RSI2_percent = DecimalParameter(0, 0.02, decimals=3,  default=0.01, space='sell')

    n_percent = IntParameter(1, 12, default=1, space="protection")
    percent_sell = DecimalParameter(-0.1, -0.01, decimals=2, default=-0.08, space="protection")
    percent_sell_stop = DecimalParameter(-0.8, -0.1, decimals=1, default=-0.8, space="protection")
    percent_sell_sma5_1d = DecimalParameter(-0.1, 0, decimals=2, default=0, space="protection")
    hours_sell = IntParameter(5, 48, default=24, space="protection")

    # buy_adx_enabled = BooleanParameter(default=True, space="buy")
    # buy_rsi_enabled = CategoricalParameter([True, False], default=False, space="buy")
    # buy_trigger = CategoricalParameter(["bb_lower", "macd_cross_signal"], default="bb_lower", space="buy")

    lim_0 = 10
    lim_1 = 18
    lim_2 = 25
    lim_3 = 51

    # ROI table:
    minimal_roi = {
        # "0": 0.015
        "0": 0.5
    }

    # Stoploss:
    stoploss = -1
    trailing_stop = True
    trailing_stop_positive = 0.001
    trailing_stop_positive_offset = 0.0175  # 0.015
    trailing_only_offset_is_reached = True

    # max_open_trades = 3

    # Optimal ticker interval for the strategy.
    timeframe = '5m'

    # Run "populate_indicators()" only for new candle.
    process_only_new_candles = False

    # These values can be overridden in the "ask_strategy" section in the config.
    use_sell_signal = True
    sell_profit_only = False
    ignore_roi_if_buy_signal = False

    # Number of candles the strategy requires before producing valid signals
    startup_candle_count: int = 30

    # Optional order type mapping.
    order_types = {
        'buy': 'limit',
        'sell': 'limit',
        'stoploss': 'market',
        'stoploss_on_exchange': False
    }

    # Optional order time in force.
    order_time_in_force = {
        'buy': 'gtc',
        'sell': 'gtc'
    }

    plot_config = {
        "main_plot": {
            "bb_lowerband": {
                "color": "white"
            },
            "bb_upperband": {
                "color": "white"
            },
            "min200": {
                "color": "yellow"
            },
            "max200": {
                "color": "yellow"
            },
            "min_n": {
                "color": "#600e82"
            },
            "max_n": {
                "color": "#600e82"
            },
            "min5_1d": {
                "color": "#6aa123",
            },
            "max5_1d": {
                "color": "red"
            },
            "max3_1d": {
                "color": "blue"
            },
            "close_1d": {
                "color": "green"
            },
            "close_1M": {
                "color": "cyan"
            },
            "min_n_1d": {
                "color": "pink"
            },
            "max_n_1d": {
                "color": "pink"
            },
            "sma5_1d": {
                "color": "black"
            }
        },
        "subplots": {
            "Rsi": {
                "rsi_1h": {
                    "color": "blue"
                },
                "rsi_1d": {
                    "color": "red"
                },
                "rsi": {
                    "color": "green"
                },
            },
            # "Percent": {
            #   "min_max200": {
            #     "color": "#c046bb"
            #   }
            # }
        }
    }

    def custom_sell(self, pair: str, trade: 'Trade', current_time: 'datetime', current_rate: float,
                    current_profit: float, **kwargs):
        dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
        last_candle = dataframe.iloc[-1].squeeze()
        previous_last_candle = dataframe.iloc[-2].squeeze()
        previous_previous_last_candle = dataframe.iloc[-2].squeeze()

        days = (current_time - trade.open_date_utc).days
        minutes = (current_time - trade.open_date_utc).seconds / 60
        hours = minutes / 60 

        if (last_candle['sma5_diff_1d'] < self.percent_sell_sma5_1d.value) & (current_profit < -0.05):
            return 'sell_sma5_1d'

        if (current_profit < self.percent_sell_stop.value) & (hours >= self.hours_sell.value) & (last_candle['percent_1h'] <= -0.05): 
            return 'sell_stop'

        # if (last_candle['percent1'] < -0.005) & (last_candle['max200'] == previous_previous_last_candle['max200']) & (minutes > 30):
        #     return 'sell_percent1'
        #
        # if (last_candle['percent3'] < -0.005) & (last_candle['percent1'] < 0) \
        #         & (previous_last_candle['percent1'] < 0) & (previous_previous_last_candle['percent1'] < 0) & (minutes > 30):
        #     return 'sell_percent3'

        #if last_candle['percent' + str(self.n_percent.value)] < self.percent_sell.value:
        #    return 'sell_lost_percent_' + str(self.n_percent.value)

        if (last_candle['rsi_1h'] < self.lim_1):
            max_percent = self.max_percent.value
            max_profit = self.max_profit.value
        else:
            if (last_candle['rsi_1h'] < self.lim_2):
                max_percent = self.max_percent2.value
                max_profit = self.max_profit2.value
            else:
                max_percent = self.max_percent3.value
                max_profit = self.max_profit3.value

        if (current_profit > max_profit) & (
                #(last_candle['percent1'] < -max_percent) |
                (last_candle['percent3'] < -max_percent) | (last_candle['percent5'] < -max_percent)):
                #& (last_candle['close'] > last_candle['max3_1d']):
            return 'h_percent_quick'

        if (current_profit > 0) & (previous_last_candle['rsi'] > self.sell_h_RSI.value):
            return 'h_over_rsi'

        if (current_profit > 0) & (previous_last_candle['rsi'] > self.sell_h_RSI2.value) & \
                (last_candle['percent1'] < - self.sell_h_RSI2_percent.value):
            return 'h_over_rsi_2'

        if (current_profit > 0) & (previous_last_candle['rsi'] > self.sell_h_RSI3.value) & \
                (last_candle['close'] >= last_candle['max3_1d']):
            return 'h_over_rsi_max'

    def informative_pairs(self):
        # get access to all pairs available in whitelist.
        pairs = self.dp.current_whitelist()
        informative_pairs = [(pair, '1d') for pair in pairs]
        informative_pairs += [(pair, '1M') for pair in pairs]
        informative_pairs += [(pair, '1h') for pair in pairs]
        return informative_pairs

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # print(self.min_percent.value, self.min_percent2.value, self.min_percent3.value,
        #       self.buy_decalage.value, self.buy_decalage2.value, self.buy_decalage3.value,
        #       )
        dataframe['min200'] = ta.MIN(dataframe['close'], timeperiod=200)
        dataframe['max200'] = ta.MAX(dataframe['close'], timeperiod=200)
        period = int(self.min_n * 24)
        dataframe['min_n'] = ta.MIN(dataframe['close'], timeperiod=period)
        dataframe['max_n'] = ta.MAX(dataframe['close'], timeperiod=period)
        dataframe['min_max_n'] = (dataframe['max_n'] - dataframe['min_n']) / dataframe['min_n']
        for n in range(1, 25):
            dataframe["percent" + str(n)] = dataframe['close'].pct_change(n)

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

        ################### INFORMATIVE 1h
        informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe="1h")
        informative["rsi"] = ta.RSI(informative)
        informative["rsi3"] = ta.RSI(informative, 3)
        # informative["mrsi3"] = informative["rsi"].pct_change(3)
        informative['r_rsi'] = (informative['rsi3'].div(10).round())
        informative['percent'] = informative['close'].pct_change(1)
        # for n in range(1, 5):
        #     informative["percent" + str(n)] = informative['close'].pct_change(n)
        dataframe = merge_informative_pair(dataframe, informative, self.timeframe, "1h", ffill=True)

        ################### INFORMATIVE 1d
        informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe="1d")
        informative["rsi"] = ta.RSI(informative, period=5)
        informative['min5'] = ta.MIN(informative['close'], timeperiod=5)
        informative['max3'] = ta.MAX(informative['close'], timeperiod=3)
        informative['max5'] = ta.MAX(informative['close'], timeperiod=5)
        informative['sma5'] = ta.SMA(informative['close'], timeperiod=5)
        informative['min_n'] = ta.MIN(informative['close'], timeperiod=14)
        informative['max_n'] = ta.MAX(informative['close'], timeperiod=14)
        informative['min_max_n'] = (informative['max_n'] - informative['min_n']) / informative['min_n']
        informative['percent'] = informative['close'].pct_change(1)
        informative['sma5_diff'] = informative['sma5'] - informative['sma5'].shift(1)

        dataframe = merge_informative_pair(dataframe, informative, self.timeframe, "1d", ffill=True)

        ################### INFORMATIVE 1M
        informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe="1M")
        dataframe = merge_informative_pair(dataframe, informative, self.timeframe, "1M", ffill=True)

        return dataframe

    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = [
            (dataframe['close'] <= dataframe['min_n_1d']), # * (1 + (dataframe['min_max_n_1d'] / self.min_percent3.value))),
            (dataframe['close'] <= dataframe['min_n'] * (1 + (dataframe['min_max_n'] / self.min_percent4.value))),
            (dataframe['min_n'].shift(self.buy_decalage4.value) == dataframe['min_n']),
            (dataframe['min_n_1d'] / dataframe['min_n'] > 1 + self.rapport_min_n.value / 100)
        ]
        # GUARDS AND TRENDS
        if conditions:
            dataframe.loc[(reduce(lambda x, y: x & y, conditions)),
                ['buy', 'buy_tag']] = (1, 'buy_4')

        return dataframe

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