Files
zszq-trs/YLErpDAL/QdpModule/VolSurfaceBuilder.cs
T
2024-05-09 14:06:26 +08:00

140 lines
4.2 KiB
C#

using Qdp.ComputeService.Data.CommonModels.MarketInfos;
using Qdp.Foundation.Implementations;
using Qdp.Pricing.Base.Utilities;
using Qdp.Pricing.Ecosystem.Utilities;
using Qdp.Pricing.Library.Common.MathMethods.VolTermStructure;
using System.Diagnostics;
using YLErp.Configuration;
using YLErp.Models;
using YLErp.QdpModule.Constants;
namespace YLErp.QdpModule
{
/// <summary>
/// VolSurface构建器
/// </summary>
public class VolSurfaceBuilder
{
/// <summary>
///
/// </summary>
public string volSurfaceName { get; set; }
/// <summary>
/// 默认使用MoneynessVol
/// </summary>
public string volSurfaceType { get; set; } = "MoneynessVol";
/// <summary>
/// 默认使用BiLinear
/// </summary>
public string interpolation { get; set; } = ConsVolMethod.BiLinear;
/// <summary>
/// 默认使用PS.Config.ErpElement.VolSurfaceKeyDateShift
/// </summary>
public VolSurfaceKeyDateShiftEnum volSurfaceKeyDateShift { get; set; }
/// <summary>
/// 默认使用PS.Config.ErpElement.VolSurfaceTermExcludeEndDate
/// </summary>
public bool excludeEndDate { get; set; }
public VolSurfaceVector vector { get; set; }
public VolSurfaceBuilder()
{
//默认值不要随意更改
volSurfaceKeyDateShift = PS.Config.ErpElement.VolSurfaceKeyDateShift;
excludeEndDate = PS.Config.ErpElement.VolSurfaceTermExcludeEndDate;
}
/// <summary>
///
/// </summary>
public VolSurfaceBuilder SetVectors(IEnumerable<SingleVol> volTable, double addVol = 0, bool isAddVolPercent = true)
{
vector = VolSurfaceVector.Create(volTable, addVol, isAddVolPercent);
return this;
}
/// <summary>
///
/// </summary>
public VolSurfaceWrap Build(Date date)
{
if (date is null)
{
throw new ArgumentNullException(nameof(date));
}
if (string.IsNullOrEmpty(volSurfaceName))
{
throw new Exception("volSurfaceName不能为空");
}
if (vector is null)
{
throw new ArgumentNullException(nameof(vector));
}
Debug.Assert(vector.expires != null);
Debug.Assert(vector.strikes != null);
Debug.Assert(vector.vols != null);
if (string.IsNullOrWhiteSpace(volSurfaceType))
{
volSurfaceType = "MoneynessVol";
}
else
{
volSurfaceType = string.Equals("MoneynessVol", volSurfaceType, StringComparison.OrdinalIgnoreCase) ? "MoneynessVol" : "StrikeVol";
}
if (string.IsNullOrWhiteSpace(interpolation))
{
interpolation = ConsVolMethod.BiLinear;
}
var dates = QdpHelper.ConvertTenorsToDates(date, vector.expires, volSurfaceKeyDateShift, excludeEndDate);
var volSurface = new VolSurfMktData(volSurfaceName, dates.ToArray(), vector.strikes, vector.vols, interpolation, volSurfaceType);
var surface = volSurface.ToImpliedVolSurface(date, dc: BLL.valuedateBLL.SystemDate.CurveDayCount?.TrimToNull() ?? "Bus244");
return new VolSurfaceWrap
{
VolSurface = surface,
VolSurfaceName = volSurfaceName,
ValueDate = date.DateTime.ToString("yyyy-MM-dd")
};
}
public VolSurfaceWrap Build(DateTime date)
{
return Build(new Date(date.Date));
}
/// <summary>
///
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public VolSurfaceWrap Build(string date)
{
var wrap = Build(date.ToDate());
wrap.ValueDate = date;
return wrap;
}
}
/// <summary>
/// VolSurface构建结果包装
/// </summary>
public class VolSurfaceWrap
{
public string ValueDate { get; set; }
public ImpliedVolSurface VolSurface { get; set; }
public string VolSurfaceName { get; set; }
}
}