Friends,
I'm looking to put together a simple MA crossover that takes in multiple tickers. I have done this, however I haven't found a way to call the portfolio so that a it knows when to go long or liquidate. I have tried passing self.symbols into the ‘holdings’ variable, but get the error:
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'list'>) method. Please checkout the API documentation.
at OnData
holdings = self.Portfolio[self.symbols].Quantity
Obviously I can't pass through self.symbols because it is a list. So what is then the better alterative?
Cheers
Michael
Fred Painchaud
Hi Michael,
I did not read your entire code, I focused on your error and question. So I recommend the following:
Note that this is conceptual. My points are:
1- Note that you should save the symbol objects from the AddEquity method. Symbols are objects that identify your equity much more precisely than just the string of their ticker.
2- You will need to pass one symbol at a time to get the quantity out of your portfolio.
That being said, your code needs more work at other places. For instance, those two lines in your Initialize:
are inside a loop and thus each time around, the variables self.fast and self.slow are being overwritten. What's left is that you only have the two SMA indicators for the LAST symbol in your list (in this case SPY). All the previous ones are created but you cannot reference them later in your code so you cannot use their Current.Value. In other words, they become useless.
Exact same idea with the code I gave you myself for the quantity assigned to the holdings variable. It was to illustrate that you will need to iterate. But that code is wrong in the sense that again, you will only be left with the quantity of your last symbol…
SO
You will need a list to store your SMA indicators. And you will need to iterate on all your symbols and apply your algo to each symbol, one at a time. As is, your code trades all symbols BUT all based only on SPY SMAs….. Well, MAYBE that is your intention. But then, you are still creating SMA indicators for all symbols so that is why I thought you wanted to trade Tesla according to its SMAs, Apple according to its SMAs, etc. Not everything according to SPY's SMAs.
If you need more help after reading this / trying this out, do not hesitate to report back.
F
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.
Mike A
Fred,
Thanks for your suggestions. You are right, I wanted MA signals as per the price of each ticker itself. It appears Vladimir's code works great for this.
I did input your code unfortunately I get the error:
Nonetheless. Thank you both!
M
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!