using System; using System.Linq; using System.Text.RegularExpressions; using YLErp.Abstract.DataProviders; using YLErp.DBModels; using YLErp.Modules; namespace YLErp.Helpers { class InnerUnderlyingDataProvider : IUnderlyingDataProvider { /// /// 获取标的数据 /// public underlying_manager GetUnderlying(int underlyingId) { return DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingId); } /// /// 获取标的数据 /// public underlying_manager GetUnderlying(string underlyingCode) { return DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingCode); } /// /// /// public underlying_manager GetUnderlying(string underlyingCode, out double contractSize) { var um = DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingCode); contractSize = um?.ContractSize ?? 1; return um; } /// /// 获取品种数据 /// public Variety GetVariety(int varietyId) { return DataCacheProvider.GetVarietyDataSource().GetData(varietyId); } /// /// 获取品种信息 /// public Variety GetVariety(string underlyingCode, out double contractSize) { var um = GetUnderlying(underlyingCode); contractSize = um?.ContractSize ?? 1; return DataCacheProvider.GetVarietyDataSource().GetData(um?.UnderlyingTypeId ?? 0); } /// /// 获取品种数据 /// /// 标的代码 public Variety GetVariety(string underlyingCode) { var um = GetUnderlying(underlyingCode); return DataCacheProvider.GetVarietyDataSource().GetData(um?.UnderlyingTypeId ?? 0); } /// /// 根据标的代码获取份额和数量的乘积因子 /// public int GetCountRatio(string underlyingCode) { return DataCacheProvider.GetUnderlyingDataSource().GetCountRatio(underlyingCode); } /// /// 尝试获取标的过期日(股票:2099-01-01) /// public bool TryGetMaturityDate(string underlyingCode, out DateTime date) { date = DateTime.MinValue; if (string.IsNullOrEmpty(underlyingCode)) { return false; } var data = DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingCode); if (data == null) return false; date = data.UnderlyingInstrumentType == "Stock" ? DateTime.Now.AddYears(3) : data.MaturityDate ?? DateTime.MinValue; return true; } /// /// 根据标的代码获取当前价格 /// public double GetPrice(string underlyingCode) { return DataCacheProvider.GetUnderlyingDataSource().TryGetPrice(underlyingCode, out var price) ? price : 0; } /// /// 根据标的代码获取当前价格 /// public bool TryGetPrice(string underlyingCode, out double price) { return DataCacheProvider.GetUnderlyingDataSource().TryGetPrice(underlyingCode, out price); } /// /// 获取相关性 /// public virtual CorrelationTable GetCorrelation(int underlyingId1, int underlyingId2) { return DataCacheProvider.GetCorrelationDataSource().AsQueryable().FirstOrDefault( n => (n.UnderlyingId1 == underlyingId1 && n.UnderlyingId2 == underlyingId2) || (n.UnderlyingId1 == underlyingId2 && n.UnderlyingId2 == underlyingId1)); } /// /// 获取场外期权标的信息 /// public virtual ExchangeListOption GetExchange_List_Option(string ContractCode) { return new ExchangeListOption { CreateTime = DateTime.Now, ContractSize = 10, ContractCode = ContractCode, ExerciseMode = "European", MarginRate = 0, MaturityDate = DateTime.Today.AddMonths(1), MarketCode = "TEST", OpenDate = DateTime.Today.AddDays(-10), OptionType = ContractCode.Contains("-C-") ? "看涨" : "看跌", PrevClosePrice = 12, Price = 12.5, PriceTick = 0.1, PriceTime = DateTime.Now, Strike = int.Parse(Regex.Match(ContractCode, "\\d+$").Value), UnderlyingCode = "RB00" }; } /// /// 获取组合标的 /// public SyntheticUnderlying GetSyntheticUnderlying(string underlyingCode) { return DataCacheProvider.GetUnderlyingDataSource().GetSyntheticUnderlying(underlyingCode); } } }