Files
zszq-trs/YLErpDAL/Modules/VolatilityModule/VolatilityBuilder.cs
T
2024-05-09 14:06:26 +08:00

172 lines
5.4 KiB
C#

using YLErp.QdpModule;
using YLErp.QdpModule.Constants;
namespace YLErp.DBModels
{
/// <summary>
/// 波动率构造
/// </summary>
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
};
}
/// <summary>
/// 必须设置--波动率值
/// </summary>
public VolatilityBuilder SetData(double vol)
{
_volatility.SetData(QdpVolHelper.GenerateFlatSingleVols(vol));
return this;
}
/// <summary>
/// 必须设置--波动率值
/// </summary>
public VolatilityBuilder SetDefaultData()
{
_volatility.SetData(QdpVolHelper.GenerateFlatSingleVols(ConsVolInfos.defVol));
return this;
}
/// <summary>
/// 必须设置--标的信息
/// </summary>
public VolatilityBuilder SetUnderlying(int underlyingId, string underlyingCode)
{
_volatility.UnderlyingId = underlyingId;
_volatility.ContractCode = underlyingCode;
return this;
}
/// <summary>
/// 必须设置--标的信息
/// </summary>
public VolatilityBuilder SetUnderlying(IUnderlyingBasic underlying)
{
if (underlying is null)
{
throw new ArgumentNullException(nameof(underlying));
}
_volatility.UnderlyingId = underlying.id;
_volatility.ContractCode = underlying.UnderlyingCode;
return this;
}
/// <summary>
/// 可选配置--波动率类型
/// </summary>
public VolatilityBuilder SetVolType(string volType)
{
_volatility.VolType = volType;
return this;
}
/// <summary>
/// 可选配置--波动率模式
/// </summary>
public VolatilityBuilder SetVolMode(string volMode)
{
_volatility.VolSurfaceMode = volMode;
return this;
}
/// <summary>
/// 可选配置--插值方法
/// </summary>
public VolatilityBuilder SetInterpolationMethod(string interMethod)
{
_volatility.InterpolationMethod = interMethod;
return this;
}
/// <summary>
/// 可选配置--操作人信息
/// </summary>
public VolatilityBuilder SetOpt(int optid, string optName)
{
_volatility.OptId = optid;
_volatility.OptName = optName;
return this;
}
/// <summary>
/// 可选配置--用户组
/// </summary>
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;
}
/// <summary>
///
/// </summary>
public static VolatilityBuilder CreateMoneynessVolBuilder(DateTime quotationDate, string volType = ConsVolInfos.defVolType)
{
return new VolatilityBuilder(quotationDate, "MoneynessVol").SetVolType(volType);
}
}
}