Hi all, I hope everyone is well.
I’ve been working heavily on new and more complex algorithms this year, and I have upped my LEAN proficiency quite significantly. I thought I’d just share this little piece of somewhat simple code in the hope it may save someone some time in optimizing their algorithms. The code outputs 2 new graphs detailing the weighting of each sector within the portfolio and the profitability of each sector; this may help you optimise your algorithm by identifying sectors that don’t work so well or sectors that work the best with your factor if you are trading a factor investing strategy. For example, in the backtest provided you can see that, over the period of time selected, communication services stocks have worked very poorly with the chosen factor whereas energy stocks have worked very well with the chosen factor. This backtest attached contains the code so you are welcome to run this and observe. The charting code is taken from a more complex algorithm I’m currently live trading; I’ve pasted the charting code into this simplified demo version of my code so don’t expect anything special from the algorithm itself it’s just a simple script which is longing stocks with high Free Cash Flow Yield every week. The charting code should be implementable into any other code and sharing this charting code specifically is the purpose of this discussion.
How it works:
The following is performed for each sector to create the first plot: Each week the code combines the realised net profit and the unrealised profit for each of the currently invested securities. These values are then summed. The realised net profit of all securities previously but not currently invested is then added to this value. This is plotted once a week on Friday before market close. There is a commented option to change the first plot from a line chart to a stacked area chart if you prefer this.
The second plot provides the absolute weighting of each sector within the portfolio as a percentage. This is useful data but the trend of the data is hard to interpret because of how the lines jump so I am considering converting this to a moving average. Another graph type may also work but I can't find any that are suitable. Please let me know if you have any suggestions.
Please note a new dictionary is created containing the stocks in the dynamic universe and is appended when necessary. This dictionary is appended with new securities each time a new universe is created (everyday). The fine fundamental data is stored in this dictionary as the value, and the security symbol is stored as the key. The reason for using this rather than accessing the inbuilt Portfolio or Securities dictionary for the fundamental data is primarily because of bugs I experienced where on certain days fine fundamental data was unavailable for specific securities. This only happened rarely in backtesting (once a year maybe) but was a pain. There were also issues where certain data would always appear as ‘None’ particularly after algorithm restart when paper trading live. The method I have used has avoided all issues for me in all cases with more complex code and optimisations involving sectors (ie. balancing weighting of sectors), and hence is why I have chosen to use it despite it appearing inefficient. Nonetheless, if anyone has any suggestions to make my method more efficient I would appreciate the feedback.
Difficulty livetrading: There are complications when moving this to live trading as obviously the dictionary resets when the algorithm restarts so additional code is necessary to add all currently invested securities to the dictionary if the algorithm restarts with a portfolio already invested. I have removed the code that performs this from this version since this version here is only for backtesting. If anyone is interested in this I can add it to the discussion but it is not too complex to write.
Thoughts?
I am currently trying to figure out a way to combine these 2 charts into one to give the relative/proportionate profitability of each sector as a more accurate method of measuring the most profitable sectors. For example, a sector might be allocated 5% of the portfolio but profit only slightly less than a sector allocated 15%. This therefore makes that sector proportionately better. I have had many ideas of how to do this but there always seems to be an issue leading to inaccuracy in my method. The primary issue seems to be in pulling the NetProfit of stocks no longer invested and equating this with the OLD weighting of that sector when that profit was made rather than the current weighting. If anyone has any ideas, please let me know.
I hope this is useful to someone – I wish to start making more contributions to the community after having learned so much from the contributions of others. I am keen to keep learning as much as possible so please give me your thoughts.
Miles
Miles Mitchell
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!