import pandas as pd
import pandas_ta_classic as ta

def calculate_indicators(df: pd.DataFrame) -> pd.DataFrame:
    # Asegurar nombres de columnas estándar
    df.columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume']
    
    # EMAs
    df['ema9'] = ta.ema(df['close'], length=9)
    df['ema21'] = ta.ema(df['close'], length=21)
    df['ema50'] = ta.ema(df['close'], length=50)
    
    # RSI
    df['rsi'] = ta.rsi(df['close'], length=14)
    
    # MACD
    macd = ta.macd(df['close'], fast=12, slow=26, signal=9)
    df['macd'] = macd['MACD_12_26_9']
    df['macd_signal'] = macd['MACDs_12_26_9']
    df['macd_hist'] = macd['MACDh_12_26_9']
    
    # Bollinger Bands
    bbands = ta.bbands(df['close'], length=20, std=2)
    df['bb_upper'] = bbands['BBU_20_2.0']
    df['bb_lower'] = bbands['BBL_20_2.0']
    
    # ATR (para SL/TP)
    df['atr'] = ta.atr(df['high'], df['low'], df['close'], length=14)
    
    # Volumen SMA
    df['vol_sma20'] = ta.sma(df['volume'], length=20)
    
    return df.dropna().iloc[-1] # Devolver la última fila con datos completos