the shaded are is a support/resistance zone, as the price tries multiple times to go past that point and succeeds with that long green candle. How to get the computer to recognize these points it an interesting exercise in programming. The logic is this:

1. gather all open, high, low close prices in an array and sort them.

2. define a parameter, called epsilon that will be the sensitivity for the zone.

3. start from the first price and group all prices less than epsilon away from this in a list.

4. remove the added prices from the original array and continue group the remaining prices.

in the end you will have a list of lists. The more items in the list, the stronger the support/resistance around that area.

The worst case running time for the above algorithm is n^2 if all the prices are more than epsilon away from each other, which is not very good. So we need to improve on that.

We can build the groups as we are iterating the original array like this:

1. maintain a list of groups, initialized with a single group containing the first price.

2. as we are iterating over the array, if the current element is less than epsilon away from the head

of the group we add it to that group. Otherwise we create a new group and add it into that group.

Code: Select all

```
Collections.sort(data);
SuperList<SuperList<Double>> groups = new SuperList<>();
groups.add(new SuperList<>());
groups.get(0).add(data.get(0));
for (int i = 1; i < data.size(); i++) {
SuperList<Double> group = groups.getLast();
Double x = data.get(i);
if (Math.abs(x - group.get(0)) < epsilon) {
group.add(x);
} else {
SuperList<Double> newList = new SuperList<>();
newList.add(x);
groups.add(newList);
}
}
```