# https://quantpedia.com/Screener/Details/20
from datetime import timedelta
from decimal import Decimal
class BullCallSpreadAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 3, 1)
self.SetEndDate(2018, 10, 1)
self.SetCash(50000)
equity = self.AddEquity("SPY", Resolution.Minute)
option = self.AddOption("SPY", Resolution.Minute)
self.symbol = equity.Symbol
option.SetFilter(self.UniverseFunc)
self.SetBenchmark(equity.Symbol)
def OnData(self,slice):
for i in slice.OptionChains:
chains = i.Value
if not self.Portfolio.Invested:
# divide option chains into call and put options
calls = list(filter(lambda x: x.Right == OptionRight.Call, chains))
puts = list(filter(lambda x: x.Right == OptionRight.Put, chains))
# if lists are empty return
if not calls or not puts: return
underlying_price = self.Securities[self.symbol].Price
expiries = [i.Expiry for i in puts]
# determine expiration date nearly one month
expiry = min(expiries, key=lambda x: abs((x.date()-self.Time.date()).days-30))
strikes = [i.Strike for i in puts]
# determine at-the-money strike
strike = min(strikes, key=lambda x: abs(x-underlying_price))
# determine 15% out-of-the-money strike
otm_strike = min(strikes, key = lambda x:abs(x-Decimal(0.85)*underlying_price))
self.atm_call = [i for i in calls if i.Expiry == expiry and i.Strike == strike]
self.atm_put = [i for i in puts if i.Expiry == expiry and i.Strike == strike]
self.otm_put = [i for i in puts if i.Expiry == expiry and i.Strike == otm_strike]
if self.atm_call and self.atm_put and self.otm_put:
# sell at-the-money straddle
self.Sell(self.atm_call[0].Symbol, 1)
self.Sell(self.atm_put[0].Symbol, 1)
# buy 15% out-of-the-money put
self.Buy(self.otm_put[0].Symbol, 1)
if self.Portfolio[self.symbol].Invested:
self.Liquidate(self.symbol)
def OnOrderEvent(self, orderEvent):
self.Log(str(orderEvent))
def UniverseFunc(self, universe):
return universe.IncludeWeeklys().Strikes(-20, 20).Expiration(timedelta(25), timedelta(35))
Can someone explain why Running the following backtest results Runtime Error (Open Stacktrace) ? What does this error mean?
Also, why it takes so long to run it? Any ways to speed it up?
Alexandre Catarino
Hi maxim gir ,
Sorry about the long wait.
I look at this issue a couple of days after you wrote this report.
We couldn't reproduce the runtime error and the information you gave us was not sufficient (you can click on the Open Stacktrace, it is a link, to provide detailed information with the error stacktrace).Â
On the speed issue, I ran the algorithm without the logic in OnData (the fastest an algorithm can run) and the speed looked pretty good (around 180k data points per second). With OnData, in other words, with the full code, it ran with 90k points per second.
Meanwhile, we made some improved and I've just run the algorithm with the following performance:
Algorithm Id:(...) completed in 1610.93 seconds at 236k data points per second.
Processing total of 380,627,682 data points.
It took 26:51 which is a while, but it has processed over 380 million data points...Â
You can speed it up by narrowing the universe (SetFilter selecting fewer contracts) or using OptionChainProvider to add contracts manually.
Rahul Chowdhury
Hey Maxim,
The reason you are receiving a runtime error while we are not is probably because you are running out of RAM.Try reducing the amount of memory you are using by reducing the size of the options universe you are accessing. You can accomplish this by changing your filter to narrow the universe.
Try setting the filter to
def UniverseFunc(self, universe):
return universe.IncludeWeeklys().Strikes(-5, 5).Expiration(timedelta(25), timedelta(35))
If the problem persists, please don't hesitate to let us know.Hope that helped!
Best
Rahul
Maxim gir
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!