Skip to content
All library documents

Screening Stocks for Threshold Price Moves Within a Rolling Window

Article Quant Q&A · Author: ejb

Summary

The document describes a way to flag stocks whose cumulative price movement crosses a chosen threshold within a rolling time window, then display those signals alongside price series. The proposed process calculates cumulative returns, tests absolute changes over rolling windows of varying lengths up to the specified horizon, and marks a date when any tested window exceeds the threshold. This identifies both upward and downward moves; it is a threshold-move indicator rather than a volatility measure alone.

The response illustrates the approach with historical stock prices and a plotting step, but does not provide a method for ranking matches by the most recent qualifying swing, despite that being part of the question. It also does not address data quality, adjusted-price choices, or the practical details of scaling the screen across a large universe. A second answer emphasizes that volatility and net price movement are distinct, since different return paths can produce different combinations of cumulative change and variability.

Key ideas

  • Calculate cumulative returns and test price changes across rolling windows up to the chosen horizon.
  • Flag a date if any tested window crosses the absolute movement threshold.
  • The threshold indicator captures directional swings in either direction but does not by itself rank signals by recency.
  • Volatility alone does not measure whether a stock’s net price move exceeded a chosen threshold.

Tags

Full text
# How can I find stocks that have had a X% price swing within Y days, sorted by recency of said swing?


# How can I find stocks that have had a X% price swing within Y days, sorted by recency of said swing?












Let's say that I want to find stocks that have moved +-20% within a 10 day period.

`ABC` would match if at `t`, `ABC` is \$1 and at `t+8`, `ABC` is \$1.20.

`XYZ` would not match if at `t`, `XYZ` is \$1 and at `t+10`, `XYZ` is \$1.10, because it did not move 20% within 10 days.

I believe that I'm looking for some kind of volatility indicator that will also allow for a time range.

How can I also sort by how recently this indicator occurred?

Thanks for any help.

## Answer by Sergey Bushmanov (score 1)

https://quant.stackexchange.com/a/22742

As soon as you're comfortable with Python, let's do this exercise in three steps:

- Download data and calculate cumulative returns (or value of your position as if you invested $1 in each of the stocks)

- Define function that will capture stock movements in excess of predefined threshold, 10% in this case. This function is going to be the "indicator" you asked for.

- Plot "indicator" on top of the stock data.

Step 1.

```
# Imports  
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas_datareader import data
%matplotlib inline
plt.rcParams['figure.figsize']=(12,5)

# Definitions of global variables
N = 10
change = .1
stocks = ['GOOG','YHOO','AAPL','IBM','MSFT']

# Download of prices
price = pd.DataFrame()
for st in stocks:
    price[st] = data.DataReader(st, 'yahoo')['Adj Close']

# Calculation of cumulative returns
return_cum = (1+price.pct_change()).cumprod()
return_cum.plot();
```

Step 2.

Now, we need to define the indicator itself that will capture any movements, positive or negative, in excess of predefined `change`. The idea here is that I roll window `N` times, each time decreasing it by 1, and put at the right end of the window 1 if absolute cumulative change is more than change required. Finally, if for any window of changing length I have 1 on a certain day, the indicator returns SINGLE `1` for that day.

```
def change_within_NDays(x,N):
    ret = pd.DataFrame()
    for days in range(N+1):
        ret[days] = pd.rolling_apply(x, days, func=lambda x: abs((x[-1]/x[0]-1)) > change)
    return ret.any(1)

indicator = return_cum.apply(lambda x: change_within_NDays(x,N))
```

Step 3.

Finally, we can visualize the indicator by drawing it over stock prices:

```
_, ax = plt.subplots()
return_cum.plot(ax=ax)
indicator.plot(ax=ax, legend=0);
```

## Answer by Dr_Be (score 0)

https://quant.stackexchange.com/a/22727

You're not looking for volatility, at least not that measure alone. Imagine two stocks A and B, A is gaining a constant 2% every day whereas B is gaining 1% one day and -1% the other day. Then look at the return over the 10 days and the volatility of the two stocks.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.