For re-investing or assigning capital, Zorro can automatically calculate the optimal capital allocation factor - named **OptimalF** - separately for every component in a portfolio strategy. It uses a computer algorithm that evaluates the balance curve for calculating the percentage of the gained capital to be reinvested for maximum profit. For instance, if **OptimalF** is **0.05**, then the maximum margin for trading that component is 5% of the profit. The margin can be smaller - for the reasons mentioned above not the full profit but only its square root should be reinvested - but it must not be higher. This algorithm was developed by **Ralph Vince** and described in
several publications (see links).

When the FACTORS flag is set, the **OptimalF** factors are calculated in a special test run at the end of the [Train] process, and stored in a file **Data\*.fac**. It's a simple text file that looks like this:

AUD/USD:ES .036 1.14 45/87 0.1 AUD/USD:ES:L .036 1.14 45/87 0.1 AUD/USD:ES:S .000 ---- 0/0 0.0 EUR/USD:VO .027 2.20 24/23 3.3 EUR/USD:VO:L .027 1.58 12/11 0.9 EUR/USD:VO:S .032 2.90 12/12 2.5 NAS100:ES .114 1.42 63/90 4.6 NAS100:ES:L .101 1.39 33/44 2.1 NAS100:ES:S .128 1.46 30/46 2.5 USD/CAD:BB .030 1.41 19/25 1.3 USD/CAD:BB:L .030 1.41 19/25 1.3 USD/CAD:BB:S .000 ---- 0/0 0.0 USD/CAD:HU .012 1.74 48/36 3.3 USD/CAD:HU:L .066 1.42 24/20 0.2 USD/CAD:HU:S .012 1.79 24/16 3.1 USD/CHF:CT .104 1.60 16/17 0.6 USD/CHF:CT:L .104 1.60 16/17 0.6 USD/CHF:CT:S .000 ---- 0/0 0.0 USD/CHF:CY .025 1.10 21/24 0.1 USD/CHF:CY:L .025 1.10 21/24 0.1 USD/CHF:CY:S .000 ---- 0/0 0.0 USD/CHF:HP .025 1.45 31/48 3.2 USD/CHF:HP:L .000 ---- 0/0 0.0 USD/CHF:HP:S .025 1.45 31/48 3.2 USD/CHF:VO .011 3.93 17/8 7.6 USD/CHF:VO:L .011 3.93 17/8 7.6 USD/CHF:VO:S .000 ---- 0/0 0.0

The first column identifies the component; it consists of the asset name and the algorithm identifier. "**S**" or "**L**" are separate statistics for short or long trades. The second column contains the **OptimalF** factors for that component.
The higher the factor, the more capital should be reinvested by the strategy
component. A **0** indicates that this component should not be
traded. The further columns contain the profit factor, the number of winning and losing trades, and the weight of the component.

As the factors are stored in a simple text file, they can be edited anytime with a text editor, even while live trading. Zorro detects if factors have been changed, and automatically reloads them. If the factors are evaluated in the strategy, as in some of the Z strategies, a component can be excluded from further trading by setting its factor to zero, or by placing a minus sign in front of it for making it negative.

The following variables can be used for evaluating or generating **OptimalF** factors in the script:

- Every algo and asset call switches the
**OptimalF**variable to the factors belonging to the new component. - In Ralph Vince's publications,
**OptimalF**is defined in a different way, requiring a formula containing the maximum loss for calculating the number of lots of a trade. Zorro's**OptimalF**factors are already adjusted by the maximum loss, and thus can be directly multiplied with the earned capital for getting the optimal margin. **OptimalF**factors are normally calculated over the whole test period even when WFO is enabled. This slightly violates the out-of-sample test philosophy. Therefore when using**OptimalF**factors for reinvesting profits, the real trading performance can be worse than the performance predicted by a WFO test. TrainMode can be set to calculate**OptimalF**factors individually per WFO cycle.- In a portfolio system,
**OptimalF**is separately calculated for any component. The correlations of components do not affect the calculation. **OptimalF**is affected by maximum losses in the trade history, and thus tends to decrease when the test period increases. The reason is the same as the drawdown dependency on the test period discussed under**Reinvesting profits**above.- If the balance curve has very little drawdown, theoretically the full capital can be invested in that component for maximum profit.
**OptimalF**is then set to**0.999**. Investing the full capital is not recommended in real trading, as the balance curve is not guaranteed to continue this way in the future. If a component is unprofitable, OptimalF is set to**0.000**. - Trading with portfolio strategies and money management is explained in workshop 6.
- Markowitz weights can be used alternatively for allocating capital to portfolio components. They have the disadvantage of not considering reinvestment, but the advantage of minimizing the variance of the total portfolio.

// reinvest the square root of your portfolio component profits, separately for long and short tradesif(GoLong) Margin = OptimalFLong * Capital * sqrt(1 + (WinLong-LossLong)/Capital); else Margin = OptimalFShort * Capital * sqrt(1 + (WinShort-LossShort)/Capital);// reinvest the square root of your portfolio component profitsMargin = OptimalFLong * Capital * sqrt(1 + ProfitClosed/Capital);// reinvest the square root of your total profitsMargin = OptimalFLong * Capital * sqrt(1 + (WinTotal-LossTotal)/Capital);

► latest version online