# source: https://raw.githubusercontent.com/remiotore/ccxt-freqtrade/44beaeb6a420cd8e9f2e4ea93e11d6cfa192ee03/strategies/new_strat_1616.py

from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame


import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
from scipy.spatial.distance import cosine
import numpy as np

class Github_remiotore_ccxt_freqtrade__new_strat_1616__20260111_210550(IStrategy):

    minimal_roi = {
        "0": 0.6
    }

    stoploss = -0.9

    timeframe = '4h'

    trailing_stop = True
    trailing_only_offset_is_reached = True
    trailing_stop_positive = 0.03
    trailing_stop_positive_offset = 0.28

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=7)

        dataframe['marketMA'] = ta.SMA(dataframe, timeperiod=200)
        dataframe['fastMA'] = ta.SMA(dataframe, timeperiod=21)
        dataframe['slowMA'] = ta.SMA(dataframe, timeperiod=50)
        dataframe['entryMA'] = ta.SMA(dataframe, timeperiod=3)


        dataframe['sy1'] = dataframe['slowMA'].shift(+1)
        dataframe['sy2'] = dataframe['slowMA'].shift(+11)
        sx1 = 1
        sx2 = 11
        dataframe['sy'] = dataframe['sy1'] - dataframe['sy2']
        dataframe['sx'] = sx2 - sx1
        dataframe['slow_slope'] = dataframe['sy']/dataframe['sx']
        dataframe['fy1'] = dataframe['fastMA'].shift(+1)
        dataframe['fy2'] = dataframe['fastMA'].shift(+11)
        fx1 = 1
        fx2 = 11
        dataframe['fy'] = dataframe['fy1'] - dataframe['fy2']
        dataframe['fx'] = fx2 - fx1
        dataframe['fast_slope'] = dataframe['fy']/dataframe['fx']


        dataframe['last_lowest'] = dataframe['low'].rolling(10).min().shift(1)

        return dataframe

    plot_config = {
        "main_plot": {

            "fastMA": {"color": "red"},
            "slowMA": {"color": "blue"},
        },
        "subplots": {

            "rsi": {"rsi": {"color": "blue"}},
            "fast_slope": {"fast_slope": {"color": "red"}, "slow_slope": {"color": "blue"}},
        },
    }


    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (

                (dataframe['fast_slope'] > 0) &

                (dataframe['slow_slope'] > 0) &


                (dataframe['close'] > dataframe['entryMA'].shift(+11)) &

                (dataframe['rsi'] > 55) &

                (dataframe['fastMA'] > dataframe['slowMA'])

            ),
            'buy'] = 1

        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                (dataframe['fastMA'] < dataframe['slowMA'])
                & (dataframe['close'] < dataframe['last_lowest'])

            ),
            'sell'] = 1
        return dataframe

