from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame
import pandas as pd
import freqtrade.vendor.qtpylib.indicators as qtpylib
import talib.abstract as ta

class ScalpingPro(IStrategy):
    INTERFACE_VERSION = 3
    timeframe = '1m'
    stoploss = -0.99
    trailing_stop = False
    max_open_trades = 1
    use_sell_signal = True
    sell_profit_only = False
    ignore_roi_if_buy_signal = True

    sell_timeframe = '1m'
    sell_max_hold = {'1m': 1}

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

    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe['red_candle'] = dataframe['close'] < dataframe['open']
        dataframe['green_candle'] = dataframe['close'] > dataframe['open']
        dataframe['buy'] = (
            dataframe['red_candle'].shift(1) & 
            dataframe['green_candle']
        ).astype('int')
        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Se cierra automáticamente la operación después de un minuto
        dataframe['sell'] = 0  # No hace falta establecer una lógica compleja de venta aquí
        return dataframe

    def custom_sell(self, pair: str, trade, current_time, current_rate, current_profit, **kwargs):
        # Cerrar la operación si lleva abierta 1 minuto o más
        if (current_time - trade.open_date_utc).total_seconds() >= 60:
            return 'sell_signal'  # Indicador para cerrar la operación
        return None
