104 lines
2.9 KiB
C#
104 lines
2.9 KiB
C#
using YLErp.Abstract.DataProviders;
|
|
|
|
namespace YLErp.Modules.UnderlyingModule
|
|
{
|
|
/// <summary>
|
|
/// 篮子标的帮助类
|
|
/// </summary>
|
|
public static class BasketUnderlyingHelper
|
|
{
|
|
/// <summary>
|
|
/// 获取篮子标的价格
|
|
/// </summary>
|
|
public static BasketPriceResult GetBasketPrice(underlying_manager basketUnderlying,
|
|
IBasketPriceProvider priceProvider, bool ignoreError)
|
|
{
|
|
if (basketUnderlying is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(basketUnderlying));
|
|
}
|
|
|
|
if (!basketUnderlying.IsBasket())
|
|
{
|
|
return new BasketPriceResult();
|
|
}
|
|
|
|
return GetBasketPrice(basketUnderlying.SubData, priceProvider, ignoreError);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取篮子标的价格
|
|
/// </summary>
|
|
public static BasketPriceResult GetBasketPrice(string subData, IBasketPriceProvider priceProvider, bool ignoreError)
|
|
{
|
|
var result = new BasketPriceResult();
|
|
|
|
var list = JsonHelper.Deserialize<List<BasketUnderlyingItem>>(subData);
|
|
|
|
if (list == null || !list.Any())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
double weight = 0;
|
|
|
|
foreach (var item in list)
|
|
{
|
|
weight += item.weight;
|
|
|
|
if (priceProvider.TryGetSubPrice(item.code, out var price, out var settlePrice))
|
|
{
|
|
result.Price += item.weight * price;
|
|
result.SettlePrice += item.weight * settlePrice;
|
|
}
|
|
else if (!ignoreError)
|
|
{
|
|
if (result.ErrorCodeList == null)
|
|
{
|
|
result.ErrorCodeList = new List<string>();
|
|
}
|
|
result.ErrorCodeList.Add(item.code);
|
|
}
|
|
}
|
|
|
|
if (weight > 0)
|
|
{
|
|
result.Price /= weight;
|
|
result.SettlePrice /= weight;
|
|
}
|
|
else
|
|
{
|
|
result.Price = result.SettlePrice = 0;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取篮子标的价格结果
|
|
/// </summary>
|
|
public struct BasketPriceResult
|
|
{
|
|
/// <summary>
|
|
/// 行情价或收盘价
|
|
/// </summary>
|
|
public double Price;
|
|
|
|
/// <summary>
|
|
/// 结算价
|
|
/// </summary>
|
|
public double SettlePrice;
|
|
|
|
/// <summary>
|
|
/// 未找到价格的标的代码集合
|
|
/// </summary>
|
|
public List<string> ErrorCodeList;
|
|
|
|
/// <summary>
|
|
/// 是否存在未找到价格的标的代码集合
|
|
/// </summary>
|
|
public bool HasError => ErrorCodeList != null && ErrorCodeList.Any();
|
|
}
|
|
}
|