Volatility

Key Concepts

Introduction

Volatility models measure the historical volatility of an asset. They are mostly used to calculate the volatility of the underlying security of an Option because the implied volatility of an Option contract needs an initial guess. The historical volatility doesn't need to be the standard deviation of the asset prices. The various volatility models in LEAN each have a unique methodology to calculate volatility.

Set Models

To set the volatility model of the underlying security of an Option, set the VolatilityModel property of the Security object. The volatility model can have a different resolution than the underlying asset subscription.

// In Initialize
var underlyingSecurity= AddEquity("SPY");
underlyingSecurity.VolatilityModel = new StandardDeviationOfReturnsVolatilityModel(30);
# In Initialize
underlying_security = self.AddEquity("SPY")
underlying_security.VolatilityModel = StandardDeviationOfReturnsVolatilityModel(30)

You can also set the volatility model in a security initializer. If your algorithm has a universe of underlying assets, use the security initializer technique. In order to initialize single security subscriptions with the security initializer, call SetSecurityInitializer before you create the subscriptions.

// In Initialize
SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices)));

// Outside of the algorithm class
class MySecurityInitializer : BrokerageModelSecurityInitializer
{
    public MySecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder)
        : base(brokerageModel, securitySeeder) {}
    
    public override void Initialize(Security security)
    {
        // First, call the superclass definition
        // This method sets the reality models of each security using the default reality models of the brokerage model
        base.Initialize(security);

        // Next, overwrite the volatility model
        if (security.Type == SecurityType.Equity)
        {
            security.VolatilityModel = new StandardDeviationOfReturnsVolatilityModel(30);
        }
    }
}
# In Initialize
self.SetSecurityInitializer(MySecurityInitializer(self.BrokerageModel, FuncSecuritySeeder(self.GetLastKnownPrices)))

# Outside of the algorithm class
class MySecurityInitializer(BrokerageModelSecurityInitializer):

    def __init__(self, brokerage_model: IBrokerageModel, security_seeder: ISecuritySeeder) -> None:
        super().__init__(brokerage_model, security_seeder)

    def Initialize(self, security: Security) -> None:
        # First, call the superclass definition
        # This method sets the reality models of each security using the default reality models of the brokerage model
        super().Initialize(security)

        # Next, overwrite the volatility model
        if security.Type == SecurityType.Equity:
            security.VolatilityModel = StandardDeviationOfReturnsVolatilityModel(30)

To view all the pre-built volatility models, see Supported Models.

Default Behavior

The default underlying volatility model for Equity Options and Index Options is the StandardDeviationOfReturnsVolatilityModel based on 30 days of daily resolution data. The default underlying volatility model for Future Options is the NullVolatilityModel.

Model Structure

Volatility models should extend the BaseVolatilityModel class. Extensions of the BaseVolatilityModel class must have Update and GetHistoryRequirements methods and a Volatility property. The Update method receives Security and BaseData objects and then updates the Volatility. The GetHistoryRequirements method receives Security and DateTimedatetime objects and then returns a list of HistoryRequest objects that represent the history requests to warm up the model. Volatility models receive data at each time step in the algorithm to update their state.

public class MyVolatilityModel : BaseVolatilityModel
{
    public override decimal Volatility { get; }

    public override void SetSubscriptionDataConfigProvider(
        ISubscriptionDataConfigProvider subscriptionDataConfigProvider)
    {
        SubscriptionDataConfigProvider = subscriptionDataConfigProvider;
    }

    public override void Update(Security security, BaseData data)
    {
    }

    public override IEnumerable<HistoryRequest> GetHistoryRequirements(
        Security security,
        DateTime utcTime)
    {
        return base.GetHistoryRequirements(security, utcTime);
    }

    public IEnumerable<HistoryRequest> GetHistoryRequirements(
        Security security, 
        DateTime utcTime,
        Resolution? resolution,
        int barCount)
    {
        return base.GetHistoryRequirements(security, utcTime, resolution, barCount);
    }
}
class MyVolatilityModel(BaseVolatilityModel):
    Volatility: float = 0

    def SetSubscriptionDataConfigProvider(self,
         subscriptionDataConfigProvider: ISubscriptionDataConfigProvider) -> None:
        SubscriptionDataConfigProvider = subscriptionDataConfigProvider

    def Update(self, security: Security, data: BaseData) -> None:
        pass

    def GetHistoryRequirements(self,
         security: Security,
         utcTime: datetime,
         resolution: Resolution = None,
         barCount: int = None) -> List[HistoryRequest]:
        return super().GetHistoryRequirements(security, utcTime, resolution, barCount)

Warm Up Models

To use your volatility model as the inital guess for the implied volatility, warm up the volatility model of the underlying security. If you subscribe to all the Options in the Initialize method, set a warm-up period to warm up their volatility models. The warm-up period should provide the volatility models with enough data to compute their values.

// In Initialize
SetWarmUp(30, Resolution.Daily);

// In OnData
if (IsWarmingUp) return;
# In Initialize
self.SetWarmUp(30, Resolution.Daily)

# In OnData
if self.IsWarmingUp:
    return

If you have a dynamic universe of underlying assets and add Option contracts to your algorithm with the AddOptionContract, AddIndexOptionContract, or AddFutureOptionContract methods, warm up the volatility model when the underlying asset enters your universe. We recommend you do this inside a security initializer.

// In Initialize
SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices), this));

// Outside of the algorithm class
class MySecurityInitializer : BrokerageModelSecurityInitializer
{
    private QCAlgorithm _algorithm;

    public MySecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder, QCAlgorithm algorithm)
        : base(brokerageModel, securitySeeder) 
    {
        _algorithm = algorithm;
    }
    
    public override void Initialize(Security security)
    {
        // First, call the superclass definition
        // This method sets the reality models of each security using the default reality models of the brokerage model
        base.Initialize(security);

        // Next, overwrite and warm up the volatility model
        if (security.Type == SecurityType.Equity) // Underlying asset type
        {
            security.VolatilityModel = new StandardDeviationOfReturnsVolatilityModel(30);
            foreach (var tradeBar in _algorithm.History(security.Symbol, 30, Resolution.Daily))
            {
                security.VolatilityModel.Update(security, tradeBar);
            }
        }
    }
}
# In Initialize
self.SetSecurityInitializer(MySecurityInitializer(self.BrokerageModel, FuncSecuritySeeder(self.GetLastKnownPrices), self))

# Outside of the algorithm class
class MySecurityInitializer(BrokerageModelSecurityInitializer):

    def __init__(self, brokerage_model: IBrokerageModel, security_seeder: ISecuritySeeder, algorithm: QCAlgorithm) -> None:
        super().__init__(brokerage_model, security_seeder)
        self.algorithm = algorithm

    def Initialize(self, security: Security) -> None:
        # First, call the superclass definition
        # This method sets the reality models of each security using the default reality models of the brokerage model
        super().Initialize(security)

        # Next, overwrite and warm up the volatility model
        if security.Type == SecurityType.Equity:  # Underlying asset type
            security.VolatilityModel = StandardDeviationOfReturnsVolatilityModel(30)
            trade_bars = self.algorithm.History[TradeBar](security.Symbol, 30, Resolution.Daily)
            for trade_bar in trade_bars:
                security.VolatilityModel.Update(security, trade_bar)

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: