TA-Lib的技术分析


TA-Lib的技术分析

我尝试了TA-Lib,该库可轻松进行技术分析。 TA-Lib有一个Python包装器,很容易引入。

准备

首先,您需要安装TA-Lib。对于Mac,您可以使用Homebrew安装它。

1
$ brew install ta-lib

使它在Python中可用。

1
$ pip install TA-Lib

技术分析

导入这次使用的库。

1
2
3
4
import numpy as np
import pandas as pd
import talib
import quandl

这一次,我们将使用quandl获取数据。

1
df = quandl.get('NIKKEI/INDEX')  # quandlの場合

TA-Lib不能将DataFrame用作参数,因此请使用numpy的ndarray格式。

1
2
close = np.array(df['Close Price'])
close[:5]

无论如何,我将尝试各种技术指标。 (这一次,仅使用收盘价,而不使用使用烛台的指数。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
output = close.copy()
cols = ['Original']
# 単純移動平均(SMA: Simple Moving Average)
output = np.c_[output, talib.SMA(close)]
cols += ['SMA']
# 加重移動平均(WMA: Weighted Moving Average)
output = np.c_[output, talib.WMA(close)]
cols += ['WMA']
# 指数移動平均(EMA: Exponential Moving Average)
output = np.c_[output, talib.EMA(close)]
cols += ['EMA']
# 2重指数移動平均(DEMA: Double Exponential Moving Average)
output = np.c_[output, talib.DEMA(close)]
cols += ['DEMA']
# 3重指数移動平均(TEMA: Triple Exponential Moving Average)
output = np.c_[output, talib.T3(close)]
cols += ['TEMA']
# 三角移動平均(TMA: Triangular Moving Average)
output = np.c_[output, talib.TRIMA(close)]
cols += ['TMA']
# Kaufmanの適応型移動平均(KAMA: Kaufman Adaptive Moving Average)
output = np.c_[output, talib.KAMA(close)]
cols += ['KAMA']
# MESAの適応型移動平均(MAMA: MESA Adaptive Moving Average)
for arr in talib.MAMA(close):
    output = np.c_[output, arr]
cols += ['MAMA', 'FAMA']
# トレンドライン(Hilbert Transform - Instantaneous Trendline)
output = np.c_[output, talib.HT_TRENDLINE(close)]
cols += ['HT_TRENDLINE']
# ボリンジャー?バンド(Bollinger Bands)
for arr in talib.BBANDS(close):
    output = np.c_[output, arr]
cols += ['BBANDS_upperband', 'BBANDS_middleband', 'BBANDS_lowerband']
# MidPoint over period
output = np.c_[output, talib.MIDPOINT(close)]
cols += ['MIDPOINT']

# 変化率(ROC: Rate of change Percentage)
output = np.c_[output, talib.ROCP(close)]
cols += ['ROC']
# モメンタム(Momentum)
output = np.c_[output, talib.MOM(close)]
cols += ['MOM']
# RSI: Relative Strength Index
output = np.c_[output, talib.RSI(close)]
cols += ['RSI']
# MACD: Moving Average Convergence/Divergence
for arr in talib.MACD(close):
    output = np.c_[output, arr]
cols += ['MACD', 'MACD_signal', 'MACD_hist']
# APO: Absolute Price Oscillator
output = np.c_[output, talib.APO(close)]
cols += ['APO']
# PPO: Percentage Price Oscillator
output = np.c_[output, talib.PPO(close)]
cols += ['PPO']
# CMO: Chande Momentum Oscillator
output = np.c_[output, talib.CMO(close)]
cols += ['CMO']

# ヒルベルト変換 - Dominant Cycle Period
output = np.c_[output, talib.HT_DCPERIOD(close)]
cols += ['HT_DCPERIOD']
# ヒルベルト変換 - Dominant Cycle Phase
output = np.c_[output, talib.HT_DCPHASE(close)]
cols += ['HT_DCPHASE']
# ヒルベルト変換 - Phasor Components
for arr in talib.HT_PHASOR(close):
    output = np.c_[output, arr]
cols += ['HT_PHASOR_inphase', 'HT_PHASOR_quadrature']
# ヒルベルト変換 - SineWave
for arr in talib.HT_SINE(close):
    output = np.c_[output, arr]
cols += ['HT_SINE_sine', 'HT_SINE_leadsine']    
# ヒルベルト変換 - Trend vs Cycle Mode
output = np.c_[output, talib.HT_TRENDMODE(close)]
cols += ['HT_TRENDMODE']
output.shape

# 60日単純移動平均
output = np.c_[output, talib.SMA(close, timeperiod=60)]
cols += ['SMA60']
# 15日ボリンジャー?バンド
for arr in talib.BBANDS(close, timeperiod=15, nbdevup=2, nbdevdn=2, matype=0):
    output = np.c_[output, arr]
cols += ['BBANDS15_upperband', 'BBANDS15_middleband', 'BBANDS15_lowerband']
# 21日RSI
output = np.c_[output, talib.RSI(close, timeperiod=21)]
cols += ['RSI21']

您可以通过指定

参数的时间段来计算各种时间段。

转换为DataFrame格式并与日期相对应。

1
2
data = pd.DataFrame(output, index=df.index, columns=cols)
data.tail()

<表格>

原始

SMA

WMA

EMA

DEMA

TEMA

TMA

卡玛

MAMA

FAMA

...

HT_PHASOR_inphase

HT_PHASOR_quadrature

HT_SINE_sine

HT_SINE_leadsine

HT_TRENDMODE

SMA60

BBANDS15_upperband

BBANDS15_middleband

BBANDS15_lowerband

RSI21

日期




<身体>

2018-01-26

23631.88

23336.888333

23577.879333

23396.084808

23950.015680

23881.542216

23358.915125

23401.776887

23774.262642

23579.472022

...

21.325892

-43.817842

-0.589713

-0.988059

1.0

22920.246667

24056.610766

23800.404000

23544.197234

58.287358

2018-01-29

23629.34

23362.327333

23596.747183

23411.133530

23943.404763

23819.197807

23416.499625

23410.021533

23701.801321

23610.054347

...

9.146663

81.389471

-0.713710

-0.9999956

1.0

22947.261500

24061.838563

23794.724667

23527.610770

58.232275

2018-01-30

23291.97

23380.124000

23592.208000

23403.445560

23894.184742

23717.474237

23468.224208

23407.458663

23681.309755

23611.835732

...

139.081959

117.191239

-0.837169

-0.978716

1.0

22968.599833

24121.395394

23757.523333

23393.651273

51.451184

2018-01-31

23098.29

23393.585333

23574.025161

23383.758105

23824.419397

23575.717839

23512.524500

23402.617512

23652.158767

23612.843808

...

165.813706

-406.110394

-0.942737

-0.902462

0.0

22986.711167

24200.998868

23711.529333

23222.059798

48.076476

2018-02-01

23486.11

23424.681667

23579.994495

23390.361453

23808.770310

23470.100518

23551.332833

23405.839309

23569.134384

23601.916452

...

-122.751421

-700.414139

-0.978461

-0.837845

0.0

23004.478333

24198.805592

23696.574667

23194.343742

54.369135


5行x 36列

暂时将其写入CSV。

1
data.to_csv('NIKKEI_ta.csv')

TA-Lib也可用于创建预测模型的特征。