Greedy allocation: private API
PortfolioOptimisers.roundmult — Function
roundmult(val, prec, args...; kwargs...) -> NumberTruncate val towards zero to a multiple of prec, then round that multiple with Base.round.
Equivalent to round(div(val, prec) * prec, args...; kwargs...). This is not a round to the nearest multiple of prec: div truncates, so roundmult(7.5, 2) is 6.0 where the nearest multiple of 2 is 8.0. The trailing Base.round acts on the truncated product, so a prec below one can leave a value that is no longer a multiple of prec unless args or kwargs say otherwise. Pass RoundDown in args to suppress it.
Arguments
val::Number: Value to truncate.prec::Number: Multiple to truncate to.args...: Positional arguments forwarded toBase.round, such as aRoundingMode.kwargs...: Keyword arguments forwarded toBase.round, such asdigitsorsigdigits.
Returns
res::Number: The truncated and rounded value.
Examples
julia> PortfolioOptimisers.roundmult(7.5, 2)6.0julia> PortfolioOptimisers.roundmult(26.58, 1)26.0Related
PortfolioOptimisers.finite_sub_allocation! — Function
finite_sub_allocation!(w::VecNum, p::VecNum, cash::Number, bgt::Number,
sf::Option{<:NamedTuple}, ga::GreedyAllocation, args...)Run the greedy two-pass allocation over one side, long or short, of the portfolio.
Implements the two passes of GreedyAllocation for a single side. An empty w returns three empty vectors, the untouched cash and a zero fee.
Arguments
w::VecNum: Target weights of this side. The routine writesw ./= sum(w)through the view it is given, so a caller that needs the original weights must pass a copy.p::VecNum: Asset prices of this side, in the same order asw.cash::Number: Cash allocated to this side.bgt::Number: Budget of this side, used to rescale the realised weights.sf::Option{<:NamedTuple}: This side's charge, ofallocation_side_fees, ornothing.ga::GreedyAllocation: Allocator carryingunit,argsandkwargs.args...: Ignored. Present so that both allocators share one call shape.
Returns
shares::VecNum: Share count per asset, restored to the caller's asset order.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 fee is paid.fee::Number: The fee this side paid over the whole horizon.
Details
- The assets are sorted by descending target weight, and the answer is permuted back before it is returned.
permute_side_feesputs the rates into the same order. - The affordability test of both passes is on the cost of the purchase plus what it adds to the fee, which is
greedy_fee_delta. Testing the shares alone overdraws the budget whenever a fee is stated. - The pass starts owing
allocation_feeof the empty book. That is zero unless the side held money before the trade, because selling out is a trade and the turnover fee charges it, or unless the universe lost an asset, because the forced exit ofallocation_liquidation_feeis owed whatever the side buys.
Related
PortfolioOptimisers.greedy_fee_delta — Function
greedy_fee_delta(::Nothing, ::VecNum, shares::VecNum, ::Integer, ::Number)
greedy_fee_delta(sf::NamedTuple, p::VecNum, shares::VecNum, i::Integer, qty::Number)Charge what buying 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 needs the fee charged as it goes. The delta of each term is exact and costs no loop, so the affordability test of both passes is a test against the cost of the shares plus this number.
Algorithm
- Proportional:
T * f_p[i] * qty * p[i], the money the purchase adds. - Turnover:
T * f_Tn[i]times the change in the money traded, which can be negative when the purchase moves the position towards the money it held before the trade. - Fixed:
f_f[i], and only when the position was zero and becomes non-zero.
Arguments
sf: One side's charge, ofallocation_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