Ton,
Pleasure.
For all clarity, I'm using matrices mostly
from a portfolio (risk)
management pov, with var-covar as starting point.
Iow, I hardly look
at correlations in isolation since they don't tell
anything about
causality (regressions, for example, are better suited for
that.) Re
convergence of global correlations, there are all kinds of
economic/fundamental explanations for that, e.g. compression in
multiples.
Re randomness, one gets indeed quickly into the broader
debate of
normal distribution, law of large numbers, fractals, and black
swans.
Clearly one's time frame is relevant, e.g. lower frequency returns
have higher autocorrelations. One's choice of asset is important,
e.g.
volatility is clustering, as Engel (GARCH) has shown. Etc, etc.
To a
certain extent, this is indeed "academic", since in the final
analysis (as
Grinold & Kahn argued), unless you're an indexer:
active
management/trading = forecasting = assuming non-randomness
(with = you can
read "implies").
Finally, as you may have noticed with this
matrix-application, AB
slows down once the number of symbols becomes
large, say > 100. I've
created similar functions for Explore and CBT by
using VarSet to
calculate each "cell-array". In general, AB slows
down a lot once you
start calculations on a cross-sectional basis. This is
why I've asked
Thomasz to think about creating native matrix-afl
functions. Vote for
it in the feedback centre.
PS
--- In amibroker@xxxxxxxxxps.com,
"Ton Sieverding"
<ton.sieverding@...> wrote:
>
>
Really great stuff and thanks. Did you walk thru the iShares
country ETF's
with the Bar Replay Tool already ? No better way to
show how the world is
becoming more and more correlated ... BTW did
you ever try to create an
AFL showing the degree of randomness for a
stock ? My problem here is the
logical part of the story. How to
calculate the degree of randomness. I
want to be able to separate
random from 'non' random time series and got
stuck in the scientific
background ...
>
> Regards and thanks
again, Ton.
>
>
> ----- Original Message -----
>
From: vlanschot
> To: amibroker@xxxxxxxxxps.com
> Sent: Monday, May 28, 2007 1:46 PM
> Subject: [amibroker] Corr
Matrix via Gfx functions
>
>
> Thought I'd share the
following code.
>
> PS
>
> //Simple application of
the new Gfx functions, basically
calculating
> //the correlation
matrix of any WL of choice. You can easily
adjust
> //this code to
move to Var-Covar matrix, etc.
> // PS 28/05/07
>
>
GfxSetOverlayMode(2); // Show Only Matrix
>
> function
NrSecs(ListType,ListNr)
> {
> global NrPosPrices;
>
> if(ListType==1) ExtrList = CategoryGetSymbols
>
(categoryGroup,ListNr);
> else if(ListType==2)ExtrList =
CategoryGetSymbols
> (categoryWatchlist,ListNr);
> else
if(ListType==3)ExtrList = CategoryGetSymbols
>
(categoryIndustry,ListNr);
>
> NrSecurs=0;
>
NrPosPrices=0;
>
> for( n=0; (Ticker=StrExtract( ExtrList,
n))!= ""; n++)
> {
> NrSecurs=NrSecurs+1;
>
SetForeign(Ticker);
> Prijs=C;
>
RestorePriceArrays();
>
NrPosPrices=NrPosPrices+IIf(Prijs!=Null AND
>
Prijs>0,1,0);
> }
> return NrSecurs;
> }
>
> function PrintTxtInCell( Str, row, Col, CellWidth, CellHeight,
> HorzAdj, VertAdj, Formaat )
> {
> GfxDrawText( Str, Col
* CellWidth + HorzAdj, row * CellHeight +
> VertAdj, (Col + 1 ) *
CellWidth, (row + 1 ) * CellHeight,
Formaat);
> }
>
>
function PrintInCell( val, row, Col, CellWidth, CellHeight,
HorzAdj,
> VertAdj )
> {
> GfxDrawText( NumToStr(val), Col *
CellWidth + HorzAdj, row *
> CellHeight + VertAdj, (Col + 1 ) *
CellWidth, (row + 1 ) *
> CellHeight, 1|32|4);
> }
>
> //CH = Param("CellH",20,10,100,5);
> //CW =
Param("CellW",100,10,300,5);
> HA=0;
> VA=0;
>
> String = "" ;
> for ( x = 0 ; x < 200 ; x++ )
> {
> WList = CategoryGetName( categoryWatchlist, x );
> if (WList !=
"" )
> {
> String = String + WList +"," ;
> // Creates a
list of WatchLists, NOT symbols !!!
> }
> }
> WatchList =
ParamList ( "Watch List", String );
>
>
WLnr=CategoryFind(WatchList,categoryWatchlist);
>
WLSymbols = CategoryGetSymbols(categoryWatchlist,WLnr);
>
> NrSymbs = NrSecs(2,WLNr);
>
> CW=
Status("pxwidth")/(NrSymbs+1);// In order to allow
Rowheadings
> CH =
Status("pxheight")/(NrSymbs+1);//Colheadings
>
>
//GfxSetTextAlign(6 | 24);
>
> RetLB =
Param("RetLB",1,1,100);
> CorrLB =
Param("CorrLB",30,5,256);
>
> Start =
BarCount-RetLB;
>
> ActBar =
Min(BarCount-1,LastValue(SelectedValue(BarIndex())));
>
ActD = LastValue(SelectedValue(DateTime()));
>
>
GfxSelectFont( "MS Serif", CH/4,800);
> PrintTxtInCell( "CorrM
@:\n"+NumToStr(ActD,formatDateTime), 0,
0,CW,
> CH, HA,
VA, 1);
>
> for ( x = 0 ; (Ticker = StrExtract ( WLSymbols, x
))!="" ; x++ )
> {
> SetForeign(Ticker);
> RetTick =
ROC(C,RetLB);
> FN = FullName();
>
RestorePriceArrays();
>
> GfxSelectFont( "MS Serif",
CH/4,800);
> PrintTxtInCell( Ticker, 0, x+1 ,CW, CH, HA, VA,1|32|4
);
>
> GfxSelectPen( colorBlue );
>
> for( i = 0;
(Ticker2 = StrExtract ( WLSymbols, i))!="" ; i++ )
> {
>
SetForeign(Ticker2);
> RetTick2 = ROC(C,RetLB);
> FN2 =
FullName();
> RestorePriceArrays();
>
> if (Ticker2 ==
Ticker) Corr=1;
> else Corr = Correlation(RetTick, RetTick2,
CorrLB);
>
> GfxSelectFont( "MS Serif", CH/4,800);
>
PrintTxtInCell( Ticker2, i+1, 0, CW, CH, HA,
> VA,1|32|4);
>
GfxSelectFont( "MS Serif", CH/4);
> Kleur = IIf(Corr[ActBar]>=0,
colorGreen,colorRed);
> GfxSetTextColor(Kleur);
>
PrintInCell( Corr[ ActBar ], i+1, x+1, CW, CH, HA,
> VA);
>
//PrintInCell( Corr[ ActBar ] , i+1, x+1);
>
GfxSetTextColor(colorBlack);
>
> GfxMoveTo( 0, (i+1) *
CH);
> GfxLineTo( (x+2) * CW, (i+1) * CH );// Columns
> }
>
> GfxMoveTo( 0, (x+1)*CH); // Move to end of last
column
> //GfxLineTo( 6 * CW, i * CH );
> }
>
> for(
Col = 1; Col < NrSymbs+2; Col++ )
> {
> GfxMoveTo( Col * CW,
0);
> GfxLineTo( Col * CW, (NrSymbs+1) * CH );
> }
>
>
> Title="";
>