code: def update(self, txn):
      if self.asset != txn.asset:
          raise Exception(
              "updating position with txn for a " "different asset"
          )
  
      total_shares = self.amount + txn.amount
  
      if total_shares == 0:
          self.cost_basis = 0.0
      else:
          prev_direction = copysign(1, self.amount)
          txn_direction = copysign(1, txn.amount)
  
          if prev_direction != txn_direction:
              # we're covering a short or closing a position
              if abs(txn.amount) > abs(self.amount):
                  # we've closed the position and gone short
                  # or covered the short position and gone long
                  self.cost_basis = txn.price
          else:
              prev_cost = self.cost_basis * self.amount
              txn_cost = txn.amount * txn.price
              total_cost = prev_cost + txn_cost
              self.cost_basis = total_cost / total_shares
  
          # Update the last sale price if txn is
          # best data we have so far
          if (
              self.last_sale_date is None
              or txn.dt > self.last_sale_date
          ):
              self.last_sale_price = txn.price
              self.last_sale_date = txn.dt
  
      self.amount = total_shares
  
file_path: zipline/zipline/finance/position.py
imports: from __future__ import division
  from math import copysign
  import numpy
  import logbook
  from zipline.assets import Future
  import zipline.protocol
label_desc: Use image node_id 7 for calling the Position obj's underlying member method code with example usage: obj.update(txn) without return types