Wow Hi Jared! Thank you for even taking the time to read my question!
I've finally finished boot camp, and I've tried to piece together what I've learned there to make some semblance of what I want to build (Mostly pulling from the "Fading the Gap" and "Trailing Stop" tutorials). I'm afraid I'm still far off the mark. The main questions I still have are 1) How can I continually scan all Nasdaq stocks instead of just looking at one equity? and 2) How can I buy only when the high of the day has been broken?
I'd love if you could point me in the direction of where I can learn those things! Here is my work-in-progress attempt so far:
class NotebookProject(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 12, 1)
self.SetEndDate(2018, 12, 10)
self.SetCash(100000)
spy = self.AddEquity("SPY", Resolution.Second)
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
#Schedule event for 3:50pm to close any open positions
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(15,50), self.ClosePositions)
#Set scheduled events to scan for any security gapping up 10%+ from previous close
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("SPY", 0), self.ClosingBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 1), self.OpeningBar)
#Set this as a relatively infinite rolling window to continually scan throughout the day
self.window = RollingWindow[TradeBar](23,400)
def OnData(self, data):
if data["SPY"] is not None:
#2. Update our standard deviation indicator manually with algorithm time and TSLA's close price
self.window.Update(self.Time, data["SPY"].Close)
def OpeningBar(self):
if self.CurrentSlice["SPY"] is not None:
self.window.Add(self.CurrentSlice["TSLA"])
#3. Use IsReady to check if both volatility and the window are ready, if not ready 'return'
if not self.window.IsReady:
return
#Set up gapping calculation
delta = self.window[0].Open / self.window[1].Close
#Scan for securities gapping 10%+ over previous day's close
#?Only buy when highest price of day is broken and only if security is not already owned?
if delta > 1.1:
self.StopOrder("SPY", .05)
else:
#1. Check if the SPY price is higher that highestSPYPrice.
if self.Securities["SPY"].Close > self.highestSPYPrice:
#2. Save the new high to highestSPYPrice; then update the stop price to 98% of highestSPYPrice
self.highestSPYPrice = self.Securities["SPY"].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.highestSPYPrice * 0.98
self.stopMarketTicket.Update(updateFields)
def ClosePositions(self):
self.Liquidate("SPY")
def ClosingBar(self):
self.window.Add(self.CurrentSlice["SPY"])