是什么导致代码中出现空序列?

2024-06-23 00:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个预先创建的代码时尚.py: https://github.com/dysonance/Trendy

部分代码计算Max1和Max2 | Min1和Min2

对于我选择的每一家公司,我都会给代码输入过去100天的收盘价。你知道吗

对于一些公司来说,代码可以像预期的那样工作,但是对于超过一半的公司来说,它给了我一个ValueError。你知道吗

min2 = min(x[(min1 + window):])

ValueError: min() arg is an empty sequence

所以在我看来,min1就是问题所在。它无法计算,因此留空。然后一个空序列被送入min2。你知道吗

有人能看一下,告诉我你认为是什么导致了这个问题吗?

下面是一个我用作输入的示例文件(它给出了所描述的错误)。你知道吗

https://docs.google.com/spreadsheets/d/1xZdSGE05SfyoL9D4akSH0KlV8s2cR1CJLqLlVEzq_4k/edit?usp=sharing

以下是代码的相关部分:

def gentrends(x, window=1/3.0, charts=True):
    """
    Returns a Pandas dataframe with support and resistance lines.
    :param x: One-dimensional data set
    :param window: How long the trendlines should be. If window < 1, then it
                   will be taken as a percentage of the size of the data
    :param charts: Boolean value saying whether to print chart to screen
    """

    import numpy as np
    import pandas as pd
    from pandas_datareader import data, wb

    x = np.array(x)

    if window < 1:
        window = int(window * len(x))

    max1 = np.where(x == max(x))[0][0]  # find the index of the abs max
    min1 = np.where(x == min(x))[0][0]  # find the index of the abs min

    # First the max
    if max1 + window > len(x):
        max2 = max(x[0:(max1 - window)])
    else:
        max2 = max(x[(max1 + window):])

    # Now the min
    if min1 - window < 0:
        min2 = min(x[(min1 + window):])
    else:
        min2 = min(x[0:(min1 - window)])

    # Now find the indices of the secondary extrema
    max2 = np.where(x == max2)[0][0]  # find the index of the 2nd max
    min2 = np.where(x == min2)[0][0]  # find the index of the 2nd min

Tags: ofthe代码indexnp公司findmin
1条回答
网友
1楼 · 发布于 2024-06-23 00:30:36

我把这个贴出来,以防有人碰到这个问题。你知道吗

我输入的数据是最近100天的收盘价。但是,如果增加数据点的数量,则所经历的错误要少得多。你知道吗

在alphavantageapi中,对每日时间序列使用“full”而不是“compact”。你知道吗

相关问题 更多 >