Custom Securities

Key Concepts


To receive your custom data in the OnData method instead of in a bulk download, create a custom type and then create a data subscription. The custom data type tells LEAN where to get your data and how to read it. All custom data types must extend from BaseDataPythonData and override the Reader and GetSource methods.

Unlike custom data, native QC data gets the full benefit of security modeling like splits and dividends. Custom data is ignorant of everything about the actual market. Things like market hours, delistings, and mergers don't work with custom data.

Create Subscriptions

After you define the custom data class, in the Initialize method of your algorithm, call the AddData<T>(string ticker, Resolution resolution = Resolution.Daily) method. This method gives LEAN the T-type factory to create the objects, the name of the data, and the resolution at which to poll the data source for updates. self.AddData(Type class, string ticker, Resolution resolution = Resolution.Daily) method. This method gives LEAN the type factory to create the data objects, the name of the data, and the resolution to poll the data source for updates.

public class MyAlgorithm : QCAlgorithm
    private Symbol _symbol;
    public override void Initialize()
        _symbol = AddData<MyCustomDataType>("<name>", Resolution.Daily).Symbol;
class MyAlgorithm(QCAlgorithm): 
    def Initialize(self) -> None:
        self.symbol = self.AddData(MyCustomDataType, "<name>", Resolution.Daily).Symbol

The resolution argument should match the resolution of your custom dataset. The lowest reasonable resolution is every minute. Anything more frequent than every minute is very slow to execute. The frequency that LEAN checks the data source depends on the resolution argument. The following table shows the polling frequency of each resolution:

ResolutionUpdate Frequency
DailyEvery 30 minutes
HourEvery 30 minutes
MinuteEvery minute
SecondEvery second
TickConstantly checks for new data

Receive Custom Data

As your data reader reads your custom data file, LEAN adds the data points in the Slice it passes to your algorithm's OnData method. To collect the custom data, use the Symbol or name of your custom data subscription. You can access the Value and custom properties of your custom data class from the Slice. To access the custom properties, use the custom attributepass the property name to the GetProperty method.

public class MyAlgorithm : QCAlgorithm
    public override void OnData(Slice slice)
        if (slice.ContainsKey(_symbol))
            var customData = slice[_symbol];
            var value = customData.Value;
            var property1 = customData.Property1;

    // You can also get the data directly with OnData(<dataClass>) method
    public void OnData(MyCustomDataType slice)
        var value = slice.Value;
        var property1 = slice.Property1;
class MyAlgorithm(QCAlgorithm):
    def OnData(self, slice: Slice) -> None:
        if slice.ContainsKey(self.symbol):
            custom_data = slice[self.symbol]
            value = custom_data.Value
            property1 = custom_data.Property1

Set the Benchmark

To set your custom data source as the benchmark, in the Initialize method, call the SetBenchmark method with the Symbol of your custom data subscription.

var symbol = AddData<MyCustomDataType>("<name>", Resolution.Daily).Symbol;
self.symbol = self.AddData(MyCustomDataType, "<name>", Resolution.Daily).Symbol

Avoid Look-Ahead Bias

Look-ahead bias occurs when you make decisions with information that wouldn't be available until some time in the future. In backtesting, look-ahead bias occurs when you receive data earlier than it would actually be available in reality. If look-ahead bias seeps into your algorithm, it will perform differently between live trading and backtesting.

To avoid look-ahead bias, set the timestamp of data points to the time when the data would actually be available. A lot of external sources apply timestamps to data differently than we do. For instance, on some platforms, daily bars are displayed with the date that they opened. We display daily bars with the date they closed. If you set the EndTime to the start of the bar, you'll receive the bar earlier in backtests than you would in live trading.

Time Modeling

Data types classes in LEAN inherit from the BaseData class that defines the Time and EndTime properties. These properties represent the time period of which the data was built. If the data type occurs in a singular point in time, they have no period, so Time and EndTime are the same. Regardless of the period, LEAN uses the time when the data sample ends, EndTime, to add the sample to a Slice.

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: