!pip install bitmex
from inspect import currentframe
import numpy as np
import pandas as pd
def chk_print(data: object, count: int=0) -> None:
"""【オブジェクトチェック用 整形出力】
* 引数オブジェクト内容を見やすく整形してprint出力する
* オブジェクト名 = 値 (型, 要素数)
Args:
data (object): 表示するオブジェクト
count (int, optional): 配列, リストなどの場合に表示件数を指定 (0:全件表示)
"""
if data is None:
return
name = {id(v):k for k,v in currentframe().f_back.f_locals.items()}.get(id(data), "???")
if isinstance(data, list):
disp_count = len(data) if count is None or count == 0 else min(len(data), count)
print(f"{name} = [")
for i in range(disp_count):
print(" " + repr(data[i]) + ",")
if len(data) > disp_count:
print(" ...")
print(f"] (type = {type(data)}, len = {len(data)})\n")
elif isinstance(data, dict):
print(name + " = {")
for k,v in data.items():
print(" " + repr(k) + " : " + repr(v) + ",")
print("}" + f" (type = {type(data)}, items = {len(data.keys())})\n")
elif isinstance(data, np.ndarray) or isinstance(data, pd.core.series.Series):
disp_count = len(data) if count is None or count == 0 else min(len(data), count)
print(f"{name} =")
print(repr(data[:disp_count]))
if len(data) > disp_count:
print("...")
print(f"(type = {type(data)}, len = {len(data)})\n")
elif isinstance(data, pd.core.frame.DataFrame):
disp_count = len(data.index) if count is None or count == 0 else min(len(data.index), count)
print(f"{name} =")
print(data.head(disp_count))
if len(data.index) > disp_count:
print("...")
print(f"(type = {type(data)}, table = row:{len(data.index)} * col:{len(data.columns)})\n")
else:
print(f"{name} = {repr(data)} (type = {type(data)})\n")
int_val = 1
chk_print(int_val)
float_val = 0.5
chk_print(float_val)
str_val = "value"
chk_print(str_val)
int_val = 1 (type = <class 'int'>) float_val = 0.5 (type = <class 'float'>) str_val = 'value' (type = <class 'str'>)
from datetime import datetime
import bitmex
# API生成
api = bitmex.bitmex(test=False)
# 約定履歴取得
trades, res = api.Trade.Trade_get(symbol="XBTUSD", count=500, endTime=datetime.utcnow()).result()
chk_print(trades, 5)
# オーダーブック取得
#boards, res = api.OrderBook.OrderBook_getL2(symbol="XBTUSD", depth=100).result()
lst_trades = [[t["timestamp"], t["price"], t["side"], t["size"]] for t in trades]
chk_print(lst_trades, 5)
lst_trades = [ [datetime.datetime(2020, 2, 25, 18, 16, 29, 860000, tzinfo=tzlocal()), 9355.0, 'Buy', 1], [datetime.datetime(2020, 2, 25, 18, 16, 29, 901000, tzinfo=tzlocal()), 9355.0, 'Buy', 4], [datetime.datetime(2020, 2, 25, 18, 16, 30, 733000, tzinfo=tzlocal()), 9354.5, 'Sell', 31], [datetime.datetime(2020, 2, 25, 18, 16, 30, 874000, tzinfo=tzlocal()), 9354.5, 'Sell', 31], [datetime.datetime(2020, 2, 25, 18, 16, 30, 911000, tzinfo=tzlocal()), 9354.5, 'Sell', 2000], ... ] (type = <class 'list'>, len = 500)
dict_trade = trades[0]
chk_print(dict_trade)
dict_trade = { 'timestamp' : datetime.datetime(2020, 2, 25, 18, 16, 29, 860000, tzinfo=tzlocal()), 'symbol' : 'XBTUSD', 'side' : 'Buy', 'size' : 1, 'price' : 9355.0, 'tickDirection' : 'PlusTick', 'trdMatchID' : '6bccd723-2534-30f4-b5a1-e73be2147e0b', 'grossValue' : 10689, 'homeNotional' : 0.00010689, 'foreignNotional' : 1.0, } (type = <class 'dict'>, items = 10)
df_trades = pd.DataFrame(lst_trades, columns=["timestamp", "price", "side", "size"])
chk_print(df_trades, 5)
df_trades = timestamp price side size 0 2020-02-25 18:16:29.860000+00:00 9355.0 Buy 1 1 2020-02-25 18:16:29.901000+00:00 9355.0 Buy 4 2 2020-02-25 18:16:30.733000+00:00 9354.5 Sell 31 3 2020-02-25 18:16:30.874000+00:00 9354.5 Sell 31 4 2020-02-25 18:16:30.911000+00:00 9354.5 Sell 2000 ... (type = <class 'pandas.core.frame.DataFrame'>, table = row:500 * col:4)
sr_timestamp = df_trades.timestamp
chk_print(sr_timestamp, 5)
nd_timestamp = df_trades.timestamp.values
chk_print(nd_timestamp, 5)
sr_timestamp = 0 2020-02-25 18:16:29.860000+00:00 1 2020-02-25 18:16:29.901000+00:00 2 2020-02-25 18:16:30.733000+00:00 3 2020-02-25 18:16:30.874000+00:00 4 2020-02-25 18:16:30.911000+00:00 Name: timestamp, dtype: datetime64[ns, tzlocal()] ... (type = <class 'pandas.core.series.Series'>, len = 500) nd_timestamp = array(['2020-02-25T18:16:29.860000000', '2020-02-25T18:16:29.901000000', '2020-02-25T18:16:30.733000000', '2020-02-25T18:16:30.874000000', '2020-02-25T18:16:30.911000000'], dtype='datetime64[ns]') ... (type = <class 'numpy.ndarray'>, len = 500)