I am trying to figure out why this just runs endlessly.  No error is specified and nothing appears in the console when I try running the back test.  

See below: 

  1. class NadionResistanceShield(QCAlgorithm):
  2. #class DataConsolidationAlgorithm(QCAlgorithm):
  3. def Initialize(self):
  4. self.SetStartDate(2021, 1, 1) # Set Start Date
  5. # self.SetEndDate(2019, 1, 3)
  6. self.SetCash(25000) # Set Strategy Cash
  7. self.tickers = ["ADSK","AMD","AMZN","ASML","ATLC","DXCM","ETSY","EXPI","FB","FND","HALO","JYNT","KNSL","MED","MPWR","NFLX","PYPL","SKY","VEEV"]
  8. self.symbolDataBySymbol = {}
  9. self.MarketCaps = ["SPY", "TLT", "GLD", "VNQ"]# "QQQ"]#,"MDY","IWM"]
  10. self.marketDataBySymbol = {}
  11. self.volatilityDataBySymbol = {}
  12. self.vix = ["VIX"]
  13. self.trade = True
  14. self.atr=[]
  15. self.spy = "SPY"
  16. self.iwm = "IWM"
  17. self.mdy = "MDY"
  18. self.qqq = "QQQ"
  19. self.vix = "VIX"
  20. # Before the open
  21. # Trailing distance in $
  22. self.trail_dist = 10
  23. # Declare an attribute that we shall use for storing our
  24. # stop loss ticket.
  25. self.sl_order = None
  26. # Declare an attribute that we will use to store the last trail level
  27. # used. We will use this to decide whether to move the stop
  28. self.last_trail_level = None
  29. for symbolmark in self.MarketCaps:
  30. symbol = self.AddEquity(symbolmark, Resolution.Hour).Symbol
  31. sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
  32. sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
  33. rsi = self.RSI(symbol, 14, Resolution.Daily)
  34. self.marketDataBySymbol[symbol] = symbolMarkData(symbol, sma50, sma200, rsi)
  35. for symbolvol in self.vix:
  36. symbol = self.AddEquity(symbolmark, Resolution.Hour).Symbol
  37. rsi = self.RSI(symbol, 14, Resolution.Daily)
  38. wilr = self.WILR(symbol, 14, Resolution.Daily)
  39. self.volatilityDataBySymbol[symbol] = symbolvolData(symbol, rsi, wilr)
  40. for symbol in self.tickers:
  41. self.AddEquity(symbol, Resolution.Hour)
  42. '''For the below 3 EMA's, you can convert them to 4H bars using the colidator method'''
  43. ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
  44. sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
  45. sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
  46. sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
  47. self.sma = self.SMA(symbol, 20, Resolution.Hour, Field.Close)
  48. sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
  49. ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
  50. ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
  51. rsi = self.RSI(symbol, 14, Resolution.Daily)
  52. wilr = self.WILR(symbol, 14, Resolution.Daily)
  53. wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
  54. atr = self.ATR(symbol, 20, Resolution.Daily)
  55. self.atr.append(self.ATR(symbol, 7, Resolution.Daily))
  56. self.high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
  57. self.longtermfast = self.MAX(symbol, 40, Resolution.Daily, Field.Low)
  58. self.longtermslow = self.MAX(symbol, 50, Resolution.Daily, Field.Low)
  59. self.low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
  60. self.stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
  61. self.sma.Updated += self.OnSMA
  62. '''Consolidator method'''
  63. smaConsolidate = ExponentialMovingAverage(20, MovingAverageType.Simple)
  64. # create the 4 hour data consolidator
  65. fourHourConsolidator = TradeBarConsolidator(timedelta(hours=4))
  66. self.SubscriptionManager.AddConsolidator(symbol, fourHourConsolidator)
  67. # register the 4 hour consolidated bar data to automatically update the indicator
  68. self.RegisterIndicator(symbol, smaConsolidate, fourHourConsolidator)
  69. symbolData = SymbolData(symbol, ema10, sma20, sma200, sma7, sma50, ema20, ema50, rsi, wilr, wilr_fast, atr, smaConsolidate)
  70. self.symbolDataBySymbol[symbol] = symbolData
  71. self.spy = self.AddEquity("SPY", Resolution.Daily)
  72. # Before the open
  73. self.Schedule.On(self.DateRules.EveryDay("SPY"),
  74. self.TimeRules.AfterMarketOpen("SPY", -5),
  75. Action(self.beforeTheOpen))
  76. self.Schedule.On(self.DateRules.EveryDay("SPY"),
  77. self.TimeRules.AfterMarketOpen("SPY", 30), self.buySignals)
  78. self.Schedule.On(self.DateRules.EveryDay("SPY"),
  79. self.TimeRules.AfterMarketOpen("SPY", 30), self.sellSignals)
  80. self.Schedule.On(self.DateRules.EveryDay("SPY"),
  81. self.TimeRules.BeforeMarketClose("SPY", 10), self.buySignals)
  82. self.Schedule.On(self.DateRules.EveryDay("SPY"),
  83. self.TimeRules.BeforeMarketClose("SPY", 10), self.sellSignals)
  84. self.Schedule.On(self.DateRules.EveryDay("SPY"),
  85. self.TimeRules.BeforeMarketClose("SPY", 10), self.stopLoss)
  86. self.Schedule.On(self.DateRules.EveryDay("SPY"),
  87. self.TimeRules.AfterMarketOpen("SPY", 10), self.stopLoss)
  88. #self.AddRiskManagement(TrailingStopRiskManagementModel(0.04))
  89. self.SetWarmUp(timedelta(days=180))
  90. def beforeTheOpen(self):
  91. self.Log("SPY: {0}".format(self.spy.Close))
  92. #for i in range(len(self.tickers)):
  93. # self.Log("ATR: {0}".format(self.atr[i].Current.Value))
  94. def OnData(self, data):
  95. return
  96. # We need to check that the symbol has data before trying to access
  97. # OHLC. Otherwise an exception is raised if the data is missing.
  98. def tradeStart(self):
  99. self.trade = True
  100. def tradeEnd(self):
  101. self.trade = False
  102. def OnOrderEvent(self, OrderEvent):
  103. '''Event when the order is filled. Debug log the order fill. :OrderEvent:'''
  104. if OrderEvent.FillQuantity == 0:
  105. return
  106. # Get the filled order
  107. Order = self.Transactions.GetOrderById(OrderEvent.OrderId)
  108. # Log the filled order details
  109. self.Log("ORDER NOTIFICATION >> {} >> Status: {} Symbol: {}. Quantity: "
  110. "{}. Direction: {}. Fill Price {}".format(str(Order.Tag),
  111. str(OrderEvent.Status),
  112. str(OrderEvent.Symbol),
  113. str(OrderEvent.FillQuantity),
  114. str(OrderEvent.Direction),
  115. str(OrderEvent.FillPrice)))
  116. #self.Log(OrderEvent.FillPrice - symbolData.atr.Current.Value))
  117. def buySignals(self):
  118. if self.trade == False:
  119. return
  120. # Return if benchmark is below SMA
  121. for symbolmark, symbolMarkData in self.marketDataBySymbol.items():
  122. if (self.Securities[symbolmark].Close > symbolMarkData.rsi.Current.Value > 50):
  123. return
  124. for symbolvol, symbolvolData in self.volatilityDataBySymbol.items():
  125. if (self.Securities[symbolvol].Close > symbolvolData.wilr.Current.Value < -20):
  126. return
  127. for symbol, symbolData in self.symbolDataBySymbol.items():
  128. if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close < self.low.Current.Value) and (self.longtermfast.Current.Value > self.longtermslow.Current.Value):
  129. self.SetHoldings(symbol, .1, False, "Buy Signal")
  130. def sellSignals(self):
  131. if self.trade == False:
  132. return
  133. for symbol, symbolData in self.symbolDataBySymbol.items():
  134. if self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > self.high.Current.Value):
  135. self.Liquidate(symbol, "Sell Signal")
  136. # Update our trailing stop loss as necessary
  137. def stopLoss(self):
  138. if self.trade == False:
  139. return
  140. for symbolvol, symbolvolData in self.volatilityDataBySymbol.items():
  141. if (self.Securities[symbolvol].Close > symbolvolData.wilr.Current.Value > -25):
  142. self.Liquidate("Sell Signal")
  143. def OnSMA(self, sender, updated):
  144. if self.sma.IsReady:
  145. #self.Debug(f"SMA Updated on {self.Time} with value: {self.sma.Current.Value}")
  146. return
  147. class symbolMarkData:
  148. def __init__(self, symbol, sma50, sma200, rsi):
  149. self.Symbol = symbol
  150. self.sma50 = sma50
  151. self.sma200 = sma200
  152. self.rsi = rsi
  153. class symbolvolData:
  154. def __init__(self, symbol, rsi, wilr):
  155. self.Symbol = symbol
  156. self.rsi = rsi
  157. self.wilr = wilr
  158. class SymbolData:
  159. def __init__(self, symbol, ema10, sma20, sma50, sma200, sma7, ema20, ema50, rsi, wilr, wilr_fast, atr, smaConsolidate):
  160. self.Symbol = symbol
  161. self.ema10 = ema10
  162. self.sma20 = sma20
  163. self.sma50 = sma50
  164. self.sma200 = sma200
  165. self.sma7 = sma7
  166. self.ema20 = ema20
  167. self.ema50 = ema50
  168. self.rsi = rsi
  169. self.wilr = wilr
  170. self.wilr_fast = wilr_fast
  171. self.atr = atr
  172. #self.emaConsolidate = emaConsolidate
  173. self.smaConsolidate = smaConsolidate
+ Expand

Author

Axist

July 2021