Hello,
I am trying to implement a strategy that goes long on 10 stocks with the highest ROC from the previous day and equally weighted buys into them and then liquidates all stocks at the end of that day. This would also include a stop loss of 2% for any of those stocks. This then repeats every day, know that during backtest universe runs at midnight.
Couple of issues that I am having with this:
1. For some reason on certain days it goes long on all the stocks at 9:31 am then sells a select amount of particular stocks at 9:32 am (not the whole security amount held), I am unable to figure out why it is selling and not even selling all the stocks held?
2. I don't think the Risk Management is working as it should, as the largest loss for a given day should only be a max of 2% (with a margin of error) if all of the 10 stocks have decreased more than 2% that day. I can't figure out why it won't sell throughout the day.
3. How do you have the universe scan through stocks not only on the NYSE but also the TSX and any other major stock exchange in North America?
I have been going over examples from GitHub to learn how to connect all the different classes like Selection, Alpha, Risk Management, Portfolio, etc. to try and learn the Universe Selection (I find the documentation a bit light compared to the rest of Quantconnects documentation, and the Bootcamp examples don't touch on connecting all the different classes) Please point me in the right direction for documentation on this area of Quantconnect with Universe Select.
https://github.com/QuantConnect/Lean/tree/5d762d16b2ec7bd48fd766b772b0921862deda22/Algorithm.FrameworkI found Emilio's forum from 2018 that was asking a very similar question, but even with that detailed forum, I am still struggling to answer all my questions.
https://www.quantconnect.com/forum/discussion/4665/managing-universe-and-trades-within-algorithm-framework/p1Any help to figure out these couple of issues would be fantastic.
Tate Linzel
I was able to figure out the problem to my first two questions which were both related to the Risk Management, there was no problem but that the stocks would hit their stop loss and sell. The stocks would drop quickly in the morning and be sold the minute after they were bought.
New questions that I have as I progress the algorithm:
1. How do you limit the numbers of times a stock can consecutively be in the portfolio of securities? I would like to limit this to a max of 2 days in a row.
2. Is it possible to reallocate a stock's portfolio to another stock partway through the day? For example, I have noticed that some stock's prices won't move throughout the day, therefore have a rule if the price hasn't moved in 1 hour, then liquidate the stock and reallocate that cash to another stock currently in the portfolio that has the highest intraday PNL. Or if a stock hit it's stop loss then do the same, sell, and reallocate.
Thank you for your help.
Derek Melchin
Hi Tate,
The universe selection model implemented above currently selects the securities that have the greatest ROC from the close 2 days ago to the close of the previous trading day, not from the previous close to the current day's open. Refer to this related thread regarding the latter.
> How do you have the universe scan through stocks not only on the NYSE but also the TSX and any other major stock exchange in North America?
We currently only support the major exchanges in the USA.
> Please point me in the right direction for documentation on this area of Quantconnect with Universe Select.
We recommend reviewing the documentation here. We are in the process of updating our documentation. We appreciate the feedback and patience in the mean time.
> How do you limit the numbers of times a stock can consecutively be in the portfolio of securities? I would like to limit this to a max of 2 days in a row.
We can limit the number of consecutive days we purchase the same security by tracking which securities we purchase each day. Then in our Update method, we include
days_purchased = [security.Symbol in symbols for time, symbols in self.symbols_by_time.items()] if days_purchased and all(days_purchased): continue
See the attached backtest for reference.
> Is it possible to reallocate a stock's portfolio to another stock partway through the day?
Yes, to accomplish this, we'd need to emit insights intraday when we want to rebalance the portfolio.
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.
Tate Linzel
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!