Overall Statistics |
Total Trades 2 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Probabilistic Sharpe Ratio 0% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $2.37 Estimated Strategy Capacity $35000000.00 Lowest Capacity Asset SPY R735QTJ8XC9X |
class OpeningRangeBreakout(QCAlgorithm): openingBar = None def Initialize(self): self.SetStartDate(2022, 4, 28) self.SetEndDate(2022, 4, 28) self.SetCash(100000) self.stock = self.AddEquity("SPY", Resolution.Minute).Symbol self.max = self.MAX(self.stock, 30, Resolution.Minute) self.min = self.MIN(self.stock, 30, Resolution.Minute) self.SetWarmUp(timedelta(minutes=30)) self.stopMarketTicket = None self.stopMarketTicketShort = None self.highestSPYPrice = 0 # definitely higher than this self.lowestSPYPrice = 1000000 #almost certainly lower than this... self.tradeLimit = True self.spy30High = 0 self.spy30Low = 0 # at 10AM the new method is called to save down values self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.AfterMarketOpen(self.stock, 30), self.First30Values) # if stop loss not hit, close the order before the end of the day self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.BeforeMarketClose(self.stock, 5), self.Liquidate) # saves value of indcators at 10AM def First30Values(self): self.spy30High = self.max.Current.Value self.spy30Low = self.min.Current.Value # resets the value for next day def OnEndOfDay(self): self.spy30High = 0 self.spy30Low = 0 self.stopMarketTicket = None self.stopMarketTicketShort = None self.highestSPYPrice = 0 # definitely higher than this self.lowestSPYPrice = 1000000 #almost certainly lower than this... self.tradeLimit = True def OnData(self, data): if self.IsWarmingUp: return if not (self.max.IsReady or self.min.IsReady): return if self.Portfolio.Invested: if self.Securities["SPY"].High > self.highestSPYPrice: #2. Save the new high to highestSPYPrice; then update the stop price to 90% of highestSPYPrice self.highestSPYPrice = self.Securities["SPY"].High updateFields = UpdateOrderFields() updateFields.StopPrice = self.highestSPYPrice * 0.995 if self.stopMarketTicket != None: ### your update request was returning NoneType because of your conditional logic, attempting to update tickets that did not exist self.stopMarketTicket.Update(updateFields) self.Debug("SPY.High " + str(self.Securities["SPY"].High)) self.Debug("highestSPYPrice " + str(self.highestSPYPrice)) if self.Securities["SPY"].Low < self.lowestSPYPrice: #2. Save the new high to highestSPYPrice; then update the stop price to 90% of highestSPYPrice self.lowestSPYPrice = self.Securities["SPY"].Low updateFields = UpdateOrderFields() updateFields.StopPrice = self.lowestSPYPrice * 1.005 if self.stopMarketTicketShort != None: ### your update request was returning NoneType because of your conditional logic, attempting to update tickets that did not exist self.stopMarketTicketShort.Update(updateFields) self.Debug("SPY.Low " + str(self.Securities["SPY"].Low)) self.Debug("lowestSPYPrice " + str(self.lowestSPYPrice)) if self.Time.hour < 10 or self.Time.hour > 10: return price = self.Securities[self.stock].Price if not self.Portfolio.Invested and self.tradeLimit == True: if price > self.spy30High: self.SetHoldings("SPY", 1.0) self.stopMarketTicket = self.StopMarketOrder("SPY", -self.Portfolio["SPY"].Quantity, 0.995 * price) self.tradeLimit = False # self.limitOrderTicket = self.LimitOrder("SPY", -self.Portfolio["SPY"].Quantity, 1.01 * price) elif price < self.spy30Low: self.SetHoldings("SPY", -1.0) self.stopMarketTicketShort = self.StopMarketOrder("SPY", -self.Portfolio["SPY"].Quantity, 1.005 * price) self.tradeLimit = False # self.limitOrderTicketShort = self.LimitOrder("SPY", -self.Portfolio["SPY"].Quantity, .99 * price) def OnOrderEvent(self, orderEvent): if orderEvent.Status != OrderStatus.Filled: return if self.stopMarketTicket is not None and self.stopMarketTicket.OrderId == orderEvent.OrderId: self.stopMarketOrderFillTime = self.Time elif self.stopMarketTicketShort is not None and self.stopMarketTicketShort.OrderId == orderEvent.OrderId: self.stopMarketOrderShortFillTime = self.Time