| Overall Statistics |
|
Total Trades 2 Average Win 0% Average Loss 0% Compounding Annual Return 3.039% Drawdown 0.000% Expectancy 0 Net Profit 0.079% Sharpe Ratio 10.302 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0.02 Beta 0.317 Annual Standard Deviation 0.002 Annual Variance 0 Information Ratio 3.837 Tracking Error 0.002 Treynor Ratio 0.079 Total Fees $1.25 |
import numpy as np
from datetime import timedelta
from QuantConnect.Securities.Option import OptionPriceModels
class OptionChainProvider(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 2, 1)
self.SetEndDate(2017, 2, 10)
self.SetCash(100000)
self.equity = self.AddEquity("SPY", Resolution.Minute)
def OnData(self,data):
if not self.Portfolio.Invested and self.IsMarketOpen(self.equity.Symbol):
# get a symbol list of all option contracts
contracts = self.OptionChainProvider.GetOptionContractList(self.equity.Symbol, data.Time)
self.underlyingPrice = self.Securities[self.equity.Symbol].Price
# filter the out-of-money call options from the contract list which expire in 20 to 30 days from now on
otm_calls = [i for i in contracts if i.ID.OptionRight == OptionRight.Call and
i.ID.StrikePrice - self.underlyingPrice > 0 and
20 < (i.ID.Date - data.Time).days < 30]
# add those contracts
for i in otm_calls:
option = self.AddOptionContract(i, Resolution.Minute)
option.PriceModel = OptionPriceModels.CrankNicolsonFD()
# get the greeks by accessing the OptionChain
if data.OptionChains.Count != 0:
for kvp in data.OptionChains:
chain = kvp.Value
self.Log(str([i.Greeks.Vega for i in chain]))
contract = sorted(sorted(chain, key = lambda x: abs(chain.Underlying.Price - x.Strike)),
key = lambda x: x.Expiry)[0]
self.MarketOrder(contract.Symbol, -1)
self.MarketOrder(self.equity.Symbol, 100)