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:
class NadionResistanceShield(QCAlgorithm):
#class DataConsolidationAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 1) # Set Start Date
# self.SetEndDate(2019, 1, 3)
self.SetCash(25000) # Set Strategy Cash
self.tickers = ["ADSK","AMD","AMZN","ASML","ATLC","DXCM","ETSY","EXPI","FB","FND","HALO","JYNT","KNSL","MED","MPWR","NFLX","PYPL","SKY","VEEV"]
self.symbolDataBySymbol = {}
self.MarketCaps = ["SPY", "TLT", "GLD", "VNQ"]# "QQQ"]#,"MDY","IWM"]
self.marketDataBySymbol = {}
self.volatilityDataBySymbol = {}
self.vix = ["VIX"]
self.trade = True
self.atr=[]
self.spy = "SPY"
self.iwm = "IWM"
self.mdy = "MDY"
self.qqq = "QQQ"
self.vix = "VIX"
# Before the open
# Trailing distance in $
self.trail_dist = 10
# Declare an attribute that we shall use for storing our
# stop loss ticket.
self.sl_order = None
# Declare an attribute that we will use to store the last trail level
# used. We will use this to decide whether to move the stop
self.last_trail_level = None
for symbolmark in self.MarketCaps:
symbol = self.AddEquity(symbolmark, Resolution.Hour).Symbol
sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
rsi = self.RSI(symbol, 14, Resolution.Daily)
self.marketDataBySymbol[symbol] = symbolMarkData(symbol, sma50, sma200, rsi)
for symbolvol in self.vix:
symbol = self.AddEquity(symbolmark, Resolution.Hour).Symbol
rsi = self.RSI(symbol, 14, Resolution.Daily)
wilr = self.WILR(symbol, 14, Resolution.Daily)
self.volatilityDataBySymbol[symbol] = symbolvolData(symbol, rsi, wilr)
for symbol in self.tickers:
self.AddEquity(symbol, Resolution.Hour)
'''For the below 3 EMA's, you can convert them to 4H bars using the colidator method'''
ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
self.sma = self.SMA(symbol, 20, Resolution.Hour, Field.Close)
sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
rsi = self.RSI(symbol, 14, Resolution.Daily)
wilr = self.WILR(symbol, 14, Resolution.Daily)
wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
atr = self.ATR(symbol, 20, Resolution.Daily)
self.atr.append(self.ATR(symbol, 7, Resolution.Daily))
self.high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
self.longtermfast = self.MAX(symbol, 40, Resolution.Daily, Field.Low)
self.longtermslow = self.MAX(symbol, 50, Resolution.Daily, Field.Low)
self.low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
self.stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
self.sma.Updated += self.OnSMA
'''Consolidator method'''
smaConsolidate = ExponentialMovingAverage(20, MovingAverageType.Simple)
# create the 4 hour data consolidator
fourHourConsolidator = TradeBarConsolidator(timedelta(hours=4))
self.SubscriptionManager.AddConsolidator(symbol, fourHourConsolidator)
# register the 4 hour consolidated bar data to automatically update the indicator
self.RegisterIndicator(symbol, smaConsolidate, fourHourConsolidator)
symbolData = SymbolData(symbol, ema10, sma20, sma200, sma7, sma50, ema20, ema50, rsi, wilr, wilr_fast, atr, smaConsolidate)
self.symbolDataBySymbol[symbol] = symbolData
self.spy = self.AddEquity("SPY", Resolution.Daily)
# Before the open
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.AfterMarketOpen("SPY", -5),
Action(self.beforeTheOpen))
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.AfterMarketOpen("SPY", 30), self.buySignals)
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.AfterMarketOpen("SPY", 30), self.sellSignals)
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.BeforeMarketClose("SPY", 10), self.buySignals)
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.BeforeMarketClose("SPY", 10), self.sellSignals)
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.BeforeMarketClose("SPY", 10), self.stopLoss)
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.AfterMarketOpen("SPY", 10), self.stopLoss)
#self.AddRiskManagement(TrailingStopRiskManagementModel(0.04))
self.SetWarmUp(timedelta(days=180))
def beforeTheOpen(self):
self.Log("SPY: {0}".format(self.spy.Close))
#for i in range(len(self.tickers)):
# self.Log("ATR: {0}".format(self.atr[i].Current.Value))
def OnData(self, data):
return
# We need to check that the symbol has data before trying to access
# OHLC. Otherwise an exception is raised if the data is missing.
def tradeStart(self):
self.trade = True
def tradeEnd(self):
self.trade = False
def OnOrderEvent(self, OrderEvent):
'''Event when the order is filled. Debug log the order fill. :OrderEvent:'''
if OrderEvent.FillQuantity == 0:
return
# Get the filled order
Order = self.Transactions.GetOrderById(OrderEvent.OrderId)
# Log the filled order details
self.Log("ORDER NOTIFICATION >> {} >> Status: {} Symbol: {}. Quantity: "
"{}. Direction: {}. Fill Price {}".format(str(Order.Tag),
str(OrderEvent.Status),
str(OrderEvent.Symbol),
str(OrderEvent.FillQuantity),
str(OrderEvent.Direction),
str(OrderEvent.FillPrice)))
#self.Log(OrderEvent.FillPrice - symbolData.atr.Current.Value))
def buySignals(self):
if self.trade == False:
return
# Return if benchmark is below SMA
for symbolmark, symbolMarkData in self.marketDataBySymbol.items():
if (self.Securities[symbolmark].Close > symbolMarkData.rsi.Current.Value > 50):
return
for symbolvol, symbolvolData in self.volatilityDataBySymbol.items():
if (self.Securities[symbolvol].Close > symbolvolData.wilr.Current.Value < -20):
return
for symbol, symbolData in self.symbolDataBySymbol.items():
if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close < self.low.Current.Value) and (self.longtermfast.Current.Value > self.longtermslow.Current.Value):
self.SetHoldings(symbol, .1, False, "Buy Signal")
def sellSignals(self):
if self.trade == False:
return
for symbol, symbolData in self.symbolDataBySymbol.items():
if self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > self.high.Current.Value):
self.Liquidate(symbol, "Sell Signal")
# Update our trailing stop loss as necessary
def stopLoss(self):
if self.trade == False:
return
for symbolvol, symbolvolData in self.volatilityDataBySymbol.items():
if (self.Securities[symbolvol].Close > symbolvolData.wilr.Current.Value > -25):
self.Liquidate("Sell Signal")
def OnSMA(self, sender, updated):
if self.sma.IsReady:
#self.Debug(f"SMA Updated on {self.Time} with value: {self.sma.Current.Value}")
return
class symbolMarkData:
def __init__(self, symbol, sma50, sma200, rsi):
self.Symbol = symbol
self.sma50 = sma50
self.sma200 = sma200
self.rsi = rsi
class symbolvolData:
def __init__(self, symbol, rsi, wilr):
self.Symbol = symbol
self.rsi = rsi
self.wilr = wilr
class SymbolData:
def __init__(self, symbol, ema10, sma20, sma50, sma200, sma7, ema20, ema50, rsi, wilr, wilr_fast, atr, smaConsolidate):
self.Symbol = symbol
self.ema10 = ema10
self.sma20 = sma20
self.sma50 = sma50
self.sma200 = sma200
self.sma7 = sma7
self.ema20 = ema20
self.ema50 = ema50
self.rsi = rsi
self.wilr = wilr
self.wilr_fast = wilr_fast
self.atr = atr
#self.emaConsolidate = emaConsolidate
self.smaConsolidate = smaConsolidate
Dean Lim
I have the same problem. I shot them an email. Its best if you send an email according to them.
support@quantconnect.com
Martin Molinero
Hey!
Thanks for the report, we will look into this ASAP. Thanks for sharing a reproducing algorithm too, will help pin the issue and solve it quicker.
Best,
Martin
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.
Martin Molinero
Hey!
From the posted algorithm. line 205 `self.Liquidate("Sell Signal")` isn't valid, should be providing a symbol as an argument here. Either way backtesting should shutdown gracefully we will look into it.
Best,
Martin
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.
Colton Sellers
Merged a fix for this unexpected crash in a new version of PythonNet. The original algorithm shared will still crash, because of the `self.Liquidate("Sell Signal")`, but it won't hang like it was before. 😊
Axist
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!