Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
-3.04%
Compounding Annual Return
3.287%
Drawdown
24.000%
Expectancy
-1
Net Profit
1.718%
Sharpe Ratio
0.244
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.023
Beta
-1.357
Annual Standard Deviation
0.328
Annual Variance
0.108
Information Ratio
0.279
Tracking Error
0.558
Treynor Ratio
-0.059
Total Fees
$2.50
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from datetime import timedelta


class OptionTrader(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 9, 1)
        self.SetEndDate(2019, 3, 11)
        self.SetCash(100000)
        
        self.AddEquity("QQQ", Resolution.Hour)
        self.rsi = self.RSI("QQQ", 30, MovingAverageType.Simple, Resolution.Hour)
        self.SetWarmUp(10, Resolution.Hour)
        

        option = self.AddOption("QQQ")
        self.option_symbol = option.Symbol

        # set our strike/expiry filter for this option chain
        option.SetFilter(+1, +10, timedelta(0), timedelta(14))

        # use the underlying equity as the benchmark
        self.SetBenchmark("QQQ")

    def OnData(self, slice):
        if self.Portfolio.Invested: return

        for kvp in slice.OptionChains:
            if kvp.Key != self.option_symbol: continue
            chain = kvp.Value
            
            contracts = sorted(sorted(sorted(chain, \
                key = lambda x: abs(chain.Underlying.Price - x.Strike < 0)), \
                key = lambda x: x.Expiry, reverse=True), \
                key = lambda x: x.Right, reverse=True)
                
            option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]

            if self.rsi.IsReady:
                rsi_value = self.rsi.Current.Value
                self.Debug("RSI: "+str(rsi_value))
                
                if rsi_value < 30:
                    if len(contracts) == 0: continue
                    if len(option_invested) == 0:
                        symbol = contracts[0].Symbol
                        self.MarketOrder(symbol, 10)
                        self.Debug("Bought Option on QQQ")
                        self.Log("underlying price:" + str(chain.Underlying.Price))
                    #self.Log(contracts[0])
                
                elif rsi_value > 70:
                    #if len(contracts) == 0: continue
                    if len(option_invested) > 0:
                        self.Debug("RSI: "+str(rsi_value))
                        self.Liquidate()
                        self.Debug("Closed Option on QQQ")