For some reason I'm getting sell events before even executing any buys. All I'm trying to do is just get scatter plots whenever there's a buy or sell event.
class EmaSimple(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 6, 1) # Set Start Date
self.SetEndDate(2020, 4, 1) #Set End Date
self.SetCash(100000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.AddEquity("WORK", Resolution.Hour)
self.stock = self.Identity("WORK")
# self.spyStock = Identity("SPY")
self.__slow = self.EMA("WORK", 200, Resolution.Hour)
self.PlotIndicator("WORK", self.__slow, self.stock)
stockPlot = Chart("Trade Plot")
stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, 0))
stockPlot.AddSeries(Series('Sell', SeriesType.Scatter, 0))
self.AddChart(stockPlot)
def OnData(self, data):
if not self.__slow.IsReady: return
self.slowEMA = self.__slow.Current.Value
self.closePrice = data["WORK"].Price
tolerance = 0.0025
holdings = self.Portfolio["WORK"].Quantity
signalDelta = self.closePrice - self.slowEMA
if holdings <= 0 and self.closePrice > self.slowEMA:
self.SetHoldings("WORK", 1.0)
self.Plot("Trade Plot", 'Buy', self.closePrice)
elif holdings >= 0 and self.closePrice < self.slowEMA:
self.Liquidate("WORK")
self.Debug("SEll")
self.Plot("Trade Plot", 'Sell', self.closePrice)