# source: https://raw.githubusercontent.com/djienne/YOUTUBE_STRATEGIES_FREQTRADE/2d447c3727a8cb7d8c9bf8a6d02de2f285266a3f/tutorial/chatgpt/user_data/strategies/chatgpt.py
# directory_url: https://github.com/djienne/YOUTUBE_STRATEGIES_FREQTRADE/blob/master/tutorial/chatgpt/user_data/strategies/
# User: djienne
# Repository: YOUTUBE_STRATEGIES_FREQTRADE
# --------------------import pandas as pd
import numpy as np
from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame
import pandas_ta as ta
from freqtrade.strategy import CategoricalParameter, IntParameter, DecimalParameter

class Github_djienne_YOUTUBE_STRATEGIES_FREQTRADE__chatgpt__20260927_123622(IStrategy):
    """
    Custom Freqtrade strategy named 'Github_djienne_YOUTUBE_STRATEGIES_FREQTRADE__chatgpt__20260927_123622' generated by ChatGPT
    Uses STC, CCI, and Williams %R indicators for 1-hour timeframe.
    """

    # Timeframe and stop-loss / ROI settings
    timeframe = '1h'
    stoploss = -0.75
    minimal_roi = {
        "0": 500.0  # Very large ROI
    }

    # Define order types as market orders for all actions
    order_types = {
        "entry": "market",
        "exit": "market",
        "stoploss": "market",
        "stoploss_on_exchange": True,
    }

    # Hyperoptable parameters
    stc_period = IntParameter(10, 90, default=23, space="buy", optimize=True)
    cci_period = IntParameter(10, 90, default=20, space="buy", optimize=True)
    wr_period = IntParameter(10, 90, default=14, space="buy", optimize=True)

    will_buy_level = DecimalParameter(-50.0, -10.0, default=-20, decimals=0, space='buy', optimize=True)
    will_sell_level = DecimalParameter(-130.0, -51.0, default=-80, decimals=0, space='buy', optimize=True)

    stc_thres = DecimalParameter(0.1, 0.9, default=0.5, decimals=1, space='buy', optimize=True)
    cci_thres = IntParameter(-70, 50, default=0, space="buy", optimize=True)

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Populate the dataframe with indicators.
        - Schaff Trend Cycle (STC)
        - Commodity Channel Index (CCI)
        - Williams %R
        """
        # STC (Schaff Trend Cycle)
        sstc = ta.stc(dataframe['close'], length=self.stc_period.value, fast=23, slow=50)
        #print(sstc)
        dataframe['stc'] = sstc[list(sstc.items())[0][0]]

        # CCI (Commodity Channel Index)
        dataframe['cci'] = ta.cci(dataframe['high'], dataframe['low'], dataframe['close'], length=self.cci_period.value)

        # Williams %R
        dataframe['williamsr'] = ta.willr(dataframe['high'], dataframe['low'], dataframe['close'], length=self.wr_period.value)

        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Define entry conditions using STC, CCI, and Williams %R.
        Enter a trade if:
        - STC is above 0.5 (uptrend)
        - CCI is positive (above 0)
        - Williams %R is below -20 (not overbought)
        """
        dataframe.loc[
            (dataframe['stc'] > self.stc_thres.value) &
            (dataframe['cci'] > self.cci_thres.value) &
            (dataframe['williamsr'] < self.will_buy_level.value),
            'enter_long'] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Exit conditions:
        Exit if:
        - STC is below 0.5 (downtrend)
        - CCI is negative (below 0)
        - Williams %R is above -80 (not oversold)
        """
        dataframe.loc[
            (dataframe['stc'] < self.stc_thres.value) &
            (dataframe['cci'] < self.cci_thres.value) &
            (dataframe['williamsr'] > self.will_sell_level.value),
            'exit_long'] = 1

        return dataframe
