Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zhangshoug opened this issue Dec 20, 2018 · 2 comments
Open

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

zhangshoug opened this issue Dec 20, 2018 · 2 comments

Comments

@zhangshoug
Copy link

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
Copy link

rascaler commented Aug 19, 2019

说个解决方案,主要是因为 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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants