using YLErp.BLL; using YLErp.Commons; using YLErp.QdpModule; namespace YLErp.Modules.UnderlyingModule { /// /// 合成价差期权价格服务 /// public class SyntheticUnderlyingPriceService : YLBaseService { public SyntheticUnderlyingPriceService(OptUserInfo userInfo) : base(userInfo) { } /// /// 获取合成价差期权价格 /// public double? GetPrice(string underlyingCode) { if (string.IsNullOrEmpty(underlyingCode)) { return null; } var query = from su in DbContext.synthetic_underlying join u in DbContext.underlying_manager on su.Name equals u.UnderlyingCode join u1 in DbContext.underlying_manager on su.UnderlyingCode1 equals u1.UnderlyingCode into u1t from u1 in u1t.DefaultIfEmpty() join u2 in DbContext.underlying_manager on su.UnderlyingCode2 equals u2.UnderlyingCode into u2t from u2 in u2t.DefaultIfEmpty() join u3 in DbContext.underlying_manager on su.UnderlyingCode3 equals u3.UnderlyingCode into u3t from u3 in u3t.DefaultIfEmpty() join u4 in DbContext.underlying_manager on su.UnderlyingCode4 equals u4.UnderlyingCode into u4t from u4 in u4t.DefaultIfEmpty() where u.UnderlyingCode == underlyingCode select new { Price = (u1.Price ?? 0) * (su.Coefficient1 ?? 0) + (u2.Price ?? 0) * (su.Coefficient2 ?? 0) + (u3.Price ?? 0) * (su.Coefficient3 ?? 0) + (u4.Price ?? 0) * (su.Coefficient4 ?? 0) + (su.Constant ?? 0) }; return query.FirstOrDefault()?.Price; } /// /// 获取合成价差期权价格 /// public SyntheticPriceModel GetPriceModel(string underlyingCode) { if (string.IsNullOrEmpty(underlyingCode)) { return null; } var query = from su in DbContext.synthetic_underlying join u in DbContext.underlying_manager on su.Name equals u.UnderlyingCode join u1 in DbContext.underlying_manager on su.UnderlyingCode1 equals u1.UnderlyingCode into u1t from u1 in u1t.DefaultIfEmpty() join u2 in DbContext.underlying_manager on su.UnderlyingCode2 equals u2.UnderlyingCode into u2t from u2 in u2t.DefaultIfEmpty() join u3 in DbContext.underlying_manager on su.UnderlyingCode3 equals u3.UnderlyingCode into u3t from u3 in u3t.DefaultIfEmpty() join u4 in DbContext.underlying_manager on su.UnderlyingCode4 equals u4.UnderlyingCode into u4t from u4 in u4t.DefaultIfEmpty() where u.UnderlyingCode == underlyingCode select new { su.Constant, su1 = u1 == null ? null : new UnderlyingPriceModel { UnderlyingCode = u1.UnderlyingCode, Price = u1.Price ?? 0, Coefficient = su.Coefficient1 ?? 0, ContractSize=su.ContractSize, }, su2 = u2 == null ? null : new UnderlyingPriceModel { UnderlyingCode = u2.UnderlyingCode, Price = u2.Price ?? 0, Coefficient = su.Coefficient2 ?? 0, ContractSize = su.ContractSize }, su3 = u3 == null ? null : new UnderlyingPriceModel { UnderlyingCode = u3.UnderlyingCode, Price = u3.Price ?? 0, Coefficient = su.Coefficient3 ?? 0, ContractSize = su.ContractSize }, su4 = u4 == null ? null : new UnderlyingPriceModel { UnderlyingCode = u4.UnderlyingCode, Price = u4.Price ?? 0, Coefficient = su.Coefficient4 ?? 0, ContractSize = su.ContractSize }, }; var data = query.FirstOrDefault(); if (data == null) { return null; } var list = new[] { data.su1, data.su2, data.su3, data.su4 }; list = list.Where(n => n != null).ToArray(); var model = new SyntheticPriceModel { SuList = list, Constant = data.Constant ?? 0, Price = list.Sum(n => n.Price * n.Coefficient) + (data.Constant ?? 0) }; model.Price = OtcFormatHelper.FormatValue(model.Price, OtcFormatHelper.FormatModel.trading.umprice.precision); return model; } public SyntheticPriceModel GetPriceModel(string underlyingCode,DateTime valueDate) { valueDate = valueDate.Date; //传进来的可能是非交易日期 valueDate = PS.Config.IsGuoJun ? QdpCalendarHelper.GetNonHolidayDefore(valueDate) : QdpCalendarHelper.GetNonHoliday(valueDate); var manager = DbContext.underlying_manager.FirstOrDefault(p => p.UnderlyingCode.Equals(underlyingCode)); if (manager == null) { throw new ServiceException("未找到对应标的"); } valueDate = manager.UnderlyingInstrumentType != ConsGlobal.InstrumentType.CommodityFutures || manager.MaturityDate > valueDate ? valueDate : manager.MaturityDate.Value; if (valueDate < valuedateBLL.ValueDate) { var syntheticUnderlying = DbContext.synthetic_underlying.FirstOrDefault(p => p.Name.Equals(underlyingCode)); if (syntheticUnderlying == null) { return null; } var subUnderlyingCodes = new List(); if (!string.IsNullOrEmpty(syntheticUnderlying.UnderlyingCode1)) { subUnderlyingCodes.Add(syntheticUnderlying.UnderlyingCode1); } if (!string.IsNullOrEmpty(syntheticUnderlying.UnderlyingCode2)) { subUnderlyingCodes.Add(syntheticUnderlying.UnderlyingCode2); } if (!string.IsNullOrEmpty(syntheticUnderlying.UnderlyingCode3)) { subUnderlyingCodes.Add(syntheticUnderlying.UnderlyingCode3); } if (!string.IsNullOrEmpty(syntheticUnderlying.UnderlyingCode4)) { subUnderlyingCodes.Add(syntheticUnderlying.UnderlyingCode4); } var subList=new List(); var futurePriceList = DbContext.eod_commodity_future_price.Where(p => p.ValueDate == valueDate && subUnderlyingCodes.Contains(p.UnderlyingCode)).ToList(); if (futurePriceList != null && futurePriceList.Count > 0) { futurePriceList.ForEach(p => { subList.Add(new UnderlyingPriceModel { UnderlyingCode = p.UnderlyingCode, Price = p.ClosePrice }); subUnderlyingCodes.Remove(subUnderlyingCodes.First(d=>d.Equals(p.UnderlyingCode,StringComparison.OrdinalIgnoreCase))); }); } if (subUnderlyingCodes.Count > 0) { var stockPriceList = DbContext.eod_stock_price.Where(p => p.ValueDate == valueDate && subUnderlyingCodes.Contains(p.UnderlyingCode)).ToList(); if (stockPriceList != null && stockPriceList.Count > 0) { stockPriceList.ForEach(p => { subList.Add(new UnderlyingPriceModel { UnderlyingCode = p.UnderlyingCode, Price = p.ClosePrice }); }); } } if (subList.Count == 0) { return null; } var resultSubList = new List(); SyntheticPriceModel result = new SyntheticPriceModel { SuList = resultSubList,Constant=syntheticUnderlying.Constant??0 }; if (!string.IsNullOrEmpty(syntheticUnderlying.UnderlyingCode1)) { var sub = subList.FirstOrDefault(d => d.UnderlyingCode.Equals(syntheticUnderlying.UnderlyingCode1,StringComparison.OrdinalIgnoreCase)); if (sub != null) { sub.Coefficient = (double)syntheticUnderlying.Coefficient1; resultSubList.Add(sub); } else { resultSubList.Add(new UnderlyingPriceModel { Coefficient = (double)syntheticUnderlying.Coefficient1, UnderlyingCode = syntheticUnderlying.UnderlyingCode1, Price = 0 }); } } if (!string.IsNullOrEmpty(syntheticUnderlying.UnderlyingCode2)) { var sub = subList.FirstOrDefault(d => d.UnderlyingCode.Equals(syntheticUnderlying.UnderlyingCode2, StringComparison.OrdinalIgnoreCase)); if (sub != null) { sub.Coefficient = (double)syntheticUnderlying.Coefficient2; resultSubList.Add(sub); } else { resultSubList.Add(new UnderlyingPriceModel { Coefficient = (double)syntheticUnderlying.Coefficient2, UnderlyingCode = syntheticUnderlying.UnderlyingCode2, Price = 0 }); } } if (!string.IsNullOrEmpty(syntheticUnderlying.UnderlyingCode3)) { var sub = subList.FirstOrDefault(d => d.UnderlyingCode.Equals(syntheticUnderlying.UnderlyingCode3, StringComparison.OrdinalIgnoreCase)); if (sub != null) { sub.Coefficient = (double)syntheticUnderlying.Coefficient3; resultSubList.Add(sub); } else { resultSubList.Add(new UnderlyingPriceModel { Coefficient = (double)syntheticUnderlying.Coefficient3, UnderlyingCode = syntheticUnderlying.UnderlyingCode3, Price = 0 }); } } if (!string.IsNullOrEmpty(syntheticUnderlying.UnderlyingCode4)) { var sub = subList.FirstOrDefault(d => d.UnderlyingCode.Equals(syntheticUnderlying.UnderlyingCode4, StringComparison.OrdinalIgnoreCase)); if (sub != null) { sub.Coefficient = (double)syntheticUnderlying.Coefficient4; resultSubList.Add(sub); } else { resultSubList.Add(new UnderlyingPriceModel { Coefficient = (double)syntheticUnderlying.Coefficient4, UnderlyingCode = syntheticUnderlying.UnderlyingCode4, Price = 0 }); } } result.Price= resultSubList.Sum(n => n.Price * n.Coefficient) + result.Constant; return result; } else { return GetPriceModel(underlyingCode); } } } }