code: def _rebalance(self):
      self.info("Rebalancing")
  
      # Cancel all active/pending orders.
      for order in self.getBroker().getActiveOrders():
          self.getBroker().cancelOrder(order)
  
      cashPerAssetClass = round(
          self.getBroker().getEquity()
          / float(len(self.__instrumentsByClass)),
          2,
      )
      self.__sharesToBuy = {}
  
      # Calculate which positions should be open during the next period.
      topByClass = self._getTop()
      for assetClass in topByClass:
          instrument = topByClass[assetClass]
          self.info("Best for class %s: %s" % (assetClass, instrument))
          if instrument is not None:
              lastPrice = self.getLastPrice(instrument)
              cashForInstrument = round(
                  cashPerAssetClass
                  - self.getBroker().getShares(instrument) * lastPrice,
                  2,
              )
              # This may yield a negative value and we have to reduce this
              # position.
              self.__sharesToBuy[instrument] = int(
                  cashForInstrument / lastPrice
              )
  
      # Calculate which positions should be closed.
      for instrument in self.getBroker().getPositions():
          if instrument not in topByClass.values():
              currentShares = self.getBroker().getShares(instrument)
              assert instrument not in self.__sharesToBuy
              self.__sharesToBuy[instrument] = currentShares * -1
  
imports: from __future__ import print_function
  from pyalgotrade import strategy
  from pyalgotrade import plotter
  from pyalgotrade.barfeed import yahoofeed
  from pyalgotrade.technical import ma
  from pyalgotrade.technical import cumret
  from pyalgotrade.stratanalyzer import sharpe
  from pyalgotrade.stratanalyzer import returns
label_desc: Use image node_id 8 for calling the MarketTiming obj's underlying member method code with example usage: obj._rebalance() without return types