refactor(margin): 抽出独立 Margin 限界上下文,移除 PrepayLeg 错误继承
- 新建 YLErp.Modules.SwapModule.Margin:MarginBalance / MarginLeg / IMarginResolver(Cash/Credit/Guarantee) - MarginLeg 不继承 IInterestLegStrategy,与利息腿完全解耦(保证金是客户存款/担保,非互换腿) - 删除 PrepayLeg,工厂不再注册 mode 5/6 预付金 - 预付金用例迁入 MarginLegTest,旧 InterestLegStrategyTest 改为断言预付金已非利息腿
This commit is contained in:
@@ -8,7 +8,8 @@ namespace YLErp.Modules.SwapModule.InterestLegs;
|
||||
/// 利息腿策略工厂。按 InterestMode 返回对应策略实例。
|
||||
/// 替换原 SwapDealService.CalcNotionalByMode 的 switch,收敛 mode 分发逻辑到一处。
|
||||
///
|
||||
/// 当前界面活跃 mode:1 固定值 / 2 合约名义本金规模 / 9 标的期初全价 / 5/6 预付金。
|
||||
/// 当前界面活跃 mode:1 固定值 / 2 合约名义本金规模 / 9 标的期初全价。
|
||||
/// 注:mode 5/6 预付金已迁出本上下文(见 YLErp.Modules.SwapModule.Margin / MarginLeg),不再作为利息腿注册。
|
||||
/// 死代码 mode 3/4 不注册;半死 mode 7/8 不注册(界面已注释)。
|
||||
/// 传入未注册的 mode 会抛异常,防止静默走默认分支。
|
||||
/// </summary>
|
||||
@@ -19,8 +20,6 @@ public static class InterestLegStrategyFactory
|
||||
[InterestModeEnum.固定值] = new FixedNotionalLeg(),
|
||||
[InterestModeEnum.合约名义本金规模] = new ContractNotionalLeg(),
|
||||
[InterestModeEnum.标的期初全价] = new UnderlyingFullPriceLeg(),
|
||||
[InterestModeEnum.初始预付金] = new PrepayLeg(InterestModeEnum.初始预付金),
|
||||
[InterestModeEnum.追加预付金] = new PrepayLeg(InterestModeEnum.追加预付金),
|
||||
};
|
||||
|
||||
/// <summary>按 mode 返回对应策略。未注册的 mode 抛 ArgumentException。</summary>
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
using YLErp.DBModels;
|
||||
|
||||
namespace YLErp.Modules.SwapModule.InterestLegs;
|
||||
|
||||
/// <summary>
|
||||
/// 预付金(保证金)利息腿。mode 5 初始预付金 / mode 6 追加预付金 共用。
|
||||
///
|
||||
/// 业务本质:预付金是客户"存"在券商的钱,券商对其付息(方向与普通利息腿相反)。
|
||||
/// 计息基数 = InterestPrincipalFix(预付金余额),随平仓递减。
|
||||
///
|
||||
/// 本类只封装 CalcNotionalByMode 的计息基数计算部分:
|
||||
/// closePrincipal = Fix × closePercent
|
||||
/// posiPrincipal = Fix
|
||||
///
|
||||
/// 预付金腿的其它独有逻辑(方向翻转、orginPv 重映射、衡泰路径返还置0)不在此处,
|
||||
/// 后续分别抽成独立方法,保持每个类/方法最小。
|
||||
/// </summary>
|
||||
public sealed class PrepayLeg : IInterestLegStrategy
|
||||
{
|
||||
// 预付金两个 mode(初始/追加)共用同一套计息基数公式,构造时指定
|
||||
private readonly InterestModeEnum _mode;
|
||||
public InterestModeEnum Mode => _mode;
|
||||
|
||||
public PrepayLeg(InterestModeEnum mode)
|
||||
{
|
||||
if (mode != InterestModeEnum.初始预付金 && mode != InterestModeEnum.追加预付金)
|
||||
throw new ArgumentException($"PrepayLeg 仅支持 初始预付金/追加预付金,收到 {mode}", nameof(mode));
|
||||
_mode = mode;
|
||||
}
|
||||
|
||||
public NotionalResult CalcNotional(decimal fix, decimal posiNotional, decimal posiLong, decimal posiShort, decimal closePercent)
|
||||
=> new(fix * closePercent, fix, closePercent);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace YLErp.Modules.SwapModule.Margin;
|
||||
|
||||
/// <summary>现金保证金:计息余额 = 现金余额。</summary>
|
||||
public sealed class CashMargin : IMarginResolver
|
||||
{
|
||||
public MarginForm Form => MarginForm.Cash;
|
||||
|
||||
public MarginBalance Resolve(decimal postedAmount, decimal accrualFactor = 1m)
|
||||
=> new(postedAmount * accrualFactor);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace YLErp.Modules.SwapModule.Margin;
|
||||
|
||||
/// <summary>授信保证:计息余额 = 已用授信额度。</summary>
|
||||
public sealed class CreditMargin : IMarginResolver
|
||||
{
|
||||
public MarginForm Form => MarginForm.Credit;
|
||||
|
||||
public MarginBalance Resolve(decimal postedAmount, decimal accrualFactor = 1m)
|
||||
=> new(postedAmount * accrualFactor);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace YLErp.Modules.SwapModule.Margin;
|
||||
|
||||
/// <summary>担保品:计息余额 = 担保品市值。</summary>
|
||||
public sealed class GuaranteeMargin : IMarginResolver
|
||||
{
|
||||
public MarginForm Form => MarginForm.Guarantee;
|
||||
|
||||
public MarginBalance Resolve(decimal postedAmount, decimal accrualFactor = 1m)
|
||||
=> new(postedAmount * accrualFactor);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace YLErp.Modules.SwapModule.Margin;
|
||||
|
||||
/// <summary>保证金形态:现金 / 授信 / 担保。预留扩展。</summary>
|
||||
public enum MarginForm
|
||||
{
|
||||
/// <summary>现金保证金:计息余额 = 现金余额。</summary>
|
||||
Cash,
|
||||
/// <summary>授信保证:计息余额 = 已用授信额度。</summary>
|
||||
Credit,
|
||||
/// <summary>担保品:计息余额 = 担保品市值。</summary>
|
||||
Guarantee,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按保证金形态解析计息余额。三种形态可互换地产出一个 MarginBalance(满足 LSP),
|
||||
/// 这是保证金领域唯一合理的多态点(差异仅在"余额如何取得"),不同于错误地套用利息腿接口。
|
||||
/// 当前为铺设骨架,具体余额来源(资金流水 / 授信占用 / 担保估值)后续按形态填充。
|
||||
/// </summary>
|
||||
public interface IMarginResolver
|
||||
{
|
||||
MarginForm Form { get; }
|
||||
|
||||
MarginBalance Resolve(decimal postedAmount, decimal accrualFactor = 1m);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace YLErp.Modules.SwapModule.Margin;
|
||||
|
||||
/// <summary>
|
||||
/// 保证金计息余额。兼容现金、授信、担保等多种保证金形态,不承载 swap 平仓语义。
|
||||
/// 仅表达"本期计息的余额"这一计息基数,与利息腿的 NotionalResult(平仓/持仓本金拆分)无关。
|
||||
/// </summary>
|
||||
public readonly struct MarginBalance
|
||||
{
|
||||
/// <summary>计息余额:现金余额 / 授信占用 / 担保品市值。</summary>
|
||||
public decimal Balance { get; }
|
||||
|
||||
public MarginBalance(decimal balance)
|
||||
=> Balance = balance;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace YLErp.Modules.SwapModule.Margin;
|
||||
|
||||
/// <summary>
|
||||
/// 保证金(预付金)利息计提。独立限界上下文,不继承也不关联任何利息腿
|
||||
/// (IInterestLegStrategy / FixedNotionalLeg 等)——保证金是客户存款/担保,并非互换腿,
|
||||
/// 套用利息腿抽象是此前的类别错误(category error)。
|
||||
///
|
||||
/// 本类只负责把"保证金余额"这一计息基数正确建模与计算。方向翻转、orginPv 维度纠正、
|
||||
/// 持仓对齐等预付金独有逻辑不在本类(原散落在 SwapDealService / SwapEodPositionService),
|
||||
/// 后续按各自独立方法迁移,保持本类最小、可单测。
|
||||
/// </summary>
|
||||
public sealed class MarginLeg
|
||||
{
|
||||
/// <summary>
|
||||
/// 计算计息余额。
|
||||
/// </summary>
|
||||
/// <param name="marginBalance">保证金余额(现金余额 / 授信占用 / 担保品市值)。</param>
|
||||
/// <param name="accrualFactor">本期计息适用比例,默认 1(不缩放)。仅部分释放等场景小于 1。</param>
|
||||
/// <returns>计息基数,不含利率与日计数。</returns>
|
||||
public MarginBalance CalcBalance(decimal marginBalance, decimal accrualFactor = 1m)
|
||||
=> new(marginBalance * accrualFactor);
|
||||
}
|
||||
Reference in New Issue
Block a user