# source: https://raw.githubusercontent.com/Black-J08/freqtrade-config-strategies/c3ef1d4c2574ba7606dc36ac2de08983c12cee34/user_data/strategies/momentum.py
from pandas import DataFrame
from freqtrade.strategy import IStrategy
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib


class Github_Black_J08_freqtrade_config_strategies__momentum__20221029_194716(IStrategy):
    INTERFACE_VERSION = 2

    stoploss = -0.04
    trailing_stop = False
    timeframe = '1d'
    use_sell_signal = True
    sell_profit_only = False
    ignore_roi_if_buy_signal = False
    startup_candle_count: int = 100
    order_types = {
        'buy': 'limit',
        'sell': 'limit',
        'stoploss': 'market',
        'stoploss_on_exchange': True
    }

    minimal_roi = {
        "0": 100
    }

    @property
    def protections(self):
        return  [
            {
                "method": "CooldownPeriod",
                "stop_duration_candles": 4
            }
        ]

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # MACD
        macd = ta.MACD(dataframe)
        dataframe['macd'] = macd['macd']
        dataframe['macdsignal'] = macd['macdsignal']
        dataframe['macdhist'] = macd['macdhist']

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

        # 30 EMA
        dataframe['ema30'] = ta.EMA(dataframe, timeperiod=30)

        return dataframe

    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                ((qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal']) & (dataframe['close'] > dataframe['ema30'])) | (qtpylib.crossed_above(dataframe['rsi'], 20))) &
                (dataframe['volume'] > 0)
            ),
            'buy'] = 1
        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                (qtpylib.crossed_below(dataframe['macd'], dataframe['macdsignal']) | (qtpylib.crossed_below(dataframe['rsi'], 80))) &
                (dataframe['volume'] > 0)
            ),
            'sell'] = 1
        return dataframe