Portfolio

Cashbook

Introduction

The CashBook is a dictionary where the keys are currency tickers and the values are Cash objects. The Cash objects track the amount of each currency in the portfolio. As you buy and sell securities, LEAN credits and debits the Cash objects to reflect your cash holdings.

Account Currency

The default account currency is USD, but you can change it. All of the properties of the Portfolio object that return a currency value denominate the currency value in your account currency. Depending on your account currency and security subscriptions, LEAN may add internal security subscriptions to calculate the ValueInAccountCurrencyvalue_in_account_currency. For example, if you only add BTCETH to your algorithm and set the account currency to USDT, LEAN adds BTCUSDT and ETHUSDT as internal feeds.

To get your account currency, use the AccountCurrencyaccount_currency property.

var accountCurrency = AccountCurrency;
account_currency = self.account_currency

To set your account currency, in the Initializeinitialize method, call the SetAccountCurrencyset_account_currency method. You can only set the account currency once. LEAN ignores each additional call to SetAccountCurrencyset_account_currency.

SetAccountCurrency("EUR");
self.set_account_currency("EUR")

Settled vs Unsettled Cash

The Portfolioportfolio has two independent cashbooks. The CashBookcash_book tracks settled cash and the UnsettledCashBookunsettled_cash_book tracks unsettled cash, which you can't spend. If you trade with a margin account, trades settle immediately so LEAN credits and debits the CashBookcash_book at the time of the trade. In some cases, transactions can take time to settle. For example, Equity trades in a cash account settle in T+3. Therefore, if you sell shares of stock on Monday, the transaction settles on Thursday. In contrast, Option trades settle in T+1.

var settledCashBook = Portfolio.CashBook;
var unsettledCashBook = Portfolio.UnsettledCashBook;
settled_cash_book = self.portfolio.cash_book
unsettled_cash_book = self.portfolio.unsettled_cash_book

Track Cash Balances

To get the balance of a currency in the cash book, use the Amountamount property.

var usd = Portfolio.CashBook["USD"].Amount;
var btc = Portfolio.CashBook["BTC"].Amount;
usd = self.portfolio.cash_book["USD"].amount
btc = self.portfolio.cash_book["BTC"].amount

To get the value of a currency in the cash book, denominated in your account currency, use the ValueInAccountCurrencyvalue_in_account_currency property.

var ethValue = Portfolio.CashBook["ETH"].ValueInAccountCurrency;
eth_value = self.portfolio.cash_book["ETH"].value_in_account_currency

Deposits and Withdraws

In backtests, you can add and remove cash from the cash book. To add or remove cash, call the AddAmountadd_amount method.

var newUSDBalance = Portfolio.CashBook["USD"].AddAmount(100);
var newBTCBalance = Portfolio.CashBook["BTC"].AddAmount(-1.5m);
new_usd_balance = self.portfolio.cash_book["USD"].add_amount(100)
new_btc_balance = self.portfolio.cash_book["BTC"].add_amount(-1.5)

In live trading, add and withdraw cash through your brokerage account. If you adjust the cash balances in your algorithm with the AddAmountadd_amount method, the cash balance in your algorithm will be out of sync with the cash balance in your brokerage account.

Currency Symbols

A currency symbol is a graphic that represents the currency name. For example, $ is for dollars, € for euros, and ฿ for Bitcoin. To get the symbol of a currency in the cash book, use CurrencySymbolcurrency_symbol property.

var usdSymbol = Portfolio.CashBook["USD"].CurrencySymbol;
usd_symbol = self.portfolio.cash_book["USD"].currency_symbol

Conversion Rates

To get the conversion rate for a currency in the cashbook to your account currency, use the ConversionRateconversion_rate property.

var eurConversionRate = Portfolio.CashBook["EUR"].ConversionRate;
eur_conversion_rate = self.portfolio.cash_book["EUR"].conversion_rate

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: