---
title: "Holdings"
description: "The Portfolio is a dictionary where the key is a Symbol and the value is a SecurityHolding."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/portfolio/holdings
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/portfolio/holdings.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/08%20Portfolio/03%20Holdings
modified: 2026-11-20
index: https://www.quantconnect.com/llms.txt
---

# Holdings

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Portfolio > Holdings

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

The `portfolio` is a dictionary where the key is a `Symbol` and the value is a `SecurityHolding`.

```python
# Create shortcut helper to the SPY SecurityHolding object from the Portfolio collection.
security_holding = self.portfolio["SPY"]
```

## Properties

`SecurityHolding` objects have the following properties:

*Members of `QuantConnect.Securities.SecurityHolding` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.Securities.SecurityHolding&language=python*

```python
#  Save shortcut to SecurityHolding object, and fetch quantity and invested status.
security_holding = self.portfolio["SPY"]
quantity = security_holding.quantity
invested = security_holding.invested
```

`FutureHolding` objects also have `settled_profit` and `unsettled_profit` properties.

## Get Total Close Profit

To get the profit of a position holding if you closed the position, call the `total_close_profit` method. The value this method returns is denominated in your [account currency](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#03-Set-Account-Currency) and accounts for order fees.

```python
profit = self.portfolio["SPY"].total_close_profit()
```

The `total_close_profit` method accepts the following optional arguments:

| Argument | Data Type | Description | Default Value |
| --- | --- | --- | --- |
| `include_fees` | `bool` | Whether to reduce the profit based on the estimated fee from the [fee model](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/transaction-fees/key-concepts.md). | `True` |
| `exit_price` | `float/NoneType` | A hypothetical exit price to use for the profit calculation. If you don't provide a value, it uses the bid price for sell orders or the ask price for buy orders. | `None` |
| `entry_price` | `float/NoneType` | A hypothetical exit price to use for the profit calculation. If you don't provide a value, it uses the average price of the `SecurityHolding`. | `None` |
| `quantity` | `float/NoneType` | The quantity to liquidate. If you don't provide a value, it uses the quantity of the `SecurityHolding`. | `None` |

LEAN uses this method to define the `unrealized_profit` property.

## Get Quantity Value

To get the value of a security at any quantity, call the `get_quantity_value` method. The value this method returns is denominated in your account currency.

```python
# Get the quantity value at the current price
value_at_current_price = self.portfolio["SPY"].get_quantity_value(100)

# Get the quantity value at a specific price
value_at_specific_price = self.portfolio["SPY"].get_quantity_value(100, price=30)
```

## Set Initial Holdings

When you deploy a backtest or a new paper trading algorithm, the default initial portfolio state contains $100,000 USD. However, in the [initialize](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md) method, you can define the initial portfolio state to something else. To [set the initial cash balances](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#04-Set-Cash), call `set_cash` with the currency ticker and quantity. To set multiple currencies, call the method multiple times.

```python
self.set_cash(100000)       # Set the quantity of the account currency to 100,000
self.set_cash("BTC", 10)    # Set the Bitcoin quantity to 10
self.set_cash("EUR", 10000) # Set the EUR quantity to 10,000
```

To set the initial security holdings, [subscribe to the security](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#08-Add-Data) so that it enters your [portfolio](https://www.quantconnect.com/docs/v2/writing-algorithms/portfolio/key-concepts.md), then call the `SecurityHolding.set_holdings` method with the average purchase price and quantity.

```python
if not self.live_mode:
    self.portfolio["SPY"].set_holdings(average_price=500, quantity=100)
```

## Examples

The following examples demonstrate common practices for setting the portfolio holdings.

### Example 1: Initial Holdings

The following algorithm simulates an account with multiple initial positions and cash. Note that these initial holdings will affect the algorithm's performance.

```python
class PortfolioExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)
        # Simulate initial cash in multiple currencies.
        # Note that all currencies other than the account currency will affect the performance.
        self.set_account_currency("USD", 10000)
        self.set_cash("EUR", 1000)
        self.set_cash("JPY", 1000000)
        self.set_cash("HKD", 10000)

        # Add a security initializer that seeds the price of each asset with its
        # last known price. This ensures the holdings we add below will initialize
        # with a non-zero value.
        self.settings.seed_initial_prices = True

        # Request data for some securities in the account already.
        spy = self.add_equity("SPY").symbol
        aapl = self.add_equity("AAPL").symbol
        btcusd = self.add_crypto("BTCUSD").symbol
        xauusd = self.add_cfd("XAUUSD").symbol

        # Simulate that the account initially holds some assets.
        # Note that all initial holdings will affect the performance.
        self.portfolio[spy].set_holdings(average_price=420, quantity=102)
        self.portfolio[aapl].set_holdings(average_price=120, quantity=25)
        self.portfolio[btcusd].set_holdings(average_price=45000, quantity=0.125)
        self.portfolio[xauusd].set_holdings(average_price=1000, quantity=100)
```
