Implement threshold-based rebalancing
To implement threshold-based rebalancing, we simply set a threshold for portfolio drift. To identify this, we can continue from our preceding example. First, we need a way to mark which portfolios require rebalancing. Let’s add a Boolean flag to our Portfolio
class from before:
class Portfolio: def __init__(self, tickerString: str, expectedReturn: float, portfolioName: str, riskBucket: int): self.name = portfolioName self.riskBucket = riskBucket self.expectedReturn = expectedReturn self.allocations = [] self.needRebalancing = False
For the sake of brevity, I haven’t added the full Portfolio
class, so just add this line to the __init__()
method.
Let’s add a helper method to check whether our portfolio’s drift is higher than the threshold:
def checkNeedRebalancing...