
# --- Do not remove these libs ---
from freqtrade.strategy import ( IStrategy )
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

# Version 4
    # Remove ROI table
    # Add Custtom stop loss for tiered salesabover certail levels
    # Reformat buy trends

# Changelog
    # V3
        # added in trailing take profit
        # added additiona emas
        # added in comments

    # V2
        # Changed Long EMA period to 20

    # V1
        # Creation of the strat
        # Added EMA cross strategy based on TradingView analysis
        # Added ROI table based on some estimations


class ShaneBitty_20211002(IStrategy):
    # ROI table:
    minimal_roi = {
        "0": 100.0,
    }
    
    timeframe = "15m"

    # Stoploss: 
    # Keeping this to a fixed 5% min
    stoploss = -0.05 
    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:
    ema_xs = 3
    ema_sm = 5
    ema_md = 10
    ema_lg = 20
    ema_xl = 50

    # storage dict for custom info
    custom_info = { }


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

        # Adding EMA's into the dataframe
        dataframe['ema_xs'] = ta.EMA(dataframe, timeperiod=self.ema_xs)
        dataframe['ema_sm'] = ta.EMA(dataframe, timeperiod=self.ema_sm)
        dataframe['ema_md'] = ta.EMA(dataframe, timeperiod=self.ema_md)
        dataframe['ema_lg'] = ta.EMA(dataframe, timeperiod=self.ema_lg)
        dataframe['ema_xl'] = ta.EMA(dataframe, timeperiod=self.ema_xl)

        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['ema_sm'], dataframe['ema_md'])) 
                &
                (dataframe['ema_xs'] < dataframe['ema_xl'])
                
            ),
            '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
