Hi all,
If I run my code, I only get response/orders for the first day.
I fail in running the code every day and get errors if I try something with: self.Schedule.On(self.DateRules.EveryDay()
Any idea how I can return my code to start every day? I only want to do 1 trade per day.
from AlgorithmImports import *
class BreakoutCallBuy(QCAlgorithm):
openingBar = None
def Initialize(self):
self.SetStartDate(2022, 7, 1)
self.SetEndDate(2022, 7, 17)
self.SetCash(1000)
self.LastTime = None
self.SetBenchmark("SPY")
equitySPY= self.AddEquity("SPY", Resolution.Minute)
equitySPY.SetDataNormalizationMode(DataNormalizationMode.Raw)
self.equitySPY = equitySPY.Symbol
self.Consolidate("SPY", timedelta(minutes=30), self.OnDataConsolidated)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(9, 30),self.SetFilter)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(15, 15), self.Liquidate)
def SetFilter(self):
optionSPY = self.AddOption("SPY", Resolution.Minute)
optionSPY.SetFilter(10, 20, timedelta(14), timedelta(21))
def OnDataConsolidated(self, bar):
if bar.Time.hour == 9 and bar.Time.minute == 30:
self.openingBar = bar
def OnData(self, data):
if self.openingBar is None:
return
# Maximaal één trade per dag
if self.LastTime == self.Time.day:
return
# Trade openen als er breakout is: prijs is hoger dan hoogste prijs in eerste half uur
if data["SPY"].Close > self.openingBar.High:
for i in data.OptionChains:
chains = i.Value
self.BuyCall(chains)
self.LastTime = self.Time.day
# Bepalen welke contracten er worden gekocht
def BuyCall(self,chains):
expiry = sorted(chains,key = lambda x: x.Expiry, reverse=True)[0].Expiry
calls = [i for i in chains if i.Expiry == expiry and i.Right == OptionRight.Call]
call_contracts = sorted(sorted(calls,
key = lambda x: abs(x.Strike - x.UnderlyingLastPrice)),\
key = lambda x: x.AskPrice, reverse=False)
if len(call_contracts) == 0:
return
self.call = call_contracts[0]
# Aantal contracten
quantity = int(300 / (self.call.AskPrice*100))
self.Buy(self.call.Symbol, quantity)
self.LimitOrder(self.call.Symbol, -quantity, round((self.call.AskPrice*1.25),2))
self.StopMarketOrder(self.call.Symbol, -quantity, round((self.call.AskPrice*0.80),2))
# Orders in logboek
def OnOrderEvent(self, orderEvent):
self.Log(str(orderEvent))
Louis Szeto
Hi Alex
That is normal behavior because no option contracts fit the criteria (10, 20, timedelta(14), timedelta(21)) after the first date of your backtest. Adjust the start and end time of your algorithm.
Also, we don't need to set a scheduled method for option filtering. You may just do
Best
Louis
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.
Alex Veraart
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!