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, or -1 for
ignoring the component. |

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 that is
only sufficient for a few shares, calculate first the asset weights with the markowitz or
**distribute**functions, then use the weights as**Values**for a**knapsack**optimization. - For a large budget that can buy many shares, 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 less diversified portfolio with accordingly higher risk.
- For a fixed ratio of different asset classes - for instance, 70%
equities and 30% bonds - call
**distribute**twice with stocks and bonds, then multiply the stock weights with**0.7**and the bond weights with**0.3**. - For optimizing a portfolio rebalancing system, make sure to setf(TrainMode,TRADES) because trade size matters. Make also sure to select an asset before calling optimize.

// knapsack testvoid 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); }

// portfolio rebalancingvoid run() { ... assetList("MyPortfolio");// max 100 assetsstatic var Weights[100],Strengths[100]; if(month(0) != month(1))// rebalance any month{// calculate asset strengthswhile(asset(loop(Assets))) Strengths[Itor1] = RET(30*PERIOD);// enter positions of the the 4 strongest assetsdistribute(Weights,Strengths,NumAssetsListed,4,0); int i; for(i=0; i<NumAssetsListed; i++) { asset(Assets[i]); int NewShares = Balance * Weights[i]/priceClose(0) - LotsPool; if(NewShares > 0) enterLong(NewShares); else if(NewShares < 0) exitLong("",0,-NewShares); } } }