Problem Description
Solution
To set the base currency of your trading account in QuantConnect, you use the set_account_currency method within your algorithm's initialize function.
This setting tells the LEAN engine to denominate your portfolio value, cash, and margin requirements in the specified currency code (e.g., "EUR", "GBP", "BTC", "USDT").
Implementation Details
- Method:
self.set_account_currency("CURRENCY_CODE") - Placement: Must be called inside the
initializemethod. - Cash: When you call
self.set_cash(amount), that amount is interpreted as being in the currency you set. - Data Conversion: If you trade assets quoted in a different currency (e.g., trading US stocks while your account is in EUR), LEAN will automatically look for the necessary currency pair (e.g., EURUSD) to perform currency conversions for portfolio valuation.
Code Example
The following example sets the account currency to Euros (EUR). It initializes the account with €100,000 and trades a US Equity (SPY). LEAN automatically handles the USD-to-EUR conversion for reporting portfolio value.
from AlgorithmImports import *
class SetBaseCurrencyAlgorithm(QCAlgorithm):
def initialize(self):
# 1. Set the start and end dates for the backtest
self.set_start_date(2023, 1, 1)
self.set_end_date(2023, 12, 1)
# 2. Set the Account Currency to Euro
# This must be a standard 3-letter currency code (e.g., EUR, GBP, JPY)
# or a crypto code if supported by the brokerage model (e.g., USDT, BTC).
self.set_account_currency("EUR")
# 3. Set Strategy Cash
# Because we set the account currency to EUR, this represents €100,000.
self.set_cash(100000)
# 4. Add an asset (US Equity, quoted in USD)
self.spy = self.add_equity("SPY", Resolution.DAILY).symbol
# Optional: Set a brokerage model that supports this currency setup
# Interactive Brokers supports multi-currency accounts.
self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.MARGIN)
def on_data(self, data: Slice):
if not self.portfolio.invested:
# Buy SPY. The engine will convert our EUR buying power
# to USD to execute the trade.
self.set_holdings(self.spy, 0.5)
def on_end_of_algorithm(self):
# Log the final value. This will be denominated in EUR.
self.log(f"Final Portfolio Value (EUR): {self.portfolio.total_portfolio_value}")
Q&A
Q: What happens if I trade an asset quoted in a currency for which there is no conversion data?
A: If LEAN cannot find a conversion rate between the asset's quote currency and your account currency (e.g., trading a stock in ZAR while the account is in EUR, and no EURZAR or USDZAR data is available), the engine may throw an error or fail to calculate portfolio statistics correctly. You may need to manually add the currency conversion pair using self.add_forex or self.add_crypto.
Q: Can I use cryptocurrency as my base currency?
A: Yes. You can set the account currency to "BTC", "ETH", or stablecoins like "USDT". This is common when simulating crypto-native exchanges like Binance or Bybit. For example:
self.set_brokerage_model(BrokerageName.BYBIT, AccountType.CASH)
self.set_account_currency("USDT")
Q: Does set_account_currency affect how set_cash works?
A: Yes. set_cash sets the starting balance in the account currency. If you do not call set_account_currency, the default is USD. If you call self.set_account_currency("GBP") and self.set_cash(10000), you start with £10,000.
Q: How does this affect margin requirements?
A: Margin requirements are calculated in the asset's quote currency and then converted back to the account currency to check against your available buying power. Currency fluctuations between the asset currency and account currency will affect your real-time margin usage.