Let's say I have time series of stock prices for many stocks. What's the best way to sort the stocks based on which have been going up/stayed the same relative to others? Can this be done with a weighted average, putting more weight on the most recent numbers, to account for trends?


migrated from stats.stackexchange.com Oct 18 '11 at 17:51

This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.

  • 5
    $\begingroup$ A question for the quant.SE probably. The whole problem is defining what is valuable. The result will depend on this definition. $\endgroup$ – mpiktas Mar 1 '11 at 6:50
  • $\begingroup$ Well, I'm trying to define the problem. I started with a plain average, but I want to weigh more recent data higher. So thus weighted average. $\endgroup$ – user2664 Mar 2 '11 at 3:34
  • $\begingroup$ Since this user has long since left stack exchange, we are left wondering whether he really was looking to measure value, or merely momentum (as his explanation of value implies). I have edited the question assuming the user intended to ask how to compute momentum. $\endgroup$ – Tal Fishman Oct 18 '11 at 18:33

This entire approach hinges on how you define "value." Once you've defined value, you can define a metric for "stability" or "risk." A working hypothesis would be that stock that have been stably valuable in the past would continue to be valuable in the future. Of course, this is a hypothesis.

Let's say (for sake of an example, this is not financial advice) you define value as "mean of log returns" and stability as "standard deviation of log returns." You could then sort your stocks by these metrics and pick ones with a high value and a low risk.

If you want to get fancy, you can use another metric of risk, such as drawdown. You can also do a rolling analysis or use bootstrap re-sampling to distributions around your "value" and "stability" metrics.

Here's some code in R that illustrates my example:

#Load Data
rm(list = ls(all = TRUE)) #CLEAR WORKSPACE
myStocks <- c('AAPL','MSFT','GOOG','F')
Data <- na.omit(cbind(Cl(AAPL),Cl(MSFT),Cl(GOOG),Cl(F)))
names(Data) <- myStocks

#Define value and risk
returns <- function(x) {diff(log(x))}
value <- function(x) {mean(returns(x))}
risk <- function(x) {sd(returns(x))}

#Estimate value and risk
StockScreen <- data.frame(  risk=apply(as.matrix(Data),2,risk),

#Estimate value and risk, different risk measure
risk <- function(x) {maxDrawdown(returns(x))}
StockScreen <- data.frame(  risk=apply(as.matrix(Data),2,risk),

#Boostrap value and risk measure

bootstrap <- function(x) {
    reps <- as.data.frame(meboot(as.ts(x), reps=100)$ensemble)
    valueDistribution <- apply(reps,2,value)
    riskDistribution <- apply(reps,2,risk)

    valueMean <- mean(valueDistribution)
    valueSD <- sd(valueDistribution)
    riskMean <- mean(riskDistribution)
    riskSD <- sd(riskDistribution)


bootstrapScreen <- ldply(as.list(Data),bootstrap,.progress = "text")
row.names(bootstrapScreen) <- bootstrapScreen$.id
bootstrapScreen$.id <- NULL

you might want to rank stocks on the basis of standard dev of a forecast divided by the forecast. In this way the "tighter" the value the more predictable the stock.

  • $\begingroup$ what do you mean by forecast? can you please elaborate via an example, thanks. $\endgroup$ – user2664 Mar 23 '11 at 0:12
  • $\begingroup$ Doesn't "predictable" have to do with the relationship between a forecast and reality rather than with the coefficient of variation? In the context of the question, a highly variable stock (high beta) that nevertheless trends upward rapidly (high alpha) would be considered "valuable" but a low-beta ("predictable") stock that trends downwards consistently would be considered the opposite of "valuable". $\endgroup$ – whuber Mar 29 '11 at 22:07
  • $\begingroup$ @whuber, yes that's something that I'm trying to create a model for... $\endgroup$ – user2664 Mar 30 '11 at 2:23

It sounds as if you would be interested in computing Relative Strength


You could either measure it against a benchmark index such as the Dow 30, or compute your own index from your 50 stocks and measure each individual stock against the index.

-Ralph Winters


What you are describing is momentum investing. It is typically done in two steps:

  1. Compute a momentum statistic from past prices/returns.
  2. Compare momentum statistics across all equities in your universe.

Step 1 is typically done using a moving average of past returns (it is wrong to use prices because splits and dividends will skew the results). This can be done using a simple moving average, or using exponentially weighted moving averages. In either case, your results will depend strongly on the window/half-life of the moving average. For equity momentum, most studies use momentum in the last 6-12 months excluding the most recent month or so.

Step 2 may be done either using an ad hoc sorting rule (Jegadeesh and Titman (1993) use top/bottom deciles) or with the aid of a risk model.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy