Type/to search
8
Follow
1376
Followers
2.7.1 Windows 32位系统 Python 2.7 环境 安装 talib 指标库
Tutorials
Created 2017-02-11 18:01:05  Updated 2019-08-01 09:20:22
 1
 4710

Windows 32位系统 Python 2.7 环境 安装 talib 指标库


发明者量化 平台中 使用 Python 编写策略时 如果使用 重写过的 TA 库,计算MA 、MACD、EMA 等指标(由于已内置)是不会有任何报错的。

比如用一下策略回测(不论是使用公共服务器 还是 使用 自己的 托管者 回测,都是没问题的。)

import types def main(): STATE_IDLE = -1 state = STATE_IDLE initAccount = ext.GetAccount() while True: if state == STATE_IDLE : n = ext.Cross(FastPeriod,SlowPeriod) # 指标交叉函数 if abs(n) >= EnterPeriod : opAmount = _N(initAccount.Stocks * PositionRatio,3) Dict = ext.Buy(opAmount) if n > 0 else ext.Sell(opAmount) if Dict : opAmount = Dict['amount'] state = PD_LONG if n > 0 else PD_SHORT Log("开仓详情",Dict,"交叉周期",n) else: n = ext.Cross(ExitFastPeriod,ExitSlowPeriod) # 指标交叉函数 if abs(n) >= ExitPeriod and ((state == PD_LONG and n < 0) or (state == PD_SHORT and n > 0)) : nowAccount = ext.GetAccount() Dict2 = ext.Sell(nowAccount.Stocks - initAccount.Stocks) if state == PD_LONG else ext.Buy(initAccount.Stocks - nowAccount.Stocks) state = STATE_IDLE nowAccount = ext.GetAccount() LogProfit(nowAccount.Balance - initAccount.Balance,'钱:',nowAccount.Balance,'币:',nowAccount.Stocks,'平仓详情:',Dict2,'交叉周期:',n) Sleep(Interval * 1000)

该策略 名称 : 均线策略(python版) ,使用模板: python版现货数字货币交易类库

可以直接在 策略广场上 复制获取。

该策略没有使用任何 指标函数 , 但是 使用了 “python版现货数字货币交易类库” 这个模板的 ext.Cross 导出函数,该导出函数中调用了 TA.MA 。 用到了 TA库(由于 TA库已经内置,所以没问题!)

如果改一下代码。

import types import talib # 改动 引用 talib 库 def main(): STATE_IDLE = -1 state = STATE_IDLE initAccount = ext.GetAccount() while True: records = exchange.GetRecords() ma = talib.MA(records.Close) # 改动 ,调用 talib 库的 MA 函数 即 均线指标计算 LogStatus("均值" + str(ma)) if state == STATE_IDLE : n = ext.Cross(FastPeriod,SlowPeriod) # 指标交叉函数 if abs(n) >= EnterPeriod : opAmount = _N(initAccount.Stocks * PositionRatio,3) Dict = ext.Buy(opAmount) if n > 0 else ext.Sell(opAmount) if Dict : opAmount = Dict['amount'] state = PD_LONG if n > 0 else PD_SHORT Log("开仓详情",Dict,"交叉周期",n) else: n = ext.Cross(ExitFastPeriod,ExitSlowPeriod) # 指标交叉函数 if abs(n) >= ExitPeriod and ((state == PD_LONG and n < 0) or (state == PD_SHORT and n > 0)) : nowAccount = ext.GetAccount() Dict2 = ext.Sell(nowAccount.Stocks - initAccount.Stocks) if state == PD_LONG else ext.Buy(initAccount.Stocks - nowAccount.Stocks) state = STATE_IDLE nowAccount = ext.GetAccount() LogProfit(nowAccount.Balance - initAccount.Balance,'钱:',nowAccount.Balance,'币:',nowAccount.Stocks,'平仓详情:',Dict2,'交叉周期:',n) Sleep(Interval * 1000)

在策略中 调用 talib.MA (即 使用 talib 库),则 在使用自己的托管者回测 或者 实盘 运行策略时,就会有如下报错:

img

可能会有用户发现: 我使用公共服务器回测 是没问题的呀!
没错! 因为 公共服务器 上已经安装了 talib 库。

对于自己的托管者的Python环境,只能动手给 安装一下 talib 了。
下面示范是在 windows XP系统 (即 32 位 windows) 下的 Python 2.7 环境 中安装 talib 库
网上方法比较多,这里使用一种比较简单的。

  • 1、准备 下载 安装好 Python 2.7 (win32)

    img

    注意 win32 版本 Python 2.7 如图 下载安装包。

    安装时 注意 选择上自动配置 环境变量的选项, pip 组件已经默认安装。

    img

  • 2、安装 wheel

    img

    以下是在网络上搜索的资料

python wheel怎么安装? 小灰机289 | 浏览 14404 次 推荐于2016-01-19 03:17:24 最佳答案 你装了pip吗,建议先装pip,后面安装各种python库就很方便了。 打开命令行窗口,输入下面的命令: pip install wheel 这时pip会自动在网络上下载安装wheel。 安装完成后可以敲下面的命令查看是否安装成功: pip freeze
  • 3、下载 talib 并安装

    下载地址: http://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib

    要找对应版本 和 系统 的 talib 文件 , 如图:

    img

    下载好了以后 如下图 进行安装:

    img

  • 4、本以为OK了 ,可是在使用 import talib 测试的时候 报错 显示 找不到 numpy , 看来这个 库 也要安装下。

    img

    下载 numpy
    下载地址: http://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib

    img

    安装:

    img

  • 5、测试一下

    img

    在 发明者量化 上试一下,策略使用 talib 的 指标函数

    可以了 LogStatus 的输出显示出来了。

    img

  • 以上过程中用到的 安装包 如下, 我已经传到QQ群。 用户也可以 按照教程地址 自行下载。

    img

    压缩后

    img

Related Recommendations
Comment
All comments (1)

    谢谢!

    5 years ago
  • 1
Forums
PINE Language
Get the app
iPhone Download
© 2015 - ∞ INVENTOR PTE LTD (SG)