Skip to content
13

Greedy allocation

PortfolioOptimisers.GreedyAllocationResult Type
julia
struct GreedyAllocationResult{__T_oe, __T_retcode, __T_shares, __T_cost, __T_w, __T_cash, __T_fb} <: FiniteAllocationOptimisationResult

Result type for Greedy Allocation portfolio optimisation.

Fields

  • oe: Type of the optimisation estimator that produced this result.

  • retcode: Optimisation return code.

  • shares: Number of shares allocated per asset.

  • cost: Cost of the allocation.

  • w: Realised portfolio weights.

  • cash: Remaining uninvested cash after allocation.

  • fb: Fallback result or estimator.

Related

source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

  • kwargs...: Arbitrary keyword arguments (ignored).

Returns

  • a: The input unchanged.

Examples

julia
julia> factory(nothing, 1, 2; x = 3)

julia> factory(MeanValue())
MeanValue
  w ┴ nothing

Related

source
julia
factory(res::FiniteAllocationOptimisationResult, fb::Option{<:FOptE_FOpt})

Rebuild a finite allocation result with an updated fallback optimiser fb.

Like the continuous-result generic, every finite allocation result carries fb as its last field, so the rebuild copies all fields unchanged except the trailing fb. Concrete result types may override this method when rebuilding requires more than swapping fb.

Related

source
PortfolioOptimisers.GreedyAllocation Type
julia
struct GreedyAllocation{__T_unit, __T_args, __T_kwargs, __T_fb} <: FiniteAllocationOptimisationEstimator

Greedy Allocation portfolio optimiser.

GreedyAllocation converts continuous portfolio weights to discrete share quantities using a greedy two-pass allocation: first round shares to the nearest unit multiple, then iteratively buy remaining shares with leftover cash in order of largest weight.

Mathematical definition

xi(0)=round(wiCpiunit)unit,r(0)=Cx(0)p.

Then iteratively while r>0:

i=argmaxi:pirwi,xixi+unit,rrpiunit.

Where:

  • xi(0): Initial share allocation for asset i.

  • r(0): Residual cash after initial allocation.

  • w: Target weight vector.

  • C: Available cash.

  • p: Asset price vector.

  • unit: Minimum share purchase unit.

  • i: Asset with largest weight among those affordable with remaining cash r.

  • x(0): Initial share allocation vector.

Fields

  • unit: Minimum purchase unit (e.g., price per share or lot size).

  • args: Additional positional arguments forwarded to round.

  • kwargs: Additional keyword arguments.

  • fb: Fallback result or estimator.

Constructors

julia
GreedyAllocation(;
    unit::Number = 1,
    args::Tuple = (),
    kwargs::NamedTuple = (;),
    fb::Option{<:FOptE_FOpt} = nothing
) -> GreedyAllocation

Keywords correspond to the struct's fields.

Examples

julia
julia> GreedyAllocation()
GreedyAllocation
    unit ┼ Int64: 1
    args ┼ Tuple{}: ()
  kwargs ┼ @NamedTuple{}: NamedTuple()
      fb ┴ nothing

Related

source
PortfolioOptimisers.roundmult Function
julia
roundmult(val, prec, args...; kwargs...)

Round a value to the nearest multiple of prec.

Arguments

  • val: Value to round.

  • prec: Precision (multiple to round to).

  • args...: Additional arguments passed to Base.round.

  • kwargs...: Additional keyword arguments passed to Base.round.

Returns

  • Rounded value.

Related

source
PortfolioOptimisers.finite_sub_allocation! Function
julia
finite_sub_allocation!(w, p, cash, bgt, ...)

In-place finite allocation for one side (long or short) of the portfolio using the greedy algorithm.

Modifies the allocation in-place, greedily assigning shares to assets to minimise allocation error.

Arguments

  • w: Target portfolio weights (in-place modified).

  • p: Asset prices.

  • cash: Cash available.

  • bgt: Budget target.

  • Additional parameters.

Returns

  • Modified allocation vector.

Related

source
PortfolioOptimisers.optimise Function
julia
optimise(ga::GreedyAllocation{<:Any, <:Any, <:Any, Nothing}, w::VecNum, p::VecNum,
         cash::Number = 1e6, T::Option{<:Number} = nothing,
         fees::Option{<:Fees} = nothing; kwargs...) -> GreedyAllocationResult

Run the Greedy Allocation portfolio optimisation.

Arguments

  • ga: The greedy allocation optimiser to use.

  • w: Portfolio weights vector assets × 1.

  • p: The prices of the assets in the same order as w.

  • cash: The initial cash balance.

  • T: The time horizon for the optimisation. Used to adjust the initial cash balance according to the fees charged on the portfolio for the time horizon.

  • fees: The fees to apply to the portfolio.

  • kwargs: Additional keyword arguments passed to the optimisation function.

Related

source