code: def stop_loss(p, tdate):
      r"""Trim the portfolio based on stop-loss criteria.
  
      Parameters
      ----------
      p : alphapy.Portfolio
          The portfolio for reducing positions based on ``maxloss``.
      tdate : datetime
          The date to trim any underperforming positions.
  
      Returns
      -------
      p : alphapy.Portfolio
          The reduced portfolio.
  
      Notes
      -----
  
      .. warning:: The portfolio management functions ``stop_loss``,
         ``balance``, and ``kick_out`` are not part of the
         main **StockStream** pipeline, and thus have not been
         thoroughly tested. Feel free to exercise the code and
         report any issues.
  
      """
      positions = p.positions
      maxloss = p.maxloss
      for key in positions:
          pos = positions[key]
          nr = pos.netreturn
          if nr <= -maxloss:
              p = close_position(p, pos, tdate)
      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 13 for calling a global function with example usage: stop_loss(p, tdate) and returns: p