Hi all,
I am sure this has already been solved on the forum, however I couldn't really find what I was looking for in other threads I read.
I am having an Algo that uses a dynamic universe filters stocks based on fundamental ratios.
Currently I allocate 100% of the free portfoliovalue to the securities received by the algo by
self.portfolioTargets = [PortfolioTarget(symbol, 1/(len(self.activeStocks))
for symbol in self.activeStocks]
and then
self.SetHoldings(self.portfolioTargets)
The issue clearly is that I am always investing 100% of the cash and in order to open a new position a previous one has to be liquidated as well as that it is obviously not optimal to have 100% allocated to for example 1 positions.
Does anyone have model where if for example only 1 or 2 positions are opened only a smal percentage of the cash is allocated so that if in the next day one or more positions are added enough cash is onhand to open these?
Still learning so I appreciate all the help, thank you!
Fred Painchaud
Hi Patryski,
You are very close to the answer already so you're learning fast. In your code:
you allocate a % of the portfolio to each symbol. We do not have your complete code but I am assuming that if you change that code for this:
you will have 10% of your portfolio always liquid for future buys. You can use 0.8, 0.7, as you wish. You will most likely have to add some code so you can use that spare cash if you spot an opportunity. At a high level, you would need to add those new assets to self.activeStocks which you probably do when you want them to become active and your SetHoldings() call buys them.
Fred
Patryski
Hi Fred Painchaud,
thank you for your input! Yes, I have done that before, but the issue is exactly that I dont know how to use the then remaining buying power or what kind of code is needed to use the remaining buying power during the next rebalancing.
Patryski
Fred Painchaud
Hi Patryski,
I'd say that if you can share your complete code, it would be easier to help you out with that.
Fred
Patryski
Fred Painchaud
Okay, I hope this helps. I attached a backtest with the a variation of the code.
Fred Painchaud
Ok. Looking at that right now.
Re-reading your original post, what you want to do is if your activeStocks (your Universe basically) contains say only 1 asset, you do not want to put 100% of your portfolio to that asset, you would like to enter say only 30% of your portfolio value. So that when new assets are added to your Universe, you already have room to buy them, and SetHoldings will not have to liquidate part of your previous position before buying new ones. Is that it?
If so, I am not currenly able to help you as I still don't know those details of the platform. What you need to do for sure is to track how many % of your portfolio is currently invested. That may or may not already be accessible somewhere, that's one thing I don't know right now. Say it is 30%. Then, if you have enough new assets in the Universe (enough is for you to decide, what is enough, 2? 3? 4? etc), and you do not want to touch the 30% already invested, you need to take 70% and assign it to the new assets, so say there are 3 new assets, that would be .7/3 for each of them. Really sorry I don't know what you don't know here…
A quick hack which might work is:
So yeah, that could do it. But that being said, I am wondering why you say that “it is obviously not optimal to have 100% allocated to for example 1 positions.”. If that asset is the only one that meets all your criteria, it should be optimal. But maybe it is because then, you think it exposes you too much / too much risk?
Anyway, it's none of my business. Hope the code above and comments help to some extent.
Fred
Patryski
Thank you Fred, the code you provided seems logical.
Yes, it is a matter of risk.
Patryski
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!