仮想通貨のOHLCVデータ
ビットコイン BTC_JPY の 1時間足 のOHLCVデータを用意します。
使用するデータの期間は 3年半 としました。
open high low close volume timestamp 2019-01-19 12:00:00+00:00 408644.0 408800.0 407998.0 408380.0 12.38 2019-01-19 13:00:00+00:00 408380.0 408380.0 407431.0 407900.0 32.53 2019-01-19 14:00:00+00:00 407900.0 408343.0 407231.0 407304.0 14.85 2019-01-19 15:00:00+00:00 408000.0 408050.0 407500.0 408000.0 2.65 2019-01-19 16:00:00+00:00 407300.0 408693.0 407257.0 408333.0 7.01 ... ... ... ... ... ... 2022-07-19 08:00:00+00:00 2998857.0 3021886.0 2985959.0 3019666.0 156.30 2022-07-19 09:00:00+00:00 3019224.0 3024654.0 3001833.0 3015632.0 92.79 2022-07-19 10:00:00+00:00 3016245.0 3041685.0 3001117.0 3024848.0 133.01 2022-07-19 11:00:00+00:00 3025721.0 3056504.0 3011697.0 3021942.0 204.96 2022-07-19 12:00:00+00:00 3022319.0 3055000.0 3021862.0 3051209.0 214.94
通常の価格の移動平均線
TA-Lib を使って、仮想通貨の価格そのままの SMA を算出します。
Windows11にTA-Libをインストールimport talib # SMAを算出する。 periods = [10, 200] for period in periods: df['SMA_' + str(period)] = talib.SMA(df['close'], timeperiod=period) print(df.loc[:, ['close', 'SMA_10', 'SMA_200']])
close SMA_10 SMA_200 timestamp 2019-01-19 12:00:00+00:00 408380.0 NaN NaN 2019-01-19 13:00:00+00:00 407900.0 NaN NaN 2019-01-19 14:00:00+00:00 407304.0 NaN NaN 2019-01-19 15:00:00+00:00 408000.0 NaN NaN 2019-01-19 16:00:00+00:00 408333.0 NaN NaN ... ... ... ... 2022-07-19 08:00:00+00:00 3019666.0 3039166.0 2850317.680 2022-07-19 09:00:00+00:00 3015632.0 3030700.0 2851316.580 2022-07-19 10:00:00+00:00 3024848.0 3024230.0 2852367.355 2022-07-19 11:00:00+00:00 3021942.0 3021643.1 2853413.625 2022-07-19 12:00:00+00:00 3051209.0 3021362.5 2854622.170
対数価格の移動平均線
仮想通貨の価格の対数価格を算出し、対数価格の SMA を算出します。
# 対数価格を算出する。 df['close_log'] = df['close'].apply(np.log1p) # SMAを算出する。 periods = [10, 200] for period in periods: df['LOG_SMA_' + str(period)] = talib.SMA(df['close_log'], timeperiod=period) print(df.loc[:, ['close', 'close_log', 'LOG_SMA_10', 'LOG_SMA_200']])
close close_log LOG_SMA_10 LOG_SMA_200 timestamp 2019-01-19 12:00:00+00:00 408380.0 12.919956 NaN NaN 2019-01-19 13:00:00+00:00 407900.0 12.918780 NaN NaN 2019-01-19 14:00:00+00:00 407304.0 12.917318 NaN NaN 2019-01-19 15:00:00+00:00 408000.0 12.919025 NaN NaN 2019-01-19 16:00:00+00:00 408333.0 12.919841 NaN NaN ... ... ... ... ... 2022-07-19 08:00:00+00:00 3019666.0 14.920657 14.927036 14.862091 2022-07-19 09:00:00+00:00 3015632.0 14.919320 14.924268 14.862433 2022-07-19 10:00:00+00:00 3024848.0 14.922372 14.922151 14.862793 2022-07-19 11:00:00+00:00 3021942.0 14.921411 14.921299 14.863152 2022-07-19 12:00:00+00:00 3051209.0 14.931049 14.921207 14.863565
対数価格の変動量の移動平均線
株式や仮想通貨の対数価格の増減は正規分布に従うということだったので、対数価格の変動量のSMAを算出します。
ついでに、通常価格の変動量のSMAも算出しておきます。
# 前のBarの値との差分値を算出する。 df['close_diff'] = df['close'] - df['close'].shift(1) df['close_log_diff'] = df['close_log'] - df['close_log'].shift(1) # SMAを算出する。 periods = [10, 200] for period in periods: df['DIFF_SMA_' + str(period)] = talib.SMA(df['close_diff'], timeperiod=period) df['LOG_DIFF_SMA_' + str(period)] = talib.SMA(df['close_log_diff'], timeperiod=period) print(df.loc[:, ['close', 'close_diff', 'DIFF_SMA_10', 'DIFF_SMA_200', 'close_log', 'close_log_diff', 'LOG_DIFF_SMA_10', 'LOG_DIFF_SMA_200']])
close close_diff DIFF_SMA_10 DIFF_SMA_200 close_log close_log_diff LOG_DIFF_SMA_10 LOG_DIFF_SMA_200 timestamp 2019-01-19 12:00:00+00:00 408380.0 NaN NaN NaN 12.919956 NaN NaN NaN 2019-01-19 13:00:00+00:00 407900.0 -480.0 NaN NaN 12.918780 -0.001176 NaN NaN 2019-01-19 14:00:00+00:00 407304.0 -596.0 NaN NaN 12.917318 -0.001462 NaN NaN 2019-01-19 15:00:00+00:00 408000.0 696.0 NaN NaN 12.919025 0.001707 NaN NaN 2019-01-19 16:00:00+00:00 408333.0 333.0 NaN NaN 12.919841 0.000816 NaN NaN ... ... ... ... ... ... ... ... ... 2022-07-19 08:00:00+00:00 3019666.0 20797.0 780.4 904.070 14.920657 0.006911 0.000259 0.000309 2022-07-19 09:00:00+00:00 3015632.0 -4034.0 -8466.0 998.900 14.919320 -0.001337 -0.002769 0.000343 2022-07-19 10:00:00+00:00 3024848.0 9216.0 -6470.0 1050.775 14.922372 0.003051 -0.002116 0.000360 2022-07-19 11:00:00+00:00 3021942.0 -2906.0 -2586.9 1046.270 14.921411 -0.000961 -0.000852 0.000359 2022-07-19 12:00:00+00:00 3051209.0 29267.0 -280.6 1208.545 14.931049 0.009638 -0.000092 0.000413
グラフで視覚化する
データは揃ったので、グラフ化してみます。
仮想通貨を長期で見るとかなり上下に変動していますが、対数価格の変動量のグラフを見ると、どの期間で切り取っても定常性があるので、データ量が多いほど助かる機械学習には向いているのかもしれません。