Hi Members, I am new to Quantconnect and trying my best to convert ideas into Algorithm Framework. As a learning experience, Below is my basic idea:

  1. Filter out mid cap stocks with universe selection
  2. Alpha generation based on fast/slow Moving Average Momentum + Volume + Price action. I Liquidate and re-balance my portfolio quarterly
  3. Portfolio construction takes the inputs from Alpha Model and converts into Targets. Here I have gone with custom Portfolio construction because:
  • I want to have only 10 Stocks at max in my portfolio in a quarter.
  • I wish to liquidate my entire portfolio quarterly

 

I am facing below issues regarding above approach, request members to suggest better implementation than what I have opted for to solve these:

  • Question 1: Could not use Equal Weighted Portfolio Construction Model(with 0.1 weight) as all 10 of my Insights are NOT generated at the same time/day, I am waiting for right price action of my candidate before deciding to add it as part of portfolio, so Insights are generated based on it. For example, among filtered candidate in the universe/criteria, I may enter 2 on 2nd Jan, while 3rd and 4th candidate I will buy on 10th Jan.
    Solution I did: Created custom portfolio construction model and returned targets from CreateTargets function whenever I receive them, kept one variable to track how many stocks I hold in my portfolio to have max count as 10.
  • Question 2: Could not re balance/liquidate all my portfolio quarterly in my custom portfolio construction model. I tried many ways to achieve it :
    i.passed re-balance function while creating instance of my custom portfolio construction:
    self.SetPortfolioConstruction(MyCustomWeightingPortfolioConstructionModel(self.shallRebalance))
    and called it like below:
     
def __init__(self,rebalance=None):
        super().__init__(rebalance) # not working
        if rebalance is not None: # not working either
             self.SetRebalancingFunc(rebalance)

  ii.I tried to define IsRebalanceDue manually, but seems it's not being called at all:

def IsRebalanceDue(self, insights, algorithmUtc):
   if self.rebalance is not None:
       return self.rebalance(algorithmUtc)
       return True

  Solution I did: After many attempts, I am doing it now inside OnData

def OnData(self,slice):
        if self.Time < self.rebalanceTime:
            return
        else:
            #till found better place, liquidated from here
            self.Liquidate()
            self.rebalanceTime = Expiry.EndOfQuarter(self.Time)
  • Question 3: Where shall I keep the track of re-balancing logic to achieve both: avoid code repetition and adhere to separation of concerns methodology? Currently I need to keep it at multiple places :
    i.While universe selection to return Unchanged universe if same quarter
    ii.in OnData function to check if to Liquidate portfolio on quarter end
    iii.I need it in Alpha creation to modify weights if we are close to quarter end