Securities

Key Concepts

Introduction

A security is an individual financial asset that you can trade on an exchange. LEAN models these unique assets with a Security object, which the AddEquityadd_equity, AddCryptoadd_crypto, and similar methods return. The Securitiessecurities property of the QCAlgorithm class is a dictionary where the keys are Symbol objects and the values are Security objects. The Securitiessecurities dictionary contains all of the securities that have been in the algorithm during its lifetime.

private Symbol _symbol;
_symbol = AddEquity("SPY").symbol;
self._symbol = self.add_equity("SPY").symbol

Price

To get the current price of a security, index the Securitiessecurities dictionary and then access the pricePrice member.

var price = Securities[_symbol].Price;
price = self.securities[self._symbol].price

Holdings

The holdingsHoldings property of a Security object contains information about the investment state and history of the security.

var holdings = Securities[_symbol].Holdings;
var quantity = holdings.Quantity;
holdings = self.securities[self._symbol].holdings
quantity = holdings.quantity

Alternatively, you can get the current holdings of a security with the Portfolioportfolio dictionary.

var holdings = Portfolio[_symbol];
var quantity = holdings.Quantity;
holdings = self.portfolio[self._symbol]
quantity = holdings.quantity

Reality Models

Security objects contain references to the security level reality models. To customize the behavior of each security, configure its reality models.

Tradable Status

The IsTradableis_tradable property shows whether you can trade a security. The property value is true when the security is in the universe, even if the data starts at a later day. Indices, canonical Option securities, and continuous Futures contracts are not tradable. In live mode, custom data objects are also not tradable, even if the custom data represents a tradable asset.

var tradable = Securities[_symbol].IsTradable;
tradable = self.securities[self._symbol].is_tradable

Symbol Properties

The SymbolPropertiessymbol_properties property of a Security contains properties for that specific security, such as the lot size and the minimum price variation.

var symbolProperties = Securities[_symbol].SymbolProperties;
var lotSize = symbolProperties.LotSize;
var minimumPriceVariation = symbolProperties.MinimumPriceVariation;
symbol_properties = self.securities[self._symbol].symbol_properties
lot_size = symbolProperties.lot_size
minimum_price_variation = symbolProperties.minimum_price_variation

Market Hours

The Exchangeexchange property of a Security contains information about the exchange that lists the security. For example, the Exchange.Hoursexchange.hours property contains information on the trading days and hours.

var exchange = Securities[_symbol].Exchange;
var hours = exchange.Hours;
var isOpenNow = hours.isOpen(Time, extendedMarketHours: false);
var minimumPriceVariation = symbolProperties.MinimumPriceVariation;
exchange = self.securities[self._symbol].exchange
hours = exchange.hours
is_open_now = hours.is_open(self.time, extended_market_hours=False)

Custom Security Properties

You can add propertiesattributes to the Security object. For example, you can add an exponential moving average.

dynamic equity = AddEquity("SPY");
equity.ema = EMA(equity.Symbol, 10, Resolution.Daily);
equity = self.add_equity("SPY")
equity.ema = self.ema(equity.symbol, 10, Resolution.DAILY)

This feature is helpful because you can get the Security object from the Securitiessecurities object.

var ema = (Securities["SPY"] as dynamic).ema.Current.Value;
ema = self.securities["SPY"].ema.current.value

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: