refactor: save current workspace state including FinlyticAnalyzer fixes, FinlyticApp trade route alignment, and DTO audit documentation
This commit is contained in:
@@ -17,7 +17,9 @@ namespace FinlyticTechnicalAnalysis.Services;
|
||||
|
||||
public interface ITechnicalAnalysisDbService
|
||||
{
|
||||
Task<TechnicalAnalysisDto?> GetAnalysisAsync(string isin, bool forceRefresh = false, CancellationToken cancellationToken = default);
|
||||
Task<TechnicalAnalysisDto?> GetAnalysisAsync(string isin, bool forceRefresh = false, string? ticker = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
Task<LivePriceDto?> GetLivePriceAsync(string isin, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -29,10 +31,7 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
private readonly ITechnicalAnalysisCalculator _calculator;
|
||||
private readonly ILogger<TechnicalAnalysisDbService> _logger;
|
||||
|
||||
// Cache Layer 1: In-Memory Candles Cache (TTL: 15 Minuten)
|
||||
private static readonly ConcurrentDictionary<string, (List<MarketCandleEntity> Candles, string Symbol, string Currency, DateTime FetchedAt)> _candleCache = new();
|
||||
|
||||
// Per-ISIN Semaphores zur Vermeidung von Cache-Stampedes
|
||||
private static readonly ConcurrentDictionary<string, SemaphoreSlim> _perIsinLocks = new();
|
||||
private static readonly TimeSpan CandleCacheTtl = TimeSpan.FromMinutes(15);
|
||||
private static readonly TimeSpan DbCacheTtl = TimeSpan.FromHours(1);
|
||||
@@ -51,26 +50,30 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<TechnicalAnalysisDto?> GetAnalysisAsync(string isin, bool forceRefresh = false, CancellationToken cancellationToken = default)
|
||||
public async Task<TechnicalAnalysisDto?> GetAnalysisAsync(string isin, bool forceRefresh = false, string? ticker = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(isin)) return null;
|
||||
var cleanIsin = isin.Trim().ToUpperInvariant();
|
||||
|
||||
// 1. Layer-1: Fast-Path aus In-Memory Cache (wenn kein forceRefresh)
|
||||
if (!forceRefresh && _candleCache.TryGetValue(cleanIsin, out var ramEntry) && DateTime.UtcNow - ramEntry.FetchedAt < CandleCacheTtl)
|
||||
if (!forceRefresh && _candleCache.TryGetValue(cleanIsin, out var ramEntry) &&
|
||||
DateTime.UtcNow - ramEntry.FetchedAt < CandleCacheTtl &&
|
||||
(string.IsNullOrWhiteSpace(ticker) || string.Equals(ramEntry.Symbol, ticker, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
_logger.LogDebug("[{Channel}] RAM-Cache Hit for ISIN {Isin}. Merging live price...", "TechnicalAnalysisChannel", cleanIsin);
|
||||
return await BuildAnalysisWithLivePriceAsync(cleanIsin, ramEntry.Candles, ramEntry.Symbol, ramEntry.Currency, cancellationToken);
|
||||
}
|
||||
|
||||
// Semaphor für ISIN holen (verhindert doppelte parallele Abfragen der gleichen ISIN)
|
||||
var semaphore = _perIsinLocks.GetOrAdd(cleanIsin, _ => new SemaphoreSlim(1, 1));
|
||||
await semaphore.WaitAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
// Re-Check nach Lock-Erhalt
|
||||
if (!forceRefresh && _candleCache.TryGetValue(cleanIsin, out ramEntry) && DateTime.UtcNow - ramEntry.FetchedAt < CandleCacheTtl)
|
||||
if (!forceRefresh && _candleCache.TryGetValue(cleanIsin, out ramEntry) &&
|
||||
DateTime.UtcNow - ramEntry.FetchedAt < CandleCacheTtl &&
|
||||
(string.IsNullOrWhiteSpace(ticker) || string.Equals(ramEntry.Symbol, ticker, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return await BuildAnalysisWithLivePriceAsync(cleanIsin, ramEntry.Candles, ramEntry.Symbol, ramEntry.Currency, cancellationToken);
|
||||
}
|
||||
@@ -78,7 +81,7 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
// 2. Layer-2: Prüfen ob frische Daten in der Datenbank liegen
|
||||
if (!forceRefresh)
|
||||
{
|
||||
var dbDto = await GetFromDbCacheAsync(cleanIsin, cancellationToken);
|
||||
var dbDto = await GetFromDbCacheAsync(cleanIsin, ticker, cancellationToken);
|
||||
if (dbDto != null)
|
||||
{
|
||||
_logger.LogDebug("[{Channel}] DB-Cache Hit for ISIN {Isin}.", "TechnicalAnalysisChannel", cleanIsin);
|
||||
@@ -86,13 +89,11 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Cache Miss / ForceRefresh: Vollständige Neuberechnung
|
||||
return await FullRefreshAsync(cleanIsin, cancellationToken);
|
||||
return await FullRefreshAsync(cleanIsin, ticker, cancellationToken);
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphore.Release();
|
||||
// Speicher aufräumen, falls Lock nicht mehr genutzt wird
|
||||
if (semaphore.CurrentCount == 1)
|
||||
{
|
||||
_perIsinLocks.TryRemove(cleanIsin, out _);
|
||||
@@ -105,38 +106,41 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
if (string.IsNullOrWhiteSpace(isin)) return null;
|
||||
var cleanIsin = isin.Trim().ToUpperInvariant();
|
||||
|
||||
var (livePrice, liveBid, liveAsk) = await FetchLivePriceAsync(cleanIsin, cancellationToken);
|
||||
var (livePrice, liveBid, liveAsk, preChange) = await FetchLivePriceAsync(cleanIsin, cancellationToken);
|
||||
if (!livePrice.HasValue) return null;
|
||||
|
||||
return new LivePriceDto(
|
||||
cleanIsin,
|
||||
Math.Round(livePrice.Value, 2),
|
||||
0m, // Percent change optional
|
||||
preChange ?? 0m,
|
||||
liveBid.HasValue ? Math.Round(liveBid.Value, 2) : null,
|
||||
liveAsk.HasValue ? Math.Round(liveAsk.Value, 2) : null
|
||||
);
|
||||
}
|
||||
|
||||
private async Task<TechnicalAnalysisDto?> FullRefreshAsync(string cleanIsin, CancellationToken cancellationToken)
|
||||
private async Task<TechnicalAnalysisDto?> FullRefreshAsync(string cleanIsin, string? requestedTicker, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Full refresh for ISIN {Isin}", "TechnicalAnalysisChannel", cleanIsin);
|
||||
_logger.LogInformation("[{Channel}] Full refresh for ISIN {Isin} (RequestedTicker: {Ticker})", "TechnicalAnalysisChannel", cleanIsin, requestedTicker ?? "None");
|
||||
|
||||
var tickerTask = _yahooScraper.ResolveTickerFromIsinAsync(cleanIsin, cancellationToken);
|
||||
var macroTask = FetchMacroDataAsync(cancellationToken);
|
||||
|
||||
string? ticker = requestedTicker;
|
||||
if (string.IsNullOrWhiteSpace(ticker))
|
||||
{
|
||||
ticker = await _yahooScraper.ResolveTickerFromIsinAsync(cleanIsin, cancellationToken);
|
||||
}
|
||||
|
||||
await Task.WhenAll(tickerTask, macroTask);
|
||||
|
||||
var ticker = await tickerTask;
|
||||
var querySymbol = !string.IsNullOrEmpty(ticker) ? ticker : cleanIsin;
|
||||
var (vix, gspc, dxy) = await macroTask;
|
||||
|
||||
var yahooResult = await _yahooScraper.FetchHistoricalCandlesWithCurrencyAsync(querySymbol, "1y", "1d", cancellationToken);
|
||||
// Lade 2y Daten für saubere Indikator-Aufwärmphasen
|
||||
var yahooResult = await _yahooScraper.FetchHistoricalCandlesWithCurrencyAsync(querySymbol, "2y", "1d", cancellationToken);
|
||||
var candles = yahooResult.Candles;
|
||||
var currency = yahooResult.Currency;
|
||||
|
||||
if (candles.Count == 0 && querySymbol != cleanIsin)
|
||||
{
|
||||
yahooResult = await _yahooScraper.FetchHistoricalCandlesWithCurrencyAsync(cleanIsin, "1y", "1d", cancellationToken);
|
||||
yahooResult = await _yahooScraper.FetchHistoricalCandlesWithCurrencyAsync(cleanIsin, "2y", "1d", cancellationToken);
|
||||
candles = yahooResult.Candles;
|
||||
currency = yahooResult.Currency;
|
||||
}
|
||||
@@ -147,67 +151,60 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
return null;
|
||||
}
|
||||
|
||||
// In RAM-Cache sichern
|
||||
_candleCache[cleanIsin] = (candles.Select(CloneCandle).ToList(), querySymbol, currency, DateTime.UtcNow);
|
||||
|
||||
// Live-Preis einpflegen
|
||||
await MergeLivePriceAsync(cleanIsin, candles, querySymbol, cancellationToken);
|
||||
await MergeLivePriceAsync(cleanIsin, candles, querySymbol, currency, cancellationToken);
|
||||
|
||||
var resultDto = BuildDto(cleanIsin, querySymbol, currency, candles, vix, gspc, dxy);
|
||||
|
||||
// Synchron und sicher in DB persistieren
|
||||
await PersistToDbCacheAsync(cleanIsin, querySymbol, resultDto, cancellationToken);
|
||||
|
||||
return resultDto;
|
||||
}
|
||||
|
||||
private async Task<TechnicalAnalysisDto> BuildAnalysisWithLivePriceAsync(
|
||||
string cleanIsin, List<MarketCandleEntity> cachedCandles, string querySymbol, string currency, CancellationToken cancellationToken)
|
||||
string cleanIsin, List<MarketCandleEntity> cachedCandles, string querySymbol, string currency,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var candles = cachedCandles.Select(CloneCandle).ToList();
|
||||
|
||||
var livePriceTask = FetchLivePriceAsync(cleanIsin, cancellationToken);
|
||||
var macroTask = FetchMacroDataAsync(cancellationToken);
|
||||
|
||||
await Task.WhenAll(livePriceTask, macroTask);
|
||||
|
||||
var (livePrice, liveBid, liveAsk) = await livePriceTask;
|
||||
var (livePrice, liveBid, liveAsk, preChange) = await livePriceTask; // Task-Result direkt nutzen
|
||||
var (vix, gspc, dxy) = await macroTask;
|
||||
|
||||
if (livePrice.HasValue && livePrice.Value > 0m)
|
||||
{
|
||||
var today = DateTime.UtcNow.Date;
|
||||
var lastCandle = candles.LastOrDefault(c => c.Timestamp.Date == today);
|
||||
if (lastCandle != null)
|
||||
{
|
||||
lastCandle.Close = livePrice.Value;
|
||||
lastCandle.High = Math.Max(lastCandle.High, livePrice.Value);
|
||||
lastCandle.Low = Math.Min(lastCandle.Low, livePrice.Value);
|
||||
if (liveBid.HasValue) lastCandle.Bid = liveBid.Value;
|
||||
if (liveAsk.HasValue) lastCandle.Ask = liveAsk.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
var prevClose = candles.LastOrDefault()?.Close ?? livePrice.Value;
|
||||
candles.Add(new MarketCandleEntity
|
||||
{
|
||||
Symbol = querySymbol, Interval = "1d", Timestamp = today,
|
||||
Open = prevClose, High = Math.Max(prevClose, livePrice.Value),
|
||||
Low = Math.Min(prevClose, livePrice.Value), Close = livePrice.Value,
|
||||
Volume = 1000, Bid = liveBid, Ask = liveAsk
|
||||
});
|
||||
}
|
||||
}
|
||||
ApplyLivePriceToCandles(cleanIsin, candles, querySymbol, currency, livePrice, liveBid, liveAsk);
|
||||
|
||||
return BuildDto(cleanIsin, querySymbol, currency, candles, vix, gspc, dxy);
|
||||
}
|
||||
|
||||
private async Task MergeLivePriceAsync(string cleanIsin, List<MarketCandleEntity> candles, string querySymbol, CancellationToken cancellationToken)
|
||||
private async Task MergeLivePriceAsync(string cleanIsin, List<MarketCandleEntity> candles, string querySymbol, string currency,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var (livePrice, liveBid, liveAsk, _) = await FetchLivePriceAsync(cleanIsin, cancellationToken);
|
||||
ApplyLivePriceToCandles(cleanIsin, candles, querySymbol, currency, livePrice, liveBid, liveAsk);
|
||||
}
|
||||
|
||||
private void ApplyLivePriceToCandles(
|
||||
string cleanIsin, List<MarketCandleEntity> candles, string querySymbol, string candleCurrency,
|
||||
decimal? livePrice, decimal? liveBid, decimal? liveAsk)
|
||||
{
|
||||
var (livePrice, liveBid, liveAsk) = await FetchLivePriceAsync(cleanIsin, cancellationToken);
|
||||
if (!livePrice.HasValue || livePrice.Value <= 0m) return;
|
||||
|
||||
// Währungsschutz: Trade Republic liefert IMMER EUR.
|
||||
// Wenn die Kerzenhistorie USD ist (z.B. AAPL), darf der EUR-Livepreis NICHT direkt injiziert werden!
|
||||
if (candleCurrency.Equals("USD", StringComparison.OrdinalIgnoreCase) && !cleanIsin.StartsWith("DE") && !cleanIsin.StartsWith("AT"))
|
||||
{
|
||||
_logger.LogDebug("[{Channel}] Skipping direct EUR live price injection for USD asset {Isin}", "TechnicalAnalysisChannel", cleanIsin);
|
||||
return;
|
||||
}
|
||||
|
||||
var today = DateTime.UtcNow.Date;
|
||||
var lastCandle = candles.LastOrDefault(c => c.Timestamp.Date == today);
|
||||
var lastCandle = candles.LastOrDefault(c => c.Timestamp.Date == today) ?? candles.LastOrDefault();
|
||||
|
||||
if (lastCandle != null)
|
||||
{
|
||||
lastCandle.Close = livePrice.Value;
|
||||
@@ -216,39 +213,42 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
if (liveBid.HasValue) lastCandle.Bid = liveBid.Value;
|
||||
if (liveAsk.HasValue) lastCandle.Ask = liveAsk.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
var prevClose = candles.LastOrDefault()?.Close ?? livePrice.Value;
|
||||
candles.Add(new MarketCandleEntity
|
||||
{
|
||||
Symbol = querySymbol, Interval = "1d", Timestamp = today,
|
||||
Open = prevClose, High = Math.Max(prevClose, livePrice.Value),
|
||||
Low = Math.Min(prevClose, livePrice.Value), Close = livePrice.Value,
|
||||
Volume = 1000, Bid = liveBid, Ask = liveAsk
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<(decimal? livePrice, decimal? liveBid, decimal? liveAsk)> FetchLivePriceAsync(string cleanIsin, CancellationToken cancellationToken)
|
||||
private async Task<(decimal? livePrice, decimal? liveBid, decimal? liveAsk, decimal? preChange)> FetchLivePriceAsync(
|
||||
string cleanIsin, CancellationToken cancellationToken)
|
||||
{
|
||||
decimal? livePrice = null;
|
||||
decimal? liveBid = null;
|
||||
decimal? liveAsk = null;
|
||||
|
||||
decimal? preChange = null;
|
||||
|
||||
try
|
||||
{
|
||||
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
cts.CancelAfter(1500); // Maximal 1.5 Sekunden Wartezeit auf Ticker
|
||||
cts.CancelAfter(1500);
|
||||
|
||||
var trTask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
|
||||
int? subId = await _trService.SubscribeRealtimeTickerAsync(cleanIsin, tick =>
|
||||
{
|
||||
if (tick.Last != null && tick.Last.PriceValue > 0m)
|
||||
decimal? effectivePrice = tick.Bid?.PriceValue > 0m
|
||||
? tick.Bid.PriceValue
|
||||
: (tick.Last?.PriceValue > 0m ? tick.Last.PriceValue : null);
|
||||
|
||||
if (effectivePrice.HasValue)
|
||||
{
|
||||
livePrice = tick.Last.PriceValue;
|
||||
livePrice = tick.Last?.PriceValue ?? effectivePrice.Value;
|
||||
liveBid = tick.Bid?.PriceValue;
|
||||
liveAsk = tick.Ask?.PriceValue;
|
||||
|
||||
decimal prePrice = tick.Pre?.PriceValue ?? 0m;
|
||||
|
||||
if (prePrice > 0m)
|
||||
{
|
||||
preChange = Math.Round(((effectivePrice.Value - prePrice) / prePrice) * 100m, 2);
|
||||
}
|
||||
|
||||
trTask.TrySetResult(true);
|
||||
}
|
||||
}, cts.Token);
|
||||
@@ -260,7 +260,7 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
await trTask.Task.WaitAsync(cts.Token);
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
|
||||
|
||||
await _trService.UnsubscribeRealtimeTickerAsync(subId.Value);
|
||||
}
|
||||
}
|
||||
@@ -269,10 +269,11 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
_logger.LogWarning(ex, "[{Channel}] Real-time price fetch skipped for ISIN {Isin}", "TechnicalAnalysisChannel", cleanIsin);
|
||||
}
|
||||
|
||||
return (livePrice, liveBid, liveAsk);
|
||||
return (livePrice, liveBid, liveAsk, preChange);
|
||||
}
|
||||
|
||||
private async Task<(MacroDataEntity vix, MacroDataEntity gspc, MacroDataEntity dxy)> FetchMacroDataAsync(CancellationToken cancellationToken)
|
||||
private async Task<(MacroDataEntity vix, MacroDataEntity gspc, MacroDataEntity dxy)> FetchMacroDataAsync(
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var vixTask = _yahooScraper.FetchMacroTickerAsync("^VIX", cancellationToken);
|
||||
var gspcTask = _yahooScraper.FetchMacroTickerAsync("^GSPC", cancellationToken);
|
||||
@@ -287,7 +288,8 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
return (vix, gspc, dxy);
|
||||
}
|
||||
|
||||
private TechnicalAnalysisDto BuildDto(string cleanIsin, string querySymbol, string currency, List<MarketCandleEntity> candles, MacroDataEntity vix, MacroDataEntity gspc, MacroDataEntity dxy)
|
||||
private TechnicalAnalysisDto BuildDto(string cleanIsin, string querySymbol, string currency,
|
||||
List<MarketCandleEntity> candles, MacroDataEntity vix, MacroDataEntity gspc, MacroDataEntity dxy)
|
||||
{
|
||||
var vixRegime = vix.Value > 25m ? "HighVolatility" : (vix.Value > 18m ? "Moderate" : "LowVolatility");
|
||||
var summaryText = $"Markt-Vola (VIX: {vix.Value:F1}) ist {vixRegime}. S&P 500 Trend ist {gspc.TrendState}. DXY: {dxy.Value:F1}.";
|
||||
@@ -312,7 +314,7 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
MarketRegime: marketRegime, Currency: currency);
|
||||
}
|
||||
|
||||
private async Task<TechnicalAnalysisDto?> GetFromDbCacheAsync(string cleanIsin, CancellationToken cancellationToken)
|
||||
private async Task<TechnicalAnalysisDto?> GetFromDbCacheAsync(string cleanIsin, string? requestedTicker, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -324,6 +326,10 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
|
||||
if (cached != null && DateTime.UtcNow - cached.CalculatedAt < DbCacheTtl)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(requestedTicker) && !string.Equals(cached.Ticker, requestedTicker, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return null; // Ticker mismatch, force refresh required
|
||||
}
|
||||
return JsonSerializer.Deserialize<TechnicalAnalysisDto>(cached.AnalysisJson);
|
||||
}
|
||||
}
|
||||
@@ -335,7 +341,8 @@ public class TechnicalAnalysisDbService : ITechnicalAnalysisDbService
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task PersistToDbCacheAsync(string cleanIsin, string querySymbol, TechnicalAnalysisDto dto, CancellationToken cancellationToken)
|
||||
private async Task PersistToDbCacheAsync(string cleanIsin, string querySymbol, TechnicalAnalysisDto dto,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user