コード例 #1
0
    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2020, 1, 5)
        self.SetCash(100000)

        # Add the option
        option = self.AddOption("AAPL")
        self.optionSymbol = option.Symbol

        # Add the initial contract filter
        option.SetFilter(-3, +3, 0, 31)

        # Define the Option Price Model
        option.PriceModel = OptionPriceModels.CrankNicolsonFD()
        #option.PriceModel = OptionPriceModels.BlackScholes()
        #option.PriceModel = OptionPriceModels.AdditiveEquiprobabilities()
        #option.PriceModel = OptionPriceModels.BaroneAdesiWhaley()
        #option.PriceModel = OptionPriceModels.BinomialCoxRossRubinstein()
        #option.PriceModel = OptionPriceModels.BinomialJarrowRudd()
        #option.PriceModel = OptionPriceModels.BinomialJoshi()
        #option.PriceModel = OptionPriceModels.BinomialLeisenReimer()
        #option.PriceModel = OptionPriceModels.BinomialTian()
        #option.PriceModel = OptionPriceModels.BinomialTrigeorgis()
        #option.PriceModel = OptionPriceModels.BjerksundStensland()
        #option.PriceModel = OptionPriceModels.Integral()

        # Set warm up with 30 trading days to warm up the underlying volatility model
        self.SetWarmUp(30, Resolution.Daily)
コード例 #2
0
    def Initialize(self):
        # this test opens position in the first day of trading, lives through stock split (7 for 1), and closes adjusted position on the second day
        self.SetStartDate(2015, 12, 24)
        self.SetEndDate(2015, 12, 24)
        self.SetCash(1000000)

        option = self.AddOption("GOOG")

        option.PriceModel = OptionPriceModels.CrankNicolsonFD()
        option.SetFilter(-2, 2, timedelta(0), timedelta(180))

        self.SetBenchmark(lambda x: 1000000)
コード例 #3
0
    def Initialize(self):
        # this test opens position in the first day of trading, lives through stock split (7 for 1), and closes adjusted position on the second day
        self.SetStartDate(2015, 12, 24)
        self.SetEndDate(2015, 12, 24)
        self.SetCash(1000000)

        option = self.AddOption("GOOG")
        option.PriceModel = OptionPriceModels.CrankNicolsonFD()
        option.SetFilter(-2, 2, timedelta(0), timedelta(180))

        self.underlying = self.AddEquity(option.Symbol.Underlying.Value)
        self.underlying.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.SetBenchmark(self.underlying.Symbol)
コード例 #4
0
    def Initialize(self):
        # this test opens position in the first day of trading, lives through stock split (7 for 1), and closes adjusted position on the second day
        self.SetStartDate(2015, 12, 24)
        self.SetEndDate(2015, 12, 24)
        self.SetCash(1000000)

        option = self.AddOption("GOOG")
        # add the initial contract filter 
        option.SetFilter(-2,2, timedelta(0), timedelta(180))

        # set the pricing model for Greeks and volatility
        # find more pricing models https://www.quantconnect.com/lean/documentation/topic27704.html
        option.PriceModel = OptionPriceModels.CrankNicolsonFD()
        # set the warm-up period for the pricing model
        self.SetWarmUp(TimeSpan.FromDays(4))
        # set the benchmark to be the initial cash
        self.SetBenchmark(lambda x: 1000000)
コード例 #5
0
 def CustomSecurityInitializer(self, security):
     #Intialize each security's prices
     security.SetDataNormalizationMode(DataNormalizationMode.Raw)
     if security.Type == SecurityType.Equity:
         #for equity intialize volatility model and perform history call
         security.VolatilityModel = StandardDeviationOfReturnsVolatilityModel(
             60)
         history = self.History(security.Symbol, 61, Resolution.Daily)
         if history.empty or 'close' not in history.columns:
             return
         for time, row in history.loc[security.Symbol].iterrows():
             trade_bar = TradeBar(time, security.Symbol, row.open, row.high,
                                  row.low, row.close, row.volume)
             security.VolatilityModel.Update(security, trade_bar)
     elif security.Type == SecurityType.Option:
         #Intialize pricing method for backtest
         security.PriceModel = OptionPriceModels.BinomialCoxRossRubinstein()
コード例 #6
0
    def Initialize(self):
        #self.SetStartDate(2014,1,1)  # Set Start Date
        self.SetStartDate(2016, 12, 1)  # Set Start Date
        self.SetEndDate(2017, 12, 1)  # Set End Date
        # self.SetStartDate(2018,10,1)  # Set Start Date
        # self.SetEndDate(2019,2,28)    # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        self.resol = Resolution.Minute  # Set Frequency
        self.tickr = "SPY"
        self.Gamma, self.Delta = d.Decimal(0.0), d.Decimal(0.0)
        self.previous_delta, self.delta_treshold = d.Decimal(0.0), d.Decimal(
            0.05)
        # Add underlying Equity
        self.equity = self.AddEquity(self.tickr, self.resol)
        self.equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.equity_symbol = self.equity.Symbol
        # Add options
        option = self.AddOption(
            self.tickr,
            self.resol)  # Add the option corresponding to underlying stock
        self.option_symbol = option.Symbol

        self.SetBenchmark(self.tickr)

        # For greeks and pricer (needs some warmup)
        option.PriceModel = OptionPriceModels.CrankNicolsonFD(
        )  # both European & American, automatically
        # Warmup is needed for Greeks calculation
        self.SetWarmUp(TimeSpan.FromDays(5))

        self._assignedOption = False
        self.call, self.put = None, None

        # Schedule an event to fire every trading day to close the options for a security the
        # time rule here tells it to fire 10 minutes before SPY's market close
        self.Schedule.On(
            self.DateRules.EveryDay(self.equity_symbol),
            self.TimeRules.BeforeMarketClose(self.equity_symbol, 10),
            Action(self.close_options))
コード例 #7
0
    def Initialize(self):
        self.SetStartDate(2016, 1, 2)  # Set Start Date
        self.SetEndDate(2017, 12, 31)
        self.SetCash(1000000)  # Set Strategy Cash

        #Stock/Option Universe
        self.stock_list = ["SPY"]

        self.equity = self.AddEquity("SPY", Resolution.Minute)
        #self.equity = self.AddEquity("SPY", Resolution.Minute)
        #self.equity = self.AddEquity("QQQ", Resolution.Minute)
        #self.equity = self.AddEquity("IWM", Resolution.Minute)

        optionA = self.AddOption("SPY")
        optionA.SetFilter(-15, 15, TimeSpan.FromDays(3),
                          TimeSpan.FromDays(10))  #Set Options Universe
        optionA.PriceModel = OptionPriceModels.CrankNicolsonFD()
        '''
        optionB = self.AddOption("DIA")
        optionB.SetFilter(-15, 15, TimeSpan.FromDays(3),TimeSpan.FromDays(10))
        optionB.PriceModel = OptionPriceModels.CrankNicolsonFD()
        optionC = self.AddOption("QQQ")
        optionC.SetFilter(-15, 15, TimeSpan.FromDays(3),TimeSpan.FromDays(10))
        optionC.PriceModel = OptionPriceModels.CrankNicolsonFD()
        optionD = self.AddOption("IWM")
        optionD.SetFilter(-15, 15, TimeSpan.FromDays(3),TimeSpan.FromDays(10))
        optionD.PriceModel = OptionPriceModels.CrankNicolsonFD()
        '''
        self.SetWarmUp(TimeSpan.FromDays(60))

        #Lookback period for historic volatility in days
        self.HVPeriod = 30
        self.shortHVPeriod = 3

        #Set spread bounds for trade execution
        self.ShortBound = {"SPY": .247, "QQQ": 1, "DIA": 1, "IWM": 1}
        self.LongBound = {"SPY": 1, "QQQ": 1, "DIA": 1, "IWM": 1}
        self.ExtremeVolBoundLower = {
            "SPY": -1,
            "QQQ": -1,
            "DIA": -1,
            "IWM": -1
        }
        self.ExtremeVolBoundUpper = {"SPY": 1, "QQQ": 1, "DIA": 1, "IWM": 1}

        #type of strategy
        self.LongStrat = "Straddle"
        #self.LongStrat = "Strangle"
        self.ShortStrat = "Short Straddle"
        #self.ShortStrat = "Butterfly"
        #self.ShortStrat = "Condor"
        #self.ShortStrat = "Iron Condor"
        #self.ShortStrat = "Iron Butterfly"

        #status of strategy
        #"None" for not invested yet, or "Straddle", "Strangle", "Butterfly", "Condor", Iron Butterfly", "Iron Condor"
        self.status = {
            "SPY": "None",
            "QQQ": "None",
            "DIA": "None",
            "IWM": "None"
        }
        self.option_symbols = {
            "SPY": (None, None),
            "QQQ": (None, None),
            "DIA": (None, None),
            "IWM": (None, None)
        }
        self.expiration = {"SPY": None, "QQQ": None, "DIA": None, "IWM": None}

        #Trade Pause due to Vol
        self.vol_spike = {"SPY": 1, "QQQ": 1, "DIA": 1, "IWM": 1}

        #Trade Pause Length
        self.days_pause_left = {"SPY": 0, "QQQ": 0, "DIA": 0, "IWM": 0}
        self.pause_length = 3

        #Stop-Loss Triggers
        self.stop_loss = {"SPY": None, "QQQ": None, "DIA": None, "IWM": None}
        self.stop_loss_percentage_bound = .6
        self.stop_percentage = 1 + self.stop_loss_percentage_bound

        #Vix Indicator
        self.vix_indicator_on = False
        self.vix_spike = False
        self.vix_lookback_period = 30
        self.vix_stdevs = 2.25
        self.past_vix = None
        self.AddEquity("VIXY", Resolution.Minute)
        self.vix_pause = 3

        #Delta-Hedge
        self.delta_hedge_on = False