# source: https://raw.githubusercontent.com/Germoso/ft_userdata/ecd7f9079a73e58d0cf4f6cc0cb4877a1e17afeb/user_data/strategies/RandomEntry.py
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
# flake8: noqa: F401
# isort: skip_file
# --- Do not remove these imports ---
import numpy as np
import pandas as pd
from datetime import datetime, timedelta, timezone
from pandas import DataFrame
from typing import Dict, Optional, Union, Tuple

from freqtrade.strategy import (
    IStrategy,
    Trade,
    Order,
    PairLocks,
    informative,  # @informative decorator
    # Hyperopt Parameters
    BooleanParameter,
    CategoricalParameter,
    DecimalParameter,
    IntParameter,
    RealParameter,
    # timeframe helpers
    timeframe_to_minutes,
    timeframe_to_next_date,
    timeframe_to_prev_date,
    # Strategy helper functions
    merge_informative_pair,
    stoploss_from_absolute,
    stoploss_from_open,
    AnnotationType,
)

# --------------------------------
# Add your lib to import here
import talib.abstract as ta
from technical import qtpylib
import logging
logger = logging.getLogger(__name__)

class Github_Germoso_ft_userdata__RandomEntry__20250608_215654(IStrategy):
    # Strategy interface version - allow new iterations of the strategy interface.
    # Check the documentation or the Sample strategy to get the latest version.
    INTERFACE_VERSION = 3

    # Optimal timeframe for the strategy.
    timeframe = "5m"

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

    # Minimal ROI designed for the strategy.
    # This attribute will be overridden if the config file contains "minimal_roi".
    minimal_roi = {
        "0": 0.06,
    }

    # Optimal stoploss designed for the strategy.
    # This attribute will be overridden if the config file contains "stoploss".
    stoploss = -0.03
    
    # Trailing stoploss
    trailing_stop = False
    # trailing_only_offset_is_reached = False
    # trailing_stop_positive = 0.01
    # trailing_stop_positive_offset = 0.0  # Disabled / not configured

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

    # These values can be overridden in the config.
    use_exit_signal = True
    exit_profit_only = False
    ignore_roi_if_entry_signal = False

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

    # Strategy parameters
    # Define parameters for random entry - probability of generating a long signal (0.5 = 50%)
    long_probability = DecimalParameter(0.1, 0.9, default=0.6, space="buy", optimize=True)
    short_probability = DecimalParameter(0.1, 0.9, default=0.4, space="buy", optimize=True)

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Generate a random number between 0 and 1 for each candle
        dataframe['random_value'] = np.random.random(size=len(dataframe))
        dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50)
        dataframe['ema200'] = ta.EMA(dataframe, timeperiod=100)
        dataframe['rsi'] = ta.RSI(dataframe)

        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

        # dataframe['enter_long'] = 0
        # dataframe['enter_short'] = 0
        
        # if((dataframe['ema50'] > dataframe['ema200']).all()):
        #     dataframe.loc[(dataframe['random_value'] < self.long_probability.value), 'enter_long'] = 1
        #     dataframe.loc[(dataframe['random_value'] < self.short_probability.value), 'enter_short'] = 1
        # else:
        #     dataframe.loc[(dataframe['random_value'] < self.short_probability.value), 'enter_long'] = 1
        #     dataframe.loc[(dataframe['random_value'] < self.long_probability.value), 'enter_short'] = 1
        dataframe.loc[(dataframe["ema50"] < dataframe["ema200"]) & (dataframe["rsi"] > 30), "enter_short"] = 1
        return dataframe

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