量化分析库Talib在win10下的安装使用记录
下载与安装
Talib不能直接用pip进行安装,需要先下载支持库。下载地址
地址,
在下边的下载中找到TA_Lib-0.4.19-cp39-cp39-win_amd64.whl
然后下载它,将其放置到
\windows\system32\
中,然后用pip install TA_Lib-0.4.19-cp39-cp39-win_amd64.whl
安装即可。
需要注意的是,一定要弄清楚版本号,否则安装会出错。文件名中,cp后边的数字39表示需要3.9版本的python,amd64表示需要64位的windows。
尝试使用
配置好VSCode,尝试以下代码
import tushare as ts
import pandas as pd
import numpy as np
import talib
from numpy.core.numeric import NaN
class TalibTest:
def __init__(self):
return
def getStockInfoFromTushare(self):
startDate = '20190101' # start query date
endDate = '20201231' # end query date
tsCode = self.GetTsCode() # Get query ts_code
ts.set_token('79cfeef7606afc45b9676ddcf9937471802ae79224210f48c6a28539')
pro = ts.pro_api()
df = pro.daily(ts_code = tsCode,start_date = startDate,end_date = endDate)
if(df is not None):
print("Get data from Tushare, over !")
# get a new dataframe of MA3 and MA7
df['MA5'] = NaN
df['MA5'] = talib.SMA(df['close'],5)
print(df)
return
def GetTsCode(self):
return '000001.SZ'
if __name__ == '__main__':
print('main function start')
test = TalibTest()
test.getStockInfoFromTushare()
输出
Get data from Tushare, over !
ts_code trade_date open high low close pre_close change pct_chg vol amount MA5
0 000001.SZ 20201231 19.21 19.58 19.02 19.34 19.20 0.14 0.7292 924503.43 1781736.285 NaN
1 000001.SZ 20201230 19.00 19.20 18.72 19.20 19.17 0.03 0.1565 978497.78 1854082.812 NaN
2 000001.SZ 20201229 18.87 19.30 18.70 19.17 18.85 0.32 1.6976 963092.23 1837947.238 NaN
3 000001.SZ 20201228 18.02 18.86 17.96 18.85 18.04 0.81 4.4900 1270337.06 2352947.321 NaN
4 000001.SZ 20201225 18.26 18.26 17.80 18.04 18.26 -0.22 -1.2048 577077.33 1038128.197 18.920
.. ... ... ... ... ... ... ... ... ... ... ... ...
482 000001.SZ 20190108 9.73 9.74 9.62 9.66 9.74 -0.08 -0.8214 402388.11 389247.795 10.002
483 000001.SZ 20190107 9.84 9.85 9.63 9.74 9.75 -0.01 -0.1026 865687.66 841166.430 9.928
484 000001.SZ 20190104 9.24 9.82 9.22 9.75 9.28 0.47 5.0647 1481159.06 1422149.888 9.838
485 000001.SZ 20190103 9.18 9.33 9.15 9.28 9.19 0.09 0.9793 415537.95 384457.707 9.674
486 000001.SZ 20190102 9.39 9.42 9.16 9.19 9.38 -0.19 -2.0256 539386.32 498695.109 9.524
[487 rows x 12 columns]
一切OK!