# source: https://raw.githubusercontent.com/rmallarapu-bc/brahma/9287745fc036c2f4c00c586e598290885c2883b9/archive/PlutoScalper2.py
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
# flake8: noqa: F401
# isort: skip_file
# --- Do not remove these libs ---
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy as np  # noqa
import pandas as pd  # noqa
# --------------------------------
# Add your lib to import here
import talib.abstract as ta
from freqtrade.strategy import (
    IStrategy,
)
from pandas import DataFrame


# This class is a sample. Feel free to customize it.
class Github_rmallarapu_bc_brahma__PlutoScalper2__20240229_213751(IStrategy):
    INTERFACE_VERSION = 3
    timeframe = "15m"
    minimal_roi = {"0": 0.005}
    stoploss = -0.3

    # Trailing stoploss
    trailing_stop = True
    trailing_stop_positive = 0.001
    trailing_stop_positive_offset = 0.002
    trailing_only_offset_is_reached = True

    # Can this strategy go short?
    can_short: bool = True

    order_types = {
        'entry': 'limit',
        'exit': 'limit',
        'stoploss': 'market',
        'stoploss_on_exchange': False
    }

    # Optional order time in force.
    order_time_in_force = {
        'entry': 'gtc',
        'exit': 'gtc'
    }

    use_exit_signal = True
    exit_profit_only = False
    ignore_roi_if_entry_signal = True

    # Run "populate_indicators()" only for new candle.
    process_only_new_candles = True

    # Number of candles the strategy requires before producing valid signals
    startup_candle_count: int = 30

    @property
    def protections(self):
        return [
            {
                "method": "CooldownPeriod",
                "stop_duration_candles": 4
            },
            {
                "method": "MaxDrawdown",
                "lookback_period_candles": 48,
                "trade_limit": 10,
                "stop_duration_candles": 1,
                "max_allowed_drawdown": 0.05
            },
            {
                "method": "StoplossGuard",
                "lookback_period_candles": 24,
                "trade_limit": 4,
                "stop_duration_candles": 2,
                "only_per_pair": False
            },
        ]

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # RSI
        dataframe["rsi"] = ta.RSI(dataframe, window=7)

        # Bollinger Bands
        bollinger = qtpylib.bollinger_bands(
            qtpylib.typical_price(dataframe), window=20, stds=2
        )
        dataframe["bb_lowerband"] = bollinger["lower"]
        dataframe["bb_middleband"] = bollinger["mid"]
        dataframe["bb_upperband"] = bollinger["upper"]
        dataframe["bb_percent"] = (dataframe["close"] - dataframe["bb_lowerband"]) / (
                dataframe["bb_upperband"] - dataframe["bb_lowerband"]
        )
        dataframe["bb_width"] = (
                                        dataframe["bb_upperband"] - dataframe["bb_lowerband"]
                                ) / dataframe["bb_middleband"]

        # TEMA - Triple Exponential Moving Average
        dataframe["tema"] = ta.TEMA(dataframe, timeperiod=9)

        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                # Signal: RSI crosses above 30
                    (qtpylib.crossed_above(dataframe["rsi"], 40))
                    & (dataframe["tema"] <= dataframe["bb_middleband"])
                    & (  # Guard: tema below BB middle
                            dataframe["tema"] > dataframe["tema"].shift(1)
                    )
                    & (  # Guard: tema is raising
                            dataframe["volume"] > 0
                    )  # Make sure Volume is not 0
            ),
            "enter_long",
        ] = 1

        dataframe.loc[
            (
                # Signal: RSI crosses above 70
                    (qtpylib.crossed_above(dataframe["rsi"], 60))
                    & (dataframe["tema"] > dataframe["bb_middleband"])
                    & (  # Guard: tema above BB middle
                            dataframe["tema"] < dataframe["tema"].shift(1)
                    )
                    & (  # Guard: tema is falling
                            dataframe["volume"] > 0
                    )  # Make sure Volume is not 0
            ),
            "enter_short",
        ] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        return super().populate_exit_trend(dataframe, metadata)
