Trading with a single asset and a single trade algorithm is usually not enough to generate a regular income. The returns fluctuate too much and can include years with no income (or even a negative one). Strategies to live from normally use a portfolio of many assets and many trade rules. For a relatively smooth income curve, you normally should have more than 10 assets and more than 10 trade algorithms in your strategy. This gives you more than hundred asset/algorithm combinations, which are the components of the strategy. They all require different strategy parameters, have different performance, and require different capital allocation and reinvestment factors for achieving the optimal overall profit from the portfolio.

Reinvesting profits - the square root rule

Many traders believe they should invest a fixed percentage - such as 1%, or for the more daring, 2% - of their account balance per trade. This is one of the most common reasons of unexpected margin calls, especially with automated systems. Here's why:

The maximum drawdown of any trade system increases over time. The longer you trade, the higher is the probability of a long loss streak and the bigger the depth of the drawdown. That's why a system, tested over 10 years, has a worse maximum drawdown than the same system tested over only 5 years. When modeling drawdown depth mathematically with a diffusion model, the maximum drawdown of a break-even system is proportional to the square root of the number of trades, and therefore also to the square root of the trading time. This also means that drawdowns have no limit. A trading system will suffer a drawdown of any depth when you wait long enough.

Drawdown also increases with the invested amount: When investing twice the volume you'll get twice the drawdown. Thus, when you reinvest a fixed percentage of your balance, the maximum drawdown grows with the balance. And the balance of a profitable system also grows proportional to the trading time.

When summing up both effects, you'll get an overproportional drawdown growth with trading time: the drawdown grows proportionally to time to the power of 1.5. The 1 comes from the reinvested profit, the 0.5 from the square root of the number of trades (in fact the exponent will be slightly higher than 1.5 as reinvested profit also grows overproportionally, but that shall not bother us here). In any case your drawdowns will grow faster than your account balance. At some point, a drawdown will inevitably exceed the balance, causing a margin call. That will happen later or sooner, dependent on the system and the reinvested percentage.

Therefore better don't invest a fixed balance percentage, no matter how often it's recommended in trading books or seminars. There are several methods to overcome the drawdown growth issue. One method is to reinvest only an amount proportional to the square root of the capital growth. Thus when your capital doubles, increase the trade volume only by a factor of about 1.4 (the square root of 2), i.e. 40%. Example: You're trading with a Margin of \$50. Your account doubles from an initial \$1000 to \$2000. You can now increase your Margin to \$70 (= 1.4 * \$50) for reinvesting your gain.

Another method is investing a variable percentage - for instance the OptimalF factor, see below - that is calculated from the real equity curve and regularly updated so that it decreases when the drawdowns increase. In both cases, the drawdowns of your system will then only grow at the same rate as your account balance, so you stay away from a margin call.

In workshop 6 you can find code examples of several methods for correctly (nor not) reinvesting profits.

Withdrawing profits

If you do not reinvest, but withdraw your profits regularly, keep a part of your profit on the account for the very same reason. As explained above, the expectancy value of the maximum drawdown depth grows with the square root of trading time. So your account balance must grow by the same factor for keeping pace with the expected drawdown. Thus, when your account doubled, you can remove only 60% of your profit and should let 40% stay on the account. This lets your account grow by the required factor 1.4 (again, the square root of 2).

Example: You start with a capital of \$1000 and want to withdraw profit whenever the system won \$300. Thus the first withdrawal is at \$1300 account balance. Your investment grew by factor 1.3; the square root of 1.3 is 1.14. \$1140 must stay on the account and you can withdraw \$160. - Now your system made another \$ 300. The account balance is now \$1440, but the total growth (without the withdrawn amount) is \$1600 / \$1000 = 1.6. The square root of 1.6 is 1.265. \$1265 must stay, and \$175 can be withdrawn.

What if you want to withdraw not the whole amount, but reinvest the rest? Here are some simple formulae that help you calculate what you can withdraw, what you can reinvest, and what should remain on the account:

Balance on account:  C+P-W
Must stay on account: C*f
Available to withdraw:   C+P-W - C*f
Available to reinvest:    C*f - W/f

where C is your initial capital at the first start of the system, P the total profit so far, W the total withdrawal so far (including what you're just withdrawing), and f the square root growth factor sqrt(1+P/C). Example: You start with a capital of \$1000 and won \$300, so your balance is now \$1300. How much capital do you have for reinvestment when you withdraw \$50?

Balance on account:  C+P-W = \$1300
Must stay on account: C*f = \$1000 * sqrt(1+\$300/\$1000) = \$1000 * 1.14 = \$1140
Available to withdraw:   C+P-W - C*f = \$1300 - \$1140 = \$160
Available to reinvest:    C*f - W/f = \$1140 - \$50/1.14 = \$1096

So when withdrawing \$50 from a \$300 win, you can increase your investment from \$1000 to \$1096 (not to \$1250 as you might have expected). Consider the difference a tax that you pay to the god of statistics. Unfortunately you'll have to pay a real tax for that, too...

Allocating 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 many 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/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 further columns display the profit factor, the number of winning and losing trades, and the weight of the component; they are normally not used in strategies.

As the factors are stored in a simple text file, they can be edited anytime with a text editor, even while trading. The higher the factor, the more capital should be reinvested by the strategy component. 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.

Variables

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

OptimalFShort

OptimalF factor, and separate factors for long and short trades of the current strategy component that is selected with the asset and algo functions. The margin to be invested per trade can be calculated by Margin = OptimalF * Capital. In [Train] mode or when the FACTORS flag is not set, the OptimalF factors are always 1. If a component is unprofitable, its OptimalF factor is zero.

OptimalFRatio

Generate OptimalF factors with the given ratio of the highest to the lowest factor (default = 0 = no ratio). For instance, at OptimalFRatio = 3 large factors are reduced and small factors are increased so that the highest OptimalF factor is 3 times the lowest factor. The average of all factors remains unchanged. Useful for preventing large component margin differences when using OptimalF factors in portfolio systems.

Remarks

• 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 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.
• 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.

Examples of different investment methods

```// reinvest the square root of your portfolio component profits, separately for long and short trades
if(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 profits
Margin = OptimalFLong * Capital * sqrt(1 + ProfitClosed/Capital);

// reinvest the square root of your total profits
Margin = OptimalFLong * Capital * sqrt(1 + (WinTotal-LossTotal)/Capital);
```