bitFlyerからビットコイン対円のティッカーを取得
かなり前からビットコインが熱かったけど放置してたら完全に乗り遅れた。 今更ながらビットコインの波に乗ろうと思う。 まずは対円の情報をbitFlyerから取得しようと思う。 簡単のため、pybitflyerというPythonライブラリーを使う。
コード
api = pybitflyer.API()
でAPIを取得して、
ticker = api.ticker(product_code='BTC_JPY')
でティッカーが取得できる。
keyもsecretもいらず非常にシンプルだが、
サーバーへ2秒置きにアクセスして10分間分のティッカーを取得し、
CSVファイルに保存して、読み込みデータを表示するまでのコードを書いた。
# coding: utf-8 import pybitflyer import pandas as pd from time import sleep from progressbar import ProgressBar SLEEP_SECOND = 2 # データ取得間隔(秒) N_MINUTE = 10 # データ取得時間(分) DIR_DATA = '../data/' # データ格納フォルダー PRODUCT_CODE = 'BTC_JPY' # 取得するデータ FILE_BTC_JPY = 'BTC_JPY_TICKER.csv' # TICKERファイル名 class Agent: def __init__(self): pass def main(self): self.get_ticker() df = self.read_ticker() """ TICKER """ def get_ticker(self): api = pybitflyer.API() tickers = [] count = 60 // SLEEP_SECOND * N_MINUTE # サーバーへのアクセス回数 pb = ProgressBar(max_value=count) for i in range(count): ticker = api.ticker(product_code=PRODUCT_CODE) tickers.append(ticker) sleep(SLEEP_SECOND) pb.update(i) # Update progressbar df = pd.DataFrame(tickers) df.to_csv(DIR_DATA+FILE_BTC_JPY, index=False) def read_ticker(self): df = pd.read_csv(DIR_DATA+FILE_BTC_JPY) df['timestamp'] = pd.to_datetime(df['timestamp']) # stringからdatetimeへ keys = ['timestamp', 'product_code', 'tick_id', \ 'best_ask', 'best_ask_size', 'best_bid', 'best_bid_size', \ 'total_ask_depth', 'total_bid_depth', 'ltp', \ 'volume', 'volume_by_product'] df = df[keys] # カラムを表示したい順に並べる df = df.set_index('timestamp') # timestampをindexに # debug print(df.head().to_string()) print('') print(df.tail().to_string()) print('') return df if __name__ == "__main__": Agent().main()
結果
上記のコードをagent.py
に保存して、一つ上にdataフォルダーを作って置けば、
実行すればdataフォルダー内にCSVファイルが出力されているはず。
それを読み込んだ結果が以下の通り。
なかなか2秒ごとにデータ取得できないもんですな。
> python .\agent.py product_code tick_id best_ask best_ask_size best_bid best_bid_size total_ask_depth total_bid_depth ltp volume volume_by_product timestamp 2017-11-15 13:57:22.963 BTC_JPY 16622557 811140.0 1.200000 810785.0 1.822747 1781.033755 4285.326827 810785.0 207454.303454 19592.381994 2017-11-15 13:57:25.343 BTC_JPY 16622606 811139.0 0.682363 810490.0 3.907947 1782.946859 4276.717027 810490.0 207466.718872 19603.633094 2017-11-15 13:57:27.250 BTC_JPY 16622669 810678.0 0.028423 810490.0 3.907947 1784.647044 4277.266086 810679.0 207473.906758 19604.627594 2017-11-15 13:57:28.953 BTC_JPY 16622708 811068.0 0.949000 810491.0 0.005685 1785.397923 4273.360839 810490.0 207478.447690 19605.862525 2017-11-15 13:57:31.313 BTC_JPY 16622740 811066.0 0.307494 810491.0 0.012506 1784.854531 4273.544345 811067.0 207487.503203 19607.357628 product_code tick_id best_ask best_ask_size best_bid best_bid_size total_ask_depth total_bid_depth ltp volume volume_by_product timestamp 2017-11-15 14:08:08.573 BTC_JPY 16637242 814999.0 0.800000 814900.0 0.167781 1714.455080 4223.328344 814900.0 206588.403105 19472.318694 2017-11-15 14:08:11.090 BTC_JPY 16637282 815000.0 5.638385 814697.0 0.320000 1714.380850 4225.357163 815000.0 206584.681053 19473.110742 2017-11-15 14:08:12.957 BTC_JPY 16637289 815000.0 4.738385 814698.0 1.120080 1713.758990 4225.517242 815000.0 206591.556565 19474.010742 2017-11-15 14:08:15.550 BTC_JPY 16637359 815000.0 1.123285 814800.0 0.160200 1710.768781 4219.641562 815000.0 206576.876997 19470.398019 2017-11-15 14:08:17.817 BTC_JPY 16637439 815000.0 0.784285 814800.0 0.160200 1700.492640 4216.927261 815000.0 206562.011497 19469.980019