class UglyMagentaRhinoceros(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) # Set Start Date
self.SetEndDate(2020, 1, 19)
self.SetCash(10000) # Set Strategy Cash
#self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
aapl = self.AddEquity("AAPL", Resolution.Minute)
self.symbol = aapl.Symbol
self.SetWarmUp(50)
self.window = RollingWindow[TradeBar](5)
self.SetBenchmark('AAPL')
self.tol = 1.001
self.lon = False
self.shortSMA = self.EMA(self.symbol,9,Resolution.Minute)
self.medSMA = self.EMA(self.symbol,21,Resolution.Minute)
self.lonSMA = self.EMA(self.symbol,50,Resolution.Minute)
self.Schedule.On(self.DateRules.EveryDay(self.symbol),
self.TimeRules.BeforeMarketClose(self.symbol, 1),
self.EveryDayBeforeMarketClose)
self.Securities["AAPL"].FeeModel = ConstantFeeModel(0)
def OnData(self, slice):
if self.IsWarmingUp:
return
if slice.Bars.ContainsKey(self.symbol) and (slice[self.symbol] is not None):
self.window.Add(slice[self.symbol])
if self.window.IsReady and (self.window[0] is not None):
#self.Log(str(self.Time)+str(slice[self.symbol])+str(" ")+str(self.window[0].Open))
firstOpen = self.window[3].Open
firstClose = self.window[3].Close
firstVol = self.window[3].Volume
secondOpen = self.window[2].Open
secondClose = self.window[2].Close
secondVol = self.window[2].Volume
thirdOpen = self.window[1].Open
thirdClose = self.window[1].Close
thirdVol = self.window[1].Volume
price = self.window[0].Open
else:
#self.Debug(str("No data on window..."))
return
#if not self.Securities["AAPL"].HasData: return
# entry conditions
if (not self.Portfolio.Invested) and (price > self.shortSMA.Current.Value * self.tol ) and (self.shortSMA.Current.Value > self.medSMA.Current.Value) and self.lon == False:
if (firstOpen < firstClose) and (self.window[4].Open >= self.window[4].Close):
if firstClose <= secondOpen <= secondClose :
if (secondClose <= thirdOpen < thirdClose) and ((secondVol > thirdVol and firstVol > thirdVol and secondVol < firstVol) or (secondVol > thirdVol and firstVol > thirdVol and secondVol > firstVol) or (((firstVol+secondVol)/2) < (firstVol+thirdVol)/2)):
if price >= thirdClose :
num_shares = int(self.CalculateOrderQuantity(self.symbol, 1))
self.MarketOrder(self.symbol,num_shares)
self.purch = price
self.Debug(str(self.Time)+str(slice[self.symbol])+str(" Buy @")+str(price))
return
# exit conditions
if self.Portfolio.Invested:
if self.purch < self.window[0].Close:
self.SetHoldings(self.symbol,0)
self.Debug(str(self.Time)+str(slice[self.symbol])+str(" Profit @ ")+str(price))
def EveryDayBeforeMarketClose(self):
# Exit
if self.Portfolio.Invested:
self.SetHoldings(self.symbol,0)
self.Debug(str(self.Time)+ str(" End of Day Close"))