Securities
Handling Data
Introduction
LEAN packages the data for your subscriptions in a Slice
object and passes it to the OnData
method of your algorithm. To avoid look-ahead bias, LEAN only provides the data that's available at the current time in your algorithm. This processing style ensures that your algorithm backtests in the same manner that it trades live. Your algorithm uses the data to make trading decisions and LEAN uses the data to update your positions, track your portfolio value, and simulate orders.
Timeslice
The Slice
that LEAN passes to the OnData
method represents all of the data for your subscriptions at a single point in time. The Slice
object contains data like Tick
objects, TradeBar
objects, QuoteBar
objects, corporate actions, and chains for Option and Future contracts. You can use the data in the Slice
to make trading decisions.
To access data in the Slice
, index it with the security Symbol
. If you use the security ticker instead of the Symbol
, LEAN automatically finds the Symbol
.
public override void OnData(Slice slice) { if (slice.ContainsKey(_symbol)) { var myData = slice[_symbol]; } }
def OnData(self, slice: Slice) -> None: if slice.ContainsKey(self.symbol): my_data = slice[self.symbol]
The following table shows the Slice
property to index based on the data format you want:
Data Format | Slice Property |
---|---|
TradeBar | Bars |
QuoteBar | QuoteBars |
Tick | Ticks |
public override void OnData(Slice slice) { if (slice.ContainsKey(_symbol)) { var bar = slice.Bars[_symbol]; } }
def OnData(self, slice: Slice) -> None: if slice.ContainsKey(self.symbol): bar = slice.Bars[self.symbol]
If you just index the Slice
instead of the preceding properties, it returns the correct object. If your data subscription provides QuoteBar
objects and you index the Slice
with the security Symbol
, it returns the QuoteBar
.
Slice
objects have the following properties:
For more information about the Slice
class, see Timeslices.