繁体   English   中英

检查列表中的序列:当序列存在时,为什么这个 function 返回“False”?

[英]Checking for Sequence in List: Why does this function returns "False", when the Sequence IS there?

在下面的代码中,我尝试在值列表中识别艾略特波浪模式。 事实上,值列表的第一部分与被识别为艾略特波浪模式所需的条件相匹配,因此 function 应该返回与识别模式相同的值列表。 相反,function 返回“False”。 这是为什么? 我想我只是在循环顺序中弄错了一些逻辑,但是在哪里?

这是代码:

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))

正在检查的波浪模式是一系列以特定顺序重复的上升趋势和下降趋势。 顺序是:上升趋势,下降趋势,上升趋势,下降趋势,上升趋势。

function check_elliott_wave() 正在尝试确定列表值中是否存在此特定波形。

为此,它首先确定列表值中的局部极值(局部最大值和最小值)。 它通过将列表中的每个值与其直接邻居进行比较并将局部极值存储在称为极值的字典中来实现这一点。

然后它通过键(它们是列表值中值的索引)对字典极值进行排序,并将结果存储在一个名为 extrema_list 的新列表中。

最后,它成对迭代 extrema_list 并检查每对局部极值是否对应于上升趋势或下降趋势。 它将趋势(“上升趋势”或“下降趋势”)存储在名为 wave_patterns 的列表中。

在遍历 extrema_list 中的所有对之后,它检查列表 wave_patterns 是否包含序列 ["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"]。 如果是,则 function 返回 True。 否则,它返回 False。

现在,在给定的值列表中,从 100 到 145 的第一部分应该是我正在寻找的模式: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

此外,当我打印出 wave_patterns 列表时,会出现以下内容:['uptrend', 'downtrend', 'uptrend', 'downtrend', 'uptrend', 'downtrend', 'uptrend', 'downtrend', 'uptrend' , '下降趋势', '下降趋势', '上升趋势', '下降趋势', '上升趋势']

因此在该列表中有以下顺序:'uptrend','downtrend','uptrend','downtrend','uptrend'。 然后,只有该列表中的第一个序列应该匹配附加条件:

该序列必须从最小值开始。 第二个最小值不能低于第一个。 第三个最小值不能低于第一个最大值。 第二大和第二小之间的差异必须是最长的距离(第三波必须是最长的波)。

为什么整个 function 返回“False”?

我在这里错过了什么? 谢谢!

由于您的trend是一个字符串,您的wave_patterns是一个字符串列表。 但是,您正在检查它是否包含其中的特定字符串列表(即wave_patterns中的值之一是否是由"uptrend", "downtrend", "uptrend", "downtrend", "uptrend" downtrend"、"uptrend" 组成的列表)。

正如您所说,您要检查此序列是否存在于wave_patterns列表中,而不是 wave_patterns 中是否有列表["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"] wave_patterns "] 。

您可以做的是将 wave_patterns 转换为字符串并检查所需的序列是否在该字符串中。 就像是:

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

应该返回True

编辑:

如果wave_patterns是以下内容,您当前检查此模式是否在wave_patterns中的方式将返回True["uptrend", "downtrend", ["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"], "downtrend"] uptrend", "downtrend", "uptrend", "downtrend", "uptrend ["uptrend", "downtrend", ["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"], "downtrend"] 请注意,其中一个值是您要查找的列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM