## Calculation of support/resistance zones

*July 23, 2019*

support/resistance zones are quite essential when trading breakouts. Support is a line below the price on the charts that the price bounced back from a couple of times. Resistance is the same, but above the price. When a human looks at a chart, itâ€™s pretty easy for them to see these points on the chart:

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:

- gather all open, high, low close prices in an array and sort them.
- define a parameter, called epsilon that will be the sensitivity for the zone.
- start from the first price and group all prices less than epsilon away from this in a list.
- 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:

- maintain a list of groups, initialized with a single group containing the first price.
- 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.

```
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);
}
}
```

I finally had the chance to plot the results of this, so I updated the post:

The red zones are the resistance which is all tuples with price above the last close price and the green zones are the support zones.

*Tags:
#fintech
#programming
*