code: def update_portfolio(p, pos, trade):
      r"""Update the portfolio positions.
  
      Parameters
      ----------
      p : alphapy.Portfolio
          Portfolio holding the position.
      pos : alphapy.Position
          Position to update.
      trade : alphapy.Trade
          Trade for updating the position and portfolio.
  
      Returns
      -------
      p : alphapy.Portfolio
          Portfolio with the revised position.
  
      """
      # update position
      ppq = abs(pos.quantity)
      pos = update_position(pos, trade)
      cpq = abs(pos.quantity)
      npq = cpq - ppq
      # update portfolio
      p.date = trade.tdate
      multiplier = pos.multiplier
      cv = trade.price * multiplier * npq
      p.cash -= cv
      return p
  
imports: from alphapy.frame import Frame
  from alphapy.frame import frame_name
  from alphapy.frame import read_frame
  from alphapy.frame import write_frame
  from alphapy.globals import MULTIPLIERS, SSEP
  from alphapy.globals import Orders
  from alphapy.space import Space
  import logging
  import math
  import numpy
  from pandas import DataFrame
  from pandas import date_range
  from pandas import Series
label_desc: Use image node_id 9 for calling a global function with example usage: update_portfolio(p, pos, trade) and returns: p