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

92 lines
4.2 KiB
C#

using YLErp.Abstract.DataProviders;
using YLErp.BLL.Eod;
using YLErp.BLL.Hedge;
using YLErp.Modules.CalculationModule.Abstract;
namespace YLErp.Modules.CalculationModule
{
/// <summary>
/// 场内期权到期pnl计算
/// </summary>
public class MaturityOptionHedgePnlCalc
{
readonly IHedgePnlCalcContext _context;
readonly IUnderlyingDataProvider _unDataProvider;
readonly IPriceProvider _unPriceProvider;
public MaturityOptionHedgePnlCalc(IHedgePnlCalcContext context)
{
_context = CalcCheckHelper.CheckHedgePnlCalcContext(context);
_unDataProvider = context.UnderlyingDataProvider;
_unPriceProvider = context.UnderlyingPriceProvider;
}
/// <summary>
/// 到期hedgePnl计算
/// </summary>
/// <param name="hedgePnl"></param>
/// <returns></returns>
public HedgePnl Calculate(HedgePnl hedgePnl, OptUserInfo UserInfo)
{
if (hedgePnl.TradeType == "场内期权" && hedgePnl.Notional != 0)
{
var exchangeOption = _unDataProvider.GetExchange_List_Option(hedgePnl.ExchangeOptionCode)?.Clone();
if (exchangeOption == null)
{
throw new Exception(String.Format("未找到合约代码为【{0}】的场内期权信息", hedgePnl.ExchangeOptionCode));
}
if (exchangeOption != null && exchangeOption.MaturityDate == _context.ValueDate)
{
var tempUm = _unDataProvider.GetUnderlying(hedgePnl.UnderlyingCode);
var cost = (double)hedgePnl.Cost;
var exchangeTrade = new ExchangeTrade()
{
TradeType = hedgePnl.TradeType,
UnderlyingCode = hedgePnl.UnderlyingCode,
UnderlyingId = hedgePnl.UnderlyingId,
OptionCode = hedgePnl.ExchangeOptionCode,
AssetBookId = hedgePnl.BookId,
TradeDate = _context.ValueDate,
TradeLots = Math.Abs(hedgePnl.Lots),
Notional = Math.Abs(hedgePnl.Notional),
TradeAmount = Math.Abs(hedgePnl.Notional) / tempUm.CountRatio,
TradeSide = hedgePnl.PositionType == "long" ? "多头平仓" : "空头平仓",
TradeSinglePrice = 0,
InstrumentType = tempUm.UnderlyingInstrumentType,
CreateTime = DateTime.Now,
MaturityDate = exchangeOption.MaturityDate,
OptionStrike = exchangeOption.Strike,
OptionType = exchangeOption.OptionType,
ExerciseMode = exchangeOption.ExerciseMode.TrimToNull() ?? "European",
IsValid = true,
OptDate = DateTime.Now,
OptId = UserInfo.UserId,
OptName = UserInfo.UserName,
TradeSource = "系统交易",
TradeNumber = DateTime.Now.ToString("yyyyMMddHHmmssfff")
};
var realPnl = cost * EodOperationBase.GetSign(exchangeTrade.TradeSide);
hedgePnl.RealizedPnL += realPnl;
hedgePnl.DailyPnL = -hedgePnl.LastPv - hedgePnl.TdCost;//重新计算
hedgePnl.TotalPnl = hedgePnl.LastTotalPnl + hedgePnl.DailyPnL;
hedgePnl.Cost = 0;
hedgePnl.Notional = 0;
hedgePnl.Pv = 0;
hedgePnl.Delta = 0;
hedgePnl.DeltaCash = 0;
hedgePnl.Gamma = 0;
hedgePnl.GammaCash = 0;
hedgePnl.Vega = 0;
hedgePnl.Theta = 0;
hedgePnl.Rho = 0;
hedgePnl.Vol = 0;
using (var db = DbContextFactory.GetYLDbContext())
{
db.ExchangeTrade.Add(exchangeTrade);
db.SaveChanges();
}
}
}
return hedgePnl;
}
}
}