0

I am developing a stock analysis program in Python

One of the fundamental things I need to is to recognise swings from price feed (open, high, low, close data)

Price data is fractal by nature - smaller structures are found within larger structures.

enter image description here

In my case I am looking for small swings within large swings. I.e. minor swings within major swings. The above example chart depicts my goal.

A few definitions to get out of the way. Each swing is made of two legs / parts - impulse leg and reaction leg

Impulse leg will be in the direction of flow of the the market

Reaction leg is against the direction of the impulse

Both impulse and reaction legs can be either up or down depending on the flow of the market Below models illustrate this definition. If there is no direction in the price, its known to be a ranging market

Swings in Up market

Swings in Down market

The next important definition is the understanding of highs and low. A new high confirms a new low while a new low confirms a new high This is illustrated by the below model

Confirming highs and lows

The below is how I have approached the matter in Python

import numpy as np
from scipy.signal import argrelextrema


def get_pivots(price: np.ndarray):
    maxima = argrelextrema(price, np.greater)
    minima = argrelextrema(price, np.less)
    return np.concatenate((price[maxima], price[minima]))

For simplicity I will be passing a flattened 1d numpy array into the above function. argrelextrema helps be identify where pivots are. I.e. identfies where the price turns.

I am wondering how I could find the nesting of pivots to form minor and major swings.

I am looking to produce a list that loosely resembles this structure.

[major swing
  [minor swing1],
  [minor swing2],
  [minor swing3
    [micro swing1], 
    [micro swing2]
  ] 
]

Sample data I have created is

    data = [5,6,7,8,9,10,11,12,13,14,16,17,18,19,20, # AB(Major) impulse
        19,18,17,16,15,14,13,12,11,10,9,8, # BC(Major) reaction
            9,10,11,12,13, 14,15, # C0C1 (Minor) impulse
            14,13,12,11,10, # C1C2 (Minor) reaction
            11, 12, 13, 14, 15, 16, 17, 18, # C2C3 (Minor) impulse
            17, 16, 15, 15, 14, 13, 12, # C3C4 (Minor) reaction
            13, 14, 15, 16, 17, 18, 19, 20, 21, # C4C5 (Minor) impulse
            20, 19, 18, 17, 16, 15, 14, # C5C6 (Minor) reaction
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 # CD (Major) impulse 
        ]

I believe some kind of recursive implementation is likely to help. I am not entirely familiar with implementation using this paradigm

neo-technoker
  • 369
  • 2
  • 8
  • 26

2 Answers2

1

Identifying major and minor trends in stock data is something, that I also struggled with long time, even if it looks like an easy job to do in the first place.

What finally worked for me is polynominal fitting! With the degree of the fitting polynomial parameter you can play around to cover more longer or shorter trends. From there you can use the turning points of the poly to get start and end of the trends.

There is one downside of this method, as it only works for backtesting. If I remember correctly, the fitting does not work well towards the end of data, which would be needed for "live" capture.

slapslash
  • 54
  • 5
  • Thanks for this.. I have been looking into this . DId you find a way to dynamically use degress based on the level of nesting in the swing structure? Happen to have nay code samples? – neo-technoker May 08 '23 at 13:51
  • 1
    no, sadly, I don't have numbers ready, but should be easy to try, especially, when you plot the raw data and the corresponding polyfit. – slapslash May 08 '23 at 13:58
0

In my eyes, the first step is to specify a concrete definition of major/minor swing. The definition should be such precise that it can be checked by a deterministic algorithm. Once you have that in hand, it helps you a lot to implement the algorithm in code.

For example, as you have mentioned them in your post, what are pivots precisely? argrelextrema causes difficulties in 'noisy' data as there are plenty of relative extrema. To relate to your sketch, you have not marked every single relative extrema but only local extrema which are such within a certain region. And that is what you have to state precisely before coding.

  • My apologies. I have tried to add some definitions and tried to clarify with pictures. I tend to see price flow as charts and struggle to specify things mathematically. I hope this clarifies the question in some ways – neo-technoker Apr 19 '23 at 11:17