0

I am trying to find all the local minima in an ArrayList. The basic idea is to find all the support levels of a stock.

A support level refers to the price level below which, historically, a stock has had difficulty falling. It is the level at which buyers tend to enter the stock.

I have attached a screenshot below, which shows all the support levels of the stock.

enter image description here

In the image, I have highlighted 3 support levels and these support points I got using the algorithm as shown below -

SEGMENT_SIZE = 4;
localMinSkipCount = 0;
noOfSkipSegments = 3;
for(i = 0; i < datapoints.size(); i = i + SEGMENT_SIZE) {
    int endIndex = i + SEGMENT_SIZE;
    if (endIndex > datapoints.size()) {
      endIndex = datapoints.size();
    }

    // Finds the min point in the segment
    double localMin = findLocalMin(i, endIndex);

    if (localMins.size() > 0) {
      lastLocalMin = localMins.get(localMins.size() - 1);
      if ( localMin >  lastLocalMin ) {
        if (localMinSkipCount > noOfSkipSegments) {
          localMins.add(localMin);
          localMinSkipCount = 0;
        }
        localMinSkipCount++;
      } else {
        // update the local min here
        localMins.set(localMins.size() - 1, localMin);
        localMinSkipCount = 0;
      }
    } else {
      localMins.add(localMin);
    }
}

Although this algorithm pretty much does the job, it is not always optimum. Would like to know if any of you have a better algorithm for finding the support levels of the stock or any suggestion to optimise this algorithm.

Rito
  • 3,092
  • 2
  • 27
  • 40
  • What do you mean by "not always optimum"? What exactly do you expect? – Henry Dec 29 '17 at 10:06
  • 1
    this probably belongs on http://codereview.stackexchange.com – Lino Dec 29 '17 at 10:07
  • Maybe https://stackoverflow.com/questions/8587047/support-resistance-algorithm-technical-analysis will help you – mallikarjun Dec 29 '17 at 10:07
  • @Henry Some examples on top of my mind are when the share is in an uptrend for a long period, it tries to find minima in that uptrend which is wrong. Also, when it is trading flat for a long period it is picking up multiple minimal, which I want to avoid. Like I am getting two minima at 300.95 and 301.89. Which is basically same only. Hope I was able to clear my requirement. – Rito Dec 29 '17 at 10:28
  • @Lino I wasn't aware of codereview; I will post my question there. – Rito Dec 29 '17 at 10:28

0 Answers0