Supported Indicators

Value At Risk

Introduction

This indicator computes 1-day VaR for a specified confidence level and lookback period

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

Using VAR Indicator

To create an automatic indicator for ValueAtRisk, call the VARvar helper method from the QCAlgorithm class. The VARvar method creates a ValueAtRisk 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 ValueAtRiskAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private ValueAtRisk _var;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _var = VAR(_symbol, 252, 0.95);
    }

    public override void OnData(Slice data)
    {

        if (_var.IsReady)
        {
            // The current value of _var is represented by itself (_var)
            // or _var.Current.Value
            Plot("ValueAtRisk", "var", _var);
        }
    }
}
class ValueAtRiskAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._var = self.var(self._symbol, 252, 0.95)

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

        if self._var.is_ready:
            # The current value of self._var is represented by self._var.current.value
            self.plot("ValueAtRisk", "var", self._var.current.value)

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

You can manually create a ValueAtRisk 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 ValueAtRiskAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private ValueAtRisk _valueatrisk;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _valueatrisk = new ValueAtRisk(252, 0.95);
    }

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

        if (_valueatrisk.IsReady)
        {
            // The current value of _valueatrisk is represented by itself (_valueatrisk)
            // or _valueatrisk.Current.Value
            Plot("ValueAtRisk", "valueatrisk", _valueatrisk);
        }
    }
}
class ValueAtRiskAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._valueatrisk = ValueAtRisk(252, 0.95)

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

        if self._valueatrisk.is_ready:
            # The current value of self._valueatrisk is represented by self._valueatrisk.current.value
            self.plot("ValueAtRisk", "valueatrisk", self._valueatrisk.current.value)

For more information about this indicator, see its referencereference.

Visualization

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

ValueAtRisk line plot.

Indicator History

To get the historical data of the ValueAtRisk 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 ValueAtRiskAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private ValueAtRisk _var;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _var = VAR(_symbol, 252, 0.95);

        var indicatorHistory = IndicatorHistory(_var, _symbol, 100, Resolution.Minute);
        var timeSpanIndicatorHistory = IndicatorHistory(_var, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
        var timePeriodIndicatorHistory = IndicatorHistory(_var, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
    }
}
class ValueAtRiskAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._var = self.var(self._symbol, 252, 0.95)

        indicator_history = self.indicator_history(self._var, self._symbol, 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._var, self._symbol, timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._var, self._symbol, 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: