cedricporter / funcat

Funcat 将同花顺、通达信、文华财经麦语言等的公式写法移植到了 Python 中。
http://funcat.org
Apache License 2.0
941 stars 421 forks source link

教程上的代码运行不起来 #32

Open zhangshoug opened 5 years ago

zhangshoug commented 5 years ago

https://github.com/cedricporter/funcat/blob/master/notebooks/funcat-tutorial.ipynb 中的代码运行到计算均线就出错误了。

如果是 bug 反馈,烦请在 issue 中描述以下问题:

1. funcat 的版本

0.3.2

2. Python 的版本

Python 3.6.5 |Anaconda

3. 是 Windows / Linux / MacOS or others?

linux mint 18.3 amd64

4. 您出现问题对应的源码 / 或者能复现问题的简易代码

$ python Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) [GCC 7.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.

from matplotlib import rcParams import matplotlib.pyplot as plt import numpy as np np.seterr(all='ignore') {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'} rcParams['figure.figsize'] = (14, 6) from funcat import * from funcat.data.tushare_backend import TushareDataBackend from funcat.data.rqalpha_data_backend import RQAlphaDataBackend backend = "rqalpha" set_data_backend(RQAlphaDataBackend("~/.rqalpha/bundle")) set_start_date("2015-01-01") S("000001.XSHG") # 设置当前关注股票 T("2016-06-01") # 设置当前观察日期 print(O, H, L, C, V) # 打印 Open High Low Close 2917.15 2929.08 2909.51 2913.51 18838642100.0 C / C[1] - 1 # 当天涨幅 -0.0010663027751299792 MA(C, 60) # 打印60日均线 Traceback (most recent call last): File "/opt/anaconda3/lib/python3.6/site-packages/talib/init.py", line 20, in wrapper for arg in chain(args, kwargs.values()) StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/opt/anaconda3/lib/python3.6/site-packages/funcat/func.py", line 31, in init series = self.func(series, arg) File "/opt/anaconda3/lib/python3.6/site-packages/talib/init.py", line 24, in wrapper return func(*args, **kwargs) File "talib/_func.pxi", line 7863, in talib._ta_lib.MA TypeError: only size-1 arrays can be converted to Python scalars

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/opt/anaconda3/lib/python3.6/site-packages/funcat/func.py", line 33, in init raise FormulaException(e) funcat.utils.FormulaException: only size-1 arrays can be converted to Python scalars

5. 您出现的错误堆栈日志信息

rascaler commented 5 years ago

说个解决方案,主要是因为 series = self.func(series, arg) 这一句,以这种方式引用talib.MA函数,将会自动添加一个MovingAverageSeries类参数,导致第一个参数不是是series,因此报错。你可以通过debug查看参数就能明白

源码

class OneArgumentSeries(NumericSeries):
    func = talib.MA

    def __init__(self, series, arg):
        if isinstance(series, NumericSeries):
            series = series.series

            try:
                series[series == np.inf] = np.nan
                series = self.func(series, arg)
            except Exception as e:
                raise FormulaException(e)
        super(OneArgumentSeries, self).__init__(series)
        self.extra_create_kwargs["arg"] = arg

修改后的代码

class BaseSeries(NumericSeries):

    def __init__(self, series, arg):
        if isinstance(series, NumericSeries):
            series = series.series

            try:
                series[series == np.inf] = np.nan
                series = eval(self.funcName)(series, arg)
            except Exception as e:
                raise FormulaException(e)
        super(BaseSeries, self).__init__(series)
        self.extra_create_kwargs["arg"] = arg

    @property
    def funcName(self):
        raise NotImplementedError
class MovingAverageSeries(BaseSeries):
    """http://www.tadoc.org/indicator/MA.htm"""
    @property
    def funcName(self):
        return 'talib.MA'
Z-Shuming commented 2 years ago

棒!解决了出现的同样问题。