Nick -
This is something that the FastTrack group has done for many years in a language called "Trade". Trade is actually a matrix processing language where the rows are tickers and the columns are bars. Very powerful for bulk operations on watchlists, but somewhat primitive in other areas. For example, it has no looping, so it made everyone get inventive in coming up with array based solutions.
So let's take the mixing of the daily ROC's of tickers. FIRST RECOGNIZE THAT THIS IS EQUIVALENT TO A DAILY RE-BALANCE. This is fine for the majority of portfolio modeling applications. There are some caveats for spreads though, that are too much to go into now.
This is a powerful technique, though, for modeling portfolios. I'll eventually do an article on AmibrokerU and show how to re-balance on other periods, or on any signal.
But for now, lets say that you want to mix two tickers 60/40 and scale it to a start value of 1000.
Here's the traditional approach to that -
rocmix = Nz( ROC( data1, 1 ) ) * 0.60 + Nz( ROC( data2, 1 ) ) * 0.40;
dailyfact = 1 + ROCmix / 100;
res = 0;
res[ 0 ]= 1000;
for ( i = 1; i < BarCount; i++ )
res[ i ] = res [ i - 1 ] * dailyfact[ i ];
Now, this works fine, but can be slow. There is a better way. Remember from Algebra that addition in the log domain converts to multiplication in the linear domain. So, we can replace the above with array logic -
rocmix = Nz( ROC( data1, 1 ) ) * 0.60 + Nz( ROC( data2, 1 ) ) * 0.40;
dailyfact = 1 + ROCs / 100;
res2 = exp( Cum( log( dailyfact ) ) ) * 1000;
(There is even a faster was to do this with another AB function, but that is probably too fine a point.)
Lastly, as always, the devil is in the details. Just to mention a one fairly major issue - Mixing data that has different start dates requires a little more work to get a weighted mix where the sum of
the weights can vary over time.
Hope this helps.
-- BruceR
--- In
amibroker@xxxxxxxxx ps.com, Nick de Peyster <nickdepeyster@ ...> wrote:
>
> I searched the documentation but couldn't find an answer on this.
>
> Let's say I have ten stocks and I want to create a composite index of how those stocks are performing. The documentation on AddToComposite shows how to combine closing prices into a composite, but this is a bad solution: a high-priced stock will dominate the performance of the index.
>
> A better calculation would be to calculate the daily return of each stock in the index, average the individual returns and record the daily return values. Then compound them out into a cumulative time series
return ... has any one tried this?
>
> It would seem pretty easy to use ROC(Close,1) with AddToComposite to get the weighted average prices. What's missing is compounding them out daily into a cumulative return curve.
>
>
>
>
>