liveBook · Manning

Transaction cost

As explained in Build a Robo-Advisor with Python (From Scratch)

A transaction cost is an expense incurred when buying, selling, transferring, or modifying an asset or contract.

A transaction cost is an expense incurred when buying, selling, transferring, or modifying an asset or contract. In investing, it extends beyond visible commissions: bid–ask spreads and tax consequences can make frequent trading expensive. section 1.2.3 section 13.1.2

Account for transaction costs when evaluating trades:

  • Include the bid–ask spread for both the asset sold and the asset purchased. section 1.2.3 section 14.3.1
  • Compare explicit costs and tax effects with the benefit motivating the trade, such as a tax-loss harvest. section 14.3.1
  • Recognize that rebalancing can realize capital gains, while frequent trading can create short-term gains that are not tax-efficient. section 1.2.3 section 13.1.2

Net cost of a tax-loss harvest

For a lot with quantity q and current price p, trade size is q × p. The implementation computes spread cost as (sell spread + buy spread) × trade size and commission cost as twice the commission rate times trade size, then subtracts both from the tax benefit. section 14.3.1

Listing 14.11 Measuring benefit of harvesting a loss, net of trading cost
def evaluate_tcost(lot: pd.Series, current_price: float,
                   sell_spread: float, buy_spread: float,
                   commission_rate: float = 0):
    """ Evaluate a loss harvest based on the cost to execute it """
    trade_size = lot['quantity'] * current_price

    spread_cost = (buy_spread + sell_spread) * trade_size
    commission_cost = 2 * commission_rate * trade_size

    return lot['tax_benefit'] - (spread_cost + commission_cost)

Use the supplied Python listing to calculate whether a harvesting trade’s tax benefit exceeds its spread and commission costs. section 14.3.1

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Quantifies spreads and commissions in a Python function, while treating taxes as a separate practical consequence of trading.
Examples
Uses frequent trading, portfolio rebalancing, housing upgrades, and a tax-loss-harvesting cost function as examples.
What it assumes
Assumes familiarity with asset trades, bid–ask spreads, capital gains, taxes, and the motivation for rebalancing or harvesting losses.
What you can do after
Evaluate/understand trades by comparing tax benefits with spread and commission costs.
How it connects
Connects transaction costs with bid–ask spreads, commissions, portfolio rebalancing, capital gains taxes, and tax-loss harvesting.

Also explained in

Compare how 4 books explain transaction cost →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.