code: def valuate_portfolio(p, tdate):
      r"""Value the portfolio based on the current positions.
  
      Parameters
      ----------
      p : alphapy.Portfolio
          Portfolio for calculating profit and return.
      tdate : datetime
          The date of valuation.
  
      Returns
      -------
      p : alphapy.Portfolio
          Portfolio with the new valuation.
  
      """
      positions = p.positions
      poslen = len(positions)
      vpos = [0] * poslen
      p.weights = [0] * poslen
      posenum = enumerate(positions)
      # save the current portfolio value
      prev_value = p.value
      # compute the total portfolio value
      value = p.cash
      for i, key in posenum:
          pos = positions[key]
          pos = valuate_position(pos, tdate)
          vpos[i] = pos.value
          value = value + vpos[i]
      p.value = value
      # now compute the weights
      for i, key in posenum:
          p.weights[i] = vpos[i] / p.value
      # update portfolio stats
      p.netprofit = p.value - prev_value
      p.netreturn = p.value / prev_value - 1.0
      p.totalprofit = p.value - p.startcap
      p.totalreturn = p.value / p.startcap - 1.0
      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 14 for calling a global function with example usage: valuate_portfolio(p, tdate) and returns: p