根据列表中的字符串名称调用函数

4 投票
2 回答
1743 浏览
提问于 2025-04-28 02:21
Terms: 
talib: Technical Analysis Library (stock market indicators, charts etc)
CDL: Candle or Candlestick

简短版本:我想根据字符串'some_function'来运行my_lib.some_function()

在quantopian.com上,我想循环调用所有以CDL开头的60个talib函数,比如talib.CDL2CROWS(),这样可以简化代码。首先把函数名作为字符串提取出来,然后根据字符串匹配来运行这些函数。

这些CDL函数的输入都是一样的,都是一段时间内的开盘价、最高价、最低价和收盘价的列表,这里为了简单起见只用一个长度为1的列表来测试。

import talib, re
import numpy as np

# Make a list of talib's function names that start with 'CDL'
cdls = re.findall('(CDL\w*)', ' '.join(dir(talib)))
# cdls[:3], the first three like ['CDL2CROWS', 'CDL3BLACKCROWS', 'CDL3INSIDE']

for cdl in cdls:
    codeobj = compile(cdl + '(np.array([3]),np.array([4]),np.array([5]),np.array([6]))', 'talib', 'exec')
    exec(codeobj)
    break
# Output:  NameError: name 'CDL2CROWS' is not defined

第二次尝试:

import talib, re
import numpy as np

cdls = re.findall('(CDL\w*)', ' '.join(dir(talib)))

for cdl in cdls:
    codeobj = compile('talib.' + cdl + '(np.array([3]),np.array([4]),np.array([5]),np.array([6]))', '', 'exec')
    exec(codeobj)
    break
# Output:  AssertionError: open is not double

我在网上没有找到那个错误。

相关链接,我在那边问的问题: https://www.quantopian.com/posts/talib-indicators (111次浏览,还没有回复)

对于对蜡烛图感兴趣的人: http://thepatternsite.com/TwoCrows.html


更新

在Anzel的帮助下,这个方法有效,可能是列表中的浮动数值是关键。

import talib, re
import numpy as np
cdls = re.findall('(CDL\w*)', ' '.join(dir(talib)))
# O, H, L, C = Open, High, Low, Close
O = [ 167.07, 170.8, 178.9, 184.48, 179.1401, 183.56, 186.7, 187.52, 189.0, 193.96 ]
H = [ 167.45, 180.47, 185.83, 185.48, 184.96, 186.3, 189.68, 191.28, 194.5, 194.23 ]
L = [ 164.2, 169.08, 178.56, 177.11, 177.65, 180.5, 185.611, 186.43, 188.0, 188.37 ]
C = [ 166.26, 177.8701, 183.4, 181.039, 182.43, 185.3, 188.61, 190.86, 193.39, 192.99 ]
for cdl in cdls: # the string that becomes the function name
    toExec = getattr(talib, cdl)
    out    = toExec(np.array(O), np.array(H), np.array(L), np.array(C))
    print str(out) + ' ' + cdl

关于如何给你转换成字符串的函数添加参数的选择:

toExec = getattr(talib, cdl)(args)
toExec()

或者

toExec = getattr(talib, cdl)
toExec(args)
暂无标签

2 个回答

6

一个更简单的方法是使用抽象库。

import talib

# All the CDL functions are under the Pattern Recognition group
for cdl in talib.get_function_groups()['Pattern Recognition']:
    # get the function object
    cdl_func = talib.abstract.Function(cdl)

    # you can use the info property to get the name of the pattern
    print('Checking', cdl_func.info['display_name'], 'pattern')

    # run the function as usual
    cdl_func(np.array(O), np.array(H), np.array(L), np.array(C))
3

如果你想根据字符串'some_function'来运行my_lib.some_function(),可以使用getattr,方法如下:

some_function = 'your_string'
toExec = getattr(my_lib, some_function)

# to call the function
toExec()

# an example using math
>>> some_function = 'sin'
>>> toExec = getattr(math, some_function)

>>> toExec
<function math.sin>
>>> toExec(90)
0.8939966636005579

更新你的代码以便运行

for cdl in cdls:
    toExec = getattr(talib, cdl)

# turns out you need to pass narray as the params
toExec(np.narray(yourlist),np.narray(yourlist),np.narray(yourlist),np.narray(yourlist))

我还建议你检查一下yourlist,因为它现在是一个一维的数组,而你需要的是多维数组。

撰写回答