diff --git a/UnitTestProject/Modules/SwapModule/InterestLegs/InterestLegStrategyTest.cs b/UnitTestProject/Modules/SwapModule/InterestLegs/InterestLegStrategyTest.cs new file mode 100644 index 00000000..9ec52504 --- /dev/null +++ b/UnitTestProject/Modules/SwapModule/InterestLegs/InterestLegStrategyTest.cs @@ -0,0 +1,114 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YLErp.DBModels.Enums; +using YLErp.Modules.SwapModule.InterestLegs; + +namespace UnitTestProject.Modules.SwapModule.InterestLegs +{ + /// + /// 利息腿策略单测。验证每个策略的 CalcNotional 与现有 CalcNotionalByMode switch 完全一致。 + /// 这组测试是后续"迁移调用点"的安全网——迁移前后行为必须不变。 + /// + [TestClass] + public class InterestLegStrategyTest + { + private const decimal Fix = 2_000_000m; // 合约固定本金 + private const decimal Notional = 100_000_000m; // 剩余名义本金 1 亿 + private const decimal LongNotional = 60_000_000m; + private const decimal ShortNotional = 40_000_000m; + + #region 固定值(mode 1):恒=Fix,不随比例变 + + [TestMethod] + public void 固定值_部分平仓_计息基数恒等于Fix() + { + var leg = new FixedNotionalLeg(); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0.5m); + + Assert.AreEqual(Fix, r.ClosePrincipal, "平仓本金恒=Fix"); + Assert.AreEqual(Fix, r.PosiPrincipal, "持仓本金恒=Fix"); + Assert.AreEqual(1m, r.ClosePercent, "有效比例恒=1(固定值不随比例缩放)"); + } + + [TestMethod] + public void 固定值_全平_计息基数仍等于Fix() + { + var leg = new FixedNotionalLeg(); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 1m); + + Assert.AreEqual(Fix, r.ClosePrincipal, "全平本金仍=Fix"); + } + + #endregion + + #region 合约名义本金规模(mode 2):按比例线性缩放 + + [TestMethod] + public void 合约名义本金_部分平仓_本金按比例缩放() + { + var leg = new ContractNotionalLeg(); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0.5m); + + Assert.AreEqual(50_000_000m, r.ClosePrincipal, "平仓本金=Notional×50%"); + Assert.AreEqual(Notional, r.PosiPrincipal, "持仓本金=Notional全额"); + Assert.AreEqual(0.5m, r.ClosePercent, "有效比例=入参"); + } + + [TestMethod] + public void 合约名义本金_全平_本金等于全额() + { + var leg = new ContractNotionalLeg(); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 1m); + + Assert.AreEqual(Notional, r.ClosePrincipal, "全平本金=Notional"); + } + + [TestMethod] + public void 合约名义本金_零平仓_本金为零() + { + var leg = new ContractNotionalLeg(); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0m); + + Assert.AreEqual(0m, r.ClosePrincipal, "零平仓本金=0"); + Assert.AreEqual(Notional, r.PosiPrincipal, "持仓本金仍=Notional"); + } + + #endregion + + #region 标的期初全价(mode 9):主路径公式与mode2相同 + + [TestMethod] + public void 标的期初全价_部分平仓_主路径公式同mode2() + { + var leg = new UnderlyingFullPriceLeg(); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0.5m); + + Assert.AreEqual(50_000_000m, r.ClosePrincipal, "平仓本金=Notional×50%(与mode2主路径一致)"); + Assert.AreEqual(Notional, r.PosiPrincipal); + Assert.AreEqual(0.5m, r.ClosePercent); + } + + [TestMethod] + public void 标的期初全价_全平_本金等于全额() + { + var leg = new UnderlyingFullPriceLeg(); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 1m); + + Assert.AreEqual(Notional, r.ClosePrincipal); + } + + #endregion + + #region 守卫:三个策略对应不同mode + + [TestMethod] + public void 三个策略对应不同枚举值() + { + Assert.AreEqual(InterestModeEnum.固定值, new FixedNotionalLeg().Mode); + Assert.AreEqual(InterestModeEnum.合约名义本金规模, new ContractNotionalLeg().Mode); + Assert.AreEqual(InterestModeEnum.标的期初全价, new UnderlyingFullPriceLeg().Mode); + } + + #endregion + } +} diff --git a/YLErpDAL/Modules/SwapModule/InterestLegs/ContractNotionalLeg.cs b/YLErpDAL/Modules/SwapModule/InterestLegs/ContractNotionalLeg.cs new file mode 100644 index 00000000..c2dff484 --- /dev/null +++ b/YLErpDAL/Modules/SwapModule/InterestLegs/ContractNotionalLeg.cs @@ -0,0 +1,16 @@ +using YLErp.DBModels; + +namespace YLErp.Modules.SwapModule.InterestLegs; + +/// +/// 合约名义本金规模利息腿(InterestMode=合约名义本金规模)。 +/// 站在"合约规模"视角:平仓本金 = 剩余名义本金 × 平仓比例。 +/// 与标的期初全价(9)在 CalcNotionalByMode 里公式相同,差异在衡泰路径 grossPrice 折算和 EOD 复利反推。 +/// +public sealed class ContractNotionalLeg : IInterestLegStrategy +{ + public InterestModeEnum Mode => InterestModeEnum.合约名义本金规模; + + public NotionalResult CalcNotional(decimal fix, decimal posiNotional, decimal posiLong, decimal posiShort, decimal closePercent) + => new(posiNotional * closePercent, posiNotional, closePercent); +} diff --git a/YLErpDAL/Modules/SwapModule/InterestLegs/FixedNotionalLeg.cs b/YLErpDAL/Modules/SwapModule/InterestLegs/FixedNotionalLeg.cs new file mode 100644 index 00000000..c77c8203 --- /dev/null +++ b/YLErpDAL/Modules/SwapModule/InterestLegs/FixedNotionalLeg.cs @@ -0,0 +1,16 @@ +using YLErp.DBModels; + +namespace YLErp.Modules.SwapModule.InterestLegs; + +/// +/// 固定值利息腿(InterestMode=固定值)。 +/// 计息基数由合约约定(InterestPrincipalFix),无论平仓比例多少都恒等于该值。 +/// 业务规则:固定值就是合同写死的固定值,永远不变。 +/// +public sealed class FixedNotionalLeg : IInterestLegStrategy +{ + public InterestModeEnum Mode => InterestModeEnum.固定值; + + public NotionalResult CalcNotional(decimal fix, decimal posiNotional, decimal posiLong, decimal posiShort, decimal closePercent) + => new(fix, fix, 1m); +} diff --git a/YLErpDAL/Modules/SwapModule/InterestLegs/IInterestLegStrategy.cs b/YLErpDAL/Modules/SwapModule/InterestLegs/IInterestLegStrategy.cs new file mode 100644 index 00000000..b3e640b2 --- /dev/null +++ b/YLErpDAL/Modules/SwapModule/InterestLegs/IInterestLegStrategy.cs @@ -0,0 +1,46 @@ +using YLErp.DBModels; + +namespace YLErp.Modules.SwapModule.InterestLegs; + +/// +/// 计息腿策略的计算结果。对应原 CalcNotionalByMode 返回的三元组,用自描述名。 +/// +public readonly struct NotionalResult +{ + /// 本次平仓部分的计息本金。 + public decimal ClosePrincipal { get; } + + /// 存续持仓部分的计息本金(全额,不缩放)。 + public decimal PosiPrincipal { get; } + + /// 有效平仓比例。固定值腿恒为 1(计息基数不随比例变);其余沿用入参。 + public decimal ClosePercent { get; } + + public NotionalResult(decimal closePrincipal, decimal posiPrincipal, decimal closePercent) + => (ClosePrincipal, PosiPrincipal, ClosePercent) = (closePrincipal, posiPrincipal, closePercent); + + public void Deconstruct(out decimal close, out decimal posi, out decimal pct) + => (close, posi, pct) = (ClosePrincipal, PosiPrincipal, ClosePercent); +} + +/// +/// 利息腿计息基数策略。每个 InterestMode 一个实现,替换原 CalcNotionalByMode 的 switch。 +/// +/// 职责单一:给定持仓参数与平仓比例,算出"平仓本金 / 存续本金 / 有效比例"。 +/// 不做计息(计息由 SwapInterest 纯函数完成),不取价(取价由 IIndexFixer 完成)。 +/// +public interface IInterestLegStrategy +{ + /// 该策略对应的计息模式。 + InterestModeEnum Mode { get; } + + /// + /// 根据持仓参数与平仓比例计算计息本金三元组。 + /// + /// 合约固定本金(固定值/预付金腿用;其余腿忽略)。 + /// 当前剩余名义本金(数量 × 全价)。 + /// 多头剩余名义本金(多空存续腿用,当前界面已禁用)。 + /// 空头剩余名义本金。 + /// 平仓比例(占剩余,0~1)。 + NotionalResult CalcNotional(decimal fix, decimal posiNotional, decimal posiLong, decimal posiShort, decimal closePercent); +} diff --git a/YLErpDAL/Modules/SwapModule/InterestLegs/UnderlyingFullPriceLeg.cs b/YLErpDAL/Modules/SwapModule/InterestLegs/UnderlyingFullPriceLeg.cs new file mode 100644 index 00000000..486695c5 --- /dev/null +++ b/YLErpDAL/Modules/SwapModule/InterestLegs/UnderlyingFullPriceLeg.cs @@ -0,0 +1,18 @@ +using YLErp.DBModels; + +namespace YLErp.Modules.SwapModule.InterestLegs; + +/// +/// 标的期初全价利息腿(InterestMode=标的期初全价)。 +/// 站在"持仓全价"视角:计息基数 = 标的含费全价(PosiGrossPrice/EntryDirtyPrice) × 数量。 +/// 主路径 CalcNotionalByMode 公式与合约名义本金规模(2)相同; +/// 差异在衡泰路径会乘 grossPrice 折算(SwapDealService.GetUnwindInterestsByHT), +/// 以及 EOD 复利部分平仓后直接返回剩余本金(禁止反推,SwapEodPositionService:1458-1465)。 +/// +public sealed class UnderlyingFullPriceLeg : IInterestLegStrategy +{ + public InterestModeEnum Mode => InterestModeEnum.标的期初全价; + + public NotionalResult CalcNotional(decimal fix, decimal posiNotional, decimal posiLong, decimal posiShort, decimal closePercent) + => new(posiNotional * closePercent, posiNotional, closePercent); +}