Forex
Handling Data
Introduction
LEAN passes the data you request to the OnData
method so you can make trading decisions. The default OnData
method accepts a Slice
object, but you can define additional OnData
methods that accept different data types. For example, if you define an OnData
method that accepts a QuoteBar
argument, it only receives QuoteBar
objects. The Slice
object that the OnData
method receives groups all the data together at a single moment in time. To access the Slice
outside of the OnData
method, use the CurrentSlice
property of your algorithm.
All the data formats use DataDictionary
objects to group data by Symbol
and provide easy access to information. The plural of the type denotes the collection of objects. For instance, the QuoteBars
DataDictionary
is made up of QuoteBar
objects. To access individual data points in the dictionary, you can index the dictionary with the Forex pair ticker or Symbol
, but we recommend you use the Symbol
.
Quotes
QuoteBar
objects are bars that consolidate NBBO quotes from the exchanges. They contain the open, high, low, and close prices of the bid and ask. The Open
, High
, Low
, and Close
properties of the QuoteBar
object are the mean of the respective bid and ask prices. If the bid or ask portion of the QuoteBar
has no data, the Open
, High
, Low
, and Close
properties of the QuoteBar
copy the values of either the Bid
or Ask
instead of taking their mean.

QuoteBar
objects have the following properties:
To get the QuoteBar
objects in the Slice
, index the QuoteBars
property of the Slice
with the Forex pair Symbol
. If the Forex pair doesn't actively get quotes or you are in the same time step as when you added the Forex pair subscription, the Slice
may not contain data for your Symbol
. To avoid issues, check if the Slice
contains data for your Forex pair before you index the Slice
with the Forex pair Symbol
.
public override void OnData(Slice slice) { if (slice.QuoteBars.ContainsKey(_symbol)) { var quoteBar = slice.QuoteBars[_symbol]; } } public void OnData(QuoteBars quoteBars) { if (quoteBars.ContainsKey(_symbol)) { var quoteBar = quoteBars[_symbol]; } }
def OnData(self, slice: Slice) -> None: if self.symbol in slice.QuoteBars: quote_bar = slice.QuoteBars[self.symbol]
You can also iterate through the QuoteBars
dictionary. The keys of the dictionary are the Symbol
objects and the values are the QuoteBar
objects.
public override void OnData(Slice slice) { foreach (var kvp in slice.QuoteBars) { var symbol = kvp.Key; var quoteBar = kvp.Value; var askPrice = quoteBar.Ask.Close; } } public void OnData(QuoteBars quoteBars) { foreach (var kvp in quoteBars) { var symbol = kvp.Key; var quoteBar = kvp.Value; var askPrice = quoteBar.Ask.Close; } }
def OnData(self, slice: Slice) -> None: for symbol, quote_bar in slice.QuoteBars.items(): ask_price = quote_bar.Ask.Close
QuoteBar
objects let LEAN incorporate spread costs into your simulated trade fills to make backtest results more realistic.
Ticks
Tick
objects represent a single trade or quote at a moment in time. A trade tick is a record of a transaction for the Forex pair. A quote tick is an offer to buy or sell the Forex pair at a specific price. Tick
objects have the following properties:
Trade ticks have a non-zero value for the Quantity
and Price
properties, but they have a zero value for the BidPrice
, BidSize
, AskPrice
, and AskSize
properties. Quote ticks have non-zero values for BidPrice
and BidSize
properties or have non-zero values for AskPrice
and AskSize
properties. To check if a tick is a trade or a quote, use the TickType
property.
In backtests, LEAN groups ticks into one millisecond buckets. In live trading, LEAN groups ticks into ~70-millisecond buckets. To get the Tick
objects in the Slice
, index the Ticks
property of the Slice
with a Symbol
. If the Forex pair doesn't actively trade or you are in the same time step as when you added the Forex pair subscription, the Slice
may not contain data for your Symbol
. To avoid issues, check if the Slice
contains data for your Forex pair before you index the Slice
with the Forex pair Symbol
.
public override void OnData(Slice slice) { if (slice.Ticks.ContainsKey(_symbol)) { var ticks = slice.Ticks[_symbol]; foreach (var tick in ticks) { var price = tick.Price; } } } public void OnData(Ticks ticks) { if (ticks.ContainsKey(_symbol)) { foreach (var tick in ticks[_symbol]) { var price = tick.Price; } } }
def OnData(self, slice: Slice) -> None: if self.symbol in slice.Ticks: ticks = slice.Ticks[self.symbol] for tick in ticks: price = tick.Price
You can also iterate through the Ticks
dictionary. The keys of the dictionary are the Symbol
objects and the values are the List<Tick>
list[Tick]
objects.
public override void OnData(Slice slice) { foreach (var kvp in slice.Ticks) { var symbol = kvp.Key; var ticks = kvp.Value; foreach (var tick in ticks) { var price = tick.Price; } } } public void OnData(Ticks ticks) { foreach (var kvp in ticks) { var symbol = kvp.Key; foreach (var tick in kvp.Value) { var price = tick.Price; } } }
def OnData(self, slice: Slice) -> None: for symbol, ticks in slice.Ticks.items(): for tick in ticks: price = tick.Price
Tick data is raw and unfiltered, so it can contain bad ticks that skew your trade results. For example, some ticks come from dark pools, which aren't tradable. We recommend you only use tick data if you understand the risks and are able to perform your own online tick filtering.