!pip install bitmex
!pip install dataclasses_json
from datetime import datetime
import bitmex
# API生成
api = bitmex.bitmex(test=False)
# 約定履歴取得(REST API)
trades, res = api.Trade.Trade_get(symbol="XBTUSD", count=10, endTime=datetime.utcnow()).result()
# 参考表示
print(trades)
[{'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 0, 948000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 1716, 'price': 9355.0, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': '6c965bec-92c7-3e24-f072-c242d0c10496', 'grossValue': 18342324, 'homeNotional': 0.18342324, 'foreignNotional': 1716.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 0, 952000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 61, 'price': 9355.0, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': '0179a113-ce4e-42f9-9c15-2467657e5f1a', 'grossValue': 652029, 'homeNotional': 0.00652029, 'foreignNotional': 61.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 1, 202000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 100, 'price': 9355.5, 'tickDirection': 'PlusTick', 'trdMatchID': '814212ce-fd45-42fb-541f-50a279d58c20', 'grossValue': 1068900, 'homeNotional': 0.010689, 'foreignNotional': 100.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 5, 827000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 3, 'price': 9355.5, 'tickDirection': 'ZeroPlusTick', 'trdMatchID': '13b61a8e-de25-28fd-62bb-90a50665de76', 'grossValue': 32067, 'homeNotional': 0.00032067, 'foreignNotional': 3.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 5, 932000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 60, 'price': 9355.5, 'tickDirection': 'ZeroPlusTick', 'trdMatchID': '14aac54a-6ed9-f5e7-1635-aa1d98d6934d', 'grossValue': 641340, 'homeNotional': 0.0064134, 'foreignNotional': 60.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 11, 80000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 1100, 'price': 9355.5, 'tickDirection': 'ZeroPlusTick', 'trdMatchID': '84a63131-c15b-5be2-0ca0-1223f3641202', 'grossValue': 11757900, 'homeNotional': 0.117579, 'foreignNotional': 1100.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 11, 427000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 100, 'price': 9355.5, 'tickDirection': 'ZeroPlusTick', 'trdMatchID': '73c97f9b-6b7d-caaa-c4aa-9104b217d1cf', 'grossValue': 1068900, 'homeNotional': 0.010689, 'foreignNotional': 100.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 13, 809000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 1, 'price': 9355.5, 'tickDirection': 'ZeroPlusTick', 'trdMatchID': '1c5f601f-e484-3c33-5ff9-4fd52f0c4d0a', 'grossValue': 10689, 'homeNotional': 0.00010689, 'foreignNotional': 1.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 14, 520000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 66, 'price': 9355.5, 'tickDirection': 'ZeroPlusTick', 'trdMatchID': '7d99554a-d165-f854-7821-a6837761048f', 'grossValue': 705474, 'homeNotional': 0.00705474, 'foreignNotional': 66.0}, {'timestamp': datetime.datetime(2020, 1, 29, 22, 10, 14, 593000, tzinfo=tzlocal()), 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 121, 'price': 9355.5, 'tickDirection': 'ZeroPlusTick', 'trdMatchID': '7e34244b-c5e8-bd81-faa9-e8b00eba5758', 'grossValue': 1293369, 'homeNotional': 0.01293369, 'foreignNotional': 121.0}]
/usr/local/lib/python3.6/dist-packages/swagger_spec_validator/validator20.py:53: SwaggerValidationWarning: Found "$ref: #/definitions/UserPreferences" with siblings that will be overwritten. See https://stackoverflow.com/a/48114924 for more information. (path #/definitions/User/properties/preferences) ref_dict['$ref'], '/'.join(path), /usr/local/lib/python3.6/dist-packages/bravado_core/spec.py:345: Warning: guid format is not registered with bravado-core! category=Warning,
from datetime import datetime
from dataclasses import dataclass
# Tradeクラス定義
@dataclass
class Trade:
timestamp: datetime = datetime.now()
price: float = 0
side: str = ""
size: float = 0
# それぞれの約定履歴からTradeクラスのインスタンスを生成
lst_trades = [Trade(t["timestamp"], t["price"], t["side"], t["size"]) for t in trades]
# 参考表示
print("lst_trades = [")
for i in range(10):
print(lst_trades[i], ",")
print("]")
lst_trades = [ Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 948000, tzinfo=tzlocal()), price=9355.0, side='Sell', size=1716) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 952000, tzinfo=tzlocal()), price=9355.0, side='Sell', size=61) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 1, 202000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=100) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 827000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=3) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 932000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=60) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 80000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=1100) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 427000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=100) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 13, 809000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=1) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 520000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=66) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 593000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=121) , ]
from dataclasses import asdict, astuple
# Tradeをdict変換
dct_trade = asdict(lst_trades[i])
print(dic_trade)
# Tradeをtuple変換
tpl_trade = astuple(lst_trades[i])
print(tpl_trade)
{'timestamp': datetime.datetime(2020, 1, 29, 21, 0, 58, 456000, tzinfo=tzlocal()), 'price': 9378.5, 'side': 'Buy', 'size': 26} (datetime.datetime(2020, 1, 29, 22, 10, 14, 593000, tzinfo=tzlocal()), 9355.5, 'Buy', 121)
import pickle
# Tradeリストをpickle保存 (バイナリ形式)
with open("trades.pickle", mode="wb") as f:
pickle.dump(lst_trades, f)
# バイナリファイルをpickle復元 (読み込み)
with open("trades.pickle", "rb") as f:
lst_trades2 = pickle.load(f)
# 参考表示
print("lst_trades2 = [")
for i in range(10):
print(lst_trades2[i], ",")
print("]")
lst_trades2 = [ Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 948000, tzinfo=tzlocal()), price=9355.0, side='Sell', size=1716) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 952000, tzinfo=tzlocal()), price=9355.0, side='Sell', size=61) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 1, 202000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=100) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 827000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=3) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 932000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=60) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 80000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=1100) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 427000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=100) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 13, 809000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=1) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 520000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=66) , Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 593000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=121) , ]
from typing import List
from dataclasses import dataclass, field
from dataclasses_json import dataclass_json
# TradeListクラス定義
@dataclass_json
@dataclass
class TradeList:
trades: List[Trade] = field(default_factory=list)
# TradeリストからTradeList生成
trade_list = TradeList(lst_trades)
# TradeListをjson変換
json_trades = trade_list.to_json(indent=4, ensure_ascii=False)
print(json_trades)
{ "trades": [ { "timestamp": 1580335800.948, "price": 9355.0, "side": "Sell", "size": 1716 }, { "timestamp": 1580335800.952, "price": 9355.0, "side": "Sell", "size": 61 }, { "timestamp": 1580335801.202, "price": 9355.5, "side": "Buy", "size": 100 }, { "timestamp": 1580335805.827, "price": 9355.5, "side": "Buy", "size": 3 }, { "timestamp": 1580335805.932, "price": 9355.5, "side": "Buy", "size": 60 }, { "timestamp": 1580335811.08, "price": 9355.5, "side": "Buy", "size": 1100 }, { "timestamp": 1580335811.427, "price": 9355.5, "side": "Buy", "size": 100 }, { "timestamp": 1580335813.809, "price": 9355.5, "side": "Buy", "size": 1 }, { "timestamp": 1580335814.52, "price": 9355.5, "side": "Buy", "size": 66 }, { "timestamp": 1580335814.593, "price": 9355.5, "side": "Buy", "size": 121 } ] }
# json_tradesをTradeListに変換
trade_list2 = TradeList.from_json(json_trades)
print(trade_list2)
TradeList(trades=[Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 948000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.0, side='Sell', size=1716), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 952000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.0, side='Sell', size=61), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 1, 202000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.5, side='Buy', size=100), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 827000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.5, side='Buy', size=3), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 932000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.5, side='Buy', size=60), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 80000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.5, side='Buy', size=1100), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 427000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.5, side='Buy', size=100), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 13, 809000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.5, side='Buy', size=1), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 520000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.5, side='Buy', size=66), Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 593000, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC')), price=9355.5, side='Buy', size=121)])