using TradingBot.Models; namespace TradingBot.Services; public class SimpleMovingAverageStrategy : ITradingStrategy { private readonly int _shortPeriod = 5; private readonly int _longPeriod = 10; public string Name => "Simple Moving Average (SMA)"; public Task AnalyzeAsync(string symbol, List historicalPrices) { if (historicalPrices.Count < _longPeriod) { return Task.FromResult(new TradingSignal { Symbol = symbol, Type = SignalType.Hold, Price = historicalPrices.LastOrDefault()?.Price ?? 0, Reason = "Dati insufficienti per l'analisi", Timestamp = DateTime.UtcNow }); } var recentPrices = historicalPrices.OrderByDescending(p => p.Timestamp).Take(_longPeriod).ToList(); var shortSMA = recentPrices.Take(_shortPeriod).Average(p => p.Price); var longSMA = recentPrices.Average(p => p.Price); var currentPrice = recentPrices.First().Price; // Strategia: Compra quando la SMA breve incrocia sopra la SMA lunga // Vendi quando la SMA breve incrocia sotto la SMA lunga if (shortSMA > longSMA * 1.02m) // 2% sopra { return Task.FromResult(new TradingSignal { Symbol = symbol, Type = SignalType.Buy, Price = currentPrice, Reason = $"SMA breve ({shortSMA:F2}) > SMA lunga ({longSMA:F2}) - Trend rialzista", Timestamp = DateTime.UtcNow }); } else if (shortSMA < longSMA * 0.98m) // 2% sotto { return Task.FromResult(new TradingSignal { Symbol = symbol, Type = SignalType.Sell, Price = currentPrice, Reason = $"SMA breve ({shortSMA:F2}) < SMA lunga ({longSMA:F2}) - Trend ribassista", Timestamp = DateTime.UtcNow }); } else { return Task.FromResult(new TradingSignal { Symbol = symbol, Type = SignalType.Hold, Price = currentPrice, Reason = $"SMA breve ({shortSMA:F2}) ? SMA lunga ({longSMA:F2}) - Nessun segnale chiaro", Timestamp = DateTime.UtcNow }); } } }