TA-Lib是一个Python库,封装了用C语言实现的金融交易技术分析的诸多常用指标。为了方便用户在DolphinDB中计算这些技术指标,我们使用DolphinDB脚本实现了TA-Lib中包含的指标函数,并封装在DolphinDB ta module (ta.dos)中。 使用ta模块需要DolphinDB Database Server 1.10.3 或以上版本。
1. 函数及参数的命名与用法规范与TA-Lib中所有函数名大写以及所有参数名小写的规范不同,ta模块中,函数名及参数名均采用驼峰式命名法。
例如,TA-Lib中DEMA函数的语法为DEMA(close, timeperiod=30)。在ta模块中相应的函数为dema(close, timePeriod)。TA-Lib中某些函数有可选参数。ta模块中,所有参数皆为必选。
为得到有意义的结果,ta模块中函数的参数timePeriod要求至少是2。
2. 使用范例
2.1 脚本中直接使用指标函数
对一个向量直接使用ta模块中的wma函数进行计算:
use ta
close = 7.2 6.97 7.08 6.74 6.49 5.9 6.26 5.9 5.35 5.63
x = wma(close, 5);
2.2 在SQL语句中分组使用
用户经常需要在数据表中对多组数据在每组内进行计算。在以下例子中,我们构造了一个包含2个股票的数据表:
close = 7.2 6.97 7.08 6.74 6.49 5.9 6.26 5.9 5.35 5.63 3.81 3.935 4.04 3.74 3.7 3.33 3.64 3.31 2.69 2.72
date = (2020.03.02 + 0..4 join 7..11).take(20)
symbol = take(`F,10) join take(`GPRO,10)
t = table(symbol, date, close)
对其中每只股票使用ta模块中的wma函数进行计算:
update t set wma = wma(close, 5) context by symbol
2.3 返回多个列的结果
某些函数会返回多个列的结果,例如函数bBands。
直接使用的例子:
close = 7.2 6.97 7.08 6.74 6.49 5.9 6.26 5.9 5.35 5.63
low, mid, high = bBands(close, 5, 2, 2, 2);
在SQL语句中使用的例子:
close = 7.2 6.97 7.08 6.74 6.49 5.9 6.26 5.9 5.35 5.63 3.81 3.935 4.04 3.74 3.7 3.33 3.64 3.31 2.69 2.72
date = (2020.03.02 + 0..4 join 7..11).take(20)
symbol = take(`F,10) join take(`GPRO,10)
t = table(symbol, date, close)
select *, bBands(close, 5, 2, 2, 2) as `high`mid`low from t context by symbol
symbol date close high mid low
------ ---------- ----- -------- -------- --------
F 2020.03.02 7.2
F 2020.03.03 6.97
F 2020.03.04 7.08
F 2020.03.05 6.74
F 2020.03.06 6.49 7.292691 6.786 6.279309
F 2020.03.09 5.9 7.294248 6.454 5.613752
F 2020.03.10 6.26 7.134406 6.328667 5.522927
F 2020.03.11 5.9 6.789441 6.130667 5.471892
F 2020.03.12 5.35 6.601667 5.828 5.054333
F 2020.03.13 5.63 6.319728 5.711333 5.102939
GPRO 2020.03.02 3.81
GPRO 2020.03.03 3.935
GPRO 2020.03.04 4.04
GPRO 2020.03.05 3.74
GPRO 2020.03.06 3.7 4.069365 3.817333 3.565302
GPRO 2020.03.09 3.33 4.133371 3.645667 3.157962
GPRO 2020.03.10 3.64 4.062941 3.609333 3.155726
GPRO 2020.03.11 3.31 3.854172 3.482667 3.111162
GPRO 2020.03.12 2.69 3.915172 3.198 2.480828
GPRO 2020.03.13 2.72 3.738386 2.993333 2.24828
3. 性能说明
ta模块中的函数与TA-Lib中对应函数相比,直接使用时的平均速度相似,但在分组计算时,ta模块中的函数性能远超TA-Lib中对应函数。本节的性能对比,我们以wma函数为例。
3.1 直接使用性能对比
在DolphinDB中:
use ta
close = 7.2 6.97 7.08 6.74 6.49 5.9 6.26 5.9 5.35 5.63
close = take(close, 1000000)
timer x = wma(close, 5);
对一个长度为1,000,000的向量直接使用ta模块中的wma函数,耗时为3毫秒。
与之对应的Python语句如下:
close = np.array([7.2,6.97,7.08,6.74,6.49,5.9,6.26,5.9,5.35,5.63,5.01,5.01,4.5,4.47,4.33])
close = np.tile(close,100000)
import time
start_time = time.time()
x = talib.WMA(close, 5)
print("--- %s seconds ---" % (time.time() - start_time))
TA-Lib中WMA函数耗时为11毫秒,为DolphinDB ta module中wma函数的3.7倍。
3.2 分组使用性能对比
在DolphinDB中,构造一个包含1000只股票,总长度为1,000,000的数据表:
n=1000000
close = rand(1.0, n)
date = take(2017.01.01 + 1..1000, n)
symbol = take(1..1000, n).sort!()
t = table(symbol, date, close)
timer update t set wma = wma(close, 5) context by symbol;
使用ta模块中的wma函数对每只股票进行计算,耗时为17毫秒。
与之对应的Python语句如下:
close = np.random.uniform(size=1000000)
symbol = np.sort(np.tile(np.arange(1,1001),1000))
date = np.tile(pd.date_range('2017-01-02', '2019-09-28'),1000)
df = pd.DataFrame(data={'symbol': symbol, 'date': date, 'close': close})
imp