diff --git a/UnitTestProject/Modules/SwapModule/Margin/MarginLegTest.cs b/UnitTestProject/Modules/SwapModule/Margin/MarginLegTest.cs index 80f2330e..156eb932 100644 --- a/UnitTestProject/Modules/SwapModule/Margin/MarginLegTest.cs +++ b/UnitTestProject/Modules/SwapModule/Margin/MarginLegTest.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using YLErp.Derivatives.Interest; using YLErp.Modules.SwapModule.Margin; namespace UnitTestProject.Modules.SwapModule.Margin @@ -66,5 +67,55 @@ namespace UnitTestProject.Modules.SwapModule.Margin } #endregion + + #region MarginAccount 计息 + + [TestMethod] + public void 计息_单利7天_余额200万年化3pct() + { + var account = new MarginAccount(new MarginBalance(2_000_000m)); + // 200万 × 3% / 365 × 7天 = 1150.68... + var r = account.AccrueInterest( + rate: 0.03m, + startDate: new System.DateTime(2026, 5, 4), + endDate: new System.DateTime(2026, 5, 11), + boundary: AccrualBoundary.StartOnly, + annualDays: 365); + + Assert.IsTrue(r.Accrued > 0, "7天利息应大于0"); + System.Console.WriteLine($"保证金7天利息={r.Accrued}"); + } + + [TestMethod] + public void 计息_零余额_利息为零() + { + var account = new MarginAccount(new MarginBalance(0m)); + var r = account.AccrueInterest(0.03m, + new System.DateTime(2026, 5, 4), new System.DateTime(2026, 5, 11), + AccrualBoundary.StartOnly, 365); + + Assert.AreEqual(0m, r.Accrued); + } + + [TestMethod] + public void 计息_释放后余额减少_利息相应减少() + { + var full = new MarginAccount(new MarginBalance(2_000_000m)); + var half = new MarginAccount(new MarginBalance(2_000_000m)); + half.Withdraw(1_000_000m); + + var rFull = full.AccrueInterest(0.03m, + new System.DateTime(2026, 5, 4), new System.DateTime(2026, 5, 11), + AccrualBoundary.StartOnly, 365); + var rHalf = half.AccrueInterest(0.03m, + new System.DateTime(2026, 5, 4), new System.DateTime(2026, 5, 11), + AccrualBoundary.StartOnly, 365); + + Assert.IsTrue(rHalf.Accrued < rFull.Accrued, "释放后利息应更少"); + Assert.IsTrue(System.Math.Abs(rFull.Accrued - rHalf.Accrued * 2m) < 0.01m, + "余额减半, 利息也应减半"); + } + + #endregion } } diff --git a/YLErpDAL/Modules/SwapModule/Margin/MarginAccount.cs b/YLErpDAL/Modules/SwapModule/Margin/MarginAccount.cs index 504bead1..57dc508f 100644 --- a/YLErpDAL/Modules/SwapModule/Margin/MarginAccount.cs +++ b/YLErpDAL/Modules/SwapModule/Margin/MarginAccount.cs @@ -1,12 +1,16 @@ +using YLErp.Derivatives.Interest; + namespace YLErp.Modules.SwapModule.Margin; /// -/// 保证金账户。管理保证金余额的变动(追加/释放/返还),不涉及利息计算—— -/// 利息由 SwapInterest 纯函数按 余额×利率×天数/年化 计算,保证金账户只提供余额。 +/// 保证金账户。管理保证金余额的变动(追加/释放/返还),并提供计息入口。 /// /// 保证金是独立的资金管理概念(初始保证金/维持保证金/保证金余额/追保), /// 与融资腿(funding leg)完全无关。现有代码把保证金塞进 InterestMode==5/6 /// 当计息腿处理是错误的,本类是正确建模的起点。 +/// +/// 利息计算委托 SwapInterest 纯函数(余额×利率×天数/年化), +/// 保证金账户只提供余额和计息入口,不自己实现计息算法。 /// public sealed class MarginAccount { @@ -23,4 +27,16 @@ public sealed class MarginAccount /// 释放/返还保证金(余额减少,不低于 0)。 public void Withdraw(decimal amount) => Balance = new MarginBalance(Math.Max(0m, Balance.Balance - amount)); + + /// + /// 按当前余额计算保证金利息。委托 SwapInterest.AccrueSimple。 + /// 保证金利息是券商对客户保证金存款付息(方向与融资腿相反)。 + /// + /// 保证金利率(年化,如 0.03 = 3%)。 + /// 计息开始日。 + /// 计息结束日。 + /// 算头算尾规则。 + /// 年化天数(365 或 360)。 + public InterestResult AccrueInterest(decimal rate, System.DateTime startDate, System.DateTime endDate, AccrualBoundary boundary, int annualDays) + => SwapInterest.AccrueSimple(Balance.Balance, rate, startDate, endDate, boundary, annualDays); }