Portfolio

Holdings

Introduction

The Portfolio 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 SettledProfit and UnsettledProfit properties.

Get Total Close Profit

To get the profit of a position holding if you closed the position, call the TotalCloseProfit 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"].TotalCloseProfit()

The TotalCloseProfit method accepts the following optional arguments:

ArgumentData TypeDescriptionDefault Value
includeFeesboolWhether to reduce the profit based on the estimated fee from the fee model.trueTrue
exitPricedecimal?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
entryPricedecimal?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 UnrealizedProfit property.

Get Quantity Value

To get the value of a security at any quantity, call the GetQuantityValue 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"].GetQuantityValue(100)

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

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: