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)
Rahul Chowdhury
Hi Glenn,
The reason you are getting sell events is because this condition is being met
elif holdings >= 0 and self.closePrice < self.slowEMA:
This is because holdings >= 0 is true when holdings = 0, that is when you are not invested.
Instead change your conditions to be False when holdings = 0.
You can also use self.Portfolio.Invested to check if you are invested.
Glenn 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.
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!