TA-Libを用いた判断指標の算出（SMA, EMA, BB, RSI, MACD, ATR）

# ■ 概要
TA-Libを用いた判断指標（SMA, EMA, BB, RSI, MACD, ATR）の算出して
mplfinanceを用いて表示させる

一連のコードは↓

https://github.com/xx-z/cryptocurrency-analysis/blob/master/TA-Lib.ipynb

# ■ 環境
device : Mac book air 2022
yfinance : 0.2.12 (Released: Feb 16, 2023)
mplfinance : 0.12.9b7
pandas : 1.3.5

# ■ 実装
## ( 1 ) データ収集 & ローソク足の表示
#### ●データ収集
yfinanceでビットコインのデータを収集しました。
データの詳細としては、2022年の日足のデータを収集しました。
```
!pip install yfinance
```
```python
#yfinanceとpandasをインポート
import yfinance as yf
import pandas as pd

#取得するデータ詳細
name = 'BTC-USD'
start = '2022-01-01'
end = '2022-12-31'

#データのダウンロード
df = yf.download(tickers=name, start=start, end=end)
df.head(3)
```
| Date       | Open         | High         | Low          | Close        | Adj Close    | Volume       |
|------------|--------------|--------------|--------------|--------------|--------------|--------------|
| 2022-01-01 | 46311.746094 | 47827.312500 | 46288.484375 | 47686.812500 | 47686.812500 | 24582667004  |
| 2022-01-02 | 47680.925781 | 47881.406250 | 46856.937500 | 47345.218750 | 47345.218750 | 27951569547  |
| 2022-01-03 | 47343.542969 | 47510.726562 | 45835.964844 | 46458.117188 | 46458.117188 | 33071628362  |

yfinanceの詳細はこの記事をご覧ください。

https://qiita.com/__x__/items/6af051cb6b4017a52bd5

#### ●ローソク足の表示
```
!pip install mplfinance
```
```python
import mplfinance as mpf

mpf.plot(df, type="candle", figratio = (4, 2), volume=True, style="yahoo")
```
<img width="600" alt="mplfinance_1" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1582644/19f513f0-b4d1-d681-c783-2d2d65532879.png">

mplfinanceの詳細はこの記事をご覧ください。

https://qiita.com/__x__/items/7d461042b138796664c6

## ( 2 ) Ta-Libをインストールする
```
!pip install TA-Lib
```

## ( 3 ) SMA（移動平均線）
#### ●SMAを算出する
```python
import talib
import numpy as np

df['SMA20'] = talib.SMA(np.array(df['Close']), 20)
df['SMA50'] = talib.SMA(np.array(df['Close']), 50)
df['SMA100'] =  talib.SMA(np.array(df['Close']), 100)
df[['SMA20', 'SMA50', 'SMA100']].tail(3)
```
| Date       | SMA20        | SMA50        | SMA100       |
|------------|--------------|--------------|--------------|
| 2022-12-29 | 16945.709570 | 16821.523574 | 18212.409941 |
| 2022-12-30 | 16919.402637 | 16801.839863 | 18192.961797 |
| 2022-12-31 | 16891.567773 | 16792.103926 | 18164.301250 |

#### ●SMAをmplfinanceを使って表示させる
```python
sma = [ mpf.make_addplot(df['SMA20'], color="red"),
        mpf.make_addplot(df['SMA50'], color="blue"),
        mpf.make_addplot(df['SMA100'], color="green")]

mpf.plot(df, type="candle", figratio = (4, 2), style="yahoo", addplot=sma)
```
<img width="600" alt="mplfinance_1" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1582644/7e737767-3880-41b3-e9ee-b1cbfff81523.png">

## ( 4 ) EMA（指数平滑移動平均線）
#### ●EMAを算出する
```python
df['EMA20'] = talib.EMA(np.array(df['Close']), 20)
df['EMA50'] = talib.EMA(np.array(df['Close']), 50)
df['EMA100'] =  talib.EMA(np.array(df['Close']), 100)
df[['EMA20', 'EMA50', 'EMA100']].tail(3)
```
| Date       | EMA20        | EMA50        | EMA100       |
|------------|--------------|--------------|--------------|
| 2022-12-29 | 16857.778948 | 17269.299841 | 18345.835924 |
| 2022-12-30 | 16833.474851 | 17243.154197 | 18311.316123 |
| 2022-12-31 | 16806.238779 | 17215.873488 | 18276.388993 |

#### ●EMAをmplfinanceを使って表示させる
```python
ema = [ mpf.make_addplot(df['EMA20'], color="red"),
        mpf.make_addplot(df['EMA50'], color="blue"),
        mpf.make_addplot(df['EMA100'], color="green")]

mpf.plot(df, type="candle", figratio = (4, 2), style="yahoo", addplot=ema)
```
<img width="600" alt="mplfinance_1" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1582644/a2743b13-9e27-2c67-ed8c-09c82c972949.png">

## ( 5 ) BB（ボリンジャーバンド）
#### ●BBを算出する
```python
bb_period = 10
sigma = 2
matype = 0

df['BB_up'], df['BB_middle'], df['BB_down'] = talib.BBANDS(np.array(df['Close']), bb_period, sigma, sigma, matype)
df[['BB_up', 'BB_middle', 'BB_down']].tail(3)
```
| Date       | BB_up        | BB_middle    | bb_down      |
|------------|--------------|--------------|--------------|
| 2022-12-29 | 17008.412859 | 16787.276953 | 16566.141048 |
| 2022-12-30 | 16987.531515 | 16756.905078 | 16526.278641 |
| 2022-12-31 | 16987.470883 | 16729.901172 | 16472.331461 |

#### ●BBをmplfinanceを使って表示させる
```python
bb = [ mpf.make_addplot(df['BB_up'], color="blue"),
        mpf.make_addplot(df['BB_middle'], color="red"),
        mpf.make_addplot(df['BB_down'], color="blue")]

mpf.plot(df, type="candle", figratio = (4, 2), style="yahoo", addplot=bb)
```
<img width="600" alt="mplfinance_1" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1582644/0f8e7170-9f11-1257-09c7-01cee95bf8ea.png">

## ( 6 ) RSI（相対力指数）
#### ●RSIを算出する
```python
rsi_period = 14

df['RSI'] = talib.RSI(df['Close'])
df['RSI_SMA14'] = talib.SMA(np.array(df['RSI']), rsi_period)
df[['RSI', 'RSI_SMA14']].tail(3)
```
| Date       | RSI        | RSI_SMA14  |
|------------|------------|------------|
| 2022-12-29 | 44.125248  | 45.155021  |
| 2022-12-30 | 43.408369  | 45.219953  |
| 2022-12-31 | 42.380864  | 45.054581  |

#### ●RSIをmplfinanceを使って表示させる
```python
rsi = [ mpf.make_addplot(df['RSI'], color="purple",panel=1),
        mpf.make_addplot(df['RSI_SMA14'], color="green",panel=1)]

mpf.plot(df, type="candle", figratio = (4, 2), style="yahoo", addplot=rsi)
```
<img width="600" alt="mplfinance_1" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1582644/c93790af-72bf-6d4c-c9fe-211fc787a48f.png">

## ( 7 ) MACD（移動平均収束拡散手法）
#### ●MACDを算出する
```python
fast_period = 12
slow_period = 26
signal_period = 9

df["MACD"], df["MACD_signal"], df["MACD_hist"] = talib.MACD(df['Close'], fast_period, slow_period, signal_period)
df[['MACD', 'MACD_signal', 'MACD_hist']].tail(3)
```
| Date       | MACD         | MACD_signal  | MACD_hist   |
|------------|--------------|--------------|-------------|
| 2022-12-29 | -125.238723  | -110.046230  | -15.192493  |
| 2022-12-30 | -130.839866  | -114.204957  | -16.634909  |
| 2022-12-31 | -138.131805  | -118.990327  | -19.141478  |

#### ●MACDをmplfinanceを使って表示させる
```python
macd = [
    mpf.make_addplot(df["MACD"], color='blue', panel=1),
    mpf.make_addplot(df["MACD_signal"], color='red', panel=1),
    mpf.make_addplot(df["MACD_hist"],type='bar', panel=1)
]

mpf.plot(df, type="candle", figratio = (4, 2), style="yahoo", addplot=macd)
```
<img width="600" alt="mplfinance_1" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1582644/6e07371c-6100-9fb3-6dea-e50594326554.png">

## ( 8 ) ATR
#### ●ATRを算出する
```python
atr_period = 3
df["ATR"] = talib.ATR(np.array(df['High']), np.array(df['Low']), np.array(df['Close']), timeperiod=atr_period)
df[['ATR']].tail(3)
```
| Date       | ATR         |
|------------|-------------|
| 2022-12-29 | 197.939283  |
| 2022-12-30 | 210.277230  |
| 2022-12-31 | 177.340419  |

#### ●ATRをmplfinanceを使って表示させる
```python
atr = [mpf.make_addplot(df["ATR"], color='blue', panel=1)]

mpf.plot(df, type="candle", figratio = (4, 2), style="yahoo", addplot=atr)
```
<img width="600" alt="mplfinance_1" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1582644/5f41c11f-d4aa-11a6-422c-3cb5a0b1d992.png">

# ■ 参考文献
https://github.com/TA-Lib/ta-lib-python