利用python炒股talib_TaLib在股票技术分析中的应用
1、TaLib与技术分析技术分析是股票分析十分有效直接的手段,在实际投资中我们常常需要计算各种简单或复杂的技术指标来分析参考
对于技术指标的定义基本都大同小异,很多都是通用的且模块化的东西
对于不会写代码或者不想自己写函数计算这些技术指标的人而言,强大的Talib给我们带来了福音
只需要调用Talib的函数,输入简单的参数就可以得到自己想要的结果
2、初看Talib
先简单看看Talib都给我们提供了那些计算技术指标的函数,按技术指标的类型列示如下:
Overlap Studies Functions
BBANDS - Bollinger Bands
upperband, middleband, lowerband = BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
DEMA - Double Exponential Moving Average
real = DEMA(close, timeperiod=30)
EMA - Exponential Moving Average
real = EMA(close, timeperiod=30)
HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline
real = HT_TRENDLINE(close)
KAMA - Kaufman Adaptive Moving Average
real = KAMA(close, timeperiod=30)
MA - Moving average
real = MA(close, timeperiod=30, matype=0)
MAMA - MESA Adaptive Moving Average
mama, fama = MAMA(close, fastlimit=0, slowlimit=0)
MAVP - Moving average with variable period
real = MAVP(close, periods, minperiod=2, maxperiod=30, matype=0)
MIDPOINT - MidPoint over period
real = MIDPOINT(close, timeperiod=14)
MIDPRICE - Midpoint Price over period
real = MIDPRICE(high, low, timeperiod=14)
SAR - Parabolic SAR
real = SAR(high, low, acceleration=0, maximum=0)
SAREXT - Parabolic SAR - Extended
real = SAREXT(high, low, startvalue=0, offsetonreverse=0, accelerationinitlong=0,
accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0)
SMA - Simple Moving Average
real = SMA(close, timeperiod=30)
T3 - Triple Exponential Moving Average (T3)
real = T3(close, timeperiod=5, vfactor=0)
TEMA - Triple Exponential Moving Average
real = TEMA(close, timeperiod=30)
TRIMA - Triangular Moving Average
real = TRIMA(close, timeperiod=30)
WMA - Weighted Moving Average
real = WMA(close, timeperiod=30)
Momentum Indicator Functions
ADX - Average Directional Movement Index
real = ADX(high, low, close, timeperiod=14)
ADXR - Average Directional Movement Index Rating
real = ADXR(high, low, close, timeperiod=14)
APO - Absolute Price Oscillator
real = APO(close, fastperiod=12, slowperiod=26, matype=0)
AROON - Aroon
aroondown, aroonup = AROON(high, low, timeperiod=14)
AROONOSC - Aroon Oscillator
real = AROONOSC(high, low, timeperiod=14)
BOP - Balance Of Power
real = BOP(open, high, low, close)
CCI - Commodity Channel Index
real = CCI(high, low, close, timeperiod=14)
CMO - Chande Momentum Oscillator
real = CMO(close, timeperiod=14)
DX - Directional Movement Index
real = DX(high, low, close, timeperiod=14)
MACD - Moving Average Convergence/Divergence
macd, macdsignal, macdhist = MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
MACDEXT - MACD with controllable MA type
macd, macdsignal, macdhist = MACDEXT(close, fastperiod=12, fastmatype=0, slowperiod=26,
slowmatype=0, signalperiod=9, signalmatype=0)
MACDFIX - Moving Average Convergence/Divergence Fix 12/26
macd, macdsignal, macdhist = MACDFIX(close, signalperiod=9)
MFI - Money Flow Index
real = MFI(high, low, close, volume, timeperiod=14)
MINUS_DI - Minus Directional Indicator
real = MINUS_DI(high, low, close, timeperiod=14)
MINUS_DM - Minus Directional Movement
real = MINUS_DM(high, low, timeperiod=14)
MOM - Momentum
real = MOM(close, timeperiod=10)
RSI - Relative Strength Index
real = RSI(close, timeperiod=14)
Volume Indicator Functions
AD - Chaikin A/D Line
real = AD(high, low, close, volume)
ADOSC - Chaikin A/D Oscillator
real = ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10)
OBV - On Balance Volume
real = OBV(close, volume)
Volatility Indicator Functions
ATR - Average True Range
real = ATR(high, low, close, timeperiod=14)
NATR - Normalized Average True Range
real = NATR(high, low, close, timeperiod=14)
TRANGE - True Range
real = TRANGE(high, low, close)Price Transform Functions
AVGPRICE - Average Price
real = AVGPRICE(open, high, low, close)
MEDPRICE - Median Price
real = MEDPRICE(high, low)
TYPPRICE - Typical Price
real = TYPPRICE(high, low, close)
WCLPRICE - Weighted Close Price
real = WCLPRICE(high, low, close)
Cycle Indicator Functions
HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period
real = HT_DCPERIOD(close)
HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase
real = HT_DCPHASE(close)
HT_PHASOR - Hilbert Transform - Phasor Components
inphase, quadrature = HT_PHASOR(close)
HT_SINE - Hilbert Transform - SineWave
sine, leadsine = HT_SINE(close)
HT_TRENDMODE - Hilbert Transform - Trend vs Cycle Mode
integer = HT_TRENDMODE(close)
Statistic Functions
BETA - Beta
real = BETA(high, low, timeperiod=5)
CORREL - Pearson's Correlation Coefficient (r)
real = CORREL(high, low, timeperiod=30)
LINEARREG - Linear Regression
real = LINEARREG(close, timeperiod=14)
LINEARREG_ANGLE - Linear Regression Angle
real = LINEARREG_ANGLE(close, timeperiod=14)
LINEARREG_INTERCEPT - Linear Regression Intercept
real = LINEARREG_INTERCEPT(close, timeperiod=14)
LINEARREG_SLOPE - Linear Regression Slope
real = LINEARREG_SLOPE(close, timeperiod=14)
STDDEV - Standard Deviation
real = STDDEV(close, timeperiod=5, nbdev=1)
TSF - Time Series Forecast
real = TSF(close, timeperiod=14)
VAR - Variance
real = VAR(close, timeperiod=5, nbdev=1)
Math Transform Functions
ACOS - Vector Trigonometric ACos
real = ACOS(close)
ASIN - Vector Trigonometric ASin
real = ASIN(close)
ATAN - Vector Trigonometric ATan
real = ATAN(close)
CEIL - Vector Ceil
real = CEIL(close)
COS - Vector Trigonometric Cos
real = COS(close)
COSH - Vector Trigonometric Cosh
real = COSH(close)
EXP - Vector Arithmetic Exp
real = EXP(close)
FLOOR - Vector Floor
real = FLOOR(close)
LN - Vector Log Natural
real = LN(close)
LOG10 - Vector Log10
real = LOG10(close)
SIN - Vector Trigonometric Sin
real = SIN(close)
SINH - Vector Trigonometric Sinh
real = SINH(close)
SQRT - Vector Square Root
real = SQRT(close)
TAN - Vector Trigonometric Tan
real = TAN(close)
TANH - Vector Trigonometric Tanh
real = TANH(close)
Math Operator Functions
ADD - Vector Arithmetic Add
real = ADD(high, low)
DIV - Vector Arithmetic Div
real = DIV(high, low)
MAX - Highest value over a specified period
real = MAX(close, timeperiod=30)
MAXINDEX - Index of highest value over a specified period
integer = MAXINDEX(close, timeperiod=30)
MIN - Lowest value over a specified period
real = MIN(close, timeperiod=30)
MININDEX - Index of lowest value over a specified period
integer = MININDEX(close, timeperiod=30)
MINMAX - Lowest and highest values over a specified period
min, max = MINMAX(close, timeperiod=30)
MINMAXINDEX - Indexes of lowest and highest values over a specified period
minidx, maxidx = MINMAXINDEX(close, timeperiod=30)
MULT - Vector Arithmetic Mult
real = MULT(high, low)
SUB - Vector Arithmetic Substraction
real = SUB(high, low)
SUM - Summation
real = SUM(close, timeperiod=30)
上面只是列举了一部分,更多函数可以参见官网,点击链接
3、如何应用:MA实例从上面可以看到,MA这个函数的参数为:real = MA(close, timeperiod=30, matype=0)
close表示收盘价序列,timeperiod指定义好均线的计算长度即几日均线,不输入的话,默认为30日,matype可以默认不用输入,然后就可以得到均线的值
所以简单来讲,只取获取收盘价序列,就可以轻松计算MA值
下面以万科A为例进行说明
data=DataAPI.MktEqudGet(ticker=u"000002",beginDate=u"20160601",endDate=u"20160804",field=u"secShortName,tradeDate,closePrice",pandas="1") #取数据
data['MA5'] = talib.MA(data['closePrice'].values, timeperiod=5) #调用talib计算5日均线的值
data.tail(10) #后十行结果
就这样,我们便捷地计算出了均线,下面计算更复杂的EMA,MACD
有关EMA,MACD的基础知识,可以参考社区帖子『研究|技术指标|第一弹』MACD
关于EMA,MACD计算的函数的描述是:
real = EMA(close, timeperiod=30)
macd, macdsignal, macdhist = MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
输入参数:close是收盘价,timeperiod指的是指数移动平均线EMA的长度,fastperiod指更短时段的EMA的长度,slowperiod指更长时段的EMA的长度,signalperiod指DEA长度返回值:注意有些地方的macdhist = 2(dif-dea),但是talib中MACD的计算是macdhist = dif-dea
data['EMA12'] = talib.EMA(data['closePrice'].values, timeperiod=12) #调用talib 计算12日移动移动平均线的值
data['EMA26'] = talib.EMA(data['closePrice'].values, timeperiod=26)
data['MACD'],data['MACDsignal'],data['MACDhist'] = talib.MACD(data['closePrice'].values)在构建策略时也会更加方便。
4、策略实战:双均线策略当5日均线上穿60日均线,买入
当5日均线下穿60日均线,卖出
我们不再需要自己写函数计算均线,只需要调用函数就可以方便的解决问题(对于更复杂的指标,talib的便捷就更加明显)
import pandas as pd
import numpy as np
import talib
start = '2013-01-01' # 回测起始时间
end = '2016-07-01' # 回测结束时间
benchmark = 'HS300' # 策略参考标准
universe = set_universe('HS300')
capital_base = 1000000 # 起始资金
freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
refresh_rate = 5 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
def initialize(account): # 初始化虚拟账户状态
pass
def handle_data(account): # 每个交易日的买入卖出指令
period1 = 5 #取5日数据
period2 = 60 #取60日数据
all_close_prices = account.get_attribute_history('closePrice', period2) # 获取历史closePrice数据
buy_list = [] # 备选买入清单
sell_list = [] # 卖出清单
for stk in account.universe:
prices = all_close_prices[stk]
if prices is None:
continue
try:
MA5 = talib.MA(prices, timeperiod=period1) # 计算5日均线
MA60 = talib.MA(prices, timeperiod=period2) #计算60日均线
except:
continue
# 买入卖出判断
if MA5[-1]-MA60[-1] > 0: #talib计算返回的MA5是一个数组,对应于日期,最后一个元素就是当前交易日前一天的5日均线值
buy_list.append(stk)
elif MA5[-1]-MA60[-1] < 0: #当5日均线下穿60日均线,卖出
sell_list.append(stk)
hold = []
buy = [] # 最终买入清单
# 买入卖出
for stk in account.valid_secpos:
# sell_list卖出
if stk in sell_list:
order_to(stk, 0)
# 其余继续持股
else:
hold.append(stk)
buy = hold
for stk in buy_list:
# 若buy_list中股票有未买入的,加入
if stk not in hold:
buy.append(stk)
if len(buy) > 0:
# 等仓位买入
amout = account.referencePortfolioValue/len(buy) # 每只股票买入数量
for stk in buy:
num = int(amout/account.referencePrice[stk] / 100.0) * 100
order_to(stk, num)
return原文链接:TaLib在股票技术分析中的应用
利用python炒股talib_TaLib在股票技术分析中的应用相关推荐
- python 股票指标库talib_TaLib在股票技术分析中的应用
1.TaLib与技术分析技术分析是股票分析十分有效直接的手段,在实际投资中我们常常需要计算各种简单或复杂的技术指标来分析参考 对于技术指标的定义基本都大同小异,很多都是通用的且模块化的东西 对于不会写 ...
- 股票技术分析方法综述
文章目录 K线 均线 MACD KDJ和RSI BOLL线 趋势理论.支撑位和压力位 形态理论 量价关系理论 道氏理论 波浪理论 江恩理论 缠论 自定义指标 K线 K线的组合形态是K线技术分析中的重要 ...
- 利用python进行股票技术分析--以茅台为例
""" 利用python进行股票技术分析–以茅台为例 通过下述策略对利用python进行股票技术分析,可以举一反三,并且能够实现复杂的技术分析策略,同时可以同python ...
- 小项目实战分享 | 用Python实现股票技术分析指标
大家好,我是王某人. 股票是很多人感兴趣的,我用Python实现了股市技术分析中常见的指标,如:MA,MACD,RSI,BOLL,ATR,KDJ,CCI,PSY等,这些指标在通达信.同花顺等软件上都能 ...
- 股票技术分析--任正德主编
股票技术分析:散户完全自救手册–任正德主编 第二章 K线分析 2.1 K线的含义及画法 K线画法和含义(日K线) 当日开盘价(a)与当日收盘价(b)组成的区域叫K线实体,若a<b,则称此K线为阳 ...
- 数据可视化:利用Python和Echarts制作“用户消费行为分析”可视化大屏
数据可视化:利用Python和Echarts制作"用户消费行为分析"可视化大屏 前言 实验目的: 准备工作: 一.创建项目: 二.建立数据库连接获取数据: 三.页面布局: 四.下载 ...
- 利用python做微信聊天记录词云分析——记录美好回忆
目录 1 概述 2 数据准备 2.1 安卓设备 2.1.1 Root手机,安装Root Explorer 2.1.2 用Root Explorer将聊天记录的数据文件导出并存入电脑 2.1.3 对En ...
- 利用python爬取猫眼电影,分析《大侦探皮卡丘》|凹凸数读
利用python爬取猫眼电影,分析<大侦探皮卡丘>,看看当皮卡丘长出绒毛,"丑拒"还是"真香"都在猫眼短评里了. 本文首发于微信公众号<凹凸数 ...
- 利用python将文件夹中子文件夹中的图片数目大于小于指定的数目分类
利用python将文件夹中子文件夹中的图片数目大于小于指定的数目分类 find ./ -name ".DS_Store" -depth -exec rm {} \; Mac 删除 ...
- 利用python炒股talib_借助talib使用技术分析指标来炒股
什么是技术分析 所谓股票的技术分析,是相对于基本面分析而言的.基本分析法着重于对一般经济情况以及各个公司的经营管理状况.行业动态等因素进行分析,以此来研究股票的价值,衡量股价的高低.而技术分析则是透过 ...
最新文章
- java方法区内存泄露_深入理解java虚拟机-第二章:java内存区域与内存泄露异常...
- 放弃Maven以后,我用了它。。。
- 关于VC单选按钮不能设置变量以及Group属性的设置问题
- 五千字详解消息通知!
- idea调试debug技巧_被我用烂的DEBUG调试技巧,专治各种搜索不到的问题
- 8.初探python之集合
- 转: databasemetadata 无法获取数据库表备注的解决方法
- Intel Core Enhanced Core架构/微架构/流水线 (12) - 数据预取
- 百练OJ:2799浮点数格式
- 加密算法实现数据通讯
- UML统一建模语言快速入门
- 第二章 工厂设计模式
- 微信小程序-猜拳小游戏
- MATLAB如何修改默认设置
- 华为Mate7-打开开发者选项
- 安卓SDK——人脸识别
- Oracle与SQL *PLUS简介
- mysql 查询关键字有中文_Laravel Backpack 后台列表页面查询关键词包含中文时报错...
- 数字化医院中智慧医疗的具体应用
- 数据结构与算法分析C++语言描述(第四版)图论学习记录