Hi guys,
Finding the Universe function impossible to use. I simply want to add a set of all large cap pharma stocks into a Universe and trade on them. Therefore, need to add the universe using manual selection. OnData should then be able to access the stocks because in virtue of them being added to the Universe they are in OnData.
In the attached nothing happens because GOOG is not even in the Universe. Therefore there is no data to trade on. Please can someone let me know why this is happenning?
I know you could simply add the GOOG data at the start using "AddEquity" but doesnt this defeat the point of having a nice function to specify your universe if you need to add every single equity indivudally anyway...??
Can obviously see the appeal if you are using more interesting methods to specify your universe but is it really necessary if I already know which stocks I wish to trade on the strategy..?
Thanks in advance for help. Personally finding the AddUniverse function very difficult to use indeed....
Jack Simonson
Hi Hugo,
You can user Universe Selection to filter for stocks by market cap and sector using Fundamental Data, and within the Universe Selection, you can add GOOG manually if you want it to be present. Additionally, it is possible to define your own custom Universe Selection class if you want to customize it further. You can find more information about this here and here.
Hugo Lu
Ok fair enough. How do I add a static universe then with a pre-defined list of stocks? I have been looking at lots of code but unfortunately haven't found anything that really works :/
Thank you for your help!
Hugo
Link Liang
Hi Hugo,
I believe you are looking for manual universe selection. There is a template you can use. When you create a new algorithm, you can find this tamplate in "Universe" section.
The main idea is, ManualUniverseSelectionModel() takes a list of Symbol objects. We can first make a list of tickers, then create a Symbol object for each ticker using QuantConnect.Symbol.Create() (Notice Symbol class is in namespace QuantConnect), and then put those symbols into a list and pass it to ManualUniverseSelectionModel().
Here is an example for manual universe selection. You can find more information about universe selection here.
namespace QuantConnect.Algorithm.CSharp { public class MultidimensionalTachyonReplicator : QCAlgorithm { public override void Initialize() { SetStartDate(2018, 10, 9); //Set Start Date SetEndDate(2018, 11, 8); SetCash(100000); //Set Strategy Cash UniverseSettings.Resolution = Resolution.Minute; /* var tickers = new[]{"SPY","GOOG","IBM"}; var symbols = new List<Symbol>(); foreach (var ticker in tickers){ symbols.Add(QuantConnect.Symbol.Create(ticker,SecurityType.Equity, Market.USA)); } */ var symbols = new[]{"SPY","GOOG","IBM"}.Select(x => QuantConnect.Symbol.Create(x,SecurityType.Equity, Market.USA)); AddUniverseSelection( new ManualUniverseSelectionModel(symbols) ); } /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here. /// Slice object keyed by symbol containing the stock data public override void OnSecuritiesChanged(SecurityChanges changes){ Log("Added:"); foreach (var added in changes.AddedSecurities){ Log(added.Symbol.Value); } } public override void OnData(Slice data) { // if (!Portfolio.Invested) // { // SetHoldings(_spy, 1); // Debug("Purchased Stock"); //} } } }
Anne
I have one further question. In the OnData section, how can I carry out my indicator on each symbol in my manual list? I have to iterate through my list, but I don't know how to do that.
My proposal has been:
def OnData(self, data): for symbol in symbols:
But that does not work: "Runtime Error: NameError : name 'symbols' is not defined
at OnData in main.py:line :: for symbol in symbols:
NameError : name 'symbols' is not defined (Open Stacktrace)"
Can someone help me please? Thanks!
Derek Melchin
Hi Anne,
We can loop through the `ActiveSecurities` instead of `symbols`.
for symbol in self.ActiveSecurities.Keys: self.Log(f"OnData: {symbol}")
See the attached backtest for reference.
Best,
Derek Melchin
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.
Hugo Lu
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!