Finding Stock Price Peaks with Reversal-Threshold Streaks
Summary
The document addresses how to identify and plot peaks in a stock price series. It notes that peak definitions vary and that a threshold parameter in a peak-finding function may be unclear. As an alternative, it describes a streak-based method: classify sustained upward and downward phases, allowing a phase to end when a counter-move reaches a chosen size, then mark each phase’s extreme as a peak or trough.
An example applies the method to Apple closing prices and plots the resulting phase endpoints. The output illustrates how the approach segments a price history into rises and declines, but the document does not specify a universal threshold or compare this rule with other peak definitions. Results therefore depend on the chosen reversal size and should be understood as a particular charting convention, not an objective signal on its own.
Key ideas
- Peak identification depends on how a peak is defined.
- A streak-based method segments prices into rising and falling phases using a counter-move threshold.
- The extreme point of each phase can serve as a peak or trough.
- The example plots phase endpoints in Apple price history, while leaving threshold selection to the user.
Tags
Full text
# R: Finding peaks on a stock price chart
# R: Finding peaks on a stock price chart
I would like to do is what I thought to be a simple task: find the locations of peaks for a certain stock, and mark those peaks on a chart.
I was surprised by a lack of appropriate examples on the internet. For example, description of findPeaks function from quantmod package does not even explain what the second argument (thresh) represent. Could somebody please explain?
So, this is what I managed to do:
```
library(quantmod)
APL <- getSymbols("AAPL", src = "yahoo", auto.assign = FALSE)
aapl.price <-AAPL$AAPL.Close
p <- findPeaks(aapl.price, 10)
plot(aapl.price, type = 'l')
points(aapl.price[p],index(aapl.price)[p], col = "red")
```
This comes up:
Obviously, there are different ways to define the peaks, but by any means the output here is not what one would want to see. Could somebody please provide any guidance?
Also, please let me know if this is best done using some other package
## Answer by Enrico Schumann (score 2)
https://quant.stackexchange.com/a/51656
As you mentioned, there are different ways to define peaks.
One method is implemented in function `streaks` in package PMwR. The function will look for phases of up or down movements that are not interrupted by countermoves of a specified size. The peaks and troughs are then the extreme points of those phases.
Here is an example:
```
library("quantmod")
library("zoo")
library("PMwR")
AAPL <- getSymbols("AAPL", src = "yahoo", auto.assign = FALSE)
aapl.price <- window(as.zoo(AAPL$AAPL.Close),
start = as.Date("2010-1-1"))
plot(aapl.price)
streaks <- streaks(aapl.price)
streaks
## start end state return
## 1 2010-01-04 2010-02-04 <NA> -0.1026120
## 2 2010-02-04 2012-09-19 up 2.6558188
## 3 2012-09-19 2013-04-19 down -0.4437687
## 4 2013-04-19 2015-02-23 up 1.3839397
## 5 2015-02-23 2016-05-12 down -0.3207519
## 6 2016-05-12 2018-10-03 up 1.5688512
## 7 2018-10-03 2019-01-03 down -0.3872969
## 8 2019-01-03 2020-02-12 up 1.3011464
## 9 2020-02-12 2020-03-13 down -0.1504585
abline(v = streaks$end)
```
(Disclosure: I am the maintainer of PMwR.)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.