Portfolio

Holdings

Introduction

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

var securityHolding = Portfolio["SPY"];
security_holding = self.portfolio["SPY"]

Properties

SecurityHolding objects have the following properties:

var securityHolding = Portfolio["SPY"];
var quantity = securityHolding.Quantity;
var invested = securityHolding.Invested;
security_holding = self.portfolio["SPY"]
quantity = security_holding.quantity
invested = security_holding.invested

FutureHolding objects also have SettledProfitsettled_profit and UnsettledProfitunsettled_profit properties.

Get Total Close Profit

To get the profit of a position holding if you closed the position, call the TotalCloseProfittotal_close_profit method. The value this method returns is denominated in your account currency and accounts for order fees.

var profit = Portfolio["SPY"].TotalCloseProfit();
profit = self.portfolio["SPY"].total_close_profit()

The TotalCloseProfittotal_close_profit method accepts the following optional arguments:

ArgumentData TypeDescriptionDefault Value
includeFeesinclude_feesboolWhether to reduce the profit based on the estimated fee from the fee model.trueTrue
exitPriceexit_pricedecimal?float/NoneTypeA 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.nullNone
entryPriceentry_pricedecimal?float/NoneTypeA hypothetical exit price to use for the profit calculation. If you don't provide a value, it uses the average price of the SecurityHolding.nullNone
quantitydecimal?float/NoneTypeThe quantity to liquidate. If you don't provide a value, it uses the quantity of the SecurityHolding.nullNone

LEAN uses this method to define the UnrealizedProfitunrealized_profit property.

Get Quantity Value

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

// Get the quantity value at the current price
var valueAtCurrentPrice = Portfolio["SPY"].GetQuantityValue(100);

// Get the quantity value at a specific price
var valueAtSpecificPrice = Portfolio["SPY"].GetQuantityValue(100, price: 30);
# 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

It is often that a portfolio consist of different currencies and assets at the initial state already. For example, breaking a previous algorithm for upgrade. Thus, we might need to set the initial holdings at initialization to mimic a complex portfolio's initial state.

To set cash, call SetCashset_cash in the Initializeinitialize method. You can set multiple currencies by multiple calls.

SetCash(100000);       // Set the quantity of the account currency to 100,000
SetCash("BTC", 10);    // Set the Bitcoin quantity to 10
SetCash("EUR", 10000); // Set the EUR quantity to 10,000
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

As per setting securities, you need to first add the security into your portfolio. Then, set the initial holding position with average acquisition price and quantity by SetHoldingsset_holdings method in the Initializeinitialize method.

if (!LiveMode)
{
    Portfolio["SPY"].SetHoldings(averagePrice: 500, quantity: 100);
}
if not self.live_mode:
    self.portfolio["SPY"].set_holdings(average_price=500, quantity=100)

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: