Overall Statistics
Total Trades
8
Average Win
0.00%
Average Loss
0%
Compounding Annual Return
-15.368%
Drawdown
1.000%
Expectancy
0
Net Profit
-0.540%
Sharpe Ratio
-2.973
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
-0.029
Beta
-6.262
Annual Standard Deviation
0.041
Annual Variance
0.002
Information Ratio
-3.333
Tracking Error
0.041
Treynor Ratio
0.02
Total Fees
$2.00
from QuantConnect.Data.Market import *
from QuantConnect.Data.Consolidators import *
from datetime import *
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Securities.Option import OptionPriceModels
import decimal as d 
import operator

class BasicTemplateOptionsAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 12, 1)
        self.SetEndDate(2017, 12, 12)
        self.SetCash(100000)
        self.symbols = []
        self.fill_price =dict()
        for ticker in ["AER",
"URI", "FISV", "BAC", "HDS", "BF.B", "NEE", "JPM", "EBAY"]:
            option = self.AddOption(ticker)
            self.symbols.append(option.Symbol)
            option.SetFilter(-2, +2, timedelta(0), timedelta(180))
            
            option.PriceModel = OptionPriceModels.CrankNicolsonFD()
            

    def OnData(self,slice):
        option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
        for symbol in self.symbols:
            invested = [option for option in option_invested if option.Underlying == symbol.Underlying]
            
            for kvp in slice.OptionChains:
                if kvp.Key == symbol:
                    chain = kvp.Value
                    contracts_iv = [x for x in chain if x.ImpliedVolatility > d.Decimal(0.4) and x.Greeks.Delta < 0.4]
                    #for x in chain:
                        #self.Log(str(x.ImpliedVolatility))
                        #self.Log(str(x.Strike))
                    contracts = sorted(sorted(sorted(contracts_iv, 
                        key = lambda x: -x.AskPrice/(x.AskPrice+
                        max(20*chain.Underlying.Price - (chain.Underlying.Price - x.Strike), 10*x.Strike))),
                        key = lambda x: x.Expiry, reverse=True),
                        key = lambda x: x.Right, reverse=True)

                    # if found, trade it
                    if len(contracts) == 0: continue
                    contract_symbol = contracts[0].Symbol
                    symbol = contracts[0].Symbol
                    
                    if self.Portfolio[contract_symbol].Invested:
                        self.Log(str(symbol)
                        +str(self.fill_price[symbol])+"::"
                        +str(self.Securities[contract_symbol].Price))
                        if self.fill_price[symbol] > self.Securities[contract_symbol].Price * d.Decimal(1.1):
                            self.Liquidate(contract_symbol)
        
                        
                    if len(invested) > 0: return 
                    ticket=self.MarketOrder(contract_symbol, -1)
                    self.fill_price[symbol] = ticket.AverageFillPrice