# source: https://raw.githubusercontent.com/acabreracmx/freqtrade/b03dd84f1da6326bbf7caf4160d7ba6f2793cc2a/Strats%26Configs/low_bb_test.py
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement

import talib.abstract as ta
from pandas import DataFrame

import freqtrade.vendor.qtpylib.indicators as qtpylib
from freqtrade.indicator_helpers import fishers_inverse
from freqtrade.strategy.interface import IStrategy


class github_acabreracmx_freqtrade__low_bb_test__20210830_101033(IStrategy):
    """

    author@: Thorsten

    works on new objectify branch!

    idea:
        buy after crossing .98 * lower_bb and sell if trailing stop loss is hit
    """

    # Minimal ROI designed for the strategy.
    # This attribute will be overridden if the config file contains "minimal_roi"
    minimal_roi = {
        "0": 0.22222748552257474,
        "33": 0.06686398563478446,
        "83": 0.03871781955201286,
        "190": 0
    }

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

    # Optimal ticker interval for the strategy
    #timeframe = '1m'

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

    # Optional time in force for orders
    order_time_in_force = {
        'buy': 'gtc',
        'sell': 'gtc',
    }

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        ##################################################################################
        # buy and sell indicators

        bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=1)
        dataframe['bb_lowerband'] = bollinger['lower']
        dataframe['bb_middleband'] = bollinger['mid']
        dataframe['bb_upperband'] = bollinger['upper']

        #dataframe['ema50'] = ta.EMA(dataframe, timeperiod=20)

        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['close'] > dataframe['ema50']) &
                qtpylib.crossed_below(dataframe['close'], 0.9891240318470478 * dataframe['bb_lowerband'])
                #(dataframe['close'] <= 0.98 * dataframe['bb_lowerband'])

            )
            ,
            '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[
            (),
            'sell'] = 0
        return dataframe
