Efficient calculation of support and resistance zones

stuff about computer science and programming
Post Reply
User avatar
dendiz
Site Admin
Posts: 114
Joined: Wed Oct 10, 2018 3:48 am

Efficient calculation of support and resistance zones

Post by dendiz » Wed Oct 10, 2018 9:30 pm

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:

1-641XGx7dP1tgwJ4X.png
1-641XGx7dP1tgwJ4X.png (8.02 KiB) Viewed 69 times
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);
            }
        }

User avatar
dendiz
Site Admin
Posts: 114
Joined: Wed Oct 10, 2018 3:48 am

Re: Efficient calculation of support and resistance zones

Post by dendiz » Sat Nov 24, 2018 2:50 am

I finally had the chance to plot the results of this, so I updated the post:
chart.png
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.

Post Reply