Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:41:16+00:00 2026-06-11T16:41:16+00:00

this is an update to the previous question that I had about locating peaks

  • 0

this is an update to the previous question that I had about locating peaks and troughs. The previous question was this:

peaks and troughs in MATLAB (but with corresponding definition of a peak and trough)

This time around, I did the suggested answer, but I think there is still something wrong with the final algorithm. Can you please tell me what I did wrong in my code? Thanks.

function [vectpeak, vecttrough]=peaktroughmodified(x,cutoff)

% This function is a modified version of the algorithm used to identify
% peaks and troughs in a series of prices. This will be used to identify
% the head and shoulders algorithm. The function gives you two vectors:
% PEAKS - an indicator vector that identifies the peaks in the function,
% and TROUGHS - an indicator vector that identifies the troughs of the
% function. The input is the vector of exchange rate series, and the cutoff
% used for refining possible peaks and troughs.

% Finding all possible peaks and troughs of our vector.
[posspeak,possploc]=findpeaks(x);
[posstrough,posstloc]=findpeaks(-x);
posspeak=posspeak';
posstrough=posstrough';

% Initialize vector of peaks and troughs.
numobs=length(x);
prelimpeaks=zeros(numobs,1); 
prelimtroughs=zeros(numobs,1);
numpeaks=numel(possploc);
numtroughs=numel(posstloc);

% Indicator for possible peaks and troughs.
for i=1:numobs
    for j=1:numpeaks
        if i==possploc(j);
            prelimpeaks(i)=1;
        end
    end
end

for i=1:numobs
    for j=1:numtroughs
       if i==posstloc(j);
            prelimtroughs(i)=1;
       end
    end
end

% Vector that gives location.
location=1:1:numobs;
location=location';

% From the list of possible peaks and troughs, find the peaks and troughs
% that fit Chang and Osler [1999] definition.
% "A peak is a local minimum at least x percent higher than the preceding
% trough, and a trough is a local minimum at least x percent lower than the
% preceding peak." [Chang and Osler, p.640]

% cutoffs
peakcutoff=1.0+cutoff; % cutoff for peaks
troughcutoff=1.0-cutoff; % cutoff for troughs


% First peak and first trough are initialized as previous peaks/troughs.

prevpeakloc=possploc(1);
prevtroughloc=posstloc(1);

% Initialize vectors of final peaks and troughs.
vectpeak=zeros(numobs,1);
vecttrough=zeros(numobs,1);

% We first check whether we start looking for peaks and troughs.
for i=1:numobs
    if prelimpeaks(i)==1;
       if i>prevtroughloc;
           ratio=x(i)/x(prevtroughloc);
           if ratio>peakcutoff;
               vectpeak(i)=1;
               prevpeakloc=location(i);
           else vectpeak(i)=0;
           end
       end
    elseif prelimtroughs(i)==1;
        if i>prevpeakloc;
            ratio=x(i)/x(prevpeakloc);
            if ratio<troughcutoff;
                vecttrough(i)=1;
                prevtroughloc=location(i);
            else vecttrough(i)=0;
            end
        end
    else
        vectpeak(i)=0;
        vecttrough(i)=0;
    end
end        
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:41:18+00:00Added an answer on June 11, 2026 at 4:41 pm

    I just ran it, and it seems to work if you make this change:

    peakcutoff= 1/cutoff; % cutoff for peaks
    troughcutoff= cutoff; % cutoff for troughs
    

    I tested it with the following code, with a cutoff of 0.1 (peaks must be 10 times larger than troughs), and it looks reasonable

    x = randn(1,100).^2;
    [vectpeak,vecttrough] = peaktroughmodified(x,0.1);
    peaks = find(vectpeak);
    troughs = find(vecttrough);
    plot(1:100,x,peaks,x(peaks),'o',troughs,x(troughs),'o')
    

    I strongly urge you to read up on vectorization in matlab. There are many wasted lines in your program, and it makes it difficult to read and will also make it very slow with big datasets. For instance, prelimpeaks and prelimtroughs can be completely defined without loops, in a single line for each:

    prelimpeaks(possploc) = 1;
    prelimtroughs(posstloc) = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up to a previous question that I had before about
In my previous question on this portal, I had asked about some insight about
NOTE: This is the simple version of my previous question that SHOULD have been
Surprisingly I was only able to find one previous question on SO about this
I had asked in my previous question for a package that lets me draw
This question is related to my previous question but you dont need to read
I inadvertently posted revisions to this previous post when I meant to update subsequent
I know that in standard SQL you can do this: update top (100) table1
I have an if statement that needs to look like this: UPDATE $(input#textbox).keypress(function(e){ key==e.which;
I had a previous question can jquery ajax call external webservice? and some good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.