Hey QC forum
I wrote an algorithm using the QC framework. The algorithm selects liquid and large stocks, and only selects the 10 best performing stocks, in the alpha model. It is long only. The risk model uses SPY and EMA, and close all trading, if the value og SPY is lower than the EMA.
If you have any critic or something else, your welcome to comment it! Hope you learn something from it
Lucas
Vladimir
Lucas
I like your framework algorithm, especially the canary symbol based RiskModelWithSpy() and
MomentumAlphaModel().
Try to decrease self.num_coarse and lookback.
I cannot find where we are using maximumDrawdown = 0.05.
Lucas
Vladimir
Thank you for the feedback. As you mentioned, maximumDrawdown = 0.05. and self.maximumDrawdown = -abs(maximumDrawdown) can be deleted, without having any effect on the algorithm. Have to look closer, when copying old code ;)
Furthermore, as Varad noted, self.Securities can be replaced with self.ActiveSecurities, it will make the algorihtm a bit faster.
The updated code is linked, with less securites in the alpha model, and less lookback
Lucas
Vladimir
Lucas
We see that reducing self.num_coarse from 100 to 40 improve
Compounding Annual Return from 17.904% to 18.829%.
Further decreasing self.num_coarse from 40 to 20 and decreasing lookback
from 420 to 60 improve Net Profit from 507.939% to 585.956%.
Toaster
Hi Lucas,
I trade a very similar strategy live. Have you tried smoothing the ROC indicator with a small MA i.e. 5?
Chris
Lucas
Hi Chris
No, I have not, but I will maybe create one, and see how it works out. I will post it on this tread, if I have the time for making it. I doubt that it will have a positive inpact on the sharpe/drawdown/net profit. We will see 😊
Have a good holiday
Lucas
Mislav Sagovac
Hi,
Thank you for great code!
I have one question for now. Why do you use both self.num_coarse and self.num_insights ?
In the end, you dont use self.num_coarse number of stocks in portfolio, but self.num_insights number of stocks?
Mislav Sagovac
I you start at 2000, the algorithm has hufe drop on the begining of the backtest. It has negative DD through whole backtest.
Lucas
Hi Mislav
For the first question. We maybe want to take 100 stocks based on volume, and only pick the top 10 perfoming stocks, based on return. The self.num_coarse is how many stocks we want to include in our return ranking, and self.num_insights is how many insights will be sent to the PCM. So, 100 is the number of stocks that we want to see the past return on, and 10 is the number of stocks being bought (if we pass the risk module, and that get sent to the execution part).
I am aware that this strategy has a huge DD when momentum stocks is doing badly. Like the 2000 IT buble. Decreasing self.num_coarse can help with this, and having a shorter EMA. Furthermore, swichting SPY with QQQ, could help. The algorithm was more of an example algorithm, and not a “make tons of money” algo ;).
It should also be noted that the standard PCM does not work well with this strategy, as less stocks is being bought than insights being sent sometimes. There is sometimes an extra rebalance (twice a month, instead of 1, the day after the first rebalance), which leads the algo to sell N amount of stocks. This can be fixed with a costum PCM model. I do still not know WHY happens with the standard PCM, and have not found another fix than just rewriting the whole thing with the CreateTargets function. Maybe Vladimir have some insights on this?
Have a good day
Lucas
Vladimir
Lucas,
A strategy backtest from 2000 with the set of parameters that I posted here in December 2021
does not look as bad as Mislav Sagovac describes and has a positive Information Ratio.
Lucas
Hi Vladimir
I was more commenting on the fact that the algo does not rebalance or maintain the amount of stocks that the algo should be doing, with the standard equal PCM module. I have now re-writen the PCM to a custom one, but never found out WHY the algo does not maintain 10 stocks in the portfolio. The algo sometimes (Dont know why) sells some stocks (again, dont know why and seems to be random how many stocks) the day after the first rebalancing. I was wondering if you had come across a problem like this, and knew how to fix it (other than re-writing the whole thing)
Backtest attached, with commented out risk-module, so its easy to see where it happens
Lucas
Vladimir
Lucas,
--> never found out WHY the algo does not maintain 10 stocks in the portfolio.
There is code in MomentumAlphaModel() which selects only stocks with positive return.
Sometime(in bear market) there are less than self.num_insights stocks with positive return in your CoarseUniverse.
Mislav Sagovac
Thanks Vladimir and Lucas on comments.
I understand the logic now.
I have started to read articles on momentum in last few weeks and found your code helpfull for start.
It seems to me, th momentum strategies in general have good returns, but high crash component and we “only” have to find good way to remove this crash component and, optionally by alterantive sae asset.
Lucas
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!