Skip to content

Allow SetAccountCurrency after SetCash without throwing#9457

Open
AlexCatarino wants to merge 1 commit intoQuantConnect:masterfrom
AlexCatarino:feature-set-account-currency-after-set-cash
Open

Allow SetAccountCurrency after SetCash without throwing#9457
AlexCatarino wants to merge 1 commit intoQuantConnect:masterfrom
AlexCatarino:feature-set-account-currency-after-set-cash

Conversation

@AlexCatarino
Copy link
Copy Markdown
Member

Description

Changes SecurityPortfolioManager.SetAccountCurrency so it no longer throws when called after SetCash. The base account currency is switched in place:

  • Different currency: the previous Cash entry stays in the CashBook with its existing balance, the new account currency starts at zero (or startingCash if provided), and a trace is logged noting the residual balance held in the previous currency. This undoes the automatic amount migration performed by CashBook.AccountCurrency's setter so balances stay in their original currency.
  • Same currency: an optional startingCash overrides the previously set amount and a trace is logged stating the new amount and the prior amount. When startingCash is omitted or equals the existing amount, nothing is logged.
  • No prior 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 SetAccountCurrency strictly before SetCash, throwing InvalidOperationException otherwise. 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 CanNotChangeAccountCurrencyAfterSettingCash test (which expected the exception) with four new tests in SecurityPortfolioManagerTests:

  • ChangeAccountCurrencyAfterSettingCashKeepsPreviousCash (covers both SetCash overloads) — verifies the previous balance is retained in the CashBook after a currency change.
  • ChangeAccountCurrencyAfterSettingCashAppliesStartingCashToNewCurrency — verifies startingCash applies to the new currency while the previous balance is preserved.
  • SetAccountCurrencyWithSameCurrencyOverridesStartingCash — verifies a same-currency call with startingCash overwrites 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

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant