Greedy finite allocation: private API

PortfolioOptimisers.finite_sub_allocation! — Function
finite_sub_allocation!(w::VecNum, p::VecNum, lu::Num_VecNum, cash::Number, bgt::Number,
                       sf::Option{<:SideFees}, ga::GreedyAllocation)

Run the greedy two-pass allocation over one side, long or short, of the portfolio.

This method runs the two passes of GreedyAllocation for a single side. It changes none of its arguments, because it sorts through views and renormalises into a new vector. The affordability test of both passes compares the cash with the cost of the purchase plus the amount of greedy_fee_delta, so a stated fee cannot overdraw the budget.

Algorithm

  1. If w is empty, charge fee, the allocation_fee of the empty book. Return three empty vectors, cash - fee and fee. An empty side can still owe the forced exit of allocation_liquidation_fee.
  2. Sort the assets by descending target weight into idx. View w, p and a vector lu in that order, and put the charge in that order with permute_side_fees.
  3. Charge fee, the fee of the empty book, and start the running cash acash = cash - fee. This fee is not zero when the side held money before the trade, because the turnover fee charges the sale, or when the universe lost an asset.
  4. Renormalise w to sum to one. A side whose target weights are all zero keeps them at zero.
  5. In the first pass, go through the order and buy n_lots, the floor of the lots of the target of each asset. Stop at the first asset whose cost is more than acash.
  6. In the second pass, while acash > 0, compute the deficit of each asset. Buy one lot of the affordable asset with the largest positive deficit, and stop when no such asset remains.
  7. Both passes count the lots of each asset, and set its share count to the lot count times its lot unit.
  8. Compute cost = p .* shares and the realised weights aw, rescaled to sum to bgt. Charge fee, the fee of the whole book, and compute acash = cash - sum(cost) - fee.
  9. Permute shares, cost and aw back to the caller's order with invperm(idx).

Arguments

  • w::VecNum: Target weights of this side.
  • p::VecNum: Asset prices of this side, in the same order as w.
  • lu::Num_VecNum: Lot unit of this side, one scalar for every asset or one value for each asset in the order of w.
  • cash::Number: Cash allocated to this side.
  • bgt::Number: Budget of this side, used to rescale the realised weights.
  • sf::Option{<:SideFees}: This side's charge, from allocation_side_fees, or nothing.
  • ga::GreedyAllocation: Allocator of the side. The method reads none of its fields, because allocation_lot_unit resolved its lot unit to lu.

Returns

  • shares::VecNum: Share count per asset, in the caller's asset order. Each entry is a whole number of lots of its lot unit.
  • cost::VecNum: shares .* p, in the caller's order.
  • aw::VecNum: Realised weights, rescaled to sum to bgt. All zero when nothing was bought.
  • acash::Number: Cash left over after the shares and the fee.
  • fee::Number: The fee that this side paid over the whole horizon.

Related

source
PortfolioOptimisers.greedy_fee_delta — Function
greedy_fee_delta(::Nothing, ::VecNum, shares::VecNum, ::Integer, ::Number)
greedy_fee_delta(sf::SideFees, p::VecNum, shares::VecNum, i::Integer, qty::Number)

Charge the amount that a purchase of qty shares of asset i adds to one side's fee.

The greedy allocator buys one asset at a time against a running cash figure, so it charges the fee purchase by purchase. The delta of each term is exact and needs no loop. The affordability test of both passes compares the cash with the cost of the shares plus this number.

Algorithm

  1. On a nothing charge, return a zero.
  2. Compute m0 = shares[i] * p[i], the money in the position before the purchase, and m1 = m0 + qty * p[i], the money after it.
  3. Proportional term: add T * prop[i] * (m1 - m0).
  4. Turnover term: add T * tn_val[i] * (abs(m1 - prev) - abs(m0 - prev)), where prev is the money that the position held before the trade. This term is negative when the purchase moves the position towards prev.
  5. Fixed term: add fixed[i] when m0 is zero and m1 is not.
  6. Return delta, the sum of the terms.

Arguments

  • sf: One side's charge, from allocation_side_fees, or nothing.
  • p::VecNum: Asset prices of this side.
  • shares::VecNum: Share count per asset, before the purchase.
  • i::Integer: The asset bought.
  • qty::Number: The share count bought.

Returns

  • delta::Number: What the purchase adds to the fee.

Related

source