# Detecting leading stocks using lag correlation

I am working on a project to find leading stocks in a stock market by using lag correlation.

Say I want to compare 2 stocks, X and Y, and I have the time series of stock prices. Assume that the time series are equally spaced and homogeneous and their log returns are stationary (in my case I have raw tick data so it's unevenly spaced and contains many gaps but I have managed to preprocess the data to make them equally spaced and run tests for stationarity). Finding the maximum lag correlation of 2 stocks is straightforward; I can use ccf() in R to find the maximum lag correlation of their log returns and the corresponding time lag.

Find_Max_CCF <- function(a,b)
{
d <- ccf(a, b, plot = FALSE)
cor = d$$acf[,,1] lag = d$$lag[,,1]
res = data.frame(cor,lag)
res_max = res[which.max(res\$cor),]
return(res_max)
}

> Find_Max_CCF(as.ts(X_logreturns), as.ts(Y_logreturns))
cor  lag
0.1459474 1200


Here, the strongest correlation occurs at time (t-1200), indicating that Y is the lagging indicator (X is the leading indicator).

Now, the problem is when I have more than 2 stocks. Say I have 3 stocks, X, Y and Z, and I want to find which stock is the leading trend of the other ones. I've been looking into comparing multiple time series using lag correlations and it seems to me that there is no literature or discussion on this topic. So I came up with an idea and here's how I think: I can find the maximum lag correlation of log returns and the corresponding time lag for each pair of stocks, take two pairings with 1 stock in common, and compare them to find which stock is the top leading stock, second leading stock and so on.

For better illustration, look at the example below.

> Find_Max_CCF(as.ts(X_logreturns), as.ts(Y_logreturns))
cor  lag
0.1459474 1200
> Find_Max_CCF(as.ts(X_logreturns), as.ts(Z_logreturns))
cor  lag
0.1495813 -480
> Find_Max_CCF(as.ts(Y_logreturns), as.ts(Z_logreturns))
cor  lag
0.1935647 -360


In this case, we have the following pairs of relation: X succeeds Y, X precedes Z, and Y precedes Z. From the first two relations, we can see that if Z succeeds X, and if X succeeds Y, then it must be that Z is leading first, followed second by X, and lastly Y. This confirms the third relation that Y precedes Z (or Z succeeds Y).

Is it correct of me to think this way? Will my idea work for comparing multiple time series? Is it too naive? Is there a better way to do this? Any help will be greatly appreciated!

you wont be finding much of a literature about this topic, the approach you have proposed is good and there should be no confusion, if you have a confusion you have the data experiment it plot it and see how it is going it the results are validated on multiple datasets use it.

I am suggesting a topic that can be of interest to you, time series similarity matching this is the topic where you can find more literature about. which uses techniques like Dynamic time warping (DTW) or Symbolic Aggregate Approximation (SAX). here are few links for your help.

https://www.r-bloggers.com/time-series-matching/

https://www.r-bloggers.com/time-series-matching-with-dynamic-time-warping/

https://roamanalytics.com/2016/11/28/shape-matching-with-time-series-data/