Migliora robustezza Dockerfile e controlli TradingBotService

- Installa wget e aggiorna healthcheck in Dockerfile (usa wget invece di curl, UID 1001 per utente non-root)
- Aggiunti controlli di nullità e validità su simboli, prezzi e segnali in TradingBotService
- Migliorata gestione delle eccezioni con stampa dello stack trace
- Filtrati dati non validi prima del calcolo degli indicatori
- Aumentata la sicurezza e la resilienza contro dati corrotti o incompleti
This commit is contained in:
2025-12-15 10:37:31 +01:00
parent e414123cd0
commit c93ccd5e4a
2 changed files with 44 additions and 12 deletions

View File

@@ -162,17 +162,23 @@ public class TradingBotService
try
{
var enabledSymbols = _assetConfigs.Values
.Where(c => c.IsEnabled)
.Where(c => c != null && c.IsEnabled)
.Select(c => c.Symbol)
.Where(s => !string.IsNullOrWhiteSpace(s))
.ToList();
if (enabledSymbols.Count == 0) return;
var prices = await _marketDataService.GetMarketPricesAsync(enabledSymbols);
if (prices == null) return;
foreach (var price in prices)
{
await ProcessAssetUpdate(price);
if (price != null)
{
await ProcessAssetUpdate(price);
}
}
UpdateGlobalStatistics();
@@ -180,11 +186,16 @@ public class TradingBotService
catch (Exception ex)
{
Console.WriteLine($"Error in UpdateAsync: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
}
}
private async Task ProcessAssetUpdate(MarketPrice price)
{
// Add null check for price
if (price == null || price.Price <= 0)
return;
if (!_assetConfigs.TryGetValue(price.Symbol, out var config) || !config.IsEnabled)
return;
@@ -216,10 +227,15 @@ public class TradingBotService
// Generate trading signal
var signal = await _strategy.AnalyzeAsync(price.Symbol, _priceHistory[price.Symbol]);
OnSignalGenerated?.Invoke(signal);
// Add null check for signal
if (signal != null)
{
OnSignalGenerated?.Invoke(signal);
// Execute trades based on strategy and configuration
await EvaluateAndExecuteTrade(price.Symbol, signal, price, config);
// Execute trades based on strategy and configuration
await EvaluateAndExecuteTrade(price.Symbol, signal, price, config);
}
}
}
@@ -339,10 +355,18 @@ public class TradingBotService
private void UpdateIndicators(string symbol)
{
var history = _priceHistory[symbol];
if (history.Count < 26) return;
if (!_priceHistory.TryGetValue(symbol, out var history) || history == null || history.Count < 26)
return;
var prices = history.Select(p => p.Price).ToList();
// Filter out null prices and extract valid price values
var prices = history
.Where(p => p != null && p.Price > 0)
.Select(p => p.Price)
.ToList();
// Ensure we still have enough data after filtering
if (prices.Count < 26)
return;
var rsi = TechnicalAnalysis.CalculateRSI(prices);
var (macd, signal, histogram) = TechnicalAnalysis.CalculateMACD(prices);
@@ -482,6 +506,9 @@ public class TradingBotService
public MarketPrice? GetLatestPrice(string symbol)
{
if (string.IsNullOrWhiteSpace(symbol))
return null;
var history = GetPriceHistory(symbol);
return history?.LastOrDefault();
}