TA-lib 异常:输入都是 NaN 错误?

问题描述

我正在尝试使用 zipline 运行回测,并使用 TA-lib 进行一些技术分析。我的数据集是巨大的(千兆比特)。因为我希望我的数据在 zipline 中工作,我有很多数据都是零作为所有年份的值,这样公司数据就可以阅读 zipline(因为 zipline 要求您拥有您的策略交易的整个持续时间的交易日历)。

错误信息:

@H_404_8@<?PHP header("Access-Control-Allow-Origin: *"); $site=$_GET['url']; $html = file_get_contents($site); //echo $html; $bytes=file_put_contents('markup.txt',$html); // Can use this if your default interpreter is Python 2.x. // Has some problem executing 'which python2'. So,absolute path is just simpler. //$python_path=exec("which python 2>&1 "); //$decision=exec("$python_path test.py $site 2>&1 "); $decision=exec("$C:/python36/python test.py $site 2>&1 "); echo $decision; ?>

我听说其他人在使用 TA-lib 时也遇到了这个错误,并且没有好的方法可以修复它。我怎么能解决这个问题?是否需要我为 MACD 创建自己的函数

令人惊讶的是,像 TA-lib 这样大的库无法处理超过特定大小的数据集。

解决方法

如果我理解正确的话。在计算指标时,需要一定的周期,例如,对于布林线,至少需要 20 个周期。所以任何小于 20 的都将是 NaN。因此,您需要检查这些值的列表。

import math
x = float('nan')
math.isnan(x)

return True or False
import numpy as np
values = [float('nan'),np.nan,55,"string",lambda x : x]
for value in values:
   print(f"{repr(value):<8} : {is_nan(value)}")

这里是 magdi 的设置,突出显示它是计算周期所必需的。小于此值的任何值都将是 NaN,当您尝试计算没有任何输入的内容时,这就是您收到此错误的原因。 Here's an example from the exchange.

def MACD(data,fastperiod,slowperiod,signalperiod):
    macd,macdsignal,macdhist = [],[],[]

    fast_ema = EMA(data,fastperiod)
    slow_ema = EMA(data,slowperiod)

    diff = []

    for k,fast in enumerate(fast_ema):
        if math.isnan(fast) or math.isnan(slow_ema[k]):
           macd.append(math.nan)
           macdsignal.append(math.nan)
        else:
          macd.append(fast-slow_ema[k])
          diff.append(macd[k])

    diff_ema = EMA(diff,signalperiod)
    macdsignal = macdsignal + diff_ema

    for k,ms in enumerate(macdsignal):
        if math.isnan(ms) or math.isnan(macd[k]):
           macdhist.append(math.nan)
        else:
           macdhist.append(macd[k] - macdsignal[k])

    return macd,macdhist
macd,macdhist = MACD(closes,12,26,9)

这个函数计算macd。 如果您传递 100 个值的列表,则 macd - 12 个值将是 nan,macdsignal - 26 将是 nan,macdhist - 9 将是 nan。