Data Point Indicators


This page explains how to create, update, and visualize LEAN data-point indicators.

Create Subscriptions

You need to subscribe to some market data in order to calculate indicator values.

var qb = new QuantBook();
var symbol = qb.AddEquity("SPY").Symbol;
qb = QuantBook()
symbol = qb.add_equity("SPY").symbol

Create Indicator Timeseries

You need to subscribe to some market data and create an indicator in order to calculate a timeseries of indicator values. In this example, use a 20-period 2-standard-deviation BollingerBands indicator.

var bb = new BollingerBands(20, 2);
bb = BollingerBands(20, 2)

You can create the indicator timeseries with the Indicator helper method or you can manually create the timeseries.

Indicator Helper Method

To create an indicator timeseries with the helper method, call the Indicator method.

var bbIndicator = qb.Indicator(bb, symbol, 50, Resolution.Daily);
bb_dataframe = qb.indicator(bb, symbol, 50, Resolution.DAILY)
Historical bollinger band data

Manually Create the Indicator Timeseries

Follow these steps to manually create the indicator timeseries:

  1. Get some historical data.
  2. var history = qb.History(symbol, 70, Resolution.Daily);
    history = qb.history[TradeBar](symbol, 70, Resolution.DAILY)
  3. Create a RollingWindow for each attribute of the indicator to hold their values.
  4. var time = new RollingWindow<DateTime>(50);
    var window = new Dictionary<string, RollingWindow<decimal>>();
    window["bollingerbands"] = new RollingWindow<decimal>(50);
    window["lowerband"] = new RollingWindow<decimal>(50);
    window["middleband"] = new RollingWindow<decimal>(50);
    window["upperband"] = new RollingWindow<decimal>(50);
    window["bandwidth"] = new RollingWindow<decimal>(50);
    window["percentb"] = new RollingWindow<decimal>(50);
    window["standarddeviation"] = new RollingWindow<decimal>(50);
    window["price"] = new RollingWindow<decimal>(50);
    window = {}
    window['time'] = RollingWindow[DateTime](50)
    window["bollingerbands"] = RollingWindow[float](50)
    window["lowerband"] = RollingWindow[float](50)
    window["middleband"] = RollingWindow[float](50)
    window["upperband"] = RollingWindow[float](50)
    window["bandwidth"] = RollingWindow[float](50)
    window["percentb"] = RollingWindow[float](50)
    window["standarddeviation"] = RollingWindow[float](50)
    window["price"] = RollingWindow[float](50)
  5. Attach a handler method to the indicator that updates the RollingWindow objects.
  6. bb.Updated += (sender, updated) => 
        var indicator = (BollingerBands)sender;
    def update_bollinger_band_window(sender: object, updated: IndicatorDataPoint) -> None:
        indicator = sender
    bb.updated += UpdateBollingerBandWindow

    When the indicator receives new data, the preceding handler method adds the new IndicatorDataPoint values into the respective RollingWindow.

  7. Iterate through the historical market data and update the indicator.
  8. foreach(var bar in history)
        bb.Update(bar.EndTime, bar.Close);
    for bar in history:
        bb.update(bar.end_time, bar.close)
  9. Display the data.
  10. Console.WriteLine($"time,{string.Join(',', window.Select(kvp => kvp.Key))}");
    foreach (var i in Enumerable.Range(0, 5).Reverse())
        var data = string.Join(", ", window.Select(kvp => Math.Round(kvp.Value[i],6)));
        Console.WriteLine($"{time[i]:yyyyMMdd}, {data}");
    Historical bollinger band data
  11. Populate a DataFrame with the data in the RollingWindow objects.
  12. bb_dataframe = pd.DataFrame(window).set_index('time')
    Historical bollinger band data

Plot Indicators

Jupyter Notebooks don't currently support libraries to plot historical data, but we are working on adding the functionality. Until the functionality is added, use Python to plot data point indicators.

You need to create an indicator timeseries to plot the indicator values.

Follow these steps to plot the indicator values:

  1. Select the columns/features to plot.
  2. bb_plot = bb_indicator[["upperband", "middleband", "lowerband", "price"]]
  3. Call the plot method.
  4. bb_plot.plot(figsize=(15, 10), title="SPY BB(20,2)"))
  5. Show the plots.
    Line plot of bollinger band properties

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: