I've been trying to figure out how to trade symbols that are the results of universes w/o the use of securities and can't figure it out and I can't find anything about it, I've tried setting it as a variable and using it
import numpy as np
class MyCoarseUniverseAlgorithm(QCAlgorithm):
def Initialize(self):
self.AddUniverse(self.MyCoarseFilterFunction)
self.UniverseSettings.Resolution = Resolution.Daily
self.SetStartDate(2020, 6, 10)
self.SetCash(100000)
self.lookback = 20
self.ceiling, self.floor = 30, 10
self.initialStopRisk = 0.98
self.trailingStopRisk = 0.9
self.Schedule.On(self.DateRules.EveryDay(self.symbol), \
self.TimeRules.AfterMarketOpen(self.symbol, 20), \
Action(self.EveryMarketOpen))
def MyCoarseFilterFunction(self, coarse):
pass
class SelectionData(object):
def __init__(self, symbol, period):
self.volume = 0
self.symbol = symbol
self.ema = ExponentialMovingAverage(period)
self.is_above_ema = False
def update(self, time, price, volume):
self.volume = volume
if self.ema.Update(time, price):
self.is_above_ema = price > ema
def MyCoarseFilterFunction(self, coarse):
for c in coarse:
if c.Symbol not in self.stateData:
self.stateData[c.Symbol] = SelectionData(c.Symbol, 200)
avg = self.stateData[c.Symbol]
avg.update(c.EndTime, c.AdjustedPrice, c.DollarVolume)
values = [x for x in self.stateData.values() if x.is_above_ema and x.volume > 10000000]
values.sort(key=lambda x: x.volume, reverse=True)
return [ x.symbol for x in values[:10] ]
def OnData(self, data):
self.Plot("Data Chart", self.symbol, self.Securities[self.symbol].Close)
def EveryMarketOpen(self):
close = self.History(self.symbol, 31, Resolution.Daily)["close"]
todayvol = np.std(close[1:31])
yesterdayvol = np.std(close[0:30])
deltavol = (todayvol - yesterdayvol) / todayvol
self.lookback = round(self.lookback * (1 + deltavol))
if self.lookback > self.ceiling:
self.lookback = self.ceiling
elif self.lookback < self.floor:
self.lookback = self.floor
self.high = self.History(self.symbol, self.lookback, Resolution.Daily)["high"]
if not self.Securities[self.symbol].Invested and \
self.Securities[self.symbol].Close >= max(self.high[:-1]):
self.SetHoldings(self.symbol, 1)
self.breakoutlvl = max(self.high[:-1])
self.highestPrice = self.breakoutlvl
if self.Securities[self.symbol].Invested:
if not self.Transactions.GetOpenOrders(self.symbol):
self.stopMarketTicket = self.StopMarketOrder(self.symbol, \
-self.Portfolio[self.symbol].Quantity, \
self.initialStopRisk * self.breakoutlvl)
if self.Securities[self.symbol].Close > self.highestPrice and \
self.initialStopRisk * self.breakoutlvl < self.Securities[self.symbol].Close * self.trailingStopRisk:
self.highestPrice = self.Securities[self.symbol].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.Securities[self.symbol].Close * self.trailingStopRisk
self.stopMarketTicket.Update(updateFields)
self.Debug(updateFields.StopPrice)
self.Plot("Data Chart", "Stop Price", self.stopMarketTicket.Get(OrderField.StopPrice))
in the self.AddEquity and it won't work, any1 know how I would go about doing it?
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!