# indie:lang_version = 5
from indie import indicator, color, plot, param
from indie.algorithms import Ema, Sma, Rsi, Atr

@indicator('Futures Trading Signal Dashboard', overlay_main_pane=False)
@param.int('ema_fast', default=9, min=1, max=50)
@param.int('ema_slow', default=21, min=1, max=100)
@param.int('rsi_period', default=14, min=1, max=50)
@param.int('atr_period', default=14, min=1, max=50)
@param.int('vol_period', default=20, min=1, max=100)
@param.float('vol_mult', default=1.5, min=0.1, max=5.0)
@param.float('atr_threshold', default=1.5, min=0.1, max=10.0)
@param.int('score_threshold', default=3, min=1, max=5)
@plot.line(color=color.YELLOW, title="Trading Score")
@plot.line(color=color.GRAY, title="Score Threshold")
@plot.line(color=color.GREEN, title="Long Signal")
@plot.line(color=color.RED, title="Short Signal")
@plot.line(color=color.BLUE, title="Fast EMA Normalized")
@plot.line(color=color.MAROON, title="Slow EMA Normalized")
@plot.line(color=color.PURPLE, title="RSI")
@plot.line(color=color.TEAL, title="ATR Scaled")
def Main(self, ema_fast, ema_slow, rsi_period, atr_period, vol_period, 
         vol_mult, atr_threshold, score_threshold):
    
    # Calculate technical indicators
    fast_ema = Ema.new(self.close, ema_fast)
    slow_ema = Ema.new(self.close, ema_slow)
    rsi = Rsi.new(self.close, rsi_period)
    atr = Atr.new(atr_period)
    vol_sma = Sma.new(self.volume, vol_period)
    
    # Calculate trading score
    score = 0
    
    # Condition 1: Fast EMA above Slow EMA (bullish trend)
    if fast_ema[0] > slow_ema[0]:
        score += 1
    
    # Condition 2: RSI in momentum zones (above 55 or below 45)
    if rsi[0] > 55 or rsi[0] < 45:
        score += 1
    
    # Condition 3: ATR above threshold (sufficient volatility)
    if atr[0] > atr_threshold:
        score += 1
    
    # Condition 4: Volume above average (high activity)
    if self.volume[0] > vol_sma[0] * vol_mult:
        score += 1
    
    # Condition 5: Price above previous close (simple momentum proxy)
    if self.close[0] > self.close[1]:
        score += 1
    
    # Generate signals
    long_signal = 1 if score >= score_threshold and fast_ema[0] > slow_ema[0] else 0
    short_signal = -1 if score >= score_threshold and fast_ema[0] < slow_ema[0] else 0
    
    # Normalize EMAs to 0-100 scale for display (using percentage of current price)
    fast_ema_norm = (fast_ema[0] / self.close[0] - 1) * 100 + 50
    slow_ema_norm = (slow_ema[0] / self.close[0] - 1) * 100 + 50
    
    # Scale ATR for better visualization (multiply by 10)
    atr_scaled = atr[0] * 10
    
    return (score,                    # Trading Score (0-5)
            score_threshold,          # Score Threshold
            long_signal,              # Long Signal (1 or 0)
            short_signal,             # Short Signal (-1 or 0)
            fast_ema_norm,            # Fast EMA Normalized
            slow_ema_norm,            # Slow EMA Normalized
            rsi[0],                   # RSI (0-100)
            atr_scaled)               # ATR Scaled for visibility
