Skip to content

Instantly share code, notes, and snippets.

@nagishin
Created February 7, 2020 06:15
Show Gist options
  • Save nagishin/044d4639921cd0242ed5f50aaa453d72 to your computer and use it in GitHub Desktop.
Save nagishin/044d4639921cd0242ed5f50aaa453d72 to your computer and use it in GitHub Desktop.
DataClasses基本的な使い方.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "DataClasses基本的な使い方.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyM6ugIX7QWPTl2YC31dZf5v",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/nagishin/044d4639921cd0242ed5f50aaa453d72/dataclasses.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "XAJGH7QfVi25",
"colab_type": "code",
"colab": {}
},
"source": [
"!pip install bitmex\n",
"!pip install dataclasses_json"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "LAPHZBJb6Ki4",
"colab_type": "text"
},
"source": [
"### BitMEX約定履歴取得"
]
},
{
"cell_type": "code",
"metadata": {
"id": "MnSF2oLuYXiO",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 122
},
"outputId": "7fb38c88-0b7a-4b62-9e54-e7a3552ae8b4"
},
"source": [
"from datetime import datetime\n",
"import bitmex\n",
"\n",
"# API生成\n",
"api = bitmex.bitmex(test=False)\n",
"# 約定履歴取得(REST API)\n",
"trades, res = api.Trade.Trade_get(symbol=\"XBTUSD\", count=10, endTime=datetime.utcnow()).result()\n",
"\n",
"# 参考表示\n",
"print(trades)"
],
"execution_count": 71,
"outputs": [
{
"output_type": "stream",
"text": [
"[{'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}]\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"/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)\n",
" ref_dict['$ref'], '/'.join(path),\n",
"/usr/local/lib/python3.6/dist-packages/bravado_core/spec.py:345: Warning: guid format is not registered with bravado-core!\n",
" category=Warning,\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_fHEJKpS6SV3",
"colab_type": "text"
},
"source": [
"### Tradeクラス定義 (data class)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "CG7vNX2bV8LL",
"colab_type": "code",
"colab": {}
},
"source": [
"from datetime import datetime\n",
"from dataclasses import dataclass\n",
"\n",
"# Tradeクラス定義\n",
"@dataclass\n",
"class Trade:\n",
" timestamp: datetime = datetime.now()\n",
" price: float = 0\n",
" side: str = \"\"\n",
" size: float = 0"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "IFShxm8h6bH-",
"colab_type": "text"
},
"source": [
"### BitMEX約定履歴からTradeリスト生成"
]
},
{
"cell_type": "code",
"metadata": {
"id": "CsQGcd56rRId",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 221
},
"outputId": "5da05ab9-486c-4f9c-ee16-d2b6071bf850"
},
"source": [
"# それぞれの約定履歴からTradeクラスのインスタンスを生成\n",
"lst_trades = [Trade(t[\"timestamp\"], t[\"price\"], t[\"side\"], t[\"size\"]) for t in trades]\n",
"\n",
"# 参考表示\n",
"print(\"lst_trades = [\")\n",
"for i in range(10):\n",
" print(lst_trades[i], \",\")\n",
"print(\"]\")"
],
"execution_count": 73,
"outputs": [
{
"output_type": "stream",
"text": [
"lst_trades = [\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 948000, tzinfo=tzlocal()), price=9355.0, side='Sell', size=1716) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 952000, tzinfo=tzlocal()), price=9355.0, side='Sell', size=61) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 1, 202000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=100) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 827000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=3) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 932000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=60) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 80000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=1100) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 427000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=100) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 13, 809000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=1) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 520000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=66) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 593000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=121) ,\n",
"]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ReZpaaMq7e4X",
"colab_type": "text"
},
"source": [
"### Trade(data class)を辞書(dict)変換 / タプル(tuple)変換"
]
},
{
"cell_type": "code",
"metadata": {
"id": "shwH8hMRfBnx",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "f316744c-7477-4a6f-853a-de2bfd14ac67"
},
"source": [
"from dataclasses import asdict, astuple\n",
"\n",
"# Tradeをdict変換\n",
"dct_trade = asdict(lst_trades[i])\n",
"print(dic_trade)\n",
"\n",
"# Tradeをtuple変換\n",
"tpl_trade = astuple(lst_trades[i])\n",
"print(tpl_trade)"
],
"execution_count": 74,
"outputs": [
{
"output_type": "stream",
"text": [
"{'timestamp': datetime.datetime(2020, 1, 29, 21, 0, 58, 456000, tzinfo=tzlocal()), 'price': 9378.5, 'side': 'Buy', 'size': 26}\n",
"(datetime.datetime(2020, 1, 29, 22, 10, 14, 593000, tzinfo=tzlocal()), 9355.5, 'Buy', 121)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1gtLPOkZCLd1",
"colab_type": "text"
},
"source": [
"### Tradeリストをpickle保存 & 復元"
]
},
{
"cell_type": "code",
"metadata": {
"id": "TFFjT4QE-QG3",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 221
},
"outputId": "fa021b71-69ae-425e-8c75-560edc3c145c"
},
"source": [
"import pickle\n",
"\n",
"# Tradeリストをpickle保存 (バイナリ形式)\n",
"with open(\"trades.pickle\", mode=\"wb\") as f:\n",
" pickle.dump(lst_trades, f)\n",
"\n",
"# バイナリファイルをpickle復元 (読み込み)\n",
"with open(\"trades.pickle\", \"rb\") as f:\n",
" lst_trades2 = pickle.load(f)\n",
"\n",
"# 参考表示\n",
"print(\"lst_trades2 = [\")\n",
"for i in range(10):\n",
" print(lst_trades2[i], \",\")\n",
"print(\"]\")"
],
"execution_count": 75,
"outputs": [
{
"output_type": "stream",
"text": [
"lst_trades2 = [\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 948000, tzinfo=tzlocal()), price=9355.0, side='Sell', size=1716) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 0, 952000, tzinfo=tzlocal()), price=9355.0, side='Sell', size=61) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 1, 202000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=100) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 827000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=3) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 5, 932000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=60) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 80000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=1100) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 11, 427000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=100) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 13, 809000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=1) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 520000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=66) ,\n",
"Trade(timestamp=datetime.datetime(2020, 1, 29, 22, 10, 14, 593000, tzinfo=tzlocal()), price=9355.5, side='Buy', size=121) ,\n",
"]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fzpg6tRT7uX2",
"colab_type": "text"
},
"source": [
"### TradeListクラス定義 (data class & jsonシリアライズ)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "eWs7xI_CwigF",
"colab_type": "code",
"colab": {}
},
"source": [
"from typing import List\n",
"from dataclasses import dataclass, field\n",
"from dataclasses_json import dataclass_json\n",
"\n",
"# TradeListクラス定義\n",
"@dataclass_json\n",
"@dataclass\n",
"class TradeList:\n",
" trades: List[Trade] = field(default_factory=list)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "a6LnIvRe8IF3",
"colab_type": "text"
},
"source": [
"### TradeList生成 & json変換"
]
},
{
"cell_type": "code",
"metadata": {
"id": "NLuZ2C3PhGix",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"outputId": "54511db4-0369-41da-e578-bed80cecfeb7"
},
"source": [
"# TradeリストからTradeList生成\n",
"trade_list = TradeList(lst_trades)\n",
"\n",
"# TradeListをjson変換 \n",
"json_trades = trade_list.to_json(indent=4, ensure_ascii=False)\n",
"print(json_trades)"
],
"execution_count": 77,
"outputs": [
{
"output_type": "stream",
"text": [
"{\n",
" \"trades\": [\n",
" {\n",
" \"timestamp\": 1580335800.948,\n",
" \"price\": 9355.0,\n",
" \"side\": \"Sell\",\n",
" \"size\": 1716\n",
" },\n",
" {\n",
" \"timestamp\": 1580335800.952,\n",
" \"price\": 9355.0,\n",
" \"side\": \"Sell\",\n",
" \"size\": 61\n",
" },\n",
" {\n",
" \"timestamp\": 1580335801.202,\n",
" \"price\": 9355.5,\n",
" \"side\": \"Buy\",\n",
" \"size\": 100\n",
" },\n",
" {\n",
" \"timestamp\": 1580335805.827,\n",
" \"price\": 9355.5,\n",
" \"side\": \"Buy\",\n",
" \"size\": 3\n",
" },\n",
" {\n",
" \"timestamp\": 1580335805.932,\n",
" \"price\": 9355.5,\n",
" \"side\": \"Buy\",\n",
" \"size\": 60\n",
" },\n",
" {\n",
" \"timestamp\": 1580335811.08,\n",
" \"price\": 9355.5,\n",
" \"side\": \"Buy\",\n",
" \"size\": 1100\n",
" },\n",
" {\n",
" \"timestamp\": 1580335811.427,\n",
" \"price\": 9355.5,\n",
" \"side\": \"Buy\",\n",
" \"size\": 100\n",
" },\n",
" {\n",
" \"timestamp\": 1580335813.809,\n",
" \"price\": 9355.5,\n",
" \"side\": \"Buy\",\n",
" \"size\": 1\n",
" },\n",
" {\n",
" \"timestamp\": 1580335814.52,\n",
" \"price\": 9355.5,\n",
" \"side\": \"Buy\",\n",
" \"size\": 66\n",
" },\n",
" {\n",
" \"timestamp\": 1580335814.593,\n",
" \"price\": 9355.5,\n",
" \"side\": \"Buy\",\n",
" \"size\": 121\n",
" }\n",
" ]\n",
"}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dMzjRVsf9qn_",
"colab_type": "text"
},
"source": [
"### jsonをTradeList変換"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ZbvqE3J1846K",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"outputId": "b6edef2f-ea78-4aab-9c75-388505b38ddd"
},
"source": [
"# json_tradesをTradeListに変換 \n",
"trade_list2 = TradeList.from_json(json_trades)\n",
"print(trade_list2)"
],
"execution_count": 78,
"outputs": [
{
"output_type": "stream",
"text": [
"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)])\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment