Allow SetAccountCurrency after SetCash without throwing#9457
Open
AlexCatarino wants to merge 1 commit intoQuantConnect:masterfrom
Open
Allow SetAccountCurrency after SetCash without throwing#9457AlexCatarino wants to merge 1 commit intoQuantConnect:masterfrom
AlexCatarino wants to merge 1 commit intoQuantConnect:masterfrom
Conversation
Previously, calling SetAccountCurrency after SetCash threw an InvalidOperationException. The portfolio manager now switches the base account currency in place: the previous Cash entry (and its balance) is preserved in the CashBook, and a notice is logged. When the new account currency matches the existing one, an optional startingCash overrides the previously set amount and the override is logged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Changes
SecurityPortfolioManager.SetAccountCurrencyso it no longer throws when called afterSetCash. The base account currency is switched in place:Cashentry stays in theCashBookwith its existing balance, the new account currency starts at zero (orstartingCashif provided), and a trace is logged noting the residual balance held in the previous currency. This undoes the automatic amount migration performed byCashBook.AccountCurrency's setter so balances stay in their original currency.startingCashoverrides the previously set amount and a trace is logged stating the new amount and the prior amount. WhenstartingCashis omitted or equals the existing amount, nothing is logged.SetCash: behaves as before — only the existing "setting account currency to X" trace is emitted.Related Issue
N/A
Motivation and Context
The previous behavior forced users to order
SetAccountCurrencystrictly beforeSetCash, throwingInvalidOperationExceptionotherwise. Switching to a graceful in-place change with a clear log message is more forgiving and lets users override the starting amount when re-asserting the same account currency.Requires Documentation Change
No.
How Has This Been Tested?
Replaced the obsolete
CanNotChangeAccountCurrencyAfterSettingCashtest (which expected the exception) with four new tests inSecurityPortfolioManagerTests:ChangeAccountCurrencyAfterSettingCashKeepsPreviousCash(covers bothSetCashoverloads) — verifies the previous balance is retained in theCashBookafter a currency change.ChangeAccountCurrencyAfterSettingCashAppliesStartingCashToNewCurrency— verifiesstartingCashapplies to the new currency while the previous balance is preserved.SetAccountCurrencyWithSameCurrencyOverridesStartingCash— verifies a same-currency call withstartingCashoverwrites the prior amount.SetAccountCurrencyWithSameCurrencyAndNoStartingCashKeepsExistingAmount— verifies a no-op same-currency call leaves the existing amount untouched.All 8 SetAccountCurrency-related tests pass locally.
Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>