US Equity
Requesting Data
Introduction
Request US Equity data in your algorithm to receive a feed of asset prices in the OnData
on_data
method. For more information about the specific dataset we use for backtests, see the US Equities dataset listing. To trade US Equities live, you can use the QuantConnect data provider or one of the brokerage data providers.
Create Subscriptions
To create an Equity subscription, in the Initialize
initialize
method, call the AddEquity
add_equity
method. The AddEquity
add_equity
method returns an Equity
object, which contains a Symbol
symbol
property. Save a reference to the Symbol
symbol
so you can use it in OnData
on_data
to access the security data in the Slice
.
_symbol = AddEquity("SPY").Symbol;
self._symbol = self.add_equity("SPY").symbol
The AddEquity
add_equity
method creates a subscription for a single Equity asset and adds it to your user-defined universe. To create a dynamic universe of Equities, add an Equity universe or an Equity Universe Selection model.
To view the supported assets in the US Equities dataset, see the Data Explorer.
Resolutions
The following table shows the available resolutions and data formats for Equity subscriptions:
Resolution | TradeBar | QuoteBar | Trade Tick | Quote Tick |
---|---|---|---|---|
Tick TICK | ||||
Second SECOND | ||||
Minute MINUTE | ||||
Hour HOUR | ||||
Daily DAILY |
The default resolution for Equity subscriptions is Resolution.Minute
Resolution.MINUTE
. To change the resolution, pass a resolution
argument to the AddEquity
add_equity
method.
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
To create custom resolution periods, see Consolidating Data.
Asset Primary Exchange
When a stock like Apple is listed, it’s listed on Nasdaq. The open auction tick on Nasdaq is the price that’s used as the official open of the day. NYSE, BATS, and other exchanges also have opening auctions, but the only official opening price for Apple is the opening auction on the exchange where it was listed.
Supported Markets
LEAN groups all of the US Equity exchanges under Market.USA
. In live mode, the brokerage routes the orders to the exchange that provides the best price.
To set the market for a security, pass a market
argument to the AddEquity
add_equity
method.
_symbol = AddEquity("SPY", market: Market.USA).Symbol;
self._symbol = self.add_equity("SPY", market=Market.USA).symbol
The brokerage models have a default market for each asset class. If you set a brokerage model, you may not need to specify the market to use.
Fill Forward
Fill forward means if there is no data point for the current slice, LEAN uses the previous data point. Fill forward is the default data setting. If you disable fill forward, you may get stale fills or you may see trade volume as zero.
To disable fill forward for a security, set the fillForward
fill_forward
argument to false when you create the security subscription.
AddEquity("SPY", fillForward: false);
self.add_equity("SPY", fill_forward=False)
Margin and Leverage
LEAN models buying power and margin calls to ensure your algorithm stays within the margin requirements. In backtests, the default leverage for margin accounts is 2x leverage and leverage is not available for cash accounts. To change the amount of leverage you can use for a security, pass a leverage
argument to the AddEquity
add_equity
method.
_symbol = AddEquity("SPY", leverage: 3).Symbol;
self._symbol = self.add_equity("SPY", leverage=3).symbol
In live trading, the brokerage determines how much leverage you may use. For more information about the leverage they provide, see Brokerages.
Extended Market Hours
By default, your security subscriptions only cover regular trading hours. To subscribe to pre and post-market trading hours for a specific asset, enable the extendedMarketHours
extended_market_hours
argument when you create the security subscription.
AddEquity("SPY", extendedMarketHours: true);
self.add_equity("SPY", extended_market_hours=True)
You only receive extended market hours data if you create the subscription with minute, second, or tick resolution. If you create the subscription with daily or hourly resolution, the bars only reflect the regular trading hours.
To view the schedule of regular and extended market hours, see Market Hours.
Data Normalization
The data normalization mode defines how historical data is adjusted for corporate actions. The data normalization mode affects the data that LEAN passes to OnData
on_data
and the data from history requests. By default, LEAN adjusts US Equity data for splits and dividends to produce a smooth price curve, but the following data normalization modes are available for Equity subscriptions:
If you use Adjusted
ADJUSTED
, SplitAdjusted
SPLIT_ADJUSTED
, or TotalReturn
TOTAL_RETURN
, we use the entire split and dividend history to adjust historical prices. This process ensures you get the same adjusted prices, regardless of the
backtest end date. The ScaledRaw
SCALED_RAW
data normalization is only for history requests. When you use ScaledRaw
SCALED_RAW
, we use the split and dividend history before the algorithm's current time to adjust historical prices. The ScaledRaw
SCALED_RAW
data normalization model enables you to warm up indicators with adjusted data when you subscribe to Raw
RAW
security data.
To set the data normalization mode for a security, pass a dataNormalizationMode
data_normalization_mode
argument to the AddEquity
add_equity
method.
_symbol = AddEquity("SPY", dataNormalizationMode: DataNormalizationMode.Raw).Symbol;
self._symbol = self.add_equity("SPY", data_normalization_mode=DataNormalizationMode.RAW).symbol