code: def allocate_trade(p, pos, trade):
      r"""Determine the trade allocation for a given portfolio.
  
      Parameters
      ----------
      p : alphapy.Portfolio
          Portfolio that will hold the new position.
      pos : alphapy.Position
          Position to update.
      trade : alphapy.Trade
          The proposed trade.
  
      Returns
      -------
      allocation : float
          The trade size that can be allocated for the portfolio.
  
      """
      cash = p.cash
      margin = p.margin
      mincash = p.mincash
      restricted = p.restricted
      if restricted:
          kick_out(p, trade.tdate)
          stop_loss(p, trade.tdate)
      multiplier = pos.multiplier
      qpold = pos.quantity
      qtrade = trade.quantity
      qpnew = qpold + qtrade
      allocation = abs(qpnew) - abs(qpold)
      addedvalue = trade.price * multiplier * abs(allocation)
      if restricted:
          cashreserve = mincash * cash
          freemargin = (cash - cashreserve) / margin
          if addedvalue > freemargin:
              logger.info(
                  "Required free margin: %d < added value: %d",
                  freemargin,
                  addedvalue,
              )
              allocation = 0
          else:
              freecash = cash - addedvalue
              if freecash < 0:
                  p.cash = cash + freecash
      return allocation
  
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 15 for calling a global function with example usage: allocate_trade(p, pos, trade) and returns: allocation