clr.AddReference('QuantConnect.Research')
from QuantConnect.Research import QuantBook
class TachyonMultidimensionalChamber(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 11, 19) # Set Start Date
self.SetEndDate(2020, 11, 19) # Set Start Date
self.SetCash(400000) # Set Strategy Cash
self.AddUniverse(self.CoarseSelectionFunction)
self.SetSecurityInitializer(self.SecurityInitializer)
self.UniverseSettings.ExtendedMarketHours = True
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
self.UniverseSettings.Leverage = 4
#self.UniverseSettings.Resolution = Resolution.Hour #can comment/change this out
#indicators
self.atr = {}
self.sd = {}
self.hod = {}
self.lod = {}
self.open = {}
#self.pmhigh = {}
#self.pmlow = {}
for s in self.Securities:
self.Debug(self.Securities[s])
def SecurityInitializer(self, security):
security.SetLeverage(4)
def CoarseSelectionFunction(self, universe):
selected = []
for coarse in universe:
if coarse.Volume > 70000000 and coarse.Value > 10 and coarse.HasFundamentalData:
symbol = coarse.Symbol
#tickers
selected.append(symbol)
#indicators
return selected #list of objects of type Symbol
def OnSecuritiesChanged(self, changed):
for security in changed.AddedSecurities:
symbol = security.Symbol
if symbol not in self.sd:
self.sd[symbol] = SymbolData(self, symbol)
for security in changed.RemovedSecurities:
symbol = security.Symbol
self.sd.pop(symbol, None)
#is it during market hours or not (e.g. for getting hod and lod)
def isMarketHours(self):
if ( (self.Time.hour == 9 and self.Time.minute >= 30) or self.Time.hour >= 10) and (self.Time.hour <= 15):
return True
else:
return False
#sizing
def positionSize(self, stop, currPrice, dollarSize):
nShares = int(dollarSize / abs(stop - currPrice))
return nShares
# def OnData(self, data):
# '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
# Arguments:
# data: Slice object keyed by symbol containing the stock data
# '''
# #for k in self.vwap:
# # self.Log(type(k)) #<class 'QuantConnect.Symbol'>
# tradeBars = data.Bars #OHLC of past time interval
# #self.Debug(self.Time)
# self.Debug(self.Securities)
# for d in self.vwap: #can't just get tickers from data?
# if self.isMarketHours():
# #this block should not execute in practice since PM data should always be used
# if d not in self.hod:
# self.hod[d] = -1.0
# self.lod[d] = 2000000000.0
# self.open[d] = -1.0
# #get hod and lod
# if tradeBars[d].High > self.hod[d]:
# self.hod[d] = tradeBars[d].High
# if tradeBars[d].Low < self.lod[d]:
# self.lod[d] = tradeBars[d].Low
# if self.open[d] == -1.0:
# self.open[d] = tradeBars[d].Open
# ################
# # trade setups #
# ################
# #self.Debug(self.Time)
# ### above VWAP and short-term trend up, then ORB
# price = float(tradeBars[d].Close)
# vwap = self.vwap[d].Current.Value
# ema = self.ema5[d].Current.Value
# hod = float(self.hod[d])
# lod = float(self.lod[d])
# ema9 = self.ema9[d].Current.Value
# size = 10.0
# if self.Portfolio[d].Invested == False and self.Portfolio.MarginRemaining > size:
# if price > vwap and ema > vwap and (price > ((hod + lod)/2) ) and self.Time.hour < 15:
# #long
# #self.Debug("lod " + str(lod))
# stop = self.lod[d]
# shares = self.positionSize(stop, price, size)
# self.Debug(self.Time)
# self.Debug("New long")
# self.Debug("margin remaining: " + str(self.Portfolio.MarginRemaining))
# self.Debug("nShares: " + str(self.Portfolio[d].Quantity))
# self.Debug(d)
# self.Debug("nShares: " + str(shares))
# self.Debug("currPrice: " + str(price))
# self.Debug("stop: " + str(lod))
# self.Debug("buying power used: " + str(shares*price))
# self.Debug("")
# self.MarketOrder(d, shares)
# stop = -1*shares
# self.StopMarketOrder(d, stop, lod)
# elif self.Portfolio[d].Invested == True:
# if ema < ema9 and price < ema9 and self.Portfolio[d].UnrealizedProfit > size:
# self.Liquidate(d)
# if self.Time.hour >= 15:
# self.Liquidate()
# else:
# #should only need to do this once...
# self.hod[d] = -1.0
# self.lod[d] = 2000000000
# self.open[d] = -1.0
# # self.Debug(d)
# # self.Debug(self.vwap[d])
# # self.Debug(self.ema5[d])
# # self.Debug(self.hod[d])
# # self.Debug(self.lod[d])
class SymbolData:
def __init__(self, algorithm, symbol):
#algorithm.VWAP(symbol, 10000, Resolution.Minute)
self.vwap = algorithm.VWAP(symbol, 10, Resolution.Minute)
self.ema5 = algorithm.EMA(symbol, 5, Resolution.Minute)
self.ema9 = algorithm.EMA(symbol, 9, Resolution.Minute)
hist = algorithm.History(symbol, 10, Resolution.Minute).loc[symbol]
for idx, bar in hist.iterrows():
tradeBar = TradeBar(idx, symbol, bar.open, bar.high, bar.low, bar.close, bar.volume, timedelta(minutes=1))
self.ema5.Update(idx, bar.close)
self.ema9.Update(idx, bar.close)
self.vwap.Update(tradeBar)