I wanted to make my own system trade, [[Wikipedia] Moving Average](https://ja.wikipedia.org/wiki/%E7%A7%BB%E5%8B%95%E5%B9%B3%E5%9D If you wrote% 87) or [Wikipedia] MACD yourself, I knew that there was a package for creating a nice technical index called TA-LIB without having to press it by hand. Describe the introduction method, environment construction method, and execution example
python
import ta-lib as ta
pperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
[Qiita] Save / Reconstruct / Duplicate virtual environment using Anaconda The above article was very easy to understand about the virtual environment using conda, so if you are not familiar with it, please go to see it.
Terminal
conda info -e
Execution example: Base only in Default environment
This time I will clone the Base environment and create a new environment
Terminal
conda create -n <New environment name> --clone <Environment name you want to duplicate>
Execution example
Terminal
conda activate <New environment name>
Execution example
Terminal
anaconda search -t conda ta-lib
Execution example: This time Platform: linux-64, Builds: py37_0 I wanted an environment, so make a note of the corresponding environment name "quantnet / ta-lib"
Note: For Mac OS and Windows users, please select "Quantopian / ta-lib"
Also, since "Quantopian / ta-lib" only supports up to Python 3.6,
It cannot be used with the clone of the base environment (Python3.7) by the above procedure.
Create a Python 3.6 environment with conda create -n <new environment name> python = 3.6
Terminal
anaconda show <Target Package name>
Execution example
Terminal
conda install --channel https://conda.anaconda.org/quantnet ta-lib
Terminal
conda list | grep ta-lib
Execution example
This time, we will get the daily data of Bitcoin in bitFlyer from Cryptwatch. [Cryptowatch] bifFlyer BITCFXJPY daily data
python
import requests
import pandas as pd
#1 day of BTC OHLC from Cryptowatch(14400 minutes)Create a function that gets and returns in a DataFrame
def get_btc_ohlc(period=14400):
#Request to Cryptowatch
response_data = requests.get("https://api.cryptowat.ch/markets/bitflyer/btcfxjpy/ohlc",params = { "periods" : period , "after" : 1})
response_data = response_data.json()
#Transformed into a DataFrame
df_tmp = pd.DataFrame(pd.DataFrame(response_data)[:1]['result'][0],
columns=['date', 'open', 'high', 'low', 'close', 'tmp1', 'tmp2']).drop(columns=['tmp1', 'tmp2'])
#Since date is int, convert to datetime
df_tmp['date'] = pd.to_datetime(df_tmp['date'].astype(int), unit='s')
return df_tmp
#Actually get BTC OHLC information
df_btc = get_btc_ohlc()
#Take a look at the acquired data
df_btc.head()
** [Caution] Information such as open and close when creating technical indicators with TA-LIB cannot be processed by DataFrame and must be an array **
python
import numpy as np
btc_open = np.array(df_btc['open'])
btc_close = np.array(df_btc['close'])
btc_high = np.array(df_btc['high'])
btc_low = np.array(df_btc['low'])
I wrote only the representative indicators that are often used, but there are really innumerable. Please refer to TA-LIB
python
# TA-Import LIB package
import talib as ta
#Calculate Bollinger Bands
upperband, middleband, lowerband = ta.BBANDS(btc_close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
#Calculate momentum
momentum=ta.MOM(btc_close,timeperiod=10)
#Calculate RSI
rsi = ta.RSI(btc_close, timeperiod=14)
# MACD
macd, macdsignal, macdhist = ta.MACD(btc_close, fastperiod=12, slowperiod=26, signalperiod=9)
# Stochastic
slowk, slowd = ta.STOCH(btc_high, btc_low, btc_close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
# ATR
real = ta.ATR(btc_high, btc_low, btc_close, timeperiod=14)
# EMA
ema25 = ta.EMA(btc_close, timeperiod=25)
ema75 = ta.EMA(btc_close, timeperiod=75)
#In order to combine with the original DataFrame, the array of created technical indicators is put together into a DataFrame.
array_tmp = np.c_[upperband, middleband, lowerband, momentum, rsi, macd, macdsignal, macdhist, slowk, slowd, real, ema25, ema75]
df_tmp = pd.DataFrame(data=array_tmp, columns=['upperband', 'middleband', 'lowerband', 'momentum', 'rsi', 'macd', 'macdsignal', 'macdhist', 'slowk', 'slowd', 'real', 'ema25', 'ema75'])
#Join by Dataframe,Index-based guy
df_btc = df_btc.merge(df_tmp, left_index=True, right_index=True)
#Take a look at the results
df_btc.tail(10)
So far, it's close to the preparation for automatic trading, so is it so much fun? There may be no one. Next time, I would like to make an article about plotting this result and backtesting for system trading (a test like cross-validating a model by separating train and test data in machine learning).
Recommended Posts