Hello,
I'm a recent joiner and have been going through the bootcamp tutorials in order to learn the QC platform. I'm trying to replicate a strategy I configured using the Interactive Brokers Portfolio Builder & Backtester. Basically, my strategy goes Long the Bottom 10 ranked by PE Ratio and goes Short the Top 10 ranked by PE Ratio on a daily basis.
I came across the Liquid Value Stocks tutorial which does something similar by separating the top 10 and bottom 10 by Earning Yield.
def SelectFine(self, algorithm, fine):
sortedByYields = sorted(fine, key=lambda f: f.ValuationRatios.EarningYield, reverse=True)
universe = sortedByYields[:10] + sortedByYields[-10:]
return [f.Symbol for f in universe]
However, when it came to generating the daily buy/sell signals (I think you guys call it emitting Insights), the tutorial used a simple line of logic to go Long if the EarningYield was greater than 0 and Short otherwise, as below, In other words, separating out by top 10 & bottom 10 didn't matter to the trading strategy as that was not really used. Maybe the instructor didn't want to make the lesson too complex.
for security in algorithm.ActiveSecurities.Values:
direction = 1 if security.Fundamentals.ValuationRatios.EarningYield > 0 else -1
insights.append(Insight.Price(security.Symbol, timedelta(28), direction))
return insights
I, however, would like to go Short all of my top 10 stocks ranked by PE Ratio and Long all of my bottom 10 stocks on a daily basis. I've searched through the documentation & the forum for help on this but alas couldn't find anything related.
Can someone help me with some actual code to generate Long & Short signals for Top 10 and Bottom 10? Thanks in advance.
Sheikh
Louis Szeto
Hi Sheikh
You can save your selected longs and shorts as separate variables to use:
Then for updating your alpha, you can emit insights separately:
Cheers
Louis
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.
Sheikh Pancham
Hi Louis, thank you for getting back, greatly appreciated!
I am getting an error however:
I put my full code below. I am trying to be a good student and use the Algorithm Framework but if you think I should use a different approach please suggest it.
Can you help me to make the necessary modifications to get it to work pls. Also, in OnSecuritiesChanged(), how can I tell if a security in AddedSecurities should be bought or shorted. In other words, how can I tell if the security allocation in SetHoldings() should be +ve or -ve?
Thanks again!
Louis Szeto
Hi Sheikh
The objects between two classes is not inter-callable. Sadly you cannot super().__init__() the algo class as you need to call AlphaModel for manage class. My recommendation is not to use a separate class for alpha at all.
Cheers
Louis
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.
Sheikh Pancham
Thanks Louis. Can we keep this open for a bit, I may have a few other questions.
Louis Szeto
Sure :)
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.
Varad Kabade
Hi Sheikh Pancham,
To continue using the Framework for the above strategy, we recommend creating a SymbolData class with attributes as PE ratio and using a dictionary in the AlphaModel to store these data keyed by the symbol. We can update this dictionary in our OnSecuritiesChanged event handler by removing the removed securities and adding the added one, then sorting the securities by their PE score. Refer to the attached backtest. Note that we have changed the logic to calculate allocation. We assumed that you wanted to assign equal weights to securities whose insights are emitted by the AlphaModel. This done by the EqualWeightingPortfolioConstructionModel.
Best,
Varad Kabad
Sheikh Pancham
Thanks Varad, I shall try your solution as well. To be honest, I am framework agnostic & have only been trying to use the AF because in the bootcamp that seems to be what is being advocated for. I also don't want to be using what could eventually become a legacy approach, so I'll definitely give your solution a try.
Having said that, I gave the OnData() approach (as suggested by Louis) a try & it seems to be running ok. I also found some code ("for kvp in self.Portfolio") in this forum discussion:Â
https://www.quantconnect.com/forum/discussion/5647/fundamental-factor-long-short-strategy-as-an-alpha-model
that I used that allows my strategy to not flatten positions in the Portfolio if they re-appear in the longs & shorts on the next iteration. Otherwise, I'm guessing that would incur additional fees. Attached, pls have a look & let me know if you see any errors.
btw, why do you guys have a candlestick chart for the Strategy Equity? Can you advise how I can change that to see a continuous equity curve chart with the area under the curve shaded?
I want to say also that this is a great platform especially for someone like me who is not a Python expert. It is almost like plug and play. I'm glad I found QC. And the support and community forum is great too!
I have a few other questions but I'll open a new post for those.
Many thanks to both you & Louis!
Sheikh
Sheikh Pancham
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!