Excel 365 BYCOL and by BYROW Functions and a Correlation Matrix

This post details how to extract data from a large array, such as a correlation matrix, to make your workflow more efficient.

First, as the LAMBDA function is used, a brief overview is provided.

Excel 365 offers the LAMBDA function, which was detailed here.

The LAMBDA function features are threefold:

  • Assign parameters.
  • Pass the parameters to a function.
  • Assign the function a "friendly name."

Two functions are available for use with the LAMBDA function: BYCOL and by BYROW. This post details examples of using the three functions and a downloadable Excel spreadsheet sample is available at the bottom of this post. The subject of the application of the three functions is an Excel correlation matrix.

Correlation analysis by technical traders is a popular analytical technique for identifying opportunities. Excel RTD formulas can pull the data correlation values directly from CQG IC and QTrader instead of having Excel do the analysis.

The RTD formula requires two symbols, a lookback period parameter, choice of markets' data (such as the "close,"), and the time interval (i.e. 5, 10, D):

=RTD("cqg.rtd",,"StudyData", "Correlation(Symbol 1,Symbol 2,Period:=10 ,InputChoice1:=Close,InputChoice2:=Close)", "Bar", "", "Close", Time Interval, "0", "all","", "","True","T")/100

In cell C4 in the sample spreadsheet the two symbols are in cells C3 and B4, the period is from cell C2 and the interval is from cell E2. This formula is copied and pasted to cells B4 to S21:

=RTD("cqg.rtd",,"StudyData", "Correlation("&C$3&","&$B4&",Period:="&$C$2&",InputChoice1:=Close,InputChoice2:=Close)", "Bar", "", "Close",$E$2, "0", "all","", "","True","T")/100


Generally, correlation is interesting when values are very positive, such as greater than 0.90 or very negatives, such as less than -0.90. The sample spreadsheet uses color conditioning:

  • Correlations greater than 0.90 are highlighted with green backgrounds.
  • Correlations less than -0.90 are highlighted with red backgrounds.

To add the color coding the steps are select the area for the color coded conditioning go to Home/Conditional Formatting/Hight Cell Rules/Greater Than cell $I$2 is selected as that has the threshold and select the colors.

Repeat that for less than, the threshold cell reference is $K$2. You can manage the feature by selecting the same area and going to Home/Conditional Formatting/Manage Rules.


However, the user has to peer at the spreadsheet and visually trace upwards to find one symbol and visually trace to the left to identify the second symbol. A better solution is to have Excel extract the group of symbols from the greater than 0.90 group and the less than -0.90 group. The LAMBDA function plus BYCOL and BYROW functions will do this.

This will be a two step process. First, in the sample spreadsheet cell U23 is referencing cell B4 for the symbol and copied down to cell U40.


Next, using the BYCOL and the LAMBDA functions Excel will determine the columns with greater than 0.90 correlations. The LAMBDA function is counting the number of times the cells C4:S4 is greater than 0.90 and the Filter function returns the symbol in cells $C$3:$S$3. This formula is entered into cell V23 and copied down to V40.


Next, based on the first step, then using the BYROW and the LAMBDA functions Excel will return the symbols with the rows with greater than 0.90 correlations. This formula is entered into cell B23 and is not copied down. The symbols will "Spill" down. Important note: Leave cells blank from cell B24 to Cell B40 otherwise you will see the "Spill error".


Next, now that Excel provides the symbols with correlations higher than 0.90 by the symbol columns, as shown in the image above, and Excel lists the symbols by rows, what would be useful is to have only both the symbols by rows and the symbols by columns. To that end, the IFS function and the INDIRECT function are key, i.e.:


Above the IFS function is an IF THEN function and will stop at the first IF that is True. The INDIRECT function pulls data from a row using Text to state the parameters of the row.

Pulling in data by the roll will deliver "0" if there is no symbol, so the TEXT function is used to only display text. The formula below is in cell C23 and copied down to cell C40. The IFERROR function is used to display a blank cell if there is not a symbol in column B.


This is the display reduced to just symbols with correlations greater than 0.90.


For the less than -0.90 group the symbols are referenced in Cell U43 to Cell U60 from cell B4 down to B21. Here the LAMBDA function is using Low referenced in cell $K$2.


Resulting in this display.


Then in cell B43 this function is entered (do not copy down).


And this function is used in cells C43 to C60.


This is the current result.


Using the combination of BYCOL, BYROW and the LAMBDA functions can extract data from a large array and reduce it to a more usable display. Again, for those functions that "SPILL" you need to leave empty cells so the functions can properly populate with data.

You can use your own symbols in cells C3 to S3 and Cells B4 to B 21.

Requirements: CQG Integrated Client or QTrader, and Excel 365 (locally installed, not in the Cloud) or more recent.



Trading and investment carry a high level of risk, and CQG, Inc. does not make any recommendations for buying or selling any financial instruments. We offer educational information on ways to use our sophisticated CQG trading tools, but it is up to our customers and other readers to make their own trading and investment decisions or to consult with a registered investment advisor. The opinions expressed here are solely those of the author and do not reflect the opinions of CQG, Inc. or its affiliates.