---
title: "Trade Bar Indicators"
description: "Learn how to create, update, and read trade bar indicators in the research environment in QuantConnect."
canonical: https://www.quantconnect.com/docs/v2/research-environment/indicators/trade-bar-indicators
markdown: https://www.quantconnect.com/docs/v2/research-environment/indicators/trade-bar-indicators.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/04%20Research%20Environment/06%20Indicators/04%20Trade%20Bar%20Indicators
modified: 2026-11-02
index: https://www.quantconnect.com/llms.txt
---

# Trade Bar Indicators

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Research Environment](https://www.quantconnect.com/docs/v2/research-environment.md) > [Indicators](https://www.quantconnect.com/docs/v2/research-environment/indicators.md) > Trade Bar Indicators

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

This page explains how to create, update, and visualize LEAN `TradeBar` indicators.

## Create Subscriptions

You need to [subscribe to some market data](https://www.quantconnect.com/docs/v2/research-environment/datasets/us-equity.md#03-Create-Subscriptions) in order to calculate indicator values.

```python
qb = QuantBook()
symbol = qb.add_equity("SPY").symbol
```

## Create Indicator Timeseries

You need to [subscribe to some market data](https://www.quantconnect.com/docs/v2/research-environment/indicators/trade-bar-indicators.md#03-Create-Subscriptions) and create an indicator in order to calculate a timeseries of indicator values. In this example, use a 20-period `VolumeWeightedAveragePriceIndicator` indicator.

```python
vwap = VolumeWeightedAveragePriceIndicator(20)
```

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

### IndicatorHistory Helper Method

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

```python
# Create a dataframe with a date index, and columns are indicator values.
vwap_dataframe = qb.indicator_history(vwap, symbol, 50, Resolution.DAILY).data_frame
```

![Historical VWAP data](https://cdn.quantconnect.com/i/tu/indicator-tradebar-py-helper-vwap.png)

### Manually Create the Indicator Timeseries

Follow these steps to manually create the indicator timeseries:

1. Get some [historical data](https://www.quantconnect.com/docs/v2/research-environment/datasets/key-concepts.md).
   ```python
   # Request historical trading data with the daily resolution.
   history = qb.history[TradeBar](symbol, 70, Resolution.DAILY)
   ```
2. Set the indicator `window.size` for each attribute of the indicator to hold their values.
   ```python
   # Set the window.size to the desired timeseries length
   vwap.window.size = 50
   ```
3. Iterate through the historical market data and update the indicator.
   ```python
   for bar in history:
       vwap.update(bar)
   ```
4. Populate a `DataFrame` with the data in the `Indicator` object.
   ```python
   vwap_dataframe = pd.DataFrame({
       "current": pd.Series({x.end_time: x.value for x in vwap}))
   }).sort_index()
   ```
   ![Historical VWAP data](https://cdn.quantconnect.com/i/tu/indicator-tradebar-py-classic-vwap.png)

## Plot Indicators

Follow these steps to plot the indicator values:

1. Call the `plot` method.
   ```python
   vwap_indicator.plot(title="SPY VWAP(20)", figsize=(15, 10))
   ```
2. Show the plots.
   ```python
   plt.show()
   ```
   ![Line plot of VWAP](https://cdn.quantconnect.com/i/tu/indicator-tradebar-py-plot.png)

## Examples

The following examples demonstrate some common practices for researching with trade bar indicators.

### Example 1: Quick Backtest On MoneyFlowIndex

The following example demonstrates a quick backtest to testify the effectiveness of a Money Flow Index mean-reversal under the research enviornment.

```python
# Instantiate the QuantBook instance for researching.
qb = QuantBook()
# Request SPY data to work with the indicator.
symbol = qb.add_equity("SPY").symbol

# Get the historical data for trading.
history = qb.history(symbol, 252, Resolution.DAILY).close.unstack(0).iloc[:, 0]
history = history.groupby(history.index.date).sum()

# Create the Money Flow Index with parameters to be studied.
indicator = MoneyFlowIndex(10)

# Get the indicator history of the indicator.
indicator_dataframe = qb.indicator_history(indicator, symbol, 252, Resolution.DAILY).data_frame
indicator_dataframe = indicator_dataframe.groupby(indicator_dataframe.index.date).sum()

# Match index.
common_ix = sorted(set(indicator_dataframe.index).intersection(set(history.index)))
history = history.loc[common_ix]
indicator_dataframe = indicator_dataframe.loc[common_ix]

# Create a order record and return column.
# Buy if the asset is underprice (below 20), sell if overpriced (above 80)
indicator_dataframe["position"] = indicator_dataframe.apply(lambda x: 1 if x.current < 20 else -1 if x.current > 80 else 0, axis=1)
# Get the 1-day forward return.
indicator_dataframe["return"] = history.pct_change().shift(-1).fillna(0)
indicator_dataframe["return"] = indicator_dataframe["position"] * indicator_dataframe["return"]

# Obtain the cumulative return curve as a mini-backtest.
equity_curve = (indicator_dataframe["return"] + 1).cumprod()
equity_curve.plot(title="Equity Curve", ylabel="Equity", xlabel="time")
```
