# source: https://raw.githubusercontent.com/DerSalvador/freqtrade-helm-chart/a669dc11b640b0eb63aa8f8b51e9f181fd7ee43c/chart/deployed_strategies/binance-michael-k8s-namespace/VWAP.py
# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame
import freqtrade.vendor.qtpylib.indicators as qtpylib
import pandas_ta as pta
import talib.abstract as ta
# --------------------------------
# Github_DerSalvador_freqtrade_helm_chart__VWAP__20260115_122204 bands

def Github_DerSalvador_freqtrade_helm_chart__VWAP__20260115_122204B(dataframe, window_size=20, num_of_std=1):
    df = dataframe.copy()
    df['vwap'] = qtpylib.rolling_vwap(df, window=window_size)
    rolling_std = df['vwap'].rolling(window=window_size).std()
    df['vwap_low'] = df['vwap'] - rolling_std * num_of_std
    df['vwap_high'] = df['vwap'] + rolling_std * num_of_std
    return (df['vwap_low'], df['vwap'], df['vwap_high'])

def top_percent_change(dataframe: DataFrame, length: int) -> float:
    """
        Percentage change of the current close from the range maximum Open price

        :param dataframe: DataFrame The original OHLC dataframe
        :param length: int The length to look back
        """
    if length == 0:
        return (dataframe['open'] - dataframe['close']) / dataframe['close']
    else:
        return (dataframe['open'].rolling(length).max() - dataframe['close']) / dataframe['close']

class Github_DerSalvador_freqtrade_helm_chart__VWAP__20260115_122204(IStrategy):
    INTERFACE_VERSION = 3
    '\n\n    author: @jilv220\n\n    '
    # 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 = {'0': 0.02}
    # Optimal stoploss designed for the strategy
    stoploss = -0.15
    # Optimal timeframe for the strategy
    timeframe = '5m'

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        vwap_low, vwap, vwap_high = Github_DerSalvador_freqtrade_helm_chart__VWAP__20260115_122204B(dataframe, 20, 1)
        dataframe['vwap_low'] = vwap_low
        dataframe['tcp_percent_4'] = top_percent_change(dataframe, 4)
        dataframe['cti'] = pta.cti(dataframe['close'], length=20)
        # RSI
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['rsi_84'] = ta.RSI(dataframe, timeperiod=84)
        dataframe['rsi_112'] = ta.RSI(dataframe, timeperiod=112)
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[(dataframe['close'] < dataframe['vwap_low']) & (dataframe['tcp_percent_4'] > 0.04) & (dataframe['cti'] < -0.8) & (dataframe['rsi'] < 35) & (dataframe['rsi_84'] < 60) & (dataframe['rsi_112'] < 60) & (dataframe['volume'] > 0), 'entry'] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[(), 'exit'] = 1
        return dataframe