Home > OS >  Checking for Sequence in List: Why does this function returns "False", when the Sequence I
Checking for Sequence in List: Why does this function returns "False", when the Sequence I

Time:01-03

In the following Code, I try to identify Elliott Wave Patterns in a list of values. In fact, the first parts of the values list, match the conditions that are required to be identified as Elliott Wave Pattern and therefore the function should return the list of values that are identical to the identified pattern. Instead, the function returns "False". Why is that? I think I just got some logic within the order of loops wrong, but where?

Here is the Code:

values = [100, 105, 103, 107, 110, 115, 119, 117, 117, 116, 115, 110, 115, 113, 116, 120, 125, 123, 127, 130, 135, 140, 135, 137, 134, 130, 125, 121, 126, 132, 130, 135, 137, 143, 145, 140, 133, 135, 136, 130, 127, 130, 134, 129, 125, 120, 117, 120, 118, 120, 117, 115, 110, 100, 90, 80, 95, 100, 105, 110, 115,110, 108, 115, 120, 125]

threshold = 4  # minimum difference required for a local maxima or minima

# create an empty dictionary to store the local maximas and minimas
extrema = {}

# check the first value
if values[0] > values[1]:
    # add the first value as a local maxima
    extrema[0] = values[0]
elif values[0] < values[1]:
    # add the first value as a local minima
    extrema[0] = values[0]


# check the remaining values
for i in range(1, len(values)-1):
    # check for local maxima
    if values[i] > values[i-1] and values[i] > values[i 1] and values[i] > values[i-2] and values[i] > values[i 2]:
        if values[i] - min(values[i-2:i 3]) > threshold:
            # add the local maxima to the dictionary
            extrema[i] = values[i]
    # check for local minima
    elif values[i] < values[i-1] and values[i] < values[i 1] and values[i] < values[i-2] and values[i] < values[i 2]:
        if max(values[i-2:i 3]) - values[i] > threshold:
            # add the local minima to the dictionary
            extrema[i] = values[i]

            # check the last value
if values[-1] > values[-2]:
    # add the last value as a local maxima
    extrema[len(values)-1] = values[-1]
elif values[-1] < values[-2]:
    # add the last value as a local minima
    extrema[len(values)-1] = values[-1]

# update the dictionary with the last value
extrema.update({len(values)-1: values[-1]})

print(extrema)

def check_elliott_wave(extrema):
    # create a list to store the wave patterns
    wave_patterns = []
    intervals_pattern = []

    extrema_list = sorted(extrema.items())
    print(extrema_list)
    print(intervals_pattern)

    
    # check if the sequence starts with a local minima
    if extrema_list[0][1] < extrema_list[1][1]:
        # iterate over the dictionary in pairs
        for i in range(0, len(extrema_list)-1, 1):
            # get the current and next local extrema
            current = extrema_list[i][1]
            next_ = extrema_list[i 1][1]
        
            # check the trend of the current wave
            if current < next_:
                # uptrend
                trend = "uptrend"
            else:
                # downtrend
                trend = "downtrend"

            # add the trend to the wave patterns
            wave_patterns.append(trend)
            print(wave_patterns)

        # check if the wave patterns follow the required sequence
        if ["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"] in wave_patterns:
            intervals_pattern.append([extrema_list[i][1], extrema_list[i 1][1]], [extrema_list[i 2][1]], [extrema_list[i 3][1]], [extrema_list[i 4][1]], [extrema_list[i 5][1]])
            return True
        else:
            return False
    else:
        return False


    # check if the second minima is lower than the first
    if extrema_list[2][1] < extrema_list[0][1]:
        return False

    # check if the third minima is lower than the first maxima
    if extrema_list[4][1] < extrema_list[1][1]:
        return False

    # check if the length of the third wave is the longest
    if extrema_list[3][1] - extrema_list[2][1] > extrema_list[1][1] - extrema_list[0][1] and extrema_list[3][1] - extrema_list[2][1] > extrema_list[5][1] - extrema_list[4][1]:
        return True

    return intervals_pattern

# test the function
print(check_elliott_wave(extrema))

The wave pattern being checked for is a series of uptrends and downtrends that repeat in a specific sequence. The sequence is: uptrend, downtrend, uptrend, downtrend, uptrend.

The function check_elliott_wave() is trying to determine whether this specific wave pattern is present in the list values.

To do this, it first determines the local extrema (local maxima and minima) in the list values. It does this by comparing each value in the list with its immediate neighbors and storing the local extrema in a dictionary called extrema.

It then sorts the dictionary extrema by its keys (which are the indices of the values in the list values) and stores the result in a new list called extrema_list.

Finally, it iterates over extrema_list in pairs and checks whether each pair of local extrema corresponds to an uptrend or a downtrend. It stores the trend (either "uptrend" or "downtrend") in a list called wave_patterns.

After iterating over all the pairs in extrema_list, it checks whether the list wave_patterns contains the sequence ["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"]. If it does, the function returns True. Otherwise, it returns False.

Now, in the given list of values, the first part from 100 to 145 should be the pattern I am looking for: 100, 105, 103, 107, 110, 115, 119, 117, 117, 116, 115, 110, 115, 113, 116, 120, 125, 123, 127, 130, 135, 140, 135, 137, 134, 130, 125, 121, 126, 132, 130, 135, 137, 143, 145

Also when I print out the wave_patterns list, then the following appears: ['uptrend', 'downtrend', 'uptrend', 'downtrend', 'uptrend', 'downtrend', 'uptrend', 'downtrend', 'uptrend', 'downtrend', 'downtrend', 'uptrend', 'downtrend', 'uptrend']

So there is the sequence: 'uptrend', 'downtrend', 'uptrend', 'downtrend', 'uptrend' within that list. Then, only the first sequence within that list should match the additionall conditions:

The sequence has to start with a minimum. The second minimum cannot be lower than the first. The third minimum cannot be lower than the first maximum. The difference between the second maximum and the second minimum has to be the longest distance (the third wave has to be the longest wave).

Why is the whole function returning "False"?

What am I missing here? Thanks!

CodePudding user response:

Since your trend is a string, your wave_patterns is a list of strings. However, you are checking if it contains a specific list of strings within it (i.e. if one of values in wave_patterns is a list consisting of "uptrend", "downtrend", "uptrend", "downtrend", "uptrend").

As you say you want to check if this sequence is present in the wave_patterns list, not if there's a list ["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"] in wave_patterns.

What you could do instead, is convert wave_patterns to a string and check if the required sequence is in that string. Something like:

if ''.join(["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"]) in ''.join(wave_patterns):

Should return True

Edit:

The way you currently check if this pattern is in your wave_patterns would return True, if wave_patterns was the following: ["uptrend", "downtrend", ["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"], "downtrend"]. Notice that one of the values is the list you're looking for.

  • Related