{"id":854,"date":"2014-12-04T05:59:51","date_gmt":"2014-12-04T10:59:51","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=854"},"modified":"2014-12-04T15:25:56","modified_gmt":"2014-12-04T20:25:56","slug":"how-to-display-correlation-between-symbols","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/12\/04\/how-to-display-correlation-between-symbols\/","title":{"rendered":"How to display correlation between symbols"},"content":{"rendered":"

For the purpose of calculating the correlation between two data-arrays, there is a Correlation() function in AFL which can be used.<\/p>

In order to display a correlation chart, please select Analysis\u2013>Formula Editor<\/strong> menu and enter the following code:<\/p>SetChartOptions<\/span>( <\/span>0<\/span>, <\/span>chartShowArrows<\/span>|<\/span>chartShowDates<\/span>);
<\/span>Ticker <\/span>= <\/span>ParamStr<\/span>( <\/span>"Symbol"<\/span>, <\/span>Name<\/span>() );
<\/span>range <\/span>= <\/span>Param<\/span>( <\/span>"Periods"<\/span>, <\/span>252<\/span>, <\/span>2<\/span>, <\/span>500<\/span>, <\/span>1<\/span>);

<\/span>corr <\/span>= <\/span>Correlation<\/span>( <\/span>Close<\/span>, <\/span>Foreign<\/span>( <\/span>Ticker<\/span>, <\/span>"C"<\/span>), <\/span>range <\/span>);
<\/span>Plot<\/span>( <\/span>corr<\/span>, <\/span>"Correlation " <\/span>+ <\/span>Name<\/span>() + <\/span>"\/" <\/span>+ <\/span>ticker<\/span>,
      <\/span>ParamColor<\/span>( <\/span>"Color"<\/span>, <\/span>colorRed <\/span>), <\/span>ParamStyle<\/span>( <\/span>"Style"<\/span>, <\/span>styleLine <\/span>) );

<\/span>\/\/ check if different symbols are used
<\/span>if( <\/span>ticker <\/span>== <\/span>Name<\/span>() )
    <\/span>Title <\/span>= <\/span>"Please select different symbol from Parameter dialog"<\/span><\/code>

Now select Tools->Apply Indicator<\/strong>. Initially the code will pick the selected symbol\u2019s Close prices for both arrays, so we either need to change the selected ticker in the chart or the second symbol, which can be defined in Parameters<\/strong> dialog.<\/p>

We can also use Exploration feature to display a correlation matrix e.g. for the watchlist members. The below example shows the process for Watchlist 0 members.<\/p>

The formula to display correlation table looks as follows:
<\/span>\/\/ read the list of symbols from Watchlist 0
<\/span>symlist <\/span>= <\/span>CategoryGetSymbols<\/span>( <\/span>categoryWatchlist<\/span>, <\/span>0 <\/span>);

<\/span>\/\/ display only last bar from the Analysis range
<\/span>Filter <\/span>= <\/span>Status<\/span>( <\/span>"lastbarinrange" <\/span>);

<\/span>\/\/ iterate through symbols
<\/span>for ( <\/span>i <\/span>= <\/span>0<\/span>; ( <\/span>sym <\/span>= <\/span>StrExtract<\/span>( <\/span>symlist<\/span>, <\/span>i <\/span>) ) != <\/span>""<\/span>; <\/span>i<\/span>++ )
{
    <\/span>\/\/ calculate correlation over 252 bars
    <\/span>Corr <\/span>= <\/span>Correlation<\/span>( <\/span>C<\/span>, <\/span>Foreign<\/span>( <\/span>sym<\/span>, <\/span>"C" <\/span>), <\/span>252 <\/span>);

    <\/span>\/\/ set color dynamically based on correlation values
    \/\/ and display the output in exploration column
    <\/span>Clr <\/span>= <\/span>32 <\/span>+ <\/span>SelectedValue<\/span>( <\/span>Corr <\/span>) * <\/span>32<\/span>;
    <\/span>AddColumn<\/span>( <\/span>Corr<\/span>, <\/span>sym<\/span>, <\/span>1.2<\/span>,
               <\/span>ColorHSB<\/span>( <\/span>128 <\/span>+ <\/span>Clr<\/span>, <\/span>255<\/span>, <\/span>255 <\/span>),
               <\/span>ColorHSB<\/span>( <\/span>Clr<\/span>, <\/span>255<\/span>, <\/span>255 <\/span>) );
}

<\/span>SetSortColumns<\/span>( <\/span>1 <\/span>)<\/code>

To use the formula we need to do the following:<\/p>

  1. assign some symbols to watchlist 0
  2. select Analysis->Formula Editor<\/strong> menu
  3. in the AFL Editor enter<\/strong> the code listed above
  4. select Tools->Send to Analysis<\/strong> menu
  5. in the Analysis <\/strong>window, select Apply to: Filter<\/strong> (in Include<\/strong> tab hit Clear<\/strong> and pick watchlist 0)

    \"Filter<\/p>

  6. select Range: 1 Recent bar<\/strong> (in case of longer range, last bar of the range will be used for output)
  7. press Explore <\/strong>button<\/ol>

    Here is a sample output table:<\/p>

    \"Correlation<\/p>

    Be careful and try not to put 10000 items in the watch list because it would need to create a table with 10K columns. Windows has some limits on pixel width of the list view and it would truncate display when the display width (scrollable area inside list) exceeds 32767 pixels. That makes it practical only to display matrices of not more than about 1000-2000 columns.<\/p>","protected":false},"excerpt":{"rendered":"

    For the purpose of calculating the correlation between two data-arrays, there is a Correlation() function in AFL which can be used.In order to display a correlation chart, please select Analysis\u2013>Formula Editor menu and enter the following code:SetChartOptions( 0, chartShowArrows|chartShowDates);Ticker = ParamStr( "Symbol", Name() );range = Param( "Periods", 252, 2, 500, 1);corr = Correlation( Close, Foreign( Ticker, "C"), range );Plot( corr, "Correlation " + Name() + "\/" + ticker,      ParamColor( "Color", colorRed ), ParamStyle( "Style", styleLine ) );\/\/ check if different symbols are usedif( ticker == Name() )    Title = "Please select different symbol from Parameter dialog"Now select Tools->Apply Indicator. Initially the code will pick the selected symbol\u2019s Close prices for both arrays, so we […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[53,14,38,28],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/854"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":3,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"predecessor-version":[{"id":881,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/854\/revisions\/881"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}