Crypto holdings require special consideration because not all brokerages track your positions in each currency pair.

Cash vs Margin Brokerage Accounts

Some of the Crypto brokerage integrated into QuantConnect support cash and margin accounts while some only support cash accounts. If you want to short Crypto assets or use leverage, you need a margin account. Follow these steps to view which account types each brokerage supports:

  1. Open the brokerage guides.
  2. Click a Crypto brokerage.
  3. Scroll down to the Account Types section.

Virtual Pairs

All fiat and Crypto currencies are individual assets. When you buy a pair like BTCUSD, you trade USD for BTC. In this case, LEAN removes some USD from your portfolio cashbook and adds some BTC. The virtual pair BTCUSD represents your position in that trade, but the virtual pair doesn't actually exist. It simply represents an open trade. If you call the Liquidate method with the Symbol of a virtual pair, the method only liquidates the quantity in your virtual pair. For more information about liquidating Crypto postitions, see Crypto Trades.

Access Cypto Holdings

The CashBook stores the quantity of your Crypto holdings. To access your Crypto holdings, index the CashBook with the currency ticker. The values of the CashBook dictionary are Cash objects, which have the following properties:

var btcQuantity = Portfolio.CashBook["BTC"].Amount;
var btcValue = Portfolio.CashBook["BTC"].ValueInAccountCurrency;
btc_quantity = self.Portfolio.CashBook['BTC'].Amount
btc_value = self.Portfolio.CashBook['BTC'].ValueInAccountCurrency

To access the virtual pair of your Crypto trades, index the Portfolio object with the pair Symbol.

var securityHolding = Portfolio[_btcUsdSymbol];
security_holding  = self.Portfolio[self.btc_usd_symbol]

Stateful Redeployments

When you make a trade inside of a running algorithm, LEAN tracks the virtual position state for you, but it won't survive between deployments. Some brokerages save your virtual pairs, so you can load them into your algorithm when you stop and redeploy it. Depending on the brokerage you select, you may be able to manually add virtual pairs when you deploy live algorithms with the LEAN CLI or cloud deployment wizard.

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: