关于ta-lib安装包_做一个努力的人儿的博客-程序员宝宝

技术标签: Python的基本用法和应用  python  

1.市场指标计算

1.1 MA指标

import talib
import pandas as pd
import mplfinance as mpf
import matplotlib.pyplot as plt
#from matplotlib.finance import candlestick2_ohlc
from mpl_finance import candlestick2_ohlc
from mpl_finance import candlestick_ohlc
#pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

df = pd.read_csv('./000001.SZ.csv')
ma5_df=talib.MA(df['close'],timeperiod=5)
#print(ma_df)
ma10_df=talib.MA(df['close'],timeperiod=10)
ma20_df=talib.MA(df['close'],timeperiod=20)

fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(ma5_df,label='MA5')
ax.plot(ma10_df,label='MA10')
ax.plot(ma20_df,label='MA20')
candlestick2_ohlc(ax,df['open'],df['high'],df['low'],df['close'],width=0.6,colorup='red',colordown='green')
plt.legend()
plt.show()

1.2 MACD指标

diff ,dea,macd=talib.MACD(df['close'],fastperiod=12,slowperiod=26,signalperiod=9)

1.3 RSI指标

rsi=tailb.RSI(df['close'],timeperiod=14)

1.4 KDJ指标

K,D=talib.STOCH(df['high'],df['low'],df['close'],fastk_period=9,slowk_period=3,slowd_period=3)

1.5 CCI指标

CCI=talib.CCI(df['high'],df['low'],df['close'],timeperiod=14)

1.6 ATR指标

ATR=talib.ATR(df['high'],df['low'],df['close'],timeperiod=14)

1.7 OBV指标

OBV=talib.OBV(df['close'],df['vol'])

2 K线组合模式识别

2.1 晨星

nums=talib.CDLMORNINGSTAR(df['open'],df['high'],df['low'],df['close'])

2.2 昏星

nums=talib.CDLEVENINGSTAR(df['open'],df['high'],df['low'],df['close'])

2.3 锤子线

nums=talib.CDLHAMMER(df['open'],df['high'],df['low'],df['close'])

2.4 上吊线

nums=talib.CDLHANGINGMAN(df['open'],df['high'],df['low'],df['close'])

2.5 捉腰带线

nums=talib.CDLBELTHOLD(df['open'],df['high'],df['low'],df['close'])

3 FFn库

指标计算

import fnn
import pandas as pd
series=pd.Series([10000,10100,10300,10600])
#计算收益率
result=ffn.calc_total_return(series)
#计算年化收益率
ann_result=ffn.annualize(result,4,one_year=250)
#计算资产简单收益率
returns=ffn.to_return(series)
#计算夏普比率
sharpe=ffn.calc_sharpe(returns)
#计算最大回撤率
max_drawdown=ffn.calc_max_drowdown(series)
#计算所提诺比率
sortino=ffn.calc_sortino_ratio(returns)

4 两种经典策略

双均线策略

import tushare as ts

ts_pro=ts.pro_api('xxx')
df=ts_pro.daily(ts_code='000001.SZ',start_date='20180101',end_date='20191230')
df=df.reindex(index=df.index[::-1])
used_cols=['trade_date','close']
df=df[used_cols]
df.to_csv('./000001.SZ.csv',index=None)

#策略编写
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import ffn

hold=False
pos=0
capital=100000
rest=0
fee=0.0003
capital_list=[]
MA20_array=np.zeros(20)
MA10_array=np.zeros(10)
df=pd.read_csv('./000001.SZ.csv')

#数据遍历
for i in range(len(df)):
    price=df.loc[i,'close']
    date=df.loc[i,'trade_date']
    MA10_array[0:9]=MA10_array[1:10]
    MA20_array[0:91] = MA20_array[1:20]
    MA10_array[-1] = price
    MA20_array[-1] = price
    if i<20:
        continuce
    MA10 = MA10_array.mean()
    MA20 = MA20_array.mean()
#判断是否达到开仓和平仓的信号
if MA10>MA20 and hold==False:
    pos=int(capital/price/100)*100
    rest=capital-pos*price*(1+fee)
    hold=Ture
    print('buy at',date,'price',price,'capital',capital)
elif MA10<MA20 and hold==Ture:
    capital=pos*price*(1-fee)+rest
    pos=0
    hold=False
    print('sell at',date,'price',price,'capital',capital)

if hold==True:
    capital_list.append(rest+pos*price)
else:
    capital_list.append(capital)

capital_series=pd.Series(capital_list)
capital_returns=ffn.to_returns(capital_series)

RSI策略

 RSI6 = talib.RSI('rsi6_arry',timeperiod=6)[-1]
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36333776/article/details/121325445

智能推荐

SVPWM程序_晓晓李的博客-程序员宝宝

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入请问谁有基于...

spring基础了解,小白入门框架必备_weixin_42495228的博客-程序员宝宝

0.spring框架家族的学习路线spring framework–&gt;springWebMVC–&gt;springboot–&gt;springdata–&gt;springSession–&gt;springCloud1.搭建spring环境​ 最新版本5.2.8​ spring 框架中学习路线: 核心的:IOC和AOP 测试:与junit集成 持久层:事务管理 web:springMVC 组件集成:远程接口调用,任务调度操作步骤:- 引入spring的核心jar包:4个核

windows搭建MQTT服务器_mqtt环境 win11_内卷的馒头的博客-程序员宝宝

文章目录一、下载mosquitto二、配置mosquitto三、MQTTX一、下载mosquitto点击下载安装程序 下载地址二、配置mosquitto安装路径打开命令行界面设置user及password执行:mosquitto_passwd.exe -c pwfile.example -u vic设置用户名:vic 设置密码:123456配置文件检查 mosquitto.exe -c mosquitto.conf 未报错说明配置正常设置端口 默认

Intent对象详解(二)_intent添加action_tuke_tuke的博客-程序员宝宝

3,Action、Category属性与intent-filter配置Intent的Action和Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,Category则用于为Action增加额外的附加类别信息,通常,Action与Category结合使用。元素里通常可包括如下子元素:a、0~N个子元素b、0~N个子

vscode终止python运行,VSCode杀死正在运行的进程_小诺1996的博客-程序员宝宝

I have been usingPython debug and LLDB attach debug since I have code in Python and C++I have run this multiple times. It looks like everytime there is an exception in the middle of debugging, the pro...

随便推点

10-声明式API_Zeb-D的博客-程序员宝宝

[toc]命令式命令行操作很多 Kubernetes 的 API 对象,有的是用来描述应用,有的则是为应用提供各种各样的服务。但是,无一例外地,为了使用这些 API 对象提供的能力,你都需要编写一个对应的 YAML 文件交给 Kubernetes。这个 YAML 文件,正是 Kubernetes 声明式 API 所必须具备的一个要素。不过,是不是只要用 YAML 文件代替了命令行操作,就是...

Lomboz中配置JBoss 4.0.1 _溺水的鱼的博客-程序员宝宝

这个文章是对《Eclipse快速上手EJB -- 1. Lomboz + JBoss-IDE 配置1 》一文的一点补充。在那篇文章中,jboss400.server这个配置是针对JBoss 4.0.0 的,如果使用的是JBoss 4.0.1,需要使用以下的配置文件: jboss401.server serverDefinition name="JBOSS 4

各大论坛资源汇总_资源论坛_EvanDeveloper的博客-程序员宝宝

各大论坛资源合集汇总http://www.erji.net/ 聊耳机的论坛http://www.cdbest.com/ 聊刻录机,刻录光盘http://forum.ubuntu.org.cn/ 应该是国内最大的ubuntu论坛了吧http://bbs.go2eu.com/ 穷游网,撸瑟的国际旅游,我最近很感兴趣http://www.ditiezu.com/index.php 地铁...

关于php的fopen指令在服务端创建/写入文件失败_fopen php 5.6 写入失败_illumiD的博客-程序员宝宝

如下php代码:$fp=fopen(&quot;../txt/&quot;.$getSelectDate.&quot;.txt&quot;,&quot;wb&quot;) or die(&quot;error1&quot;);本地测试通过,但将该php文件上传到服务器端后测试,出现创建/写入文件失败(未检查错误报告)猜测多半为权限写入原因,在服务端控制台下写入以下代码:chmod 777 txt;(txt是要写入修改的文件所在的文件夹的目录名,赋予目录txt的读写...

Visio画UML图基本操作及技巧解析_weixin_34235371的博客-程序员宝宝

Visio如何添加枚举类型 1.  新建DataType2. 选择DataType -&amp;gt; Property -&amp;gt; Stereotype -&amp;gt;enumeration3. 添加其他的Attribute,作为Enumeration值。 本文和大家重点讨论一下用Visio画UML图基本操作,画UML图有好多种工具,VISIO只是其中一种,VISIO的动作非常轻快.很多人都在用。...

推荐文章

热门文章

相关标签