Datasets

Equity Fundamental Data

Introduction

This page explains how to request, manipulate, and visualize historical Equity Fundamental data. Corporate fundamental data is available through the US Fundamental Data from Morningstar.

Create Subscriptions

Follow these steps to subscribe to an Equity security:

  1. Load the assembly files and data types in their own cell.
  2. #load "../Initialize.csx"
  3. Import the data types.
  4. #load "../QuantConnect.csx"
    #r "../Microsoft.Data.Analysis.dll"
    
    using QuantConnect;
    using QuantConnect.Data;
    using QuantConnect.Algorithm;
    using QuantConnect.Research;
    using QuantConnect.Indicators;
    using Microsoft.Data.Analysis;
  5. Create a QuantBook.
  6. var qb = new QuantBook();
    qb = QuantBook()
  7. Call the AddEquity method with a ticker and then save a reference to the Equity Symbol.
  8. var symbols = new [] 
        {
            "AAL",  // American Airlines Group, Inc.
            "ALGT", // Allegiant Travel Company
            "ALK",  // Alaska Air Group, Inc.
            "DAL",  // Delta Air Lines, Inc.
            "LUV",  // Southwest Airlines Company
            "SKYW", // SkyWest, Inc.
            "UAL"   // United Air Lines
        }
        .Select(ticker => qb.AddEquity(ticker).Symbol);
    symbols = [qb.AddEquity(ticker).Symbol
        for ticker in [
            "AAL",   # American Airlines Group, Inc.
            "ALGT",  # Allegiant Travel Company
            "ALK",   # Alaska Air Group, Inc.
            "DAL",   # Delta Air Lines, Inc.
            "LUV",   # Southwest Airlines Company
            "SKYW",  # SkyWest, Inc.
            "UAL"    # United Air Lines
        ]]

To view the supported assets in the US Equities dataset, see the Data Explorer.

Get Historical Data

You need a subscription before you can request historical fundamental data for a US Equity.

To get historical data, call the GetFundamental method with a list of Symbol objects, a fundamental data field name, a start DateTimedatetime, and an end DateTimedatetime. The start and end times you provide are based in the notebook time zone. To view the possible fundamental data field names, see the FineFundamental attributes in Data Point Attributes. For example, to get data for airline companies over 2014, run:

var startTime = new DateTime(2014, 1, 1);
var endTime = new DateTime(2015, 1, 1);
var history = qb.GetFundamental(symbols, "ValuationRatios.PERatio", startTime, endTime);
start_time = datetime(2014, 1, 1)
end_time = datetime(2015, 1, 1)
history = qb.GetFundamental(symbols, "ValuationRatios.PERatio", start_time, end_time)

The preceding method returns the fundamental data field values that are timestamped within the defined period of time.

Wrangle Data

You need some historical data to perform wrangling operations. To display pandas objects, run a cell in a notebook with the pandas object as the last line. To display other data formats, call the print method.

You need some historical data to perform wrangling operations. Use LINQ to wrangle the data and then call the Console.WriteLine method in a Jupyter Notebook to display the data.

The DataFrame index is the EndTime of the data sample. The columns of the DataFrame are the Equity Symbol objects.

Historical PE ratio of US Equities

To get the fundamental data points for each Equity, iterate through the history request result.

To select the historical data of a single Equity, index the DataFrame with the Equity Symbol. Each history slice may not have data for all of your Equity subscriptions. To avoid issues, check if it contains data for your Equity before you index it with the Equity Symbol.

foreach (var slice in history)
{
    foreach (var symbol in symbols)
    {
        if (slice.ContainsKey(symbol))
        {
            var peRatio = slice[symbol];
        }
    }
}
history[symbols[1]]
Historical PE ratio of a single symbol

You can also iterate through each data point in the slice.

foreach (var slice in history)
{
    foreach (var kvp in slice)
    {
        var symbol = kvp.Key;
        var peRatio = kvp.Value;
    }
}

You can also use LINQ to select the historical data points.

var symbol = symbols.Last();
var values = history.Select(slice => slice[symbol]);

Plot Data

You need some historical Equity fundamental data to produce plots. You can use many of the supported plotting librariesPlot.NET package to visualize data in various formats. For example, you can plot line charts.

Follow these steps to plot line charts using built-in methodsPlotly.NET package:

  1. Call the plot method on the history DataFrame.
  2. history.plot(title='PE Ratio Over Time', figsize=(15, 8))
  3. Create Line charts.
  4. var chart1 = Chart2D.Chart.Line<DateTime, decimal, string>(
        history.Select(x => (DateTime)x.Time),
        history.Select(x => (decimal)x[aapl]),
        Name: "AAPL"
    );
    var chart2 = Chart2D.Chart.Line<DateTime, decimal, string>(
        history.Select(x => (DateTime)x.Time),
        history.Select(x => (decimal)x[goog]),
        Name: "GOOG"
    );
  5. Create a Layout.
  6. LinearAxis xAxis = new LinearAxis();
    xAxis.SetValue("title", "Time");
    LinearAxis yAxis = new LinearAxis();
    yAxis.SetValue("title", "PE Ratio");
    Title title = Title.init("AAPL & GOOG PE Ratio");
    
    Layout layout = new Layout();
    layout.SetValue("xaxis", xAxis);
    layout.SetValue("yaxis", yAxis);
    layout.SetValue("title", title);
  7. Combine the charts and assign the Layout to the chart.
  8. var chart = Plotly.NET.Chart.Combine(new []{chart1, chart2});
    chart.WithLayout(layout);
  9. Show the plot.
  10. plt.show()
    HTML(GenericChart.toChartHTML(chart))

    Line charts display the value of the property you selected in a time series.

Line plot of PE ratio of US Equities Line plot of PE ratio of AAPL & GOOG

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: