Hi,
Simple question. I'm trying to modify the "EqualWeightingPortfolioConstructionModel" in order to assign equal amounts of cash from current balance to all securities in my Universe. The process would be to take current balance at market open, divide it by the number of securities in Universe (I already added "for added in changes.AddedSecurities: self.securities.append(added)" in OnSecuritiesChanged method to collect those securities), and then once there are insights for some of those securities create the target to buy as many shares as possible with that amount of cash. Doing the below I at least fix the weight to all securities in the Universe (and not just the ones with insights as per the original model) but the algo is still constantly buying and selling to adjust that target percent. How can I create those targets correctly?
# Give equal cash to each security in our Universe
count = len([x.Symbol for x in self.securities])
percent = 0 if count == 0 else 1.0 / count
errorSymbols = {}
for insight in lastActiveInsights:
target = PortfolioTarget.Percent(algorithm, insight.Symbol, insight.Direction * percent)
if not target is None:
targets.append(target)
else:
errorSymbols[insight.Symbol] = insight.Symbol
Thanks!
Emilio
Peilin Lu
I'm not sure if I'm right, but I hope this would help. I think the change you added to the function OnSecuritiesChanged also change the "len([x.Symbol for x in self.securities])". Since the function OnSecuritiesChanged is called each time step, then the count is varying time by time and percent then change accordingly. I think this could be the reason for the algorithm constantly adjust the target percent. I think a reasonable target percentage would be the following.
symbols = list(set([x.Symbol for x in self.insightCollection])) percent = 0 if len(symbols) == 0 else 1.0 / len(symbols)
Jared Broad
Hey Emilio, do I understand your question correctly - are you trying to do the rebalancing less frequently? (i.e. the existing allocations are OK but happening too often?)
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.
Emilio Freire
Hi Guys,
Thanks for the help. So what I'm trying to achieve exactly is this. Before market opens I decide the stocks I might want to trade for the day, let's say that's 5 stocks. I might not trade all of them (and therefore I might not generate insights for all of them), but 5 will be the maximum number of securities for the day. So if I have in my account 10k, I want to divide that by 5 and get 2k ready for each of those stocks in case I decide to trade them during the day. If I trade a stock, I buy as many shares as possible with 2k given the market price. There's no rebalance during the day, just 2k worth of shares.
That's why I was using the lenght of my securities and not the lenght of my insights....
Hope that makes it more clear?
Thanks!
Emilio
Alexandre Catarino
Emilio Freire, please consider Peilin Lu's solution to manage the once-per-day rebalancing.
The only thing that is missing the maximum number of securities which you may pass as an argument to the portfolio construction model constructor or use algorithm.ActiveSecurities:
def __init__(): self.day = 0 def CreateTargets(self, algorithm, insights): count = algorithm.ActiveSecurities.Count if count == 0 or self.day == self.Time.day: return self.day = self.Time.day targets = list() for symbol in algorithm.ActiveSecurities.Keys: direction = # define direction target = PortfolioTarget.Percent(algorithm, symbol, direction / self.count) targets.append(target)
Emilio Freire
Thanks!
Sharing below the final solution I chose. Since self.securities is a fixed number of securities per day, I use that to generate the count. Then, I simply added "if not algorithm.Securities[insight.Symbol].Holdings.Invested:" to control when to generate the target. I know the below solution would cause problems for some logics, but for my particular case it does the job. It would cause an issue when doing long/short the same security, but I'm thinking that could be fixed by using the IsShort or IsLong from the Holdings Class together with the insight.Direction?
Thank you again,
Emilio
# Give equal weight to each security in our Universe (regardless of active insights) count = len([x.Symbol for x in self.securities]) percent = 0 if count == 0 else 1.0 / count errorSymbols = {} for insight in lastActiveInsights: # Create target only if not already invested in the security if not algorithm.Securities[insight.Symbol].Holdings.Invested: target = PortfolioTarget.Percent(algorithm, insight.Symbol, insight.Direction * percent) else: continue if not target is None: targets.append(target) else: errorSymbols[insight.Symbol] = insight.Symbol
Emilio Freire
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!