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.061
Tracking Error
0.192
Treynor Ratio
0
Total Fees
$0.00
from datetime import timedelta

class BasicTemplateOptionsAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 1, 1)  
        self.SetEndDate(2018, 4, 30)  
        self.SetCash(30000)
        equity = self.AddEquity("SPY", Resolution.Minute)
        option = self.AddOption("SPY", Resolution.Minute)
        self.symbol = option.Symbol
        equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
        option.SetFilter(-30, 30, timedelta(15), timedelta(45))
        self.SetBenchmark(equity.Symbol)
        

    def OnData(self,slice):
        optionchain = slice.OptionChains
        for i in slice.OptionChains:
            if i.Key != self.symbol: continue
            chains = i.Value
            contract_list = [x for x in chains]
        if (slice.OptionChains.Count == 0) or (len(contract_list) == 0): 
            return 
        if not self.Portfolio["SPY"].Quantity == 100: 
            self.TradePuts(optionchain) 
        elif self.Portfolio["SPY"].Quantity == 100:
            self.TradeCalls(optionchain) 
            
    def TradePuts(self,optionchain):
         for i in optionchain:
            if i.Key != self.symbol: continue
            chain = i.Value
            expiry = sorted(chain,key = lambda x: x.Expiry, reverse=True)[0].Expiry
            put = [i for i in chain if i.Expiry == expiry and i.Right == 1 and i.Greeks.Delta <=-30]
            put_contracts = sorted(put,key = lambda x: x.Greeks.Delta)
            if len(put_contracts) == 0: continue
            self.put = put_contracts[0]
            self.Sell(self.put.Symbol ,1)
    
    def TradeCalls(self,optionchain):
         for i in optionchain:
            if i.Key != self.symbol: continue
            chain = i.Value
            expiry = sorted(chain,key = lambda x: x.Expiry, reverse=True)[0].Expiry
            call = [i for i in chain if i.Expiry == expiry and i.Right == 0 and i.Strike == self.Portfolio["SPY"].AvergaePrice]
            if len(call) == 0: continue
            self.call = call[0]
            self.Sell(self.call.Symbol ,1)

    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))