Custom Securities

JSON Format Example


This page explains how to import custom data of a single security sourced in JSON format.

Data Format

The following code snippet shows an example JSON file

      "Date": "1997-01-01",
      "Open": 905.2,
      "High": 941.4,
      "Low": 905.2,
      "Close": 939.55,
      "SharesTraded": 38948210,
      "Tur11er(Rs.Cr)": 978.21
      "Date": "1997-01-02",
      "Open": 941.95,
      "High": 944,
      "Low": 925.05,
      "Close": 927.05,
      "SharesTraded": 49118380,
      "Tur11er(Rs.Cr)": 1150.42
      "Date": "2014-07-25",
      "Open": 7828.2,
      "High": 7840.95,
      "Low": 7748.6,
      "Close": 7790.45,
      "SharesTraded": 153936037,
      "Tur11er(Rs.Cr)": 7827.61
      "Date": "2014-07-28",
      "Open": 7792.9,
      "High": 7799.9,
      "Low": 7722.65,
      "Close": 7748.7,
      "SharesTraded": 116534670,
      "Tur11er(Rs.Cr)": 6107.78

The data in the file must be in chronological order.

Define Custom Types

To define a custom data type, inherit the BaseDataPythonData class and override the GetSource and Reader methods.

If you need to create multiple objects in your Reader method from a single line, follow these steps:

  1. In the GetSource method, pass FileFormat.UnfoldingCollection as the third argument to the SubscriptionDataSource constructor.
  2. In the Reader method, order the objects by their timestamp and then return a BaseDataCollection(endTime, config.Symbol, objects) where objects is a list of your custom data objects.
using Newtonsoft.Json;
public class MyCustomDataType : BaseData
    public string Date;
    public decimal Open = 0;
    public decimal High = 0;
    public decimal Low = 0;
    public decimal Close = 0;

    public override DateTime EndTime => Time.AddDays(1);

    public override SubscriptionDataSource GetSource(
        SubscriptionDataConfig config,
        DateTime date,
        bool isLiveMode)
        return new SubscriptionDataSource("", SubscriptionTransportMedium.RemoteFile, FileFormat.UnfoldingCollection);

    public override BaseData Reader(
        SubscriptionDataConfig config,
        string line,
        DateTime date,
        bool isLiveMode)
        if (string.IsNullOrWhiteSpace(line.Trim()))
            return null;

        var objects = JsonConvert.DeserializeObject<List<MyCustomDataType>>(line).Select(index =>
            index.Symbol = config.Symbol;
            index.Time = DateTime.Parse(x.Date, CultureInfo.InvariantCulture);
            index.Value = x.Close;
            return index;

        return new BaseDataCollection(objects.Last().EndTime, config.Symbol, objects);
class MyCustomDataType(PythonData):
    def GetSource(self,
         config: SubscriptionDataConfig,
         date: datetime,
         isLive: bool) -> SubscriptionDataSource:
        return SubscriptionDataSource("", SubscriptionTransportMedium.RemoteFile, FileFormat.UnfoldingCollection)

    def Reader(self,
         config: SubscriptionDataConfig,
         line: str,
         date: datetime,
         isLive: bool) -> BaseData:

        if not (line.strip()):
            return None

        objects = []
        data = json.loads(line)

        for datum in data:
            index = MyCustomDataType()
            index.Symbol = config.Symbol
            index.Time = datetime.strptime(datum["Date"], "%Y-%m-%d")
            index.EndTime = index.Time + timedelta(1)
            index["Open"] = float(datum["Open"])
            index["High"] = float(datum["High"])
            index["Low"] = float(datum["Low"])
            index["Close"] = float(datum["Close"])
            index.Value = index["Close"]

        return BaseDataCollection(objects[-1].EndTime, config.Symbol, objects)

Create Subscriptions

To create a subscription for the custom type, in the Initialize method, call the AddData method. The AddData method returns a Security object, which contains a Symbol. Save a reference to the Symbol so you can use it in OnData to access the security data in the Slice.

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

The AddData method creates a subscription for a single custom data asset and adds it to your user-defined universe.

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

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: