using AssetManager.Models.DTOs; using Microsoft.Extensions.Logging; namespace AssetManager.Infrastructure.Services; /// /// Mock 市场数据服务(用于开发测试) /// public class MockMarketDataService : IMarketDataService { private readonly ILogger _logger; public MockMarketDataService(ILogger logger) { _logger = logger; } public Task GetStockPriceAsync(string symbol) { _logger.LogInformation("Mock 获取股票价格: {Symbol}", symbol); // Mock 价格:基于标的代码生成一个稳定的价格 decimal basePrice = symbol.GetHashCode() % 1000 + 50; return Task.FromResult(new MarketPriceResponse { Symbol = symbol, Price = basePrice, Timestamp = DateTime.UtcNow, AssetType = "Stock" }); } public Task GetCryptoPriceAsync(string symbol) { _logger.LogInformation("Mock 获取加密货币价格: {Symbol}", symbol); decimal basePrice = symbol.GetHashCode() % 50000 + 10000; return Task.FromResult(new MarketPriceResponse { Symbol = symbol, Price = basePrice, Timestamp = DateTime.UtcNow, AssetType = "Crypto" }); } public Task> GetStockHistoricalDataAsync(string symbol, string timeframe, int limit) { _logger.LogInformation("Mock 获取股票历史数据: {Symbol}, {Timeframe}, {Limit}", symbol, timeframe, limit); return Task.FromResult(GenerateMockData(symbol, "Stock", timeframe, limit)); } public Task> GetCryptoHistoricalDataAsync(string symbol, string timeframe, int limit) { _logger.LogInformation("Mock 获取加密货币历史数据: {Symbol}, {Timeframe}, {Limit}", symbol, timeframe, limit); return Task.FromResult(GenerateMockData(symbol, "Crypto", timeframe, limit)); } /// /// 生成 Mock K 线数据(模拟一个上升趋势 + 随机波动) /// private List GenerateMockData(string symbol, string assetType, string timeframe, int limit) { var data = new List(); var random = new Random(symbol.GetHashCode()); // 基于 symbol 的稳定随机数 // 基础价格:基于 symbol 生成 decimal basePrice = symbol.GetHashCode() % 500 + 100; DateTime currentTime = DateTime.UtcNow; // 根据 timeframe 转换为时间增量 TimeSpan increment = timeframe.ToLower() switch { "1min" => TimeSpan.FromMinutes(1), "5min" => TimeSpan.FromMinutes(5), "15min" => TimeSpan.FromMinutes(15), "1h" => TimeSpan.FromHours(1), "1d" => TimeSpan.FromDays(1), "1w" => TimeSpan.FromDays(7), "1m" => TimeSpan.FromDays(30), _ => TimeSpan.FromDays(1) }; // 倒序生成(从过去到现在) for (int i = limit - 1; i >= 0; i--) { DateTime timestamp = currentTime - increment * i; // 模拟上升趋势 + 随机波动 decimal trend = (limit - i) * 0.1m; // 上升趋势 decimal noise = (decimal)(random.NextDouble() - 0.5) * 2; // ±1 的随机波动 decimal close = basePrice + trend + noise; // 生成 OHLC decimal open = close + (decimal)(random.NextDouble() - 0.5) * 1; decimal high = Math.Max(open, close) + (decimal)random.NextDouble() * 0.5m; decimal low = Math.Min(open, close) - (decimal)random.NextDouble() * 0.5m; decimal volume = (decimal)(random.NextDouble() * 1000000 + 100000); data.Add(new MarketDataResponse { Symbol = symbol, Timestamp = timestamp, Open = open, High = high, Low = low, Close = close, Volume = volume, AssetType = assetType }); } return data; } }