class_docstr: Keeps track of the life of an trade: size, price,
  commission (and value?)
  
  An trade starts at 0 can be increased and reduced and can
  be considered closed if it goes back to 0.
  
  The trade can be long (positive size) or short (negative size)
  
  An trade is not meant to be reversed (no support in the logic for it)
  
  Member Attributes:
  
    - ``ref``: unique trade identifier
    - ``status`` (``int``): one of Created, Open, Closed
    - ``tradeid``: grouping tradeid passed to orders during creation
      The default in orders is 0
    - ``size`` (``int``): current size of the trade
    - ``price`` (``float``): current price of the trade
    - ``value`` (``float``): current value of the trade
    - ``commission`` (``float``): current accumulated commission
    - ``pnl`` (``float``): current profit and loss of the trade (gross pnl)
    - ``pnlcomm`` (``float``): current profit and loss of the trade minus
      commission (net pnl)
    - ``isclosed`` (``bool``): records if the last update closed (set size to
      null the trade
    - ``isopen`` (``bool``): records if any update has opened the trade
    - ``justopened`` (``bool``): if the trade was just opened
    - ``baropen`` (``int``): bar in which this trade was opened
  
    - ``dtopen`` (``float``): float coded datetime in which the trade was
      opened
  
      - Use method ``open_datetime`` to get a Python datetime.datetime
        or use the platform provided ``num2date`` method
  
    - ``barclose`` (``int``): bar in which this trade was closed
  
    - ``dtclose`` (``float``): float coded datetime in which the trade was
      closed
  
      - Use method ``close_datetime`` to get a Python datetime.datetime
        or use the platform provided ``num2date`` method
  
    - ``barlen`` (``int``): number of bars this trade was open
    - ``historyon`` (``bool``): whether history has to be recorded
    - ``history`` (``list``): holds a list updated with each "update" event
      containing the resulting status and parameters used in the update
  
      The first entry in the history is the Opening Event
      The last entry in the history is the Closing Event
class_docstr_tok: ["Keeps","track","of","the","life","of","an","trade",":","size",",","price",",","commission","(","and","value","?",")","An","trade","starts","at","0","can","be","increased","and","reduced","and","can","be","considered","closed","if","it","goes","back","to","0",".","The","trade","can","be","long","(","positive","size",")","or","short","(","negative","size",")","An","trade","is","not","meant","to","be","reversed","(","no","support","in","the","logic","for","it",")","Member","Attributes",":","-","``","ref","``",":","unique","trade","identifier","-","``","status","``","(","``","int","``",")",":","one","of","Created",",","Open",",","Closed","-","``","tradeid","``",":","grouping","tradeid","passed","to","orders","during","creation","The","default","in","orders","is","0","-","``","size","``","(","``","int","``",")",":","current","size","of","the","trade","-","``","price","``","(","``","float","``",")",":","current","price","of","the","trade","-","``","value","``","(","``","float","``",")",":","current","value","of","the","trade","-","``","commission","``","(","``","float","``",")",":","current","accumulated","commission","-","``","pnl","``","(","``","float","``",")",":","current","profit","and","loss","of","the","trade","(","gross","pnl",")","-","``","pnlcomm","``","(","``","float","``",")",":","current","profit","and","loss","of","the","trade","minus","commission","(","net","pnl",")","-","``","isclosed","``","(","``","bool","``",")",":","records","if","the","last","update","closed","(","set","size","to","null","the","trade","-","``","isopen","``","(","``","bool","``",")",":","records","if","any","update","has","opened","the","trade","-","``","justopened","``","(","``","bool","``",")",":","if","the","trade","was","just","opened","-","``","baropen","``","(","``","int","``",")",":","bar","in","which","this","trade","was","opened","-","``","dtopen","``","(","``","float","``",")",":","float","coded","datetime","in","which","the","trade","was","opened","-","Use","method","``","open_datetime","``","to","get","a","Python","datetime.datetime","or","use","the","platform","provided","``","num2date","``","method","-","``","barclose","``","(","``","int","``",")",":","bar","in","which","this","trade","was","closed","-","``","dtclose","``","(","``","float","``",")",":","float","coded","datetime","in","which","the","trade","was","closed","-","Use","method","``","close_datetime","``","to","get","a","Python","datetime.datetime","or","use","the","platform","provided","``","num2date","``","method","-","``","barlen","``","(","``","int","``",")",":","number","of","bars","this","trade","was","open","-","``","historyon","``","(","``","bool","``",")",":","whether","history","has","to","be","recorded","-","``","history","``","(","``","list","``",")",":","holds","a","list","updated","with","each","``","update","''","event","containing","the","resulting","status","and","parameters","used","in","the","update","The","first","entry","in","the","history","is","the","Opening","Event","The","last","entry","in","the","history","is","the","Closing","Event"]
docstr: Updates the current trade. The logic does not check if the
  trade is reversed, which is not conceptually supported by the
  object.
  
  If an update sets the size attribute to 0, "closed" will be
  set to true
  
  Updates may be received twice for each order, once for the existing
  size which has been closed (sell undoing a buy) and a second time for
  the the opening part (sell reversing a buy)
  
  Args:
      order: the order object which has (completely or partially)
          generated this update
      size (int): amount to update the order
          if size has the same sign as the current trade a
          position increase will happen
          if size has the opposite sign as current op size a
          reduction/close will happen
  
      price (float): always be positive to ensure consistency
      value (float): (unused) cost incurred in new size/price op
                     Not used because the value is calculated for the
                     trade
      commission (float): incurred commission in the new size/price op
      pnl (float): (unused) generated by the executed part
                   Not used because the trade has an independent pnl
docstr_tok: ["Updates","the","current","trade",".","The","logic","does","not","check","if","the","trade","is","reversed",",","which","is","not","conceptually","supported","by","the","object",".","If","an","update","sets","the","size","attribute","to","0",",","``","closed","''","will","be","set","to","true","Updates","may","be","received","twice","for","each","order",",","once","for","the","existing","size","which","has","been","closed","(","sell","undoing","a","buy",")","and","a","second","time","for","the","the","opening","part","(","sell","reversing","a","buy",")","Args",":","order",":","the","order","object","which","has","(","completely","or","partially",")","generated","this","update","size","(","int",")",":","amount","to","update","the","order","if","size","has","the","same","sign","as","the","current","trade","a","position","increase","will","happen","if","size","has","the","opposite","sign","as","current","op","size","a","reduction\/close","will","happen","price","(","float",")",":","always","be","positive","to","ensure","consistency","value","(","float",")",":","(","unused",")","cost","incurred","in","new","size\/price","op","Not","used","because","the","value","is","calculated","for","the","trade","commission","(","float",")",":","incurred","commission","in","the","new","size\/price","op","pnl","(","float",")",":","(","unused",")","generated","by","the","executed","part","Not","used","because","the","trade","has","an","independent","pnl"]
code: def update(
      self, order, size, price, value, commission, pnl, comminfo
  ):
      """
      Updates the current trade. The logic does not check if the
      trade is reversed, which is not conceptually supported by the
      object.
  
      If an update sets the size attribute to 0, "closed" will be
      set to true
  
      Updates may be received twice for each order, once for the existing
      size which has been closed (sell undoing a buy) and a second time for
      the the opening part (sell reversing a buy)
  
      Args:
          order: the order object which has (completely or partially)
              generated this update
          size (int): amount to update the order
              if size has the same sign as the current trade a
              position increase will happen
              if size has the opposite sign as current op size a
              reduction/close will happen
  
          price (float): always be positive to ensure consistency
          value (float): (unused) cost incurred in new size/price op
                         Not used because the value is calculated for the
                         trade
          commission (float): incurred commission in the new size/price op
          pnl (float): (unused) generated by the executed part
                       Not used because the trade has an independent pnl
      """
      if not size:
          return  # empty update, skip all other calculations
  
      # Commission can only increase
      self.commission += commission
  
      # Update size and keep a reference for logic an calculations
      oldsize = self.size
      self.size += size  # size will carry the opposite sign if reducing
  
      # Check if it has been currently opened
      self.justopened = bool(not oldsize and size)
  
      if self.justopened:
          self.baropen = len(self.data)
          self.dtopen = (
              0.0 if order.p.simulated else self.data.datetime[0]
          )
          self.long = self.size > 0
  
      # Any size means the trade was opened
      self.isopen = bool(self.size)
  
      # Update current trade length
      self.barlen = len(self.data) - self.baropen
  
      # record if the position was closed (set to null)
      self.isclosed = bool(oldsize and not self.size)
  
      # record last bar for the trade
      if self.isclosed:
          self.isopen = False
          self.barclose = len(self.data)
          self.dtclose = self.data.datetime[0]
  
          self.status = self.Closed
      elif self.isopen:
          self.status = self.Open
  
      if abs(self.size) > abs(oldsize):
          # position increased (be it positive or negative)
          # update the average price
          self.price = (oldsize * self.price + size * price) / self.size
          pnl = 0.0
  
      else:  # abs(self.size) < abs(oldsize)
          # position reduced/closed
          pnl = comminfo.profitandloss(-size, self.price, price)
  
      self.pnl += pnl
      self.pnlcomm = self.pnl - self.commission
  
      self.value = comminfo.getvaluesize(self.size, self.price)
  
      # Update the history if needed
      if self.historyon:
          dt0 = self.data.datetime[0] if not order.p.simulated else 0.0
          histentry = TradeHistory(
              self.status,
              dt0,
              self.barlen,
              self.size,
              self.price,
              self.value,
              self.pnl,
              self.pnlcomm,
              self.data._tz,
          )
          histentry.doupdate(order, size, price, commission)
          self.history.append(histentry)
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import itertools
  from .utils import AutoOrderedDict
  from .utils.date import num2date
  from .utils.py3 import range
label_desc: Use image node_id 8 for calling the Trade obj's underlying member method code with example usage: obj.update(order, size, price, value, commission, pnl, comminfo) without return types