using YLErp.Abstract.DataProviders; using YLErp.Models; namespace YLErp.Modules.DataProviderModule { /// /// 日终场内期权价格提供 /// public class EodExchangeOptionPriceProvider : IPriceProvider { private bool _allInitialized; private readonly Dictionary _priceDic; public EodExchangeOptionPriceProvider(DateTime valueDate, bool useClosePrice) { ValueDate = valueDate; UseClosePrice = useClosePrice; _priceDic = new Dictionary(StringComparer.OrdinalIgnoreCase); } /// /// 取值日期 /// public DateTime ValueDate { get; } /// /// 使用收盘价 /// public bool UseClosePrice { get; } public EodExchangeOptionPriceProvider InitializeAll() { var arr = GetDatasFromDB(null); foreach (var item in arr) { _priceDic[item.OptionCode] = item; } _allInitialized = true; return this; } /// /// 根据场内期权代码初始化价格数据字典, /// 调用后提升一定性能,不调用也无关系 /// public EodExchangeOptionPriceProvider Initialize(string[] optionCodes) { if (_allInitialized) { return this; } var set = optionCodes.Where(n => !string.IsNullOrWhiteSpace(n)).ToHashSet(StringComparer.OrdinalIgnoreCase); var list = GetDatasFromDB(set); if (set.Any()) { lock (_priceDic) { foreach (var code in set) { _priceDic[code] = null; } foreach (var item in list) { _priceDic[item.OptionCode] = item; } } } return this; } /// /// 获取当前缓存中是否存在任一笔价格数据 /// public bool HasAnyPrice() { return _priceDic.Values.Any(n => n != null); } /// /// 获取价格 /// public double GetPrice(string optionCode) { var data = InnerGetPrice(optionCode); return data == null ? 0 : (UseClosePrice ? data.ClosePrice : data.SettlePrice); } /// /// 尝试获取价格 /// public bool TryGetPrice(string optionCode, out double price) { var data = InnerGetPrice(optionCode); if (data != null) { price = UseClosePrice ? data.ClosePrice : data.SettlePrice; return true; } else { price = 0; return false; } } /// /// 获取价格对象 /// public EodExchangeOptionPrice GetPriceModel(string optionCode) { return InnerGetPrice(optionCode); } /// /// 尝试获取价格对象 /// public bool TryGetPriceModel(string optionCode, out EodExchangeOptionPrice eodPrice) { return null != (eodPrice = InnerGetPrice(optionCode)); } /// /// 获取收盘价或结算价 /// private EodExchangeOptionPrice InnerGetPrice(string optionCode) { if (string.IsNullOrWhiteSpace(optionCode)) { return null; } lock (_priceDic) { if (_priceDic.TryGetValue(optionCode, out var data)) { return data; } } if (_allInitialized) { return null; } Initialize(new[] { optionCode }); lock (_priceDic) { if (_priceDic.TryGetValue(optionCode, out var data)) { return data; } } return null; } /// /// 从数据库中获取初始化数据 /// private EodExchangeOptionPrice[] GetDatasFromDB(HashSet optionCodes) { var predicate = PredicateBuilder.Create(t => t.ValueDate == ValueDate); if (optionCodes != null) { if (optionCodes.Count > 0) { predicate = predicate.And(t => optionCodes.Contains(t.ContractCode)); } else { return Array.Empty(); } } using var db = DbContextFactory.GetYLDbContext(); return db.eod_exchange_option_price.Where(predicate) .Select(n => new EodExchangeOptionPrice { OptionCode = n.ContractCode, ClosePrice = n.ClosePrice, HighPrice = n.HighPrice, LowPrice = n.LowPrice, SettlePrice = n.SettlePrice, ValueDate = n.ValueDate }).ToArray(); } } /// /// 日终场内期权结算价 /// public class EodExchangeOptionPrice { /// /// 取值日 /// public DateTime ValueDate { get; set; } /// /// 期权代码 /// public string OptionCode { get; set; } /// /// 收盘价 /// public double ClosePrice { get; set; } /// /// 结算价 /// public double SettlePrice { get; set; } /// /// 最高价 /// public double? HighPrice { get; set; } /// /// 最低价 /// public double? LowPrice { get; set; } } }