2
4

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.

bybitのOHLCV(ローソク足+出来高)を全期間取得

Posted at

コード

データの取得

from datetime import datetime
import time

import requests
import pandas as pd

url = "https://api.bybit.com/v5/market/kline"
symbol = "BTCUSDT"
# spot,linear,inverse
category = "linear"
# 1,3,5,15,30,60,120,240,360,720,D,M,W
interval = 15

timestamp = int(time.time())
values = []

while True:
    params = {
        "symbol": symbol,
        "interval": interval,
        "category": category,
        "start": (timestamp - 200 * 60 * interval) * 1000,
        "end": timestamp * 1000,
        "limit": 200
    }

    response = requests.get(url, params=params)
    response_data = response.json()

    if len(response_data["result"]["list"]) == 0:
        break

    values += response_data["result"]["list"]
    timestamp -= 200 * 60 * interval

data = pd.DataFrame(values)
data.to_csv(f"bybit_{symbol}_{category}_{interval}.csv", index=False)

取得したデータ

スクリーンショット 2023-04-23 15.30.04.png

取得したデータの整形

data = pd.read_csv(f"bybit_{symbol}_{category}_{interval}.csv")

# カラム名を修正
data.columns = ["timestamp", "open", "high", "low", "close", "volume", "turnover"]
# タイムスタンプをdatetime型に変換
data["datetime"] = (data['timestamp'].astype(int)/1000).apply(datetime.fromtimestamp)
# 昇順に並び替え
data.sort_values("datetime", inplace=True)
# "datetime"をインデックスに設定
data.set_index("datetime", inplace=True)

整形後のデータ

スクリーンショット 2023-04-23 15.28.32.png

2
4
0

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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?