# --- Do not remove these libs ---
from freqtrade.strategy import ( DecimalParameter, 
                                IStrategy, IntParameter )
import freqtrade.vendor.qtpylib.indicators as qtpylib
from freqtrade.persistence import Trade
from datetime import datetime
from pandas import DataFrame
# --------------------------------

import talib.abstract as ta




class ShaneFibby_20211002(IStrategy):
    timeframe = '15m'
    # ROI table:
    minimal_roi = {
        "0": 100.0,
    }
    # Stoploss: 
    # Keeping this to a fixed 5% min
    stoploss = -0.1 
    use_custom_stoploss = True

    # Not used
    trailing_stop = True
    trailing_stop_positive = 0.03
    trailing_stop_positive_offset = 0.1
    trailing_only_offset_is_reached = True


    # Indicator values:
    # division_amount = DecimalParameter(2.00, 2.03, decimals=2, default=3, space="buy")

    bb_sma_length = IntParameter(45, 80, default=49, space="buy")
    bb_std_dev_length = IntParameter(45, 80, default=64, space="buy")
    bb_lower_offset = DecimalParameter(2, 6, decimals=1, default=3, space="buy")

    fib_sma_len = IntParameter(15, 50, default=50, space="buy")
    fib_atr_len = IntParameter(15, 50, default=14, space="buy")
    fib_lower_value = DecimalParameter(3, 5, decimals=2, default=4.236, space="buy")
    

    # storage dict for custom info
    custom_info = { }


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

        # BB lower band
        bb_sma_value = ta.SMA(dataframe, self.bb_sma_length.value)
        bb_std_dev_value = ta.STDDEV(dataframe, self.bb_std_dev_length.value)

        bb_lower_band = bb_sma_value - (bb_std_dev_value * self.bb_lower_offset.value)



        # Fib lower band
        fib_atr_value = ta.ATR(dataframe, self.fib_atr_len.value)
        fib_sma_value = ta.SMA(dataframe,self.fib_sma_len.value)

        fib_lower_band = fib_sma_value - fib_atr_value * self.fib_lower_value.value


        # populate dataframe 
        dataframe['bb_lower_band'] = bb_lower_band
        dataframe['fib_lower_band'] = fib_lower_band
        dataframe['ema_200'] = ta.EMA(dataframe, 200)
        # dataframe['average_value'] = (fib_lower_band + bb_lower_band) / self.division_amount.value

        return dataframe


    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # basic buy methods to keep the strategy complex
        dataframe.loc[
            (
                
                (qtpylib.crossed_above(dataframe['fib_lower_band'], dataframe['bb_lower_band']))
                &
                dataframe['close'] < dataframe['ema_200'] 
                
            ),
            'buy'] = 1

        return dataframe


    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # This is essentailly ignored as we're using strict ROI / Stoploss / TTP sale scenarios
        dataframe.loc[
            (
                (dataframe['volume'] > 0) 
            ),
            'sell'] = 0
        return dataframe

    def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
                        current_rate: float, current_profit: float, **kwargs) -> float:
        new_stoploss = 1

        if (current_profit > 0.25):
            new_stoploss = 0.05
        elif (current_profit > 0.1):
            new_stoploss = 0.03
        elif (current_profit > 0.06):
            new_stoploss = 0.02
        elif (current_profit > 0.03):
            new_stoploss = 0.01

        return new_stoploss
