feat(市场数据服务): 添加获取价格和历史数据的模拟方法
新增 GetPriceAsync 和 GetHistoricalDataAsync 方法到 MockMarketDataService 类中,用于模拟市场数据服务。同时添加 Microsoft.Extensions.Caching.Abstractions 包依赖以支持后续缓存功能。
This commit is contained in:
parent
53b4f4501e
commit
4ac8a5f063
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AssetManager.Models\AssetManager.Models.csproj" />
|
||||
@ -6,6 +6,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="10.0.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -122,4 +122,50 @@ public class MockMarketDataService : IMarketDataService
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// 新增到 MockMarketDataService 类中
|
||||
public async Task<MarketPriceResponse> GetPriceAsync(string symbol, string assetType)
|
||||
{
|
||||
_logger.LogInformation("Mock 获取价格: {Symbol}, 资产类型: {AssetType}", symbol, assetType);
|
||||
// Mock 一个随机价格
|
||||
var random = new Random();
|
||||
return await Task.FromResult(new MarketPriceResponse
|
||||
{
|
||||
Symbol = symbol,
|
||||
Price = (decimal)(random.NextDouble() * 100 + 50),
|
||||
PreviousClose = (decimal)(random.NextDouble() * 100 + 45),
|
||||
Timestamp = DateTime.UtcNow,
|
||||
AssetType = assetType
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<List<MarketDataResponse>> GetHistoricalDataAsync(string symbol, string assetType, string timeframe, int limit)
|
||||
{
|
||||
_logger.LogInformation("Mock 获取历史数据: {Symbol}, 资产类型: {AssetType}, 周期: {Timeframe}, 数量: {Limit}",
|
||||
symbol, assetType, timeframe, limit);
|
||||
// Mock 历史数据
|
||||
var result = new List<MarketDataResponse>();
|
||||
var random = new Random();
|
||||
var basePrice = (decimal)(random.NextDouble() * 100 + 50);
|
||||
|
||||
for (int i = 0; i < limit; i++)
|
||||
{
|
||||
var change = (decimal)(random.NextDouble() * 10 - 5);
|
||||
basePrice += change;
|
||||
result.Add(new MarketDataResponse
|
||||
{
|
||||
Symbol = symbol,
|
||||
Open = basePrice - (decimal)random.NextDouble() * 2,
|
||||
High = basePrice + (decimal)random.NextDouble() * 2,
|
||||
Low = basePrice - (decimal)random.NextDouble() * 2,
|
||||
Close = basePrice,
|
||||
Volume = (decimal)random.NextDouble() * 1000000,
|
||||
Timestamp = DateTime.UtcNow.AddDays(-(limit - i)),
|
||||
AssetType = assetType
|
||||
});
|
||||
}
|
||||
|
||||
return await Task.FromResult(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user