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.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.vwap = {}
self.atr = {}
self.ema5 = {} #on 1 minute chart
self.ema9 = {}
self.hod = {}
self.lod = {}
self.open = {}
#self.pmhigh = {}
#self.pmlow = {}
for s in self.Securities:
self.Debug(self.Securities[s])
def CoarseSelectionFunction(self, universe):
selected = []
for coarse in universe:
if coarse.Volume > 70000000 and coarse.Value > 10 and coarse.HasFundamentalData:
#tickers
selected.append(coarse.Symbol)
#indicators
self.vwap[coarse.Symbol] = self.VWAP(self.AddEquity(coarse.Symbol.Value, Resolution.Minute, Market.USA, True, 1, True).Symbol, 100000)
self.ema5[coarse.Symbol] = self.EMA(self.AddEquity(coarse.Symbol.Value, Resolution.Minute, Market.USA, True, 1, True).Symbol, 5)
self.ema9[coarse.Symbol] = self.EMA(self.AddEquity(coarse.Symbol.Value, Resolution.Minute, Market.USA, True, 1, True).Symbol, 9)
return selected #list of objects of type Symbol
#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])