swaschke / pypnf

GNU General Public License v2.0
18 stars 15 forks source link

Add P&F patterns #13

Open fivethreeo opened 6 months ago

fivethreeo commented 6 months ago

https://school.stockcharts.com/doku.php?id=chart_analysis:pnf_charts:pnf_alerts

Would be great to add if possible

fivethreeo commented 6 months ago

I am givig triangles a try, got it working too


    def get_triangles(self):

        mtx = self.matrix.copy()
        # find high and low index for each column; sign indicates trend direction
        T = [np.repeat([np.arange(1, np.size(mtx, 0) + 1, 1)], np.size(mtx, 1), axis=0)][0].transpose() * mtx
        T = np.abs(T)

        highs = np.zeros(np.size(mtx, 1))
        lows = np.zeros(np.size(mtx, 1))

        heights = np.zeros(np.size(mtx, 1))

        for n in range(0, np.size(mtx, 1)):
            column = T[np.where(T[:, n] != 0), n]
            highs[n] = np.max(column)
            lows[n] = np.min(column)
            heights[n] = highs[n] - lows[n]

        triangles = {'column index': [],
                     'box index': [],
                     'width': [],
                     'height': [],
                     'trend': []
                     }

        if not self.breakouts:
            self.get_breakouts()
        for n in range(1, np.size(self.breakouts["trend"])):
            trend = self.breakouts["trend"][n]
            if self.breakouts["width"][n] == 3:
                i = self.breakouts["column index"][n] - 1
                height = heights[i] + 2
                high = highs[i] + 1
                i -= 1
                hits = 1
                while (height == heights[i] or height-1 == heights[i] or height+1 == heights[i]) \
                    and (highs[i] == high or highs[i] == high-1 or highs[i] == high + 1) and (i > 0):
                    print(n, i, height, high, heights[i], highs[i], hits)
                    height = heights[i] + 2
                    high = highs[i] + 1
                    hits += 1
                    i -= 1

                if hits > 3:
                    triangles['column index'].append(self.breakouts["column index"][n])
                    triangles['box index'].append(self.breakouts["box index"][n])
                    triangles['width'].append(hits)
                    triangles['height'].append(height)
                    triangles['trend'].append(trend)

        self.triangles = triangles
        print(triangles)
        return self.triangles
fivethreeo commented 6 months ago

A work in progress https://github.com/fivethreeo/pypnf/tree/patterns