Supported Indicators

Beta

Introduction

In technical analysis Beta indicator is used to measure volatility or risk of a target (ETF) relative to the overall risk (volatility) of the reference (market indexes). The Beta indicators compares target's price movement to the movements of the indexes over the same period of time. It is common practice to use the SPX index as a benchmark of the overall reference market when it comes to Beta calculations. The indicator only updates when both assets have a price for a time step. When a bar is missing for one of the assets, the indicator value fills forward to improve the accuracy of the indicator.

To view the implementation of this indicator, see the LEAN GitHub repository.

Using B Indicator

To create an automatic indicator for Beta, call the Bb helper method from the QCAlgorithm class. The Bb method creates a Beta object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. In most cases, you should call the helper method in the Initializeinitialize method.

public class BetaAlgorithm : QCAlgorithm
{
    private Symbol _symbol,_reference;
    private Beta _b;

    public override void Initialize()
    {
        _symbol = AddEquity("QQQ", Resolution.Daily).Symbol;
        _reference = AddEquity("SPY", Resolution.Daily).Symbol;
        _b = B(_symbol, _reference, 20);
    }

    public override void OnData(Slice data)
    {

        if (_b.IsReady)
        {
            // The current value of _b is represented by itself (_b)
            // or _b.Current.Value
            Plot("Beta", "b", _b);
        }
    }
}
class BetaAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("QQQ", Resolution.DAILY).symbol
        self._reference = self.add_equity("SPY", Resolution.DAILY).symbol
        self._b = self.b(self._symbol, self._reference, 20)

    def on_data(self, slice: Slice) -> None:

        if self._b.is_ready:
            # The current value of self._b is represented by self._b.current.value
            self.plot("Beta", "b", self._b.current.value)

For more information about this method, see the QCAlgorithm classQCAlgorithm class.

You can manually create a Beta indicator, so it doesn't automatically update. Manual indicators let you update their values with any data you choose.

Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Updateupdate method. The indicator will only be ready after you prime it with enough data.

public class BetaAlgorithm : QCAlgorithm
{
    private Symbol _symbol,_reference;
    private Beta _beta;

    public override void Initialize()
    {
        _symbol = AddEquity("QQQ", Resolution.Daily).Symbol;
        _reference = AddEquity("SPY", Resolution.Daily).Symbol;
        _beta = new Beta(_symbol, _reference, 20);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
            _beta.Update(bar);
        if (data.Bars.TryGetValue(_reference, out bar))
            _beta.Update(bar);

        if (_beta.IsReady)
        {
            // The current value of _beta is represented by itself (_beta)
            // or _beta.Current.Value
            Plot("Beta", "beta", _beta);
        }
    }
}
class BetaAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("QQQ", Resolution.DAILY).symbol
        self._reference = self.add_equity("SPY", Resolution.DAILY).symbol
        self._beta = Beta(self._symbol, self._reference, 20)

    def on_data(self, slice: Slice) -> None:
        bar = slice.bars.get(self._symbol)
        if bar:
            self._beta.update(bar)
        bar = slice.bars.get(self._reference)
        if bar:
            self._beta.update(bar)

        if self._beta.is_ready:
            # The current value of self._beta is represented by self._beta.current.value
            self.plot("Beta", "beta", self._beta.current.value)

For more information about this indicator, see its referencereference.

Visualization

The following plot shows values for some of the Beta indicator properties:

Beta line plot.

Indicator History

To get the historical data of the Beta indicator, call the IndicatorHistoryself.indicator_history method. This method resets your indicator, makes a history request, and updates the indicator with the historical data. Just like with regular history requests, the IndicatorHistoryindicator_history method supports time periods based on a trailing number of bars, a trailing period of time, or a defined period of time. If you don't provide a resolution argument, it defaults to match the resolution of the security subscription.

public class BetaAlgorithm : QCAlgorithm
{
    private Symbol _symbol,_reference;
    private Beta _b;

    public override void Initialize()
    {
        _symbol = AddEquity("QQQ", Resolution.Daily).Symbol;
        _reference = AddEquity("SPY", Resolution.Daily).Symbol;
        _b = B(_symbol, _reference, 20);

        var indicatorHistory = IndicatorHistory(_b, new[] { _symbol, _reference }, 100, Resolution.Minute);
        var timeSpanIndicatorHistory = IndicatorHistory(_b, new[] { _symbol, _reference }, TimeSpan.FromDays(10), Resolution.Minute);
        var timePeriodIndicatorHistory = IndicatorHistory(_b, new[] { _symbol, _reference }, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
    }
}
class BetaAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("QQQ", Resolution.DAILY).symbol
        self._reference = self.add_equity("SPY", Resolution.DAILY).symbol
        self._b = self.b(self._symbol, self._reference, 20)

        indicator_history = self.indicator_history(self._b, [ self._symbol, self._reference ], 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._b, [ self._symbol, self._reference ], timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._b, [ self._symbol, self._reference ], datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)
    

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: