日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 股票指标库talib_TaLib在股票技术分析中的应用

發(fā)布時間:2023/12/15 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 股票指标库talib_TaLib在股票技术分析中的应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、TaLib與技術(shù)分析技術(shù)分析是股票分析十分有效直接的手段,在實際投資中我們常常需要計算各種簡單或復(fù)雜的技術(shù)指標(biāo)來分析參考

對于技術(shù)指標(biāo)的定義基本都大同小異,很多都是通用的且模塊化的東西

對于不會寫代碼或者不想自己寫函數(shù)計算這些技術(shù)指標(biāo)的人而言,強大的Talib給我們帶來了福音

只需要調(diào)用Talib的函數(shù),輸入簡單的參數(shù)就可以得到自己想要的結(jié)果

2、初看Talib

先簡單看看Talib都給我們提供了那些計算技術(shù)指標(biāo)的函數(shù),按技術(shù)指標(biāo)的類型列示如下:

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)

上面只是列舉了一部分,更多函數(shù)可以參見官網(wǎng),點擊鏈接

3、如何應(yīng)用:MA實例從上面可以看到,MA這個函數(shù)的參數(shù)為:real = MA(close, timeperiod=30, matype=0)

close表示收盤價序列,timeperiod指定義好均線的計算長度即幾日均線,不輸入的話,默認(rèn)為30日,matype可以默認(rèn)不用輸入,然后就可以得到均線的值

所以簡單來講,只取獲取收盤價序列,就可以輕松計算MA值

下面以萬科A為例進(jìn)行說明

data=DataAPI.MktEqudGet(ticker=u"000002",beginDate=u"20160601",endDate=u"20160804",field=u"secShortName,tradeDate,closePrice",pandas="1") #取數(shù)據(jù)

data['MA5'] = talib.MA(data['closePrice'].values, timeperiod=5) #調(diào)用talib計算5日均線的值

data.tail(10) #后十行結(jié)果

就這樣,我們便捷地計算出了均線,下面計算更復(fù)雜的EMA,MACD

有關(guān)EMA,MACD的基礎(chǔ)知識,可以參考社區(qū)帖子『研究|技術(shù)指標(biāo)|第一彈』MACD

關(guān)于EMA,MACD計算的函數(shù)的描述是:

real = EMA(close, timeperiod=30)

macd, macdsignal, macdhist = MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)

輸入?yún)?shù):close是收盤價,timeperiod指的是指數(shù)移動平均線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) #調(diào)用talib 計算12日移動移動平均線的值

data['EMA26'] = talib.EMA(data['closePrice'].values, timeperiod=26)

data['MACD'],data['MACDsignal'],data['MACDhist'] = talib.MACD(data['closePrice'].values)在構(gòu)建策略時也會更加方便。

4、策略實戰(zhàn):雙均線策略當(dāng)5日均線上穿60日均線,買入

當(dāng)5日均線下穿60日均線,賣出

我們不再需要自己寫函數(shù)計算均線,只需要調(diào)用函數(shù)就可以方便的解決問題(對于更復(fù)雜的指標(biāo),talib的便捷就更加明顯)

import pandas as pd

import numpy as np

import talib

start = '2013-01-01' # 回測起始時間

end = '2016-07-01' # 回測結(jié)束時間

benchmark = 'HS300' # 策略參考標(biāo)準(zhǔn)

universe = set_universe('HS300')

capital_base = 1000000 # 起始資金

freq = 'd' # 策略類型,'d'表示日間策略使用日線回測,'m'表示日內(nèi)策略使用分鐘線回測

refresh_rate = 5 # 調(diào)倉頻率,表示執(zhí)行handle_data的時間間隔,若freq = 'd'時間間隔的單位為交易日,若freq = 'm'時間間隔為分鐘

def initialize(account): # 初始化虛擬賬戶狀態(tài)

pass

def handle_data(account): # 每個交易日的買入賣出指令

period1 = 5 #取5日數(shù)據(jù)

period2 = 60 #取60日數(shù)據(jù)

all_close_prices = account.get_attribute_history('closePrice', period2) # 獲取歷史closePrice數(shù)據(jù)

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是一個數(shù)組,對應(yīng)于日期,最后一個元素就是當(dāng)前交易日前一天的5日均線值

buy_list.append(stk)

elif MA5[-1]-MA60[-1] < 0: #當(dāng)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)

# 其余繼續(xù)持股

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) # 每只股票買入數(shù)量

for stk in buy:

num = int(amout/account.referencePrice[stk] / 100.0) * 100

order_to(stk, num)

return原文鏈接:TaLib在股票技術(shù)分析中的應(yīng)用

總結(jié)

以上是生活随笔為你收集整理的python 股票指标库talib_TaLib在股票技术分析中的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。