Finding all the local mins in an ArrayList

124 Views Asked by At

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.

0

There are 0 best solutions below