# source: https://raw.githubusercontent.com/rmallarapu-bc/brahma/9287745fc036c2f4c00c586e598290885c2883b9/archive/Aladdin1.py
import freqtrade.vendor.qtpylib.indicators as qtpylib
import talib.abstract as ta
from freqtrade.strategy import DecimalParameter
from pandas import DataFrame
from SnowMass import SnowMass
from freqtrade.strategy import IStrategy

# class Github_rmallarapu_bc_brahma__Aladdin1__20240229_213751(SnowMass):
class Github_rmallarapu_bc_brahma__Aladdin1__20240229_213751(IStrategy):
    timeframe = "1h"
    minimal_roi = {"0": 0.1}
    stoploss = -0.05

    hold_file = f"hold-trades.json"
    buy_params = { "buy_limit": 0.943 }

    # Trailing stoploss
    trailing_stop = True
    trailing_stop_positive = 0.02
    trailing_stop_positive_offset = 0.1
    trailing_only_offset_is_reached = False

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

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

    buy_limit = DecimalParameter(0.90, 0.98, default=0.98, space='buy', decimals=3, optimize=True, load=True)

    @property
    def protections(self):
        return [
            {
                "method": "CooldownPeriod",
                "stop_duration_candles": 1
            },
            {
                "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)

        # 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"]

        # 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[
            (
                    (dataframe['close'] > self.buy_limit.value * dataframe['bb_middleband'])
                    & (qtpylib.crossed_below(dataframe["rsi"], 20))
                    & (dataframe["tema"] > dataframe["bb_middleband"])
                    & (dataframe["tema"] < dataframe["tema"].shift(1))
                    & (dataframe["volume"] > 0)
            ),
            "enter_short",
        ] = 1

        dataframe.loc[
            (
                    (dataframe['close'] > self.buy_limit.value * dataframe['bb_middleband'])
                    & (qtpylib.crossed_above(dataframe["rsi"], 70))
                    & (dataframe["tema"] > dataframe["bb_middleband"])
                    & (dataframe["tema"] < dataframe["tema"].shift(1))
                    & (dataframe["volume"] > 0)
            ),
            "enter_long",
        ] = 1

        return dataframe

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


