**Diversification** is an important factor for getting a regular income with algorithmic trading. Therefore, many successful trading strategies trade not only with a portfolio of assets, but also with a portfolio of strategies. Of course, all components of the portfolio must be separately optimized. Here's a script that does that automatically. It trades with several assets on several time frames simultaneously, and uses both trend and counter-trend strategy algorithms (**Workshop6.c**):

// Counter trend trading function from Workshop 5function tradeCounterTrend() { TimeFrame = 4;// 4 hour time framevars Price = series(price(0)); vars Filtered = series(BandPass(Price,optimize(30,25,35),0.5));

vars Signal = series(FisherN(Filtered,500));

var Threshold = optimize(1,0.5,2,0.1); Stop = optimize(4,2,10) * ATR(100);

Trail = 4*ATR(100);

if(crossUnder(Signal,-Threshold)) enterLong(); else if(crossOver(Signal,Threshold)) enterShort(); }// Trend trading function from Workshop 4function tradeTrend() { TimeFrame = 1;// 1 hour time framevars Price = series(price(0)); vars Trend = series(LowPass(Price,optimize(500,300,700)));

Stop = optimize(4,2,10) * ATR(100);

Trail = 0;

vars MMI_Raw = series(MMI(Price,300)); vars MMI_Smooth = series(LowPass(MMI_Raw,500)); if(falling(MMI_Smooth)) { if(valley(Trend)) enterLong(); else if(peak(Trend)) enterShort(); } } function run() { set(PARAMETERS+FACTORS+LOGFILE);BarPeriod = 60;LookBack = 2000;StartDate = 2005;NumWFOCycles = 10;Capital = 10000;

if(ReTrain) { UpdateDays = -1; SelectWFO = -1; reset(FACTORS); }// double portfolio loopwhile(asset(loop("EUR/USD","USD/JPY"))) while(algo(loop("TRND","CNTR"))) { Margin = 0.5 * OptimalF * Capital; if(Algo == "TRND") tradeTrend(); else if(Algo == "CNTR") tradeCounterTrend(); } }

For testing a portfolio strategy, you'll first need historical data for more assets, since the Zorro installation has only EUR/USD included. Go to the Zorro download page, and download the M1 price histories from 2002 on (that's normally 3 large files). Unzip the content in your **History** folder. You now have the recent history of the major index and Forex pairs. For this strategy we'll need the USD/JPY history from 2005 on.

The strategy is divided into 3 different functions: **tradeTrend** for trend trading, **tradeCounterTrend** for counter trend trading, and the **run** function that sets up the parameters, selects the assets, sets up the trade volume, and calls the two trade functions.

The **tradeCounterTrend** function is almost the same as in the last workshop. At the begin of the function, the
**TimeFrame** is defined**. **The trend trading strategy from workshop 4 used 60-minutes bars, while counter trend trading was based on 240-minutes bars. When we trade both together in the same script, we'll need time frames of 60 and of 240 minutes.
Since **BarPeriod** is set to 60 minutes, it needs a **TimeFrame** of **4** bars for getting the 240 minutes period for the counter trend strategy. **TimeFrame** affects all subsequent **price** and **series** calls, and all indicators using those series, including the **ATR** function.

The **tradeTrend** function uses the same algorithm as in workshop 4. The **TimeFrame** variable is set to **1**, so this strategy is still based on a 60-minutes bar period. The **LowPass** time period and the stop loss value are now optimized with the method explained in the last workshop.
The **Trail** variable is explicitely set to **0**,
since Trend trading works best when profitable trades last a long time. Tailing would stop them too early. Since the **Trail** variable was already set in the **tradeCounterTrend** function, it must now be reset to **0**,
meaning no trailing. When predefined variables are used anywhere in the script,
make sure that they have the right value at any place where they are needed.

The core of the strategy
are the two 'nested' **while** loops. The loop function takes a number of arguments, and returns one of them every time it is called. On the first call it returns the first parameter, which is the string **"EUR/USD"**.
When called the next time, it returns **"USD/JPY"**. And on all further calls it returns **0**, as there are no further arguments. The string returned by the **loop** function is now used as argument to the asset function. This function selects the traded asset, just as if it had been choosen with the [Asset]
scrollbox. If we had wanted to trade the same strategy with more assets - for instance, also with commodities or stock indices - we just can add more arguments to the **loop** function, like this:

**while(asset(loop("EUR/USD","USD/CHF","USD/JPY","AUD/USD","XAU/USD","USOil","SPX500","NAS100",...)))**

and the rest of the code would remain the same. However, we do not only want
to trade with several assets, we also want to trade each asset with different
algorithms. For this, nested inside the first while loop is another while loop
that calls the algo function. It also returns **0** (= false)
at the end of the list. Thus, for every repetition of the first loop, the second loop repeats 2 times, first with "TRND" and then with "CNTR". As these strings are now stored in the
**Algo** variable, we use them for calling the trade function in the inner code of the double loop. The **if** condition checks if the **Algo** string is identical to a given string constant (**== "TRND"**) and returns nonzero, i.e. true, in that case. So when **Algo** was set to **"TRND"** by the algo function, **tradeTrend** is called; otherwise **tradeCounterTrend** is called.

Due to the two nested loops, the inner code is run four times per **run()**
call, equivalent to the 4 asset/algorithm combinations - the **components** - of the strategy. If we had instead entered 10 assets and 5 algorithms in the loops, we had 50 (10*5) components and the inner code would be run fifty times every
bar. All parts of the script that are affected by the asset - for instance, retrieving prices or calling indicators - must be inside the loop. This is fulfilled here because anything asset related happens inside the two called trading functions.

The trade volume is controlled by this line:

**Margin = 0.5 * OptimalF * Capital; **

We're using a money management method developed by **Ralph Vince**. He published a computer algorithm that evaluates every component's balance curve for calculating the optimal percentage of the gained capital to be reinvested. This percentage is called the **OptimalF** factor (you can see it also in the **OptF** column in the performance report). Multiply **OptimalF** with the capital, and you'll get the maximum margin to be allocated to a certain trade. Normally, people try to stay well below this maximum margin. **OptimalF** is specific to the strategy component and calculated from historical performance, meaning there's no guarantee that this performance will continue in the future. Exceeding the maximum margin is worse than staying below it, therefore it's recommended to only invest about 50% of **OptimalF**.

Margin is one of the methods for determining the amount invested per trade. Other methods were giving the number of lots or the money at risk. **Margin** is only a fixed percentage of the real trade volume - for instance 1% at 1:100 leverage - that the broker keeps as a deposit. If **Margin** is left at its default value, Zorro always buys 1 lot, the minimum allowed trade size. The higher the margin, the higher the number of lots and the higher the profit or loss. **Capital** is a variable set to our initial investment ($10,000) for determining its annual growth (CAGR). For determining the optimal margin for the strategy component, this **Capital** is multiplied with the 50% of the **OptimalF** factor. For generating
not only parameters, but also the **OptimalF** factors in the training run, the **FACTORS** flag
must be set.

Now it's time to [Train] the strategy. Because we have now 4 components, 4x more bars, and twice as many cycles, the training process will take much longer than in the last workshop. As soon as it's finished, let's examine the resulting parameters. Use the
script editor to open the **Workshop6.par** file in the **Data** folder. This file contains the optimized parameters from the most recent WFO cycle. It could look like this:

EUR/USD:TRND +106 6.91 => 1.144 EUR/USD:CNTR 0.976 1.10 6.93 => 3.056 USD/JPY:TRND 386 6.93 => 3.089 USD/JPY:CNTR 1.73 1.09 5.75 => 1.899

As we can see, parameters for every asset/algo combination are optimized separately. Each line begins with the identifier for the component, consisting of asset and algorithm separated by a colon. Then follows the list of parameters. This allows Zorro to load the correct parameters at every **asset()** and **algo()** call. We can see that the **tradeTrend()** function uses two parameters and the **tradeCounterTrend()** function three, and their optimal values are slightly different for the **"EUR/USD"** and the **"USD/JPY"** assets. If a '+' sign appears in front of a parameter, its most robust value has been found at the start or end of its range. The last number behind the **"=>"** is not a parameter, but the result of the **objective** function of the final optimization run; it is for your information only and not used in the strategy.

The generated **OptimalF** factors can be found in the **Workshop6.fac** file:

EUR/USD:CNTR .079 1.61 155/95 43.3

EUR/USD:CNTR:L .022 1.12 62/48 3.9

EUR/USD:CNTR:S .105 2.05 93/47 39.4

EUR/USD:TRND .138 1.83 59/222 22.9

EUR/USD:TRND:L .021 1.10 25/113 1.2

EUR/USD:TRND:S .206 2.48 34/109 21.6

USD/JPY:CNTR .079 1.44 110/72 18.3

USD/JPY:CNTR:L .132 1.73 59/34 12.9

USD/JPY:CNTR:S .039 1.23 51/38 5.4

USD/JPY:TRND .119 1.73 64/250 15.6

USD/JPY:TRND:L .191 2.56 28/125 16.4

USD/JPY:TRND:S .000 0.93 36/125 -0.8

The factors are in the first column, separately for every component and separately for long (**":L"**) and short (**":S"**) trades (your own list might look different when you tested a different time period).

Finally, let's have a look at the performance of this portfolio strategy. Click [Test], then click [Result] (again, your equity curve can look different when testing a different time period).

You can see in the chart below that now two different algorithms trade simulaneously. The long green lines are from trend trading where positions are hold a relatively long time, the shorter lines are from counter-trend trading. The combined strategy does both at the same time. It generated about 40% annual CAGR in the walk forward test.

What happens when we reinvest our profits? Modify this line (changes in red):

** Margin = 0.5 * OptimalF * Capital * sqrt(1 + ProfitClosed/Capital); **

ProfitClosed is the sum of the profits of all closed trades of the portfolio component. **1 + ProfitClosed/Capital** is our capital growth factor. Let's examine the strange formula with an example. Assume we started with $10000 capital and the component made $20000 profit. The inner term inside the parentheses is then **1 + $20000/$10000 = 3**. This is the growth of the capital: we started with $10000 and have now $3000 on our account, so it grew by factor 3. The square root of 3 is ~1.7, and this is the factor used for reinvesting our profits.

Why the square root? Here's the background in short. In short term trading
with high leverage, the required capital is mainly determined by the expected
maximum drawdown. The maximum drawdown of any trade system increases over time.
A system tested over 10 years has worse drawdowns than the same system tested over only 5 years. When modeling drawdown depth mathematically with a diffusion model, the maximum drawdown is proportional to the square root of the trading time.
But the maximum drawdown is also proportional to the invested amount: When investing twice the volume you'll get twice the drawdown. Thus,
if you would reinvest a fixed percentage of your balance, as suggested in most
trading books, the maximum drawdown would grow by both effects proportionally to time to the power of **1.5**.
It will thus grow faster than your account balance. This means that at some point, a drawdown will inevitably exceed the balance, causing a margin call. There are several methods to overcome this
problem, and one of them 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%**.
That's the famous **square root rule** that applies to all high
leverage strategies. If you're interested, you
can find the formula explained in detail in the Black Book,
and also a comparison of several other reinvestment methods.

Since only the investment method has changed in the script above, the system needs not be retrained. Clicking [Test] is enough. Reinvesting the capital by the square root rule increases the CAGR to about 50%.

- The TimeFrame variable allows different time frames for algorithms or indicators within the same strategy.
- The
**loop**function can be used to go through a list of assets or algorithms. - The
**asset**and**algo**functions select a certain asset and set up an algorithm identifier. - The
**OptimalF**factors give the maximum capital allocation to a certain strategy component. - Reinvest only the square root of your profit.