Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-0.315
Tracking Error
0.132
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
from AlgorithmImports import *

class BasicTemplateIndexOptionsAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.SetStartDate(2021, 1, 4)
        self.SetEndDate(2021, 2, 1)
        self.SetCash(1000000)

        self.spx = self.AddIndex("VIX", Resolution.Daily).Symbol
        spxOptions = self.AddIndexOption(self.spx, Resolution.Daily)
        spxOptions.SetFilter(lambda x: x.CallsOnly())

        self.emaSlow = self.EMA(self.spx, 80, Resolution.Daily)
        self.emaFast = self.EMA(self.spx, 200, Resolution.Daily)

    def OnData(self, data: Slice) -> None:
        if self.spx not in data.Bars or not self.emaSlow.IsReady:
            return

        for chain in data.OptionChains.Values:
            for contract in chain.Contracts.Values:
                if self.Portfolio.Invested:
                    continue

                if (self.emaFast > self.emaSlow and contract.Right == OptionRight.Call) or \
                    (self.emaFast < self.emaSlow and contract.Right == OptionRight.Put):

                    self.Liquidate(self.InvertOption(contract.Symbol))
                    self.MarketOrder(contract.Symbol, 1)

    def OnEndOfAlgorithm(self) -> None:
        if self.Portfolio[self.spx].TotalSaleVolume > 0:
            raise Exception("Index is not tradable.")

        if self.Portfolio.TotalSaleVolume == 0:
            raise Exception("Trade volume should be greater than zero by the end of this algorithm")

    def InvertOption(self, symbol: Symbol) -> Symbol:
        return Symbol.CreateOption(
            symbol.Underlying,
            symbol.ID.Market,
            symbol.ID.OptionStyle,
            OptionRight.Put if symbol.ID.OptionRight == OptionRight.Call else OptionRight.Call,
            symbol.ID.StrikePrice,
            symbol.ID.Date
        )