feat(swap): 实现预付金利息腿实时剩余本金计算功能
- 新增 ResolveInterestLegPositionsAsOf 方法处理预付金利息腿实时本金计算 - 添加 FindCompletedFlowEvents 方法查询已完成的现金流事件 - 在日终持仓服务中集成实时剩余本金计算逻辑 - 移除日终重复扣减预付金本金的逻辑 - 优化利息端估值计算方式 - 添加部分平仓后预付金日终按实时剩余本金计息的测试用例 - 增加平仓日预付金日终不得重复扣减实时剩余本金的验证 - 完善历史回放场景下的本金调整功能
This commit is contained in:
@@ -665,6 +665,58 @@ namespace YLErp.Modules.SwapModule
|
||||
return p;
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算预付金腿当前真实持仓 (当前持仓+未来持仓)
|
||||
/// </summary>
|
||||
/// <param name="origPositions"></param>
|
||||
/// <param name="realPositions"></param>
|
||||
/// <param name="completedFlowEvents"></param>
|
||||
/// <param name="settleDate"></param>
|
||||
/// <returns></returns>
|
||||
public static List<swap_position> ResolveInterestLegPositionsAsOf(
|
||||
List<swap_position> origPositions, List<swap_position> realPositions,
|
||||
IEnumerable<swap_flow_event> completedFlowEvents, DateTime settleDate)
|
||||
{
|
||||
realPositions ??= new List<swap_position>();
|
||||
var futureFlows = (completedFlowEvents ?? Enumerable.Empty<swap_flow_event>())
|
||||
.Where(x => x.EventType == (int)SwapEventTypeEnum.平仓 && x.EventDate > settleDate)
|
||||
.ToList();
|
||||
var originalNotional = origPositions.Where(x => x.PosiDirection > 0)
|
||||
.Sum(x => x.PosiNotionalValue);
|
||||
var futureCloseNotional = futureFlows.Where(x => x.PositionType > 0)
|
||||
.Sum(x => x.TradingAmount);
|
||||
var hasNotionalFlows = futureCloseNotional > 0 && originalNotional > 0;
|
||||
var futureClosePrincipal = futureFlows
|
||||
.Where(x => x.InterestMode == (int)InterestModeEnum.初始预付金
|
||||
|| x.InterestMode == (int)InterestModeEnum.追加预付金)
|
||||
.GroupBy(x => x.PositionId)
|
||||
.ToDictionary(x => x.Key, x => x.Sum(v => v.InterestPrincipal));
|
||||
|
||||
return origPositions.Where(x => x.PosiDirection == 0).Select(p =>
|
||||
{
|
||||
if (p.InterestMode == (int)InterestModeEnum.初始预付金
|
||||
|| p.InterestMode == (int)InterestModeEnum.追加预付金)
|
||||
{
|
||||
var realLeg = realPositions.FirstOrDefault(r => r.PositionId == p.id);
|
||||
if (realLeg != null)
|
||||
{
|
||||
var futurePrincipal = hasNotionalFlows
|
||||
? p.InterestPrincipalFix * futureCloseNotional / originalNotional
|
||||
: futureClosePrincipal.TryGetValue(p.id, out var flowPrincipal) ? flowPrincipal : 0m;
|
||||
var asOfPrincipal = realLeg.InterestPrincipalFix + futurePrincipal;
|
||||
asOfPrincipal = Math.Min(p.InterestPrincipalFix, Math.Max(0m, asOfPrincipal));
|
||||
if (asOfPrincipal != p.InterestPrincipalFix)
|
||||
{
|
||||
var clone = p.Clone();
|
||||
clone.InterestPrincipalFix = asOfPrincipal;
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public static decimal ResolveUnwindPreviousNotional(
|
||||
eod_swap lastEod,
|
||||
|
||||
@@ -307,6 +307,14 @@ namespace YLErp.Modules.SwapModule
|
||||
return DbContext.swap_flow_event.Where(eventExpression).ToList();
|
||||
}
|
||||
|
||||
protected virtual List<swap_flow_event> FindCompletedFlowEvents(List<int> tradeIds)
|
||||
{
|
||||
return DbContext.swap_flow_event
|
||||
.Where(x => tradeIds.Contains(x.SwapTradeId)
|
||||
&& x.DataState == (int)SwapFlowDateStateEnum.完成)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
@@ -352,6 +360,7 @@ namespace YLErp.Modules.SwapModule
|
||||
var tradeRealPositionList = allTradePositionList.Where(t => !t.IsInitial).ToList();
|
||||
var tradeExtendList = FindTradeExtends(tradeIds);
|
||||
var eodSwapList = FindEodSwapsByDate(preSettleDate);
|
||||
var completedFlowEvents = FindCompletedFlowEvents(tradeIds);
|
||||
List<int> eventTyps = new List<int>() { (int)SwapEventTypeEnum.平仓, (int)SwapEventTypeEnum.互换, (int)SwapEventTypeEnum.自动互换 };
|
||||
foreach (var td in tradeQueryList)
|
||||
{
|
||||
@@ -364,7 +373,10 @@ namespace YLErp.Modules.SwapModule
|
||||
var realPositions = tradeRealPositionList.Where(s => s.SwapTradeId == td.id);
|
||||
var posiList = positions.Where(x => x.PosiQuantity > 0).ToList();
|
||||
var realPosiList = realPositions.ToList();
|
||||
var interestList = positions.Where(x => x.InterestDirection > 0).ToList();
|
||||
var tradeCompletedFlowEvents = completedFlowEvents.Where(x => x.SwapTradeId == td.id).ToList();
|
||||
var interestList = SwapDealService.ResolveInterestLegPositionsAsOf(
|
||||
positions.ToList(), realPosiList, tradeCompletedFlowEvents, settleDate)
|
||||
.Where(x => x.InterestDirection > 0).ToList();
|
||||
DateTime posiDate = td.TradeDate.Value;//交易日期
|
||||
var lastEodSwap = eodSwapList.FirstOrDefault(x => x.SwapTradeId == td.id);
|
||||
//上一交易日无日终归档,且不是交易日期,且当前收盘日期不是交易日期,报错
|
||||
@@ -1351,8 +1363,9 @@ namespace YLErp.Modules.SwapModule
|
||||
//持仓内容-利息腿
|
||||
newEodPayPosition.InterestDirection = position.InterestDirection;
|
||||
newEodPayPosition.InterestMode = position.InterestMode;
|
||||
// ResolveInterestLegPositions 已提供平仓后的实时剩余本金,日终不再重复扣减。
|
||||
newEodPayPosition.InterestPrincipalFix = position.InterestPrincipalFix;
|
||||
newEodPayPosition.InterestPrincipalFix *= (1 - closePercent);
|
||||
// newEodPayPosition.InterestPrincipalFix *= (1 - closePercent);
|
||||
newEodPayPosition.InterestRateDefault = position.InterestRateDefault;
|
||||
newEodPayPosition.InterestSwapInterval = position.InterestSwapInterval;
|
||||
newEodPayPosition.IsAnnualized = position.IsAnnualized;
|
||||
@@ -1364,9 +1377,11 @@ namespace YLErp.Modules.SwapModule
|
||||
newEodPayPosition.interest_rest_days = position.interest_rest_days;
|
||||
newEodPayPosition.interest_rule = position.interest_rule;
|
||||
//利息端估值用信息
|
||||
newEodPayPosition.TdInterestPrincipal = position.InterestMode == (int)InterestModeEnum.标的期初全价
|
||||
? posiNotionalValue
|
||||
: interests.Count > 0 ? interests.First().InterestPrincipal : 0;
|
||||
newEodPayPosition.TdInterestPrincipal = interestModes.Contains(position.InterestMode)
|
||||
? position.InterestPrincipalFix
|
||||
: position.InterestMode == (int)InterestModeEnum.标的期初全价
|
||||
? posiNotionalValue
|
||||
: interests.Count > 0 ? interests.First().InterestPrincipal : 0;
|
||||
if (interval != null)
|
||||
{
|
||||
newEodPayPosition.TdInterestRate = interval.Rate;
|
||||
|
||||
Reference in New Issue
Block a user