Weights |
Output array of size N to receive
M normalized weights. The other elements are
0. |

Values |
Input array of size N, for instance projected
profits, momentums, or Sharpe ratios of
portfolio components. |

N |
Number of elements in the Weights,
Values, and
Caps arrays. |

M |
Max number of resulting nonzero weights, or 0 for
distributing all N weights. |

Caps |
Array of weight limits in the 0..1 range, or
0 for no weight limit. |

Cap |
Global limit in the 0..1 range to be
applied to all weights, or 0 for no weight limits. |

Items |
Output array of size N to receive the
weight-equivalent amounts for all assets. |

Costs |
Input array of size N containing the current
asset cost, such as price or margin cost. |

Weights |
Input array of size N containing the normalized
portfolio weights.The sum must be 1. |

N |
Number of elements of the arrays. |

Budget |
Available capital to distribute, for instance the current account equity. |

Items |
Output array of size N to receive the
optimal amounts for all assets. |

Costs |
Input array of size N containing the current
asset cost, such as price or margin cost. |

Values |
Input array of size N containing projected
prices or expected
profits. |

N |
Number of elements of the arrays. |

Budget |
Available capital to distribute, for instance the current account equity. |

Cap |
Maximum Budget weight per asset in the
0..1 range (f.i. 0.5 = max 50% of
Budget), or 0 for no asset limits. |

- For distributing small capital, asset weights can be calculated with the markowitz or
**distribute**functions, then used as**Values**for a**knapsack**optimization. - For a large budget, weight-based capital distribution is normally preferable to knapsack optimization. The knapsack algorithm allocates most of the capital to a few assets with the highest value/price ratio. This results sometimes in better backtest performance, but produces a not diversified portfolio with accordingly higher risk.

void main() { var wt[5] = {30,40,70,80,90}; var val[5] = {40,50,100,110,130}; int Items[5]; var Total = knapsack(Items,wt,val,5,500,0); printf("\nItems: %i %i %i %i %i => %.2f", Items[0],Items[1],Items[2],Items[3],Items[4],Total); }