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
- If
wis empty, chargefee, theallocation_feeof the empty book. Return three empty vectors,cash - feeandfee. An empty side can still owe the forced exit ofallocation_liquidation_fee. - Sort the assets by descending target weight into
idx. Vieww,pand a vectorluin that order, and put the charge in that order withpermute_side_fees. - Charge
fee, the fee of the empty book, and start the running cashacash = 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. - Renormalise
wto sum to one. A side whose target weights are all zero keeps them at zero. - 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 whosecostis more thanacash. - In the second pass, while
acash > 0, compute thedeficitof each asset. Buy one lot of the affordable asset with the largest positive deficit, and stop when no such asset remains. - Both passes count the lots of each asset, and set its share count to the lot count times its lot unit.
- Compute
cost = p .* sharesand the realised weightsaw, rescaled to sum tobgt. Chargefee, the fee of the whole book, and computeacash = cash - sum(cost) - fee. - Permute
shares,costandawback to the caller's order withinvperm(idx).
Arguments
w::VecNum: Target weights of this side.p::VecNum: Asset prices of this side, in the same order asw.lu::Num_VecNum: Lot unit of this side, one scalar for every asset or one value for each asset in the order ofw.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, fromallocation_side_fees, ornothing.ga::GreedyAllocation: Allocator of the side. The method reads none of its fields, becauseallocation_lot_unitresolved its lot unit tolu.
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 tobgt. 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
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
- On a
nothingcharge, return a zero. - Compute
m0 = shares[i] * p[i], the money in the position before the purchase, andm1 = m0 + qty * p[i], the money after it. - Proportional term: add
T * prop[i] * (m1 - m0). - Turnover term: add
T * tn_val[i] * (abs(m1 - prev) - abs(m0 - prev)), whereprevis the money that the position held before the trade. This term is negative when the purchase moves the position towardsprev. - Fixed term: add
fixed[i]whenm0is zero andm1is not. - Return
delta, the sum of the terms.
Arguments
sf: One side's charge, fromallocation_side_fees, ornothing.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