code: def rebalance(context, data):
      # Pipeline data will be a dataframe with boolean columns named 'longs' and
      # 'shorts'.
      pipeline_data = context.pipeline_data
      all_assets = pipeline_data.index
  
      longs = all_assets[pipeline_data.longs]
      shorts = all_assets[pipeline_data.shorts]
  
      record(universe_size=len(all_assets))
  
      # Build a 2x-leveraged, equal-weight, long-short portfolio.
      one_third = 1.0 / 3.0
      for asset in longs:
          order_target_percent(asset, one_third)
  
      for asset in shorts:
          order_target_percent(asset, -one_third)
  
      # Remove any assets that should no longer be in our portfolio.
      portfolio_assets = longs | shorts
      positions = context.portfolio.positions
      for asset in viewkeys(positions) - set(portfolio_assets):
          # This will fail if the asset was removed from our portfolio because it
          # was delisted.
          if data.can_trade(asset):
              order_target_percent(asset, 0)
  
file_path: zipline/zipline/examples/momentum_pipeline.py
imports: from six import viewkeys
  from zipline.api import attach_pipeline, date_rules, order_target_percent, pipeline_output, record, schedule_function
  from zipline.finance import commission, slippage
  from zipline.pipeline import Pipeline
  from zipline.pipeline.factors import RSI
label_desc: Use image node_id 2 for calling a global function with example usage: rebalance(context, data) without return types