提问人:BenjaminDiLorenzo 提问时间:1/2/2023 更新时间:1/2/2023 访问量:43
检查列表中的序列:当序列存在时,为什么此函数返回“False”?
Checking for Sequence in List: Why does this function returns "False", when the Sequence IS there?
问:
在下面的代码中,我尝试在值列表中识别艾略特波浪形态。事实上,值列表的第一部分与需要标识为 Elliott 波浪形态的条件匹配,因此该函数应返回与标识的形态相同的值列表。 相反,该函数返回“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))
正在检查的波浪形态是一系列以特定顺序重复的上升趋势和下降趋势。顺序是:上升趋势、下降趋势、上升趋势、下降趋势、上升趋势。
函数 check_elliott_wave() 试图确定列表值中是否存在此特定波形。
为此,它首先确定列表值中的局部极值(局部最大值和最小值)。它通过将列表中的每个值与其直接邻居进行比较并将局部极值存储在名为 extrema 的字典中来实现此目的。
然后,它按字典极值(即列表值中值的索引)对字典极值进行排序,并将结果存储在名为 extrema_list 的新列表中。
最后,它成对地遍历extrema_list,并检查每对局部极值是对应于上升趋势还是下降趋势。它将趋势(“上升趋势”或“下降趋势”)存储在名为wave_patterns的列表中。
在遍历extrema_list中的所有货币对后,它会检查列表wave_patterns是否包含序列 [“上升趋势”、“下降趋势”、“上升趋势”、“下降趋势”、“上升趋势”]。如果是这样,则该函数返回 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列表时,会出现以下内容: ['上升趋势', '下降趋势', '上升趋势', '下降趋势', '上升趋势', '下降趋势', '上升趋势', '下降趋势', '上升趋势', '下降趋势', '下降趋势', '上升趋势', '上升趋势']
所以有序列:“上升趋势”、“下降趋势”、“上升趋势”、“下降趋势”、“上升趋势” 在该列表中。 然后,只有该列表中的第一个序列应与 additionall 条件匹配:
序列必须从最小值开始。 第二个最小值不能低于第一个最小值。 第三个最小值不能低于第一个最大值。 第二个最大值和第二个最小值之间的差值必须是最长的距离(第三个波必须是最长的波)。
为什么整个函数返回“False”?
我在这里错过了什么? 谢谢!
答:
由于 ur 是一个字符串,因此 ur 是一个字符串列表。但是,您正在检查它是否包含其中的特定字符串列表(即,如果中的值之一是由 组成的列表)。trend
wave_patterns
wave_patterns
"uptrend", "downtrend", "uptrend", "downtrend", "uptrend"
正如您所说,您要检查列表中是否存在此序列,而不是 中是否有列表。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
True
wave_patterns
["uptrend", "downtrend", ["uptrend", "downtrend", "uptrend", "downtrend", "uptrend"], "downtrend"]
评论
in list
上一个:如何在 Python 中处理迭代
下一个:使用基于日期的聚合数据运行循环
评论