为拆三类腿做准备,按 Strategy 模式封装 CalcNotionalByMode 的 switch。 本次零生产代码改动,全部为新增小文件,每个类单一职责<50行。 新增(策略接口+值对象): - InterestLegs/IInterestLegStrategy.cs NotionalResult 值对象(closePrincipal/posiPrincipal/closePercent) + IInterestLegStrategy 接口(每个mode一个实现) 新增(3个活跃利息腿mode): - InterestLegs/FixedNotionalLeg.cs mode 1 固定值 计息基数恒=InterestPrincipalFix, 不随平仓比例变化(合同写死的固定值) - InterestLegs/ContractNotionalLeg.cs mode 2 合约名义本金规模 平仓本金=posiNotional×closePercent, 按比例线性缩放 - InterestLegs/UnderlyingFullPriceLeg.cs mode 9 标的期初全价 主路径公式同mode2, 差异在衡泰路径grossPrice折算+EOD复利反推 确认现状: - 界面实际只有3个活跃利息腿mode(1/2/9), TradeView.cshtml:330-349 - mode 3(持仓名义本金)/4(持仓市值) 零引用=死代码, 本次不实现 - mode 7/8(多空存续) 界面已注释掉, 本次不实现 - mode 5/6(预付金) 属保证金维度, 后续单独做PrepayLeg 新增测试(8个,全过): - InterestLegStrategyTest.cs 覆盖各mode的部分平仓/全平/零平仓场景 验证策略行为与现有CalcNotionalByMode switch完全一致
47 lines
2.2 KiB
C#
47 lines
2.2 KiB
C#
using YLErp.DBModels;
|
||
|
||
namespace YLErp.Modules.SwapModule.InterestLegs;
|
||
|
||
/// <summary>
|
||
/// 计息腿策略的计算结果。对应原 CalcNotionalByMode 返回的三元组,用自描述名。
|
||
/// </summary>
|
||
public readonly struct NotionalResult
|
||
{
|
||
/// <summary>本次平仓部分的计息本金。</summary>
|
||
public decimal ClosePrincipal { get; }
|
||
|
||
/// <summary>存续持仓部分的计息本金(全额,不缩放)。</summary>
|
||
public decimal PosiPrincipal { get; }
|
||
|
||
/// <summary>有效平仓比例。固定值腿恒为 1(计息基数不随比例变);其余沿用入参。</summary>
|
||
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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 利息腿计息基数策略。每个 InterestMode 一个实现,替换原 CalcNotionalByMode 的 switch。
|
||
///
|
||
/// 职责单一:给定持仓参数与平仓比例,算出"平仓本金 / 存续本金 / 有效比例"。
|
||
/// 不做计息(计息由 SwapInterest 纯函数完成),不取价(取价由 IIndexFixer 完成)。
|
||
/// </summary>
|
||
public interface IInterestLegStrategy
|
||
{
|
||
/// <summary>该策略对应的计息模式。</summary>
|
||
InterestModeEnum Mode { get; }
|
||
|
||
/// <summary>
|
||
/// 根据持仓参数与平仓比例计算计息本金三元组。
|
||
/// </summary>
|
||
/// <param name="fix">合约固定本金(固定值/预付金腿用;其余腿忽略)。</param>
|
||
/// <param name="posiNotional">当前剩余名义本金(数量 × 全价)。</param>
|
||
/// <param name="posiLong">多头剩余名义本金(多空存续腿用,当前界面已禁用)。</param>
|
||
/// <param name="posiShort">空头剩余名义本金。</param>
|
||
/// <param name="closePercent">平仓比例(占剩余,0~1)。</param>
|
||
NotionalResult CalcNotional(decimal fix, decimal posiNotional, decimal posiLong, decimal posiShort, decimal closePercent);
|
||
}
|