Does anyone know of an algorithm for finding all the peaks and valleys in a time series?
Quote from naifwonder:
To determine the highs and lows of a trend, you must first determine how much a move constitutes a trend classification. If the market moves 1 pt, do you consider it a trend? The lower this value, the more highs and lows you will get.
Take the following scenario:
The market starts at 1340.00, moves up to 1342.00, moves down to 1340.75, then up to 1345.00, then back down to 1341.00
(the above movements are hypothetical and realistically, the market would never move so smoothly without tick fluctuations. I have simplified it for the sake of demonstration.)
In the above scenario, if you consider a 1 pt move a trend, you would have the following high and low values:
Low: 1340.00
High: 1342.00
Low: 1340.75
High: 1345.00
Low: 1341.00
If you were to consider a trend as being at least a 3 pt move, then you would have the following:
Low: 1340.00
High: 1345.00
Low: 1341.00
What you want to consider a trend is your choice. The code below should get you started. It is written in C# for use in the NinjaTrader platform. Most programming languages have a similar syntax so it should carry over to whatever platform you are using fairly easily. I have read yours posts in the past Frost so I know that you are familiar with coding. If you want any clarification though, feel free to ask.
_________________________
// Detecting highs and lows of a trend
// Variables
double trend_high = 0; // Stores the value of the high (peak) of the trend
double trend_low = 99999; // Stores the value for the low (valley) of the trend
double trend_min_move = 2.00; // The minimum the market has to move to be considered a trend
// Monitor price movement to determine trend highs and lows
if(High[0] > trend_high
&& (High[0] - trend_low) >= trend_min_move)
{
// A new trend high has been detected.
trend_high = High[0];
}
if(Low[0] < trend_low
&& trend_low <= (trend_high - trend_min_move)
{
// A new trend low has been detected
trend_low = Low[0];
}
_____________________
In the above code, I used 2 pts to define the minimum the market has to move to be considered a trend. This number was randomly chosen. Substitute it with what you find works. Also, note that the trend_high and trend_low variables each have a default value. I have done this so the code has a value to compare against to initially determine trend highs and lows. Furthermore, do not try to combine the two IF conditions in the code with an ELSE IF statement. Doing so will prevent the code from properly determining highs and lows if a trend high and low both occur in the same bar.
btw.. I just wrote the above code right now and have never actually tested it. It should work though.
Quote from dtrader98:
Seems interesting. I suppose you could also filter it to only plot the last point (blue or yellow), between transitions between a yellow and blue marker;
thus marking local min and max's within the minimum spread (resolution) you identified.
Thanks for sharing. I'll try to play with it more if I get a chance.