# source: https://raw.githubusercontent.com/DutchCryptoDad/Strategies/268d3468338d4663bad303a1cda9965b608274d6/adxmomentum-berlinguyinca/ADXMomentum.py
# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame
import talib.abstract as ta


# --------------------------------


class github_DutchCryptoDad_Strategies__ADXMomentum__20220808_134443(IStrategy):
    """

    author@: Gert Wohlgemuth

    converted from:

        https://github.com/sthewissen/Mynt/blob/master/src/Mynt.Core/Strategies/AdxMomentum.cs

    """

    # Minimal ROI designed for the strategy.
    # adjust based on market conditions. We would recommend to keep it low for quick turn arounds
    # This attribute will be overridden if the config file contains "minimal_roi"
    minimal_roi = {
            # DCD: The ROI is set to be 1%. SO after 1% profit it exits the trade.
        "0": 0.01
    }

    # Optimal stoploss designed for the strategy
    # DCD: The bot will exit the trade if it has a loss of 25% after entering a trade.
    stoploss = -0.25

    # Optimal timeframe for the strategy
    # DCD: Original timeframe is 1 hour.
    timeframe = '1h'

    # Number of candles the strategy requires before producing valid signals
    # DCD: The strategy starts working after 20 candles (so after 20 hours of waiting with the
    # original timeframe.
    startup_candle_count: int = 20

    # Use this section if you want to plot the indicators on a chart after backtesting
    plot_config = {
        'main_plot': {
            # Create sma line
            'sma': {'color': 'blue'},
        },
        'subplots': {
            # Create rsi subplot
            "rsi": {
                'rsi': {'color': 'orange'},
                'rsi_buy_hline': {'color': 'grey','plotly': {'opacity': 0.4}},
                'rsi_sell_hline': {'color': 'grey','plotly': {'opacity': 0.4}}
            },
        },
    }

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # DCD: ADX will be used to indicate trend strength
        dataframe['adx'] = ta.ADX(dataframe, timeperiod=14)
        # DCD: DI+ and DI- for trend direction
        dataframe['plus_di'] = ta.PLUS_DI(dataframe, timeperiod=25)
        dataframe['minus_di'] = ta.MINUS_DI(dataframe, timeperiod=25)
        # DCD: (Parabolic?) SAR is used
        dataframe['sar'] = ta.SAR(dataframe)
        # DCD: MOM (Momentum?) indicator is used
        dataframe['mom'] = ta.MOM(dataframe, timeperiod=14)

        return dataframe

    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                # DCD: When ADX is above 25 and MOM is above 0 and DI+ is bullish and DI+ > DI- then BUY
                # SAR is not used here so I see.
                    (dataframe['adx'] > 25) &
                    (dataframe['mom'] > 0) &
                    (dataframe['plus_di'] > 25) &
                    (dataframe['plus_di'] > dataframe['minus_di'])

            ),
            'buy'] = 1
        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                # DCD: Sell signal is given when mom is below 0 and DI+ < DI-
                # Other indicators have still buy parameter settings
                    (dataframe['adx'] > 25) &
                    (dataframe['mom'] < 0) &
                    (dataframe['minus_di'] > 25) &
                    (dataframe['plus_di'] < dataframe['minus_di'])

            ),
            'sell'] = 1
        return dataframe
