using YLErp.QdpModule; using YLErp.QdpModule.Constants; namespace YLErp.DBModels { /// /// 波动率构造 /// public class VolatilityBuilder { readonly volatility _volatility; public VolatilityBuilder(DateTime quotationDate, string volMode = ConsVolInfos.defVolMode) { _volatility = new volatility { QuotationDate = quotationDate, InterpolationMethod = ConsVolInfos.defInterpolationMethod, VolSurfaceMode = string.IsNullOrEmpty(volMode) ? ConsVolInfos.defVolMode : volMode, VolType = ConsVolInfos.defVolType, UnderlyingId = null, ContractCode = null, OptId = 0, OptDate = DateTime.Now, OptName = ConsVolInfos.defOptName }; } /// /// 必须设置--波动率值 /// public VolatilityBuilder SetData(double vol) { _volatility.SetData(QdpVolHelper.GenerateFlatSingleVols(vol)); return this; } /// /// 必须设置--波动率值 /// public VolatilityBuilder SetDefaultData() { _volatility.SetData(QdpVolHelper.GenerateFlatSingleVols(ConsVolInfos.defVol)); return this; } /// /// 必须设置--标的信息 /// public VolatilityBuilder SetUnderlying(int underlyingId, string underlyingCode) { _volatility.UnderlyingId = underlyingId; _volatility.ContractCode = underlyingCode; return this; } /// /// 必须设置--标的信息 /// public VolatilityBuilder SetUnderlying(IUnderlyingBasic underlying) { if (underlying is null) { throw new ArgumentNullException(nameof(underlying)); } _volatility.UnderlyingId = underlying.id; _volatility.ContractCode = underlying.UnderlyingCode; return this; } /// /// 可选配置--波动率类型 /// public VolatilityBuilder SetVolType(string volType) { _volatility.VolType = volType; return this; } /// /// 可选配置--波动率模式 /// public VolatilityBuilder SetVolMode(string volMode) { _volatility.VolSurfaceMode = volMode; return this; } /// /// 可选配置--插值方法 /// public VolatilityBuilder SetInterpolationMethod(string interMethod) { _volatility.InterpolationMethod = interMethod; return this; } /// /// 可选配置--操作人信息 /// public VolatilityBuilder SetOpt(int optid, string optName) { _volatility.OptId = optid; _volatility.OptName = optName; return this; } /// /// 可选配置--用户组 /// public VolatilityBuilder SetUserGroup(string userGroup) { _volatility.UserGroup = userGroup; return this; } public volatility Build(IUnderlyingBasic underlying) { return SetUnderlying(underlying).Build(); } public volatility Build(double vol) { return SetData(vol).Build(); } public volatility Build() { if (string.IsNullOrEmpty(_volatility.ContractCode)) { throw new ArgumentException("ContractCode 必须有值", nameof(_volatility.ContractCode)); } if (_volatility.QuotationDate.Year < 2000) { throw new ArgumentException("QuotationDate 必须为有效值,合约代码:" + _volatility.ContractCode, nameof(_volatility.QuotationDate)); } if (string.IsNullOrEmpty(_volatility.VolType)) { throw new ArgumentException("VolType 必须有值,合约代码:" + _volatility.ContractCode, nameof(_volatility.VolType)); } if (string.IsNullOrEmpty(_volatility.InterpolationMethod)) { throw new ArgumentException("InterpolationMethod 必须有值,合约代码:" + _volatility.ContractCode, nameof(_volatility.InterpolationMethod)); } if (string.IsNullOrEmpty(_volatility.VolSurfaceMode)) { throw new ArgumentException("VolSurfaceMode 必须有值,合约代码:" + _volatility.ContractCode, nameof(_volatility.VolSurfaceMode)); } if (string.IsNullOrEmpty(_volatility.Data)) { throw new ArgumentException("VolData 必须有值,合约代码:" + _volatility.ContractCode, nameof(_volatility.Data)); } return _volatility; } /// /// /// public static VolatilityBuilder CreateMoneynessVolBuilder(DateTime quotationDate, string volType = ConsVolInfos.defVolType) { return new VolatilityBuilder(quotationDate, "MoneynessVol").SetVolType(volType); } } }