I have a monthly rebalance algo, and it rebalances 10 stocks each month. I want to separate this single updated positions stock list into 2 separate lists, a liquidate stock list and a buy stock list. This is for the purpose of going into 2 Debug lines, then emails and text messages.
I've tried getting these lists to come out as a single string, but just can't figure it out, and have worked around it to at least iterate through the list, but this gives a Debug output line for each buy and sell stock symbol, and I don't want 20 messages on each rebalance, I just want 2. Can anyone help?
def rebalance(self):
sorted_symbolData = sorted(self.symbolDataDict, key=lambda x: self.symbolDataDict[x].Volatility())
# pick the stocks with the lowest volatility
long_stocks = sorted_symbolData[:self.stocks]
stocks_invested = [x.Key for x in self.Portfolio if x.Value.Invested]
# liquidate stocks in the current portfolio that are not in the new positions list.
for i in stocks_invested:
if i not in long_stocks:
self.Debug("Sell: " + str(i))
self.Liquidate(i)
# determine the new stocks to buy that are not currently in positions.
for i in long_stocks:
if i not in stocks_invested:
self.Debug("Buy: " + str(i))
thanks!
mark
Mark hatlan
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!