Files
zszq-trs/YLErpDAL/Modules/TradeRiskCalcModule/DongZhengDeltaT1Service.cs
2024-05-09 14:06:26 +08:00

105 lines
5.0 KiB
C#

using YLErp.Modules.CalculationModule;
using YLErp.Modules.TradeRiskCalcModule.TaskRunner;
namespace YLErp.Modules.TradeRiskCalcModule
{
public class DongZhengDeltaT1Service
{
public void ReCalcDeltaT1(TradingRiskResult curResult, DateTime valueTime)
{
var tradeSource = TradeRiskCalcTaskRunner.TradeDataSource.GetOtcTrades();
foreach (var item in curResult.TradeRiskList)
{
if (item.viewList == null)
{
continue;
}
item.DeltaT1 = 0;
item.DeltaT1Lots = 0;
foreach (var item2 in item.viewList)
{
trade td = new trade();
if (item2.TradeType == "场内期权")
{
var tempUm = DataCacheProvider.GetUnderlyingDataSource().GetData(item2.UnderlyingCode);
var exchangeOption = DataCacheProvider.GetExchangeListOptionDataSource().GetData(item2.ExchangeOptionCode);
td = new trade
{
TradeType = item2.TradeType,
UnderlyingCode = item2.UnderlyingCode,
UnderlyingId = item2.UnderlyingId ?? 0,
TradeDate = valueTime.Date,
BuySell = item2.BuySell,
StartDate = valueTime.Date,
ExerciseDate = item2.ExerciseDate,
MaturityDate = tempUm.MaturityDate,
TradePrice = Math.Abs(item2.Cost),
TradeStatus = "确认成交",
ExerciseMode = exchangeOption.ExerciseMode,
OptionType = exchangeOption.OptionType,
Strike = item2.Strike,
Notional = item2.Notional,
UnderlyingInstrumentType = tempUm.UnderlyingInstrumentType,
ExchangeOptionCode = item2.ExchangeOptionCode,
AssetId = item2.BookId,
id = 0,
UnderlyingAssetClass = tempUm.UnderlyingType,
NoRiskRate = BLL.valuedateBLL.SysRiskFreeRate(),
DividendRate = tempUm.DividendRate ?? BLL.valuedateBLL.SysRiskFreeRate(),
};
}
else
{
td = tradeSource.FirstOrDefault(n => n.id == item2.TradeId);
}
if (td != null)
{
var un = DataCacheProvider.GetUnderlyingDataSource().GetData(item2.UnderlyingCode);
if (new List<string>() { "远期", "商品期货", "股票", "自定义交易", "收益互换" }.Contains(td.TradeType))
{
item2.DeltaT1 = item2.Delta;
}
else
{
var req = new OptionValueCalcRequest(item2.NoRiskRate ?? BLL.valuedateBLL.SysRiskFreeRate())
{
calcScenario = Enums.CalcScenarioEnum.RealtimeRisk,
pricingRequest = Qdp.Pricing.Base.Implementations.PricingRequest.Delta,
spotPrices = new[] { item2.SpotPrice ?? 0 },
vols = new[] { item2.Vol },
timeToMaturityDays = TradeCalcHelper.CalculateTTMDays(valueTime.Date, td.ExerciseDate.Value, un?.UnderlyingTypeId ?? 0, false, serverDateTime: valueTime)
};
if (td.TradeType == "亚式期权")
{
req.fixings = AsiaOptionProvider.Default.GetFixingString(valueTime.Date, td, td.trade_asian_option);
}
try
{
if (valueTime > td.ExerciseDate)
{
item2.DeltaT1 = 0;
}
else
{
var result = OptionCalculatorV2.GetOptionValueResult(valueTime.Date, td, req, out _);
item2.DeltaT1 = result.Delta;
}
}
catch { }
}
item2.DeltaT1Lots = un == null || un.ContractSize < 1 ? item2.DeltaT1 : item2.DeltaT1 / un.ContractSize;
}
item.DeltaT1 += (item2.DeltaT1 ?? 0);
item.DeltaT1Lots += (item2.DeltaT1Lots ?? 0);
}
}
}
}
}