#!/usr/bin/env python # coding: utf-8 # In[128]: import time, requests # 取得件数 count = 5 # 現在時刻と取得開始時刻 to_time = int(time.time()) from_time = to_time - (count - 1) * 60 # OHLCVデータ取得 param = {"period": 1, "from": from_time, "to": to_time} url = "https://www.bitmex.com/api/udf/history?symbol=XBTUSD&resolution={period}&from={from}&to={to}".format(**param) d = requests.get(url).json() # 取得結果を表示(json:dict0) display(d, type(d)) # requestで取得したjson結果はOHLCVの各列がリストとなっている print("timestamp :", d["t"], type(d["t"])) print("open :", d["o"], type(d["o"])) print("high :", d["h"], type(d["h"])) print("low :", d["l"], type(d["l"])) print("close :", d["c"], type(d["c"])) print("volume :", d["v"], type(d["v"])) # In[129]: # 各列リストからOHLCV二次元配列を作成 # for文 lst_ohlcv =[] for i in range(len(d["t"])): ohlcv = [d["t"][i], d["o"][i], d["h"][i], d["l"][i], d["c"][i], d["v"][i]] lst_ohlcv.append(ohlcv) display(lst_ohlcv, type(lst_ohlcv)) # リスト内包表記 lst_ohlcv2 = [list(ohlcv) for ohlcv in zip(d["t"], d["o"], d["h"], d["l"], d["c"], d["v"])] display(lst_ohlcv2, type(lst_ohlcv2)) # In[130]: # OHLCV二次元配列からデータ取得 print("最新足のOHLCVリストを取得") lst_newest = lst_ohlcv[-1] print(lst_newest, type(lst_newest)) print("\n最も古い終値を取得") oldest_close = lst_ohlcv[0][4] print(oldest_close, type(oldest_close)) print("\n始値リストを取得") lst_open = [ohlcv[1] for ohlcv in lst_ohlcv] print(lst_open, type(lst_open)) print("\n新⇒古順のOHLCVリストを取得(反転)") lst_reverse = lst_ohlcv[::-1] display(lst_reverse, type(lst_reverse)) # In[131]: import pandas as pd import numpy as np # リスト⇒Series sr_timestamp = pd.Series(d["t"]) sr_open = pd.Series(d["o"]) sr_high = pd.Series(d["h"]) sr_low = pd.Series(d["l"]) sr_close = pd.Series(d["c"]) sr_volume = pd.Series(d["v"]) print(sr_timestamp, type(sr_timestamp)) print(sr_open, type(sr_open)) print(sr_high, type(sr_high)) print(sr_low, type(sr_low)) print(sr_close, type(sr_close)) print(sr_volume, type(sr_volume)) # In[132]: # Series⇒リスト lst_t = sr_timestamp.tolist() lst_o = sr_open.tolist() lst_h = sr_high.tolist() lst_l = sr_low.tolist() lst_c = sr_close.tolist() lst_v = sr_volume.tolist() print("timestamp :", lst_t, type(lst_t)) print("open :", lst_o, type(lst_o)) print("high :", lst_h, type(lst_h)) print("low :", lst_l, type(lst_l)) print("close :", lst_c, type(lst_c)) print("volume :", lst_v, type(lst_v) ,"\n") # Series⇒ndarray nda_t = sr_timestamp.values nda_o = sr_open.values nda_h = sr_high.values nda_l = sr_low.values nda_c = sr_close.values nda_v = sr_volume.values print("timestamp :", nda_t, type(nda_t)) print("open :", nda_o, type(nda_o)) print("high :", nda_h, type(nda_h)) print("low :", nda_l, type(nda_l)) print("close :", nda_c, type(nda_c)) print("volume :", nda_v, type(nda_v)) # In[133]: from collections import OrderedDict # DataFrame作成 # OHLCV二次元配列⇒DataFrame df_ohlcv = pd.DataFrame(lst_ohlcv, columns=["timestamp", "open", "high", "low", "close", "volume"]) display(df_ohlcv, type(df_ohlcv)) # OrderedDict⇒DataFrame df_ohlcv2 = pd.DataFrame(OrderedDict({ "timestamp" : d["t"], "open" : d["o"], "high" : d["h"], "low" : d["l"], "close" : d["c"], "volume" : d["v"], })) display(df_ohlcv2, type(df_ohlcv2)) # In[134]: # DataFrameからデータ取得 print("最新(最終行)の終値を取得") last_close = df_ohlcv.iloc[-1, 4] print(last_close, type(last_close)) print("\n最も古い(先頭)行を取得") row_first = df_ohlcv.iloc[0] # Series display(row_first, type(row_first)) # リスト変換 lst_first = row_first.tolist() display(lst_first, type(lst_first)) print("終値列を取得") col_close = df_ohlcv["close"] # Series display(col_close, type(col_close)) # リスト変換 lst_close = col_close.tolist() display(lst_close, type(lst_close)) print("2~4行目の時刻, 始値, 終値列の取得") rng_toc = df_ohlcv.iloc[2:5, [0,1,4]] # DataFrame display(rng_toc, type(rng_toc)) # リスト変換 lst_toc = rng_toc.values.tolist() display(lst_toc, type(lst_toc)) # In[135]: # DataFrameインデックス print("timestampをインデックスに設定") df_ohlcv.set_index("timestamp", inplace=True) display(df_ohlcv) print("timestampインデックスを解除") df_ohlcv.reset_index(inplace=True) display(df_ohlcv) # In[136]: # DataFrameデータ追加/削除 # 最終行をもう1行追加 df_new = pd.DataFrame([[0, 1, 2, 3, 4, 5]], columns=["timestamp", "open", "high", "low", "close", "volume"]) df_ohlcv = df_ohlcv.append(df_new, ignore_index=True) display(df_ohlcv) # 最終行を1行削除 df_ohlcv.drop(len(df_ohlcv) - 1, axis=0, inplace=True) display(df_ohlcv) # datetime列を追加 df_ohlcv["datetime"] = pd.to_datetime(df_ohlcv["timestamp"], unit="s") display(df_ohlcv) # datetime列を削除 df_ohlcv.drop("datetime", axis=1, inplace=True) display(df_ohlcv) # In[137]: # DataFrame列の並び替え df_ohlcv = df_ohlcv[["open", "high", "low", "close", "volume", "timestamp"]] display(df_ohlcv) # DataFrame列名変更 df_ohlcv.rename( columns={ "open" : "始値", "high" : "高値", "low" : "安値", "close" : "終値", "volume" : "出来高", "timestamp" : "タイムスタンプ", }, inplace=True) display(df_ohlcv) # In[138]: # DataFrame行反転 df_ohlcv = df_ohlcv.iloc[::-1] display(df_ohlcv) # DataFrame列反転 df_ohlcv = df_ohlcv.iloc[:, ::-1] display(df_ohlcv)