52 lines
2.1 KiB
C#
52 lines
2.1 KiB
C#
using System;
|
|
using YLErp.DBModels;
|
|
using YLErp.DBModels.Enums;
|
|
using YLErp.Modules;
|
|
|
|
namespace YLErp.Helpers
|
|
{
|
|
/// <summary>
|
|
/// DV01计算工具类
|
|
/// DV01 = 方向 * (券面总额(万元) / 单位面值) * 中债基点价值
|
|
/// </summary>
|
|
public static class Dv01Helper
|
|
{
|
|
/// <summary>
|
|
/// 计算DV01
|
|
/// </summary>
|
|
/// <param name="underlyingCode">标的代码</param>
|
|
/// <param name="posiQuantity">持仓数量</param>
|
|
/// <param name="posiDirection">收支方向: 1=收取, 2=支付</param>
|
|
/// <param name="positionType">多空方向: 1=多头, 2=空头</param>
|
|
/// <param name="vobp">中债基点价值</param>
|
|
/// <returns>DV01值</returns>
|
|
public static decimal CalcDv01(string underlyingCode, decimal posiQuantity, int posiDirection, int positionType, decimal vobp)
|
|
{
|
|
if (vobp == 0 || posiQuantity == 0) return 0;
|
|
|
|
var um = DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingCode);
|
|
if (um == null || !um.IsBond()) return 0;
|
|
|
|
// 方向: 支付多头=-1, 收取多头=1, 支付空头=1, 收取空头=-1
|
|
// = (PosiDirection==收取?1:-1) * (PositionType==多头?1:-1)
|
|
int direction = (posiDirection == (int)SwapDirectionEnum.收取 ? 1 : -1)
|
|
* (positionType == (int)PositionTypeFlag.Long ? 1 : -1);
|
|
|
|
// 券面总额(万元) = 交易数量 / 10000
|
|
double faceValueWan = Convert.ToDouble(posiQuantity) / 10000d;
|
|
|
|
// 单位面值, 默认100
|
|
decimal unitFaceValue = 100m;
|
|
if (!string.IsNullOrEmpty(um.ExJson))
|
|
{
|
|
var bond = JsonHelper.Deserialize<UnderlyingBond>(um.ExJson);
|
|
if (bond?.Price != null && bond.Price != 0)
|
|
unitFaceValue = bond.Price.Value;
|
|
}
|
|
|
|
decimal result = Convert.ToDecimal(direction * (faceValueWan / Convert.ToDouble(unitFaceValue)) * Convert.ToDouble(vobp));
|
|
return Math.Round(result, 4);
|
|
}
|
|
}
|
|
}
|