3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Bitbank から ohlcv を取得しよう!

Last updated at Posted at 2022-03-09

この記事のメリット

取引開始時まで遡って収集することができる。

Bitbank は金融庁により暗号資産交換業者として登録されているためホワイトな取引を行うことが可能。

暗号資産交換業者登録一覧

注意点

この記事は投資・投機を推奨するものではありません。
ご自身でリスク等をしっかりと確認しご自身の判断で実行するようにお願いいたします。

ライブラリインポート

Python3
import time
import json
import requests
import numpy as np
import pandas as pd
from datetime import datetime, date, timedelta

通貨ペアと期間指定

Python3
headers = {'Content-Type': 'application/json'}
api_url_base = 'https://public.bitbank.cc'
pair = 'xym_jpy'
period = '1min'

# 通貨ペアの例 pair
 #btc_jpy, xrp_jpy, xrp_btc, ltc_jpy,
 #ltc_btc, eth_jpy, eth_btc, mona_jpy,
 #mona_btc, bcc_jpy, bcc_btc, xlm_jpy, xlm_btc

# 期間の例 period
 #1min, 5min, 15min, 30min, 1hour

開始日を設定して収集

Python3
df_xym = pd.DataFrame([])

s = 0
n=datetime(year=2021, month=10, day=5) + timedelta(days=s)


def api_ohlcv():
    api_url = '{0}/{1}/candlestick/{2}/{3}'.format(api_url_base, pair, period, timestamp)
    response = requests.get(api_url, headers=headers)

    if response.status_code == 200:
        ohlcv = json.loads(response.content.decode('utf-8'))['data']['candlestick'][0]['ohlcv']
        return ohlcv
    else:
        return None


# 日本時間の AM 9:00 前に実行しエラーが出た場合には下記の timedelta(days=1) を timedelta(days=2) に変更してください。

while (n<=(datetime.today() - timedelta(days=1))):
    n=datetime(year=2021, month=10, day=5) + timedelta(days=s)
    timestamp = "{0:%Y%m%d}".format(n)
    ohlcv = api_ohlcv()
    open, high, low, close, volume, timestamp = [],[],[],[],[],[]
    for i in ohlcv:
        open.append(i[0])
        high.append(i[1])
        low.append(i[2])
        close.append(i[3])
        volume.append(i[4])
        time_str = str(i[5])
        timestamp.append(datetime.fromtimestamp(int(time_str[:10])).strftime('%Y/%m/%d %H:%M:%S'))



    date_time_index = pd.to_datetime(timestamp) # convert to DateTimeIndex type
    df = pd.DataFrame({'Open': open, 'High': high, 'Low': low, 'Close': close, 'Volume': volume}, index=date_time_index)
    df_xym = pd.concat([df_xym, df], ignore_index=False)

    s += 1

カラム名変更、index を datetime に、float 型に変更

Python3
df_xym = df_xym.reset_index()
df_xym.columns = ['Time','Open','High','Low','Close','Volume']
df_xym.set_index('Time', inplace=True)
df_xym = df_xym.astype(float)
df_xym

# df へコピーする場合
df_xym = df_xym.reset_index()
df_xym.columns = ['Time','Open','High','Low','Close','Volume']
df_xym.set_index('Time', inplace=True)
df_xym = df_xym.astype(float)
df = df_xym.copy(deep=False) 
df

Screenshot 2022-02-22 at 17-10-31 Untitled ipynb (3) - JupyterLab.png

参考文献

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?