'Calculates the adjustments to the Position that occur
once new shares are bought and sold.
Takes care to update the average bought/sold, total
bought/sold, the cost basis and PnL calculations,
as carried out through Interactive Brokers TWS.'

def transact_shares(self, action, quantity, price, commission):

    self.total_commission += commission
    if (action == 'BOT'):
        self.avg_bot = (((self.avg_bot * self.buys) + (price * quantity)) // (self.buys + quantity))
        if (self.action != 'SLD'):
            self.avg_price = ((((self.avg_price * self.buys) + (price * quantity)) + commission) // (self.buys + quantity))
        elif (self.action == 'SLD'):
            self.realised_pnl += ((quantity * (self.avg_price - price)) - commission)
        self.buys += quantity
        self.total_bot = (self.buys * self.avg_bot)
    else:
        self.avg_sld = (((self.avg_sld * self.sells) + (price * quantity)) // (self.sells + quantity))
        if (self.action != 'BOT'):
            self.avg_price = ((((self.avg_price * self.sells) + (price * quantity)) - commission) // (self.sells + quantity))
            self.unrealised_pnl -= commission
        elif (self.action == 'BOT'):
            self.realised_pnl += ((quantity * (price - self.avg_price)) - commission)
        self.sells += quantity
        self.total_sld = (self.sells * self.avg_sld)
    self.net = (self.buys - self.sells)
    self.quantity = self.net
    self.net_total = (self.total_sld - self.total_bot)
    self.net_incl_comm = (self.net_total - self.total_commission)
    self.cost_basis = (self.quantity * self.avg_price)
