Indicators

Trade Bar Indicators

Introduction

This page explains how to create, update, and visualize LEAN TradeBar 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 VolumeWeightedAveragePriceIndicator indicator.

var vwap = new VolumeWeightedAveragePriceIndicator(20);
vwap = VolumeWeightedAveragePriceIndicator(20)

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.

// Create a dataframe with a date index, and columns are indicator values.
var vwapIndicator = qb.Indicator(vwap, symbol, 50, Resolution.Daily);
# Create a dataframe with a date index, and columns are indicator values.
vwap_dataframe = qb.indicator(vwap, symbol, 50, Resolution.DAILY)
Historical VWAP data

Manually Create the Indicator Timeseries

Follow these steps to manually create the indicator timeseries:

  1. Get some historical data.
  2. // Request historical trading data with the daily resolution.
    var history = qb.History(symbol, 70, Resolution.Daily);
    # Request historical trading data with the daily resolution.
    history = qb.history[TradeBar](symbol, 70, Resolution.DAILY)
  3. Set the indicator Window.Sizewindow.size for each attribute of the indicator to hold their values.
  4. // Set the window.size to the desired timeseries length
    vwap.Window.Size = 50;
    # Set the window.size to the desired timeseries length
    vwap.window.size = 50
  5. Iterate through the historical market data and update the indicator.
  6. foreach (var bar in history)
    {
        vwap.Update(bar);
    }
    for bar in history:
        vwap.update(bar)
  7. Display the data.
  8. foreach (var i in Enumerable.Range(0, 5).Reverse())
    {
        Console.WriteLine($"{vwap[i].EndTime:yyyyMMdd} {vwap[i].Value:f4}");
    }
    Historical VWAP data
  9. Populate a DataFrame with the data in the Indicator object.
  10. vwap_dataframe = pd.DataFrame({
        "current": pd.Series({x.end_time: x.value for x in vwap}))
    }).sort_index()
    Historical VWAP 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 TradeBar indicators.

Follow these steps to plot the indicator values:

  1. Call the plot method.
  2. vwap_indicator.plot(title="SPY VWAP(20)", figsize=(15, 10))
  3. Show the plots.
  4. plt.show()
    Line plot of VWAP

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: