回滚到腾讯财经行情接口

This commit is contained in:
claw_bot 2026-03-12 05:18:46 +00:00
parent 534ba7c343
commit 57e9c69a87

View File

@ -39,7 +39,7 @@ public class MarketDataService : IMarketDataService
}
/// <summary>
/// 获取股票实时价格(使用东方财富接口,免费无限制,返回实际交易价格,和券商一致
/// 获取股票实时价格(使用腾讯财经接口,免费无限制,支持盘前盘后
/// </summary>
/// <param name="symbol">股票代码</param>
/// <returns>股票价格信息</returns>
@ -47,22 +47,36 @@ public class MarketDataService : IMarketDataService
{
try
{
_logger.LogInformation($"Requesting stock price for symbol: {symbol} (东方财富接口)");
_logger.LogInformation($"Requesting stock price for symbol: {symbol} (腾讯财经接口)");
// 东方财富美股接口secid=105.代码105代表美股
var url = $"https://push2.eastmoney.com/api/qt/stock/get?secid=105.{symbol.ToUpper()}&ut=fa5fd1943c7b386f172d6893dbfba10b&fields=f43,f60&np=1&fltt=2&invt=2";
var response = await _httpClient.GetFromJsonAsync<EastMoneyStockResponse>(url);
// 腾讯财经美股接口前缀us大写代码
var url = $"http://qt.gtimg.cn/q=us{symbol.ToUpper()}";
var response = await _httpClient.GetStringAsync(url);
if (response?.data == null || response.data.f43 <= 0)
if (string.IsNullOrEmpty(response) || !response.Contains("~"))
{
throw new Exception($"东方财富接口返回无效数据,标的: {symbol}");
throw new Exception($"腾讯财经接口返回无效数据,标的: {symbol}");
}
// 提取字段f43=最新价 f60=昨收价,单位都是分,转成元
var currentPrice = response.data.f43 / 100m;
var prevClose = response.data.f60 / 100m;
// 解析返回数据
var parts = response.Split('"')[1].Split('~');
if (parts.Length < 36)
{
throw new Exception($"腾讯财经返回字段不足,标的: {symbol}");
}
_logger.LogDebug("东方财富接口返回 {Symbol}:最新价 {CurrentPrice},昨收价 {PrevClose},涨跌额 {Change}",
// 提取字段:[3]=最新价 [4]=昨收价
if (!decimal.TryParse(parts[3], out var currentPrice) || currentPrice <= 0)
{
throw new Exception($"解析最新价失败,标的: {symbol},返回值: {parts[3]}");
}
if (!decimal.TryParse(parts[4], out var prevClose) || prevClose <= 0)
{
prevClose = currentPrice; // 解析失败用当前价当昨收
}
_logger.LogDebug("腾讯财经接口返回 {Symbol}:最新价 {CurrentPrice},昨收价 {PrevClose},涨跌额 {Change}",
symbol, currentPrice, prevClose, currentPrice - prevClose);
return new MarketPriceResponse
@ -76,35 +90,12 @@ public class MarketDataService : IMarketDataService
}
catch (Exception ex)
{
_logger.LogError(ex, $"东方财富接口获取价格失败,标的: {symbol}降级使用Tiingo接口");
_logger.LogError(ex, $"腾讯财经接口获取价格失败,标的: {symbol}降级使用Tiingo接口");
// 降级使用Tiingo接口
return await GetStockPriceFromTiingoAsync(symbol);
}
}
/// <summary>
/// 东方财富美股接口响应模型
/// </summary>
internal class EastMoneyStockResponse
{
public EastMoneyStockData? data { get; set; }
public int rc { get; set; }
public string? msg { get; set; }
}
internal class EastMoneyStockData
{
/// <summary>
/// 最新价(单位:分)
/// </summary>
public decimal f43 { get; set; }
/// <summary>
/// 昨收价(单位:分)
/// </summary>
public decimal f60 { get; set; }
}
/// <summary>
/// 从Tiingo获取股票价格降级用
/// </summary>