# 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!

New contributor
Jessica is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

### Is it correct of me to think this way?

I see no technical problem, the way you are approaching the problem.

### Will my idea work for comparing multiple time series?

Well, if it would work for a minimum of 3 stocks, then it would work for more. There are APIs you can fetch data from and apply your equations to it:

https://iexcloud.io

### Is it too naive?

It certainly does not appear so.

### Is there a better way to do this?

I think there might be many ways to do so, once you define, if already not, what leading stocks mean (e.g., highest ROI, ROE, Top Gainer). For instance, you can check at tradingview, finviz or other practical trading or maybe investing platforms to see how do they define things in practice? https://www.tradingview.com/markets/stocks-usa/market-movers-gainers/

By the way, PHP has a full library http://php.net/manual/en/book.trader.php with lots and lots of technical details that you can easily integrate to your data pipelines. Basically, if you would install and implement it, it would save like a million years of your time and you can apply much more complex methods to your data.

Not sure, but I don't think R does that. Python is also really great!

Even though I'm no expert, what you are doing looks really well and I love it! 💙💙💙

New contributor
Emma is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• By leading stocks, I mean leading indicators. For example, an economic shock can have a negative effect on stock prices, but the effect of a change in prices of some stocks can happen several periods before a change in prices of other stocks and vice versa. My goal is to find out which stocks are leading, or "ahead", of others, by using lag correlation. – Jessica 2 days ago
• @Jessica @Jessica hi there, okay! first i thought you are looking for top gainers, just got it! they are also called market movers: tradingview.com/markets/stocks-usa/market-movers-large-cap would you like to just find leading stocks or are you looking to design a new algorithm/method to do so? 🤔 are you only considering share price or you are also looking at other parameters like market cap, average volume and so on? – Emma 2 days ago