First of all, thanks for your time and effort! However, in the second part (bull market), I think you are buying but not selling the ETFs that you already have. For example:
Month 1 I have the following ETFs: A, B, C, D
Month 2 (I need to have the new top 4): A, B, C, E >> You just need to sell D, and replace it for E. In the algo you kindly attached, it doesn't sell.
# Sell Treasury Bond 1-3 years if held and buy sector ETFs #that's fine for selling the bond
for stock in context.longs:
order_target_percent(stock, 1.0 / len(context.longs))
log.info("Buy" + str(stock))
In bear markets the problem is this:
Month 1 (bear): I buy the bond
Month 2 (bear): I don't need to sell the bond (which in your algorithm it does), I just need to keep it and don't buy or sell.
Thank you very much for helping! Best regards