https://github.com/twopirllc/pandas-ta

Pandas TA 是一个基于Pandas模块开发的,具有上百个技术指标和常用指标的开源模块。它包括但不限于能够绘制62种蜡烛形态(晨星、乌云、十字星、孕线等等)、130个技术指标,如移动平均线、macd、hma、布林带、obv、aron、squeeze等等各种指标。

下面就来讲一下这个量化投资神器的安装和使用方法,如果对你有帮助,记得点个赞和在看支持一下哦。

1.准备

开始之前,你要确保Python和pip已经成功安装在电脑上,如果没有,请访问这篇文章:超详细Python安装指南 进行安装。

(可选1) 如果你用Python的目的是数据分析,可以直接安装Anaconda:Python数据分析与挖掘好帮手—Anaconda,它内置了Python和pip.

(可选2) 此外,推荐大家用VSCode编辑器来编写小型Python项目:Python 编程的最好搭档—VSCode 详细指南

Windows环境下打开Cmd(开始—运行—CMD),苹果系统环境下请打开Terminal(command+空格输入Terminal),输入命令安装依赖:

pip install pandas_ta

此外,如果你想使用上全部指标,你需要安装TA-Lib

pip install Ta-Lib

安装TA-Lib的时候可能会遇到没有VC++14.0的报错,这时候我们需要手动安装,在 https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib 中下载你对应的Python版本的 TA-Lib whl文件。

下载完成后执行以下命令:

# 公众号:二七阿尔量化
pip install D:\path\TA_lib‑0.4.24‑cp38‑cp38‑win_amd64.whl

就能成功手动安装 Ta-lib

2.Pandas TA 基本使用

为了方便介绍使用方法,我下载了沪深300的分钟级数据,如果你需要本文的全部代码及数据,请在二七阿尔量化公众号后台回复:pandas_ta 下载。

首先看看我们的数据类型:

# 公众号:二七阿尔量化
import pandas as pd
import pandas_ta as ta

sh300data = pd.read_csv("sh300_1min.csv")

print(sh300data)

#        Unnamed: 0                  day      open      high       low     close     volume
# 0               0  2022-03-07 10:47:00  4406.223  4406.352  4405.662  4405.922   54345400
# 1               1  2022-03-07 10:48:00  4406.172  4406.175  4403.834  4403.918   70803100
# 2               2  2022-03-07 10:49:00  4403.333  4403.333  4402.235  4402.340   49632500
# 3               3  2022-03-07 10:50:00  4402.330  4402.519  4401.838  4402.519   48159200

默认情况下,pandas ta 取  open 、high、low、close、volumeadj_close 列作为指标参数,本文的列名正好对应的上,无需再去特别命名。

先试试收益率计算:

sh300data = sh300data.set_index("day")
print(sh300data.ta.log_return(cumulative=True, append=True))
# 2022-03-07 10:47:00    0.000000
# 2022-03-07 10:48:00   -0.000455
# 2022-03-07 10:49:00   -0.000813
# 2022-03-07 10:50:00   -0.000773
# 2022-03-07 10:51:00   -0.000826

验证一下结果:

>>> (- 4405.922 + 4403.918) / 4405.922
-0.00045484236897518966
>>> (- 4403.918 + 4402.34) / 4403.918
-0.00035831729836920665
>>> -0.00045484236897518966 + -0.00035831729836920665
-0.0008131596673443963

结果对得上,注意我们传递了cumulative=True参数,因此每次都会将前面的收益率累加。

试一下常用指标 sma:

sma10 = sh300data.ta.sma(length=10)
print(sma10)
# day
# 2022-03-07 10:47:00          NaN
# 2022-03-07 10:48:00          NaN
# 2022-03-07 10:49:00          NaN
# 2022-03-07 10:50:00          NaN
# 2022-03-07 10:51:00          NaN
#                          ...
# 2022-07-08 14:55:00    4429.3936
# 2022-07-08 14:56:00    4428.9421
# 2022-07-08 14:57:00    4428.5996
# 2022-07-08 14:58:00    4428.3280
# 2022-07-08 15:00:00    4428.1877
# Name: SMA_10, Length: 20000, dtype: float64

支持以下全部技术指标:aberration, above, above_value, accbands, ad, adosc, adx, alma, amat, ao, aobv, apo, aroon, atr, bbands, below, below_value, bias, bop, brar, cci, cdl_pattern, cdl_z, cfo, cg, chop, cksp, cmf, cmo, coppock, cross, cross_value, cti, decay, decreasing, dema, dm, donchian, dpo, ebsw, efi, ema, entropy, eom, er, eri, fisher, fwma, ha, hilo, hl2, hlc3, hma, hwc, hwma, ichimoku, increasing, inertia, jma, kama, kc, kdj, kst, kurtosis, kvo, linreg, log_return, long_run, macd, mad, massi, mcgd, median, mfi, midpoint, midprice, mom, natr, nvi, obv, ohlc4, pdist, percent_return, pgo, ppo, psar, psl, pvi, pvo, pvol, pvr, pvt, pwma, qqe, qstick, quantile, rma, roc, rsi, rsx, rvgi, rvi, short_run, sinwma, skew, slope, sma, smi, squeeze, squeeze_pro, ssf, stc, stdev, stoch, stochrsi, supertrend, swma, t3, td_seq, tema, thermo, tos_stdevall, trima, trix, true_range, tsi, tsignals, ttm_trend, ui, uo, variance, vhf, vidya, vortex, vp, vwap, vwma, wcp, willr, wma, xsignals, zscore

试一下十字星(Doji Candlestick)形态:

doji = sh300data.ta.cdl_pattern(name="doji")
print(doji)
#                      CDL_DOJI_10_0.1
# day
# 2022-03-07 10:47:00              0.0
# 2022-03-07 10:48:00              0.0
# 2022-03-07 10:49:00              0.0
# 2022-03-07 10:50:00              0.0
# 2022-03-07 10:51:00              0.0
# ...                              ...
# 2022-07-08 14:55:00              0.0
# 2022-07-08 14:56:00            100.0
# 2022-07-08 14:57:00              0.0
# 2022-07-08 14:58:00              0.0
# 2022-07-08 15:00:00              0.0

这里的值如果出现 100 ,就是出现DOJI形态。支持以下全部蜡烛形态:

2crows, 3blackcrows, 3inside, 3linestrike, 3outside, 3starsinsouth, 3whitesoldiers, abandonedbaby, advanceblock, belthold, breakaway, closingmarubozu, concealbabyswall, counterattack, darkcloudcover, doji, dojistar, dragonflydoji, engulfing, eveningdojistar, eveningstar, gapsidesidewhite, gravestonedoji, hammer, hangingman, harami, haramicross, highwave, hikkake, hikkakemod, homingpigeon, identical3crows, inneck, inside, invertedhammer, kicking, kickingbylength, ladderbottom, longleggeddoji, longline, marubozu, matchinglow, mathold, morningdojistar, morningstar, onneck, piercing, rickshawman, risefall3methods, separatinglines, shootingstar, shortline, spinningtop, stalledpattern, sticksandwich, takuri, tasukigap, thrusting, tristar, unique3river, upsidegap2crows, xsidegap3methods

由于62种蜡烛形态太多了,你可能需要一次性捞出来,Pandas TA也支持你这么做:

# 公众号:二七阿尔量化
import pandas as pd
import pandas_ta as ta

sh300data = pd.read_csv("sh300_1min.csv")
sh300data = sh300data.set_index("day")
all_candle = sh300data.ta.cdl_pattern(name="all")
print(all_candle)
#                      CDL_2CROWS  CDL_3BLACKCROWS  CDL_3INSIDE  ...  CDL_UNIQUE3RIVER  CDL_UPSIDEGAP2CROWS  CDL_XSIDEGAP3METHODS
# day                                                            ...
# 2022-03-07 10:47:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      
# 2022-03-07 10:48:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      
# 2022-03-07 10:49:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      
# 2022-03-07 10:50:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      
# 2022-03-07 10:51:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      
# ...                         ...              ...          ...  ...               ...                  ...                   ...      
# 2022-07-08 14:55:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      
# 2022-07-08 14:56:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      
# 2022-07-08 14:57:00         0.0              0.0        100.0  ...               0.0                  0.0                   0.0      
# 2022-07-08 14:58:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      
# 2022-07-08 15:00:00         0.0              0.0          0.0  ...               0.0                  0.0                   0.0      

# [20000 rows x 62 columns]

3.高级使用

Pandas TA 还支持策略的使用:

# 公众号:二七阿尔量化
import pandas as pd
import pandas_ta as ta
from multiprocessing import Process, freeze_support

sh300data = pd.read_csv("sh300_1min.csv")
sh300data = sh300data.set_index("day")
CustomStrategy = ta.Strategy(
    name="Momo and Volatility",
    description="SMA 50,200, BBANDS, RSI, MACD and Volume SMA 20",
    ta=[
        {"kind": "sma", "length": 50},
        {"kind": "sma", "length": 200},
        {"kind": "bbands", "length": 20},
        {"kind": "rsi"},
        {"kind": "macd", "fast": 8, "slow": 21},
        {"kind": "sma", "close": "volume", "length": 20, "prefix": "VOLUME"},
    ]
)


if __name__ == '__main__':
    freeze_support()
    sh300data.ta.strategy(CustomStrategy)
    print(sh300data)
    #                          Unnamed: 0      open      high       low  ...  MACD_8_21_9  MACDh_8_21_9  MACDs_8_21_9  VOLUME_SMA_20
    # day                                                            ...
    # 2022-03-07 10:47:00           0  4406.223  4406.352  4405.662  ...          NaN           NaN           NaN            NaN        
    # 2022-03-07 10:48:00           1  4406.172  4406.175  4403.834  ...          NaN           NaN           NaN            NaN        
    # 2022-03-07 10:49:00           2  4403.333  4403.333  4402.235  ...          NaN           NaN           NaN            NaN        
    # 2022-03-07 10:50:00           3  4402.330  4402.519  4401.838  ...          NaN           NaN           NaN            NaN        
    # 2022-03-07 10:51:00           4  4402.376  4402.699  4402.129  ...          NaN           NaN           NaN            NaN        
    # ...                         ...       ...       ...       ...  ...          ...           ...           ...            ...        
    # 2022-07-08 14:55:00       19995  4428.123  4428.371  4427.098  ...    -1.700179     -0.244194     -1.455985     50578250.0        
    # 2022-07-08 14:56:00       19996  4427.209  4427.688  4426.886  ...    -1.725356     -0.215496     -1.509860     53128625.0        
    # 2022-07-08 14:57:00       19997  4427.279  4428.605  4427.279  ...    -1.583555     -0.058956     -1.524599     55393515.0        
    # 2022-07-08 14:58:00       19998  4428.268  4428.458  4428.268  ...    -1.426088      0.078808     -1.504897     53840375.0        
    # 2022-07-08 15:00:00       19999  4427.963  4428.781  4427.963  ...    -1.241029      0.211094     -1.452123     60235755.0        

    # [20000 rows x 18 columns]

可以看到,策略其实就是让你将一些技术指标提前配置好,通过调用策略能够自动将这些技术指标附加到你的数据集上,非常方便。

此外,策略计算的时候会用到多进程,多进程的并行数量也是可以控制的:

# 设置为4个核心,即最多4个并行
sh300data.ta.cores = 4

# 设置为0则不用多进程
sh300data.ta.cores = 0

# 查看并行数量
print(sh300data.ta.cores)
# 0

好啦,关于Pandas TA我们就先介绍到这里啦,如果你需要了解更多内容,可以访问官方文档:

https://github.com/twopirllc/pandas-ta

我们的文章到此就结束啦,如果你喜欢今天的 Python 教程,请持续关注Python实用宝典。

有任何问题,可以在公众号后台回复:加群,回答相应验证信息,进入互助群询问。

原创不易,希望你能在下面点个赞和在看支持我继续创作,谢谢!


​Python实用宝典 ( pythondict.com )
不只是一个宝典
欢迎关注公众号:Python实用宝典

Pandas 性能优化
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。