Live cash sync on a non-trading day double counts unsettled sale proceeds on a cash account
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with Brokerages/Brokerage.cs, especially ShouldPerformCashSync and PerformCashSync, then read Common/Securities/DelayedSettlementModel.cs and Common/Securities/SecurityPortfolioManager.cs to trace how unsettled proceeds affect cash and margin. The issue proposes skipping syncs when no exchange is open or adjusting reconciliation for unsettled cash; compare those approaches against brokerage balance conventions. Done means weekend and holiday syncs no longer double count proceeds or inflate settled buying power.
由索引模型根据 Issue 内容生成。
描述
Expected Behavior
On a live cash account, the daily brokerage cash sync (Brokerage.PerformCashSync) leaves TotalPortfolioValue unchanged when nothing has traded, deposited or withdrawn since the previous sync. Unsettled sale proceeds are counted once: either in UnsettledCashBook (before Lean's settlement) or in CashBook (after it), never in both.
Actual Behavior
After a sale on a Friday, the Saturday 07:45 ET cash sync double counts the proceeds until the Monday sync. The brokerage's balance already carries the proceeds (recognized at trade time), the sync writes that balance into CashBook, and UnsettledCashBook still holds the same proceeds because Lean settles them on the next trading day (Monday 06:00 ET). TotalPortfolioValue jumps up by the proceeds on Saturday and back down on Monday; the live Strategy Equity chart shows both jumps, and MarginRemaining (settled cash) is inflated for two days.
Observed on a live Interactive Brokers deployment (cash account, US equities, build 18226, SetBrokerageModel(InteractiveBrokersBrokerage, AccountType.Cash)):
# Friday 2026-11-06: two round trips; sells at 14:55:05 and 15:05:05 UTC, proceeds 1,128.62 USD
2026-11-06T20:55:01 TRACE:: Total margin information: TotalMarginUsed: 0.00, MarginRemaining: 1277.22
# Saturday: the sync runs although no settlement can happen before Monday
2026-11-08T12:45:00 TRACE:: Brokerage.PerformCashSync(): Sync cash balance
2026-11-08T12:45:00 TRACE:: Brokerage.PerformCashSync(): USD Delta: -1128.61
2026-11-08T12:45:10 TRACE:: Total margin information: TotalMarginUsed: 0.00, MarginRemaining: 2405.83
# Monday: Lean settled the 1,128.62 at 06:00 ET on top of the synced balance; the sync takes it out again
2026-11-09T12:45:00 TRACE:: Brokerage.PerformCashSync(): USD Delta: 1128.62
2026-11-09T12:45:10 TRACE:: Total margin information: TotalMarginUsed: 0.00, MarginRemaining: 2405.83
Strategy Equity samples from the deployment's results: 2,405.84 (2026-11-07) -> 3,534.45 (2026-11-08, exactly 2,405.84 + 1,128.62) -> 2,403.12 (2026-11-09). No order filled between those samples.
On the weekday syncs of the same run (2026-11-10 after Monday sales) there was no delta: the 06:00 ET settlement had moved the proceeds into CashBook before the 07:45 ET sync, as PR #8726 intended. The weekend is the case #8726 does not cover.
Analysis
Brokerages/Brokerage.cs:42sets the sync time to 07:45 New York;ShouldPerformCashSync(:578-588) re-arms on every New York calendar date, so the sync also runs on Saturdays, Sundays and market holidays.Brokerages/Brokerage.cs:654-660compares the brokerage balance withCashBook[currency].Amountonly and overwrites it.UnsettledCashBookis not consulted.Common/Securities/Equity/Equity.cs:33,38: T+1, settlement at 06:00.Common/Securities/DelayedSettlementModel.cs:66-77picks the settlement date by counting only days when the exchange is open, so a Friday (or pre-holiday) sale settles in Lean on the next trading day.- Between a non-trading-day sync and that settlement the proceeds are in both books.
SecurityPortfolioManager.GetMarginRemaining(Common/Securities/SecurityPortfolioManager.cs:594-597) subtractsUnsettledCashBookfromTotalPortfolioValue, soMarginRemaininggoes to the brokerage's full balance and aCashBuyingPowerModelaccount can buy with proceeds that are not yet settled at the brokerage (a cash account good-faith violation if it fills). - PR #8726 (merged 2025-04-28) fixed the weekday instance of this ordering by moving
DefaultSettlementTimeto 06:00 so that Lean settles before the sync. It assumed the sync and the settlement fall on the same day, which does not hold across a weekend or a holiday.
Proposed change
Either of these closes the gap; the first is the smaller change:
- In
ShouldPerformCashSync, skip days on which no security in the algorithm has an open exchange date (weekends, holidays): nothing trades or settles on such a day, so the Friday state stands until the Monday sync. Deposits made over the weekend would be picked up on Monday, as they are today for any deposit made after 07:45 ET. - In
PerformCashSync, reconcile against settled plus unsettled cash: setCashBook[c]tobrokerageBalance[c] - UnsettledCashBook[c].Amount(and log the delta against the sum), so that proceeds the brokerage recognized at trade time are not booked twice. This also covers a weekday where a brokerage credits a sale before Lean's 06:00 settlement.
Open question: brokerages differ in whether the reported balance includes unsettled proceeds (IB documents CashBalance as recognized at trade time; SettledCash is a separate key). Option 2 is only correct for the trade-time convention, so option 1 may be the safer default with option 2 behind a brokerage-model flag.
Reproduce
Not reproducible from a backtest: PerformCashSync runs only in live mode and the defect needs a brokerage balance across a weekend. The evidence is the live syslog quoted above (deltas, margin information) and the deployment's results archive (Strategy Equity samples).
Intercom conversation 215476803054103.
Syslog: L-902077a4f6c1f22ed66a958a2e229f37
Checklist
- I have completely filled out this template
- I have confirmed that this issue exists on the current
masterbranch - I have confirmed that this is not a duplicate issue by searching issues
- I have provided detailed steps to reproduce the issue
- 主要语言
- C#
- 星标
- 22.5k
- 派生
- 5.4k
- 平均合并
- 22 小时 6 分钟
- 30 天内合并 PR
- 81
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
QuantConnect/Lean 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
QuantConnect/Lean#10082 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
QuantConnect/Lean#10059 ·
维护者通常 1 天内回复
-
library-request
难度 2/5 1-3 小时 新手友好度 76/100
QuantConnect/Lean#10034 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
QuantConnect/Lean#9965 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
QuantConnect/Lean#9955 ·
维护者通常 1 天内回复
查看 QuantConnect/Lean 的全部 Issue
相似的 Issue
-
bug emitter:client:csharp
难度 2/5 1-3 小时 新手友好度 84/100
microsoft/typespec#12572 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
needs-area-label
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 2 天内回复
-
AWS Lambda: KeyNotFoundException drops HTTP-triggered transactions when allowAllHeaders is disabled未关闭bug
难度 2/5 1-3 小时 新手友好度 70/100
newrelic/newrelic-dotnet-agent#3949 · 1 条评论 ·
维护者通常 1 天内回复
-
area-networking
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/aspnetcore#70545 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 87/100
维护者通常 1 天内回复