🚀 AI One-Click Generation of quantconnect Strategy Code

How to Access the Current Value of a Technical Indicator in QuantConnect

quantconnect | Published on: 2025/12/3
The following content is generated by EasyQuant .

Problem Description

How do I get the current value from a Bollinger Band indicator I created?

Solution

To get the current value from a Bollinger Band indicator in QuantConnect, you need to access its specific components: the Upper Band, Middle Band, and Lower Band.

Since the Bollinger Band is a composite indicator, you cannot just ask for the "value" of the main object. Instead, you access the .current.value of its specific properties.

Key Properties

Assuming your indicator variable is named self.bb_indicator:

  • Upper Band: self.bb_indicator.upper_band.current.value
  • Middle Band: self.bb_indicator.middle_band.current.value
  • Lower Band: self.bb_indicator.lower_band.current.value

Complete Implementation Example

Here is a fully functional algorithm demonstrating how to initialize the indicator and access these values inside the on_data method.

from AlgorithmImports import *

class BollingerBandsExample(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2023, 1, 1)
        self.set_end_date(2023, 6, 1)
        self.set_cash(100000)

        # 1. Add the security
        self.symbol = self.add_equity("SPY", Resolution.DAILY).symbol

        # 2. Initialize the Bollinger Band Indicator
        # Parameters: Symbol, Period (20), Standard Deviations (2), Moving Average Type
        self.bb_indicator = self.bb(self.symbol, 20, 2, MovingAverageType.SIMPLE)
        
        # Optional: Warm up the indicator so it is ready immediately
        self.set_warm_up(20)

    def on_data(self, slice: Slice):
        # 3. Check if the indicator is ready to use
        if not self.bb_indicator.is_ready:
            return

        # 4. Get the current values
        upper = self.bb_indicator.upper_band.current.value
        middle = self.bb_indicator.middle_band.current.value
        lower = self.bb_indicator.lower_band.current.value
        
        # Get the current price of the asset
        current_price = self.securities[self.symbol].price

        # Example Logic: Buy if price drops below lower band
        if not self.portfolio.invested:
            if current_price < lower:
                self.set_holdings(self.symbol, 1)
                self.log(f"Buy Signal: Price {current_price} < Lower Band {lower}")
        
        # Example Logic: Sell if price rises above upper band
        elif self.portfolio.invested:
            if current_price > upper:
                self.liquidate(self.symbol)
                self.log(f"Sell Signal: Price {current_price} > Upper Band {upper}")

Important Details

  1. is_ready Check: Always check if not self.bb_indicator.is_ready: return before accessing values. If the algorithm hasn't processed enough data points (e.g., fewer than 20 bars for a 20-period BB), the values will be 0 or invalid.
  2. PEP8 Naming: In the modern QuantConnect Python API, properties are snake_case (upper_band, current.value). If you are looking at older examples, you might see PascalCase (UpperBand, Current.Value), but snake_case is the current standard.
  3. Standard Deviation: The middle_band is effectively the Simple Moving Average (SMA) of the price. The upper and lower bands are calculated by adding/subtracting the standard deviation multiplied by the k factor (usually 2).

Q&A

Q: Can I get the Standard Deviation value directly from the Bollinger Band object?
A: Yes. The Bollinger Band object has a property called standard_deviation. You can access its value using self.bb_indicator.standard_deviation.current.value.

Q: How do I access the value from the previous bar (1 step back)?
A: You can use the window index on the indicator. For example, self.bb_indicator.upper_band[1] returns the value of the upper band 1 bar ago. [0] is equivalent to .current.value.

Q: Why does my code throw an error saying 'BollingerBands' object has no attribute 'Current'?
A: This happens because the Bollinger Band is a container for three lines, not a single line. You must specify which line you want (e.g., .upper_band) before asking for .current.