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?
Shile Wen
Hi Caio,
We can iterate through ActiveSecurities.Keys, which are the Symbols created by the Universe Selection, though it also includes securities that were removed but that are still invested, which means we need to liquidate them before using ActiveSecurities. I've shown an example in the attached backtest.
Best,
Shile Wen
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.
Hussain
What if we're using the algorithm framework i.e. implementations of AlphaModel and PortfolioConstructionModel?
Would you advise we use algorithm.ActiveSecurities.Keys in the AlphaModel's Update(algorithm, data)?
My guess is while PortfolioConstructionModel discards insights pertaining to removed securities, you wouldn't want to be producing fresh insights for these in the AlphaModel implementation.
Therefore we should probably detect and liquidate removed securities in the AlphaModel's OnSecuritiesChanged, which presumably gets called prior to the AlphaModel's Update().
What do you think?
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.
Shile Wen
Hi Hussain,
We typically have a SymbolData/SelectionData dictionary pattern shown in this BootCamp when using an Alpha Model, which means we can iterate over the keys of the dictionary instead of the ActiveSecurities when generating insights. Inside the Alpha Model's OnSecuritiesChanged, we typically remove keys for removed securities.
Best,
Shile Wen
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.
Erik Bengtson
Cool James. I will take look. I might migrate what I'm doing to yours baseline implementation.I was just about to create an implementation for turtle soup strategy, better to start from a good basis
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.
Erik Bengtson
Thanks Jay, I will take a look at that option.
James, here my version of your baseline project. I will try to merge with the changes I've done on the previous project. The code looks cleaner.
GIT GenericTree updates
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.
Erik Bengtson
and of course, the current BB strategy implementation there must be only for trading ranges
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.
Erik Bengtson
James,
I updated some code with fixes and to make it compatible with QC
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.
James Smith
Thanks Erik. I'm going to try to integrate your changes as soon as I can. I can work without a pull request but you can go that route if you prefer. I'm giving genetic programming using this setup a lot of attention so feel free to suggest improvements or report any issues. The number one thing that helps me out is getting a third-party opinion on things. I have made quite a lot of changes to this and the genetic optimizer project and am getting fairly pleasing results.
In terms of an optimization rig, I have an old 4 slot server capable of 24 cores that I obtained for basically peanuts. I don't know how the costs stack up over time against cloud compute. I imagine it would even out after a few weeks of running 24/7 as long as power costs aren't too high.
For live hosting of trading algorithms +1 for digitalocean.
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.
Erik Bengtson
James, I will check how to do a pull request. Not really familiar with that.
Next steps for me are the integration of additional signals in order of creating a few strategies. The additional signals I'm looking at is the Autochartist, integration with rest based NN services.
I will provide further feedback as soon as I progress using this framework, but immeditelly I think the configuration is rather verbose, I will change it into 2 steps, to make it more human friendly.
Thanks for this.
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.
James Smith
I've merged the Bollinger and Channel breakout from your fork. Seems like a great idea to allow a survival period for the approximate coincidence of signals. I'm wondering whether this could use QuantConnect.Indicators.RollingWindow?
You're right that the Optimizer configuration is unwieldy for this level of complexity. I may address that sometime soon. In the meantime I'm using a few scripts and tools to shift json around.
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!