code: def exec_trade(p, name, order, quantity, price, tdate):
      r"""Execute a trade for a portfolio.
  
      Parameters
      ----------
      p : alphapy.Portfolio
          Portfolio in which to trade.
      name : str
          The symbol to trade.
      order : alphapy.Orders
          Long or short trade for entry or exit.
      quantity : int
          The quantity for the order.
      price : str
          The execution price of the trade.
      tdate : datetime
          The date and time of the trade.
  
      Returns
      -------
      tsize : float
          The executed trade size.
  
      Other Parameters
      ----------------
      Frame.frames : dict
          Dataframe for the price data.
  
      """
      # see if the position already exists
      if name in p.positions:
          pos = p.positions[name]
          newpos = False
      else:
          pos = Position(p, name, tdate)
          newpos = True
      # check the dynamic position sizing variable
      if not p.posby:
          tsize = quantity
      else:
          if order == Orders.le or order == Orders.se:
              pf = Frame.frames[frame_name(name, p.space)].df
              cv = float(pf.loc[tdate][p.posby])
              tsize = math.trunc((p.value * p.fixedfrac) / cv)
              if quantity < 0:
                  tsize = -tsize
          else:
              tsize = -pos.quantity
      # instantiate and allocate the trade
      newtrade = Trade(name, order, tsize, price, tdate)
      allocation = allocate_trade(p, pos, newtrade)
      if allocation != 0:
          # create a new position if necessary
          if newpos:
              p = add_position(p, name, pos)
              p.npos += 1
          # update the portfolio
          p = update_portfolio(p, pos, newtrade)
          # if net position is zero, then close the position
          pflat = pos.quantity == 0
          if pflat:
              p = close_position(p, pos, tdate)
              p.npos -= 1
      else:
          logger.info("Trade Allocation for %s is 0", name)
      # return trade size
      return tsize
  
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 16 for calling a global function with example usage: exec_trade(p, name, order, quantity, price, tdate) and returns: tsize