I am trying to run the below code but cannot get pass the build stage (error: Cannot get rid of this error: Build Error: File: n/a Line:0 Column:0 - Python class failed to compile)
Any suggestions on why this error is occurring? Seems to be random.
class PriceCrossingEMA(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 1, 1)
self.SetEndDate(2021, 12, 31)
self.SetCash(100000)
self.symbol = self.AddEquity("QQQ", Resolution.Minute).Symbol
self.Consolidate(self.symbol, timedelta(minutes=5), self.BarHandler)
self.emaShort = self.EMA(self.symbol, 50, Resolution.Minute)
self.RegisterIndicator(self.symbol, self.emaShort, timedelta(minutes=5))
self.emaLong = self.EMA(self.symbol, 200, Resolution.Minute)
self.RegisterIndicator(self.symbol, self.emaLong, timedelta(minutes=5))
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.lastEmaShort = None
self.lastEmaLong = None
self.lastPriceHigh = None
self.lastPriceLow = None
self.lastPriceClose = None
def OnData(self, data):
if not self.emaLong.IsReady:
return
if self.lastEmaShort is None \
or self.lastEmaLong is None \
or self.lastPriceHigh is None \
or self.lastPriceLow is None \
or self.lastPriceClose is None:
self.lastEmaShort = self.emaShort.Current.Value
self.lastEmaLong = self.emaLong.Current.Value
self.lastPriceHigh = self.Securities[self.symbol].High
self.lastPriceLow = self.Securities[self.symbol].Low
self.lastPriceClose = self.Securities[self.symbol].Close
if not self.Portfolio.Invested:
#price crossing over/down EMA Long
if self.lastPriceClose > self.lastEmaLong and self.Securities[self.symbol].Close < self.emaLong.Current.Value:
self.SetHoldings(self.symbol, -1)
self.stopPrice = self.Securities[self.symbol].High
self.tradeQuantity = self.Portfolio[self.symbol].Quantity
self.StopMarketOrder(self.symbol, self.tradeQuantity, self.stopPrice)
#price crossing over/up EMA Long
elif self.lastPriceClose < self.lastEmaLong and self.Securities[self.symbol].Close > self.emaLong.Current.Value:
self.SetHoldings(self.symbol, 1)
self.stopPrice = self.Securities[self.symbol].Low
self.tradeQuantity = self.Portfolio[self.symbol].Quantity
self.StopMarketOrder(self.symbol, -self.tradeQuantity, self.stopPrice)
if self.Portfolio[self.symbol].IsShort:
if self.Securities[self.symbol].Close > self.emaShort.Current.Value:
self.Transactions.CancelOpenOrders()
self.Liquidate()
elif self.Portfolio[self.symbol].IsLong:
if self.Securities[self.symbol].Close < self.emaShort.Current.Value:
self.Transactions.CancelOpenOrders()
self.Liquidate()
self.lastEmaShort = self.emaShort.Current.Value
self.lastEmaLong = self.emaLong.Current.Value
self.lastPriceHigh = self.Securities[self.symbol].High
self.lastPriceLow = self.Securities[self.symbol].Low
self.lastPriceClose = self.Securities[self.symbol].Close
def BarHandler(self, consolidated):
return
#def OnOrderEvent(self, orderEvent):
# if orderEvent.Status == OrderStatus.Filled:
Vladimir
James HawkinsÂ
There are some inconsistencies in the code you posted like this on line 44:
The attached backtest can be a good starting point to continue developing your algorithm.
If you are satisfied with my answer, please accept it and don't forget to like it
Vladimir
James HawkinsÂ
Did you have a chance to try what I recommended to you?
James Hawkins
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!