# source: https://raw.githubusercontent.com/DerSalvador/freqtrade-helm-chart/a669dc11b640b0eb63aa8f8b51e9f181fd7ee43c/chart/deployed_strategies/binance-michael-k8s-namespace/e6v34.py
# --- Do not remove these libs ---
import freqtrade.vendor.qtpylib.indicators as qtpylib
import talib.abstract as ta
from freqtrade.strategy.interface import IStrategy
from freqtrade.strategy import RealParameter, IntParameter
from functools import reduce
from pandas import DataFrame
shma = 15  #short hull moving average
lhma = 35  #long hull moving average
# shma_c = 10 #short hull moving average de cierre
# lhma_c = 25 #long hull moving average de cierre
pwill = 12  #period williams r
pvol = 100  #period pvt
# pmv = 50 #period ema williams r

class Github_DerSalvador_freqtrade_helm_chart__e6v34__20260115_122204(IStrategy):
    INTERFACE_VERSION = 3
    bwill = RealParameter(-25, -15, default=-20, space='entry')
    swill = RealParameter(-55, -35, default=-50, space='exit')
    # bwill = -20 #entry williams r
    # swill = -50 #exit williams r
    # ROI table:
    minimal_roi = {'0': 0.555, '300': 0.35, '500': 0.247, '1200': 0.0936, '2100': 0.04, '4000': 0}
    # Stoploss:
    stoploss = -0.54
    # Trailing stop:
    trailing_stop = True
    trailing_stop_positive = 0.18
    trailing_stop_positive_offset = 0.2
    trailing_only_offset_is_reached = True
    # Optimal timeframe use it in your config
    timeframe = '15m'

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # SMA - ex Moving Average
        dataframe[f'hma{shma}'] = qtpylib.hma(dataframe['close'], window=shma)
        dataframe[f'hma{lhma}'] = qtpylib.hma(dataframe['close'], window=lhma)
        # dataframe[f'hma{shma_c}'] = qtpylib.hma(dataframe['close'], window=shma_c)
        # dataframe[f'hma{lhma_c}'] = qtpylib.hma(dataframe['close'], window=lhma_c)
        dataframe['willr'] = ta.WILLR(dataframe['high'], dataframe['low'], dataframe['close'], timeperiod=pwill)
        # dataframe['will_mean'] = ta.EMA(dataframe, timeperiod=pmv, price='willr')
        dataframe['vol_mean'] = ta.EMA(dataframe, timeperiod=pvol, price='volume')
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = []
        conditions.append(dataframe[f'hma{shma}'].shift(1) - dataframe[f'hma{lhma}'].shift(1) < dataframe[f'hma{shma}'] - dataframe[f'hma{lhma}'])
        conditions.append(dataframe[f'hma{shma}'].shift(2) - dataframe[f'hma{lhma}'].shift(2) < dataframe[f'hma{shma}'].shift(1) - dataframe[f'hma{lhma}'].shift(1))
        conditions.append(dataframe['willr'] > self.bwill.value)
        conditions.append(dataframe['willr'].shift(1) < self.bwill.value)
        # conditions.append(dataframe['will_mean'] > -50)
        conditions.append(dataframe['volume'] > dataframe['vol_mean'])
        if conditions:
            dataframe.loc[reduce(lambda x, y: x & y, conditions), 'entry'] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # ( (dataframe[f'hma{lhma_c}'] > dataframe[f'hma{shma_c}']) &
        # (dataframe[f'hma{lhma_c}'].shift(1) < dataframe[f'hma{shma_c}'].shift(1)) ) |
        dataframe.loc[(dataframe['willr'] < self.swill.value) & (dataframe['willr'].shift(1) > self.swill.value), 'exit'] = 1
        return dataframe