[約定履歴取得]
get_trades(exchange=Exchange.BitMEX, ut_from=None, ut_to=None,
reverse=False, tstype="UTS")
[自注文の約定履歴取得]
get_executions(exchange=Exchange.BitMEX, ut_from=None, ut_to=None,
reverse=False, tstype="UTS")
[証拠金推移取得(bitFlyerのみ)]
get_collaterals(ut_from=None, ut_to=None, reverse=False, tstype="UTS")
[OHLCV取得]
get_ohlcv(exchange=Exchange.BitMEX, period="1m", count=1000,
reverse=False, tstype="UTS")
[四本値取得]
get_ohlcv_col(exchange=Exchange.BitMEX, period="1m", col="close",
count=1000, reverse=False)
[オーダーブック取得]
get_orderbook(exchange=Exchange.BitMEX, depth=10)
[価格情報取得]
get_ticker(exchange=Exchange.BitMEX)
[ポジション情報]
get_position(exchange=Exchange.BitMEX)
[残高情報取得]
get_balance(exchange=Exchange.BitMEX)
[約定履歴から指定期間のOHLCVを生成]
trades_to_ohlcv(df, period="1H", label="left")
[OHLCVを指定期間にリサンプリング]
resample_ohlcv(df, period="1H", label="left")
[DataFrameのtimestamp列を指定period単位に丸める]
round_timestamp(df, col="index", period=60)
[DataFrameをソート]
sort_df(df, by="index", ascending=True, reset_index=False)
[DataFrameを反転]
reverse_df(df, axis="index", reset_index=False)
[日付変換]
dt_to_ut(dt, unit="s")
ut_to_dt(ut, unit="s", tz=None)
dt_to_str(dt, fmt="%Y-%m-%dT%H:%M:%S.%fZ")
str_to_dt(str_dt, fmt="%Y-%m-%dT%H:%M:%S.%fZ", tz=None)
# coding: utf-8
from datetime import datetime
import time, pytz
import pandas as pd
import numpy as np
from data_tools import Exchange as exc # 取引所Enumクラス
from data_tools import DataTools as dtl # データ取得・編集ツールクラス
get_trades(exchange=Exchange.BitMEX, ut_from=None, ut_to=None,
reverse=False, tstype="UTS")
[@param]
exchange(Exchange(enum)) 処理対象取引所
ut_from(int) 取得開始 UnixTime (None : 1時間前時刻)
ut_to(int) 取得終了 UnixTime (None : 現在時刻)
reverse(bool) 並び順(True:新->古, False:古->新)
tstype(string) timestampデータ型
"UTMS":UnixTime(ミリ秒)
"UTS" :UnixTime(秒)
"DT" :datetime
"STMS":日付文字列(%Y-%m-%dT%H:%M:%S.%fZ)
"STS" :日付文字列(%Y-%m-%dT%H:%M:%S)
[return]
DataFrame
columns=["timestamp", "side", "price", "size"]
# 取得期間
to_time = int(time.time()) # 現在時刻UnixTime
from_time = to_time - 5 * 60 # 5分前UnixTime
# BitMEX
df_trades_bm = dtl.get_trades(exchange=exc.BitMEX, ut_from=from_time, ut_to=to_time,
reverse=False, tstype="UTS")
print("[BitMEX trades: %d]" % len(df_trades_bm.index))
display(df_trades_bm.head())
# bitFlyer
df_trades_bf = dtl.get_trades(exchange=exc.bitFlyer, ut_from=from_time, ut_to=to_time,
reverse=True, tstype="DT")
print("[bitFlyer trades: %d]" % len(df_trades_bf.index))
display(df_trades_bf.head())
Success API request. time: 2018-10-26 14:43:02.760000+00:00 get_trades: 215 RateLimit: 299/300 Reset: 1540564988 [BitMEX trades: 215]
/usr/local/lib/python3.6/site-packages/bravado_core/spec.py:271: Warning: guid format is not registered with bravado-core! category=Warning,
timestamp | side | price | size | |
---|---|---|---|---|
0 | 1540564686 | Sell | 6395.5 | 2000 |
1 | 1540564689 | Sell | 6395.5 | 300 |
2 | 1540564693 | Sell | 6395.5 | 20000 |
3 | 1540564693 | Buy | 6396.0 | 4000 |
4 | 1540564695 | Sell | 6395.5 | 500 |
Find last_id(522825692). Now getting trades. count:500 time:2018-10-26T14:42:50.513 Now getting trades. count:1000 time:2018-10-26T14:42:25.327 Now getting trades. count:1500 time:2018-10-26T14:42:04.903 Now getting trades. count:2000 time:2018-10-26T14:41:38.453 Now getting trades. count:2500 time:2018-10-26T14:41:20.827 Now getting trades. count:3000 time:2018-10-26T14:41:10.157 Now getting trades. count:3500 time:2018-10-26T14:40:59.877 Now getting trades. count:4000 time:2018-10-26T14:40:53.033 Now getting trades. count:4500 time:2018-10-26T14:40:38.36 Now getting trades. count:5000 time:2018-10-26T14:40:25.313 Now getting trades. count:5500 time:2018-10-26T14:40:10.91 Now getting trades. count:6000 time:2018-10-26T14:39:49.783 Now getting trades. count:6500 time:2018-10-26T14:39:24.287 Now getting trades. count:7000 time:2018-10-26T14:39:07.207 Now getting trades. count:7500 time:2018-10-26T14:38:47.677 Now getting trades. count:8000 time:2018-10-26T14:38:34.413 Now getting trades. count:8500 time:2018-10-26T14:38:16.257 Now getting trades. count:9000 time:2018-10-26T14:38:03.07 Completed getting trades. count:9000 time:2018-10-26T14:38:03.07 [bitFlyer trades: 8891]
timestamp | side | price | size | |
---|---|---|---|---|
0 | 2018-10-26 14:43:05.747000+00:00 | SELL | 733462.0 | 0.01 |
1 | 2018-10-26 14:43:05.637000+00:00 | SELL | 733462.0 | 0.10 |
2 | 2018-10-26 14:43:05.603000+00:00 | SELL | 733462.0 | 0.42 |
3 | 2018-10-26 14:43:05.603000+00:00 | SELL | 733469.0 | 0.07 |
4 | 2018-10-26 14:43:05.543000+00:00 | BUY | 733477.0 | 0.01 |
get_executions(exchange=Exchange.BitMEX, ut_from=None, ut_to=None,
reverse=False, tstype="UTS")
[@param]
exchange(Exchange(enum)) 処理対象取引所
ut_from(int) 取得開始 UnixTime (None : 1時間前時刻)
ut_to(int) 取得終了 UnixTime (None : 現在時刻)
reverse(bool) 並び順(True:新->古, False:古->新)
tstype(string) timestampデータ型
"UTMS":UnixTime(ミリ秒)
"UTS" :UnixTime(秒)
"DT" :datetime
"STMS":日付文字列(%Y-%m-%dT%H:%M:%S.%fZ)
"STS" :日付文字列(%Y-%m-%dT%H:%M:%S)
[return]
DataFrame
columns=["timestamp", "side", "price", "size", "comm"]
# 取得期間
to_time = int(time.time()) # 現在時刻UnixTime
from_time = to_time - 5 * 60 # 5分前UnixTime
# BitMEX
df_exec_bm = dtl.get_executions(exchange=exc.BitMEX, ut_from=from_time, ut_to=to_time,
reverse=False, tstype="UTS")
print("[BitMEX exec: %d]" % len(df_exec_bm.index))
display(df_exec_bm.head())
# bitFlyer
df_exec_bf = dtl.get_executions(exchange=exc.bitFlyer, ut_from=from_time, ut_to=to_time,
reverse=False, tstype="DT")
print("[bitFlyer exec: %d]" % len(df_exec_bf.index))
display(df_exec_bf.head())
[BitMEX exec: 0]
timestamp | side | price | size | comm |
---|
Find last_id(521683412). Now getting execs. count:500 time:2018-10-26T00:39:34.94 Completed getting execs. count:500 time:2018-10-26T00:39:34.94 [bitFlyer exec: 9]
timestamp | side | price | size | comm | |
---|---|---|---|---|---|
0 | 2018-10-26 14:40:00.957000+00:00 | BUY | 733101.0 | 0.300000 | 0.0 |
1 | 2018-10-26 14:41:04.483000+00:00 | SELL | 733327.0 | 0.300000 | 0.0 |
2 | 2018-10-26 14:41:19.563000+00:00 | SELL | 733457.0 | 0.250383 | 0.0 |
3 | 2018-10-26 14:41:20.670000+00:00 | SELL | 733457.0 | 0.023894 | 0.0 |
4 | 2018-10-26 14:41:20.827000+00:00 | SELL | 733457.0 | 0.025723 | 0.0 |
get_collaterals(ut_from=None, ut_to=None, reverse=False, tstype="UTS")
[@param]
ut_from(int) 取得開始 UnixTime (None : 1時間前時刻)
ut_to(int) 取得終了 UnixTime (None : 現在時刻)
reverse(bool) 並び順(True:新->古, False:古->新)
tstype(string) timestampデータ型
"UTMS":UnixTime(ミリ秒)
"UTS" :UnixTime(秒)
"DT" :datetime
"STMS":日付文字列(%Y-%m-%dT%H:%M:%S.%fZ)
"STS" :日付文字列(%Y-%m-%dT%H:%M:%S)
[return]
DataFrame
columns=["timestamp", "change", "amount"]
# 取得期間
to_time = int(time.time()) # 現在時刻UnixTime
from_time = to_time - 5 * 60 # 5分前UnixTime
# bitFlyer
df_coll_bf = dtl.get_collaterals(ut_from=from_time, ut_to=to_time,
reverse=False, tstype="UTS")
print("[bitFlyer coll: %d]" % len(df_coll_bf.index))
display(df_coll_bf.head())
Find last_id(17270). Now getting colls. count:500 time:2018-10-25T08:23:40.553 Completed getting colls. count:500 time:2018-10-25T08:23:40.553 [bitFlyer coll: 3]
timestamp | change | amount | |
---|---|---|---|
0 | 1540564864 | 68.0 | 76468.0 |
1 | 1540564898 | 68.0 | 76536.0 |
2 | 1540564936 | 68.0 | 76604.0 |
get_ohlcv(exchange=Exchange.BitMEX, period="1m", count=1000,
reverse=False, tstype="UTS")
[@param]
exchange(Exchange(enum)) 処理対象取引所
period(string) 時間足(1m, 3m, 5m, 15m, 30m,
1h, 2h, 3h, 4h, 6h, 8h, 12h, 1d)
count(int) 取得行数
reverse(bool) 並び順(True:新->古, False:古->新)
tstype(string) timestampデータ型
"UTMS":UnixTime(ミリ秒)
"UTS" :UnixTime(秒)
"DT" :datetime
"STMS":日付文字列(%Y-%m-%dT%H:%M:%S.%fZ)
"STS" :日付文字列(%Y-%m-%dT%H:%M:%S)
[return]
DataFrame
columns=["timestamp", "open", "high", "low", "close", "volume"]
# BitMEX
df_ohlcv_bm = dtl.get_ohlcv(exchange=exc.BitMEX, period="1m", count=1000,
reverse=False, tstype="UTS")
print("[BitMEX ohlcv: %d]" % len(df_ohlcv_bm.index))
display(df_ohlcv_bm.head())
# bitFlyer
df_ohlcv_bf = dtl.get_ohlcv(exchange=exc.bitFlyer, period="1h", count=200,
reverse=True, tstype="DT")
print("[bitFlyer ohlcv: %d]" % len(df_ohlcv_bf.index))
display(df_ohlcv_bf.head())
[BitMEX ohlcv: 1000]
timestamp | open | high | low | close | volume | |
---|---|---|---|---|---|---|
0 | 1540505040 | 6400.0 | 6400.5 | 6400.0 | 6400.0 | 60108 |
1 | 1540505100 | 6400.0 | 6400.5 | 6400.0 | 6400.5 | 86305 |
2 | 1540505160 | 6400.5 | 6400.5 | 6400.0 | 6400.0 | 27621 |
3 | 1540505220 | 6400.0 | 6400.5 | 6400.0 | 6400.0 | 21739 |
4 | 1540505280 | 6400.0 | 6400.5 | 6400.0 | 6400.0 | 88933 |
[bitFlyer ohlcv: 200]
timestamp | open | high | low | close | volume | |
---|---|---|---|---|---|---|
0 | 2018-10-26 14:00:00 | 735738 | 736562 | 733333 | 734167 | 9488.410 |
1 | 2018-10-26 13:00:00 | 736433 | 736673 | 735555 | 735750 | 5311.239 |
2 | 2018-10-26 12:00:00 | 736241 | 737100 | 736100 | 736443 | 4266.680 |
3 | 2018-10-26 11:00:00 | 736450 | 736798 | 735219 | 736241 | 5736.741 |
4 | 2018-10-26 10:00:00 | 738242 | 738613 | 736317 | 736463 | 10863.747 |
get_ohlcv_col(exchange=Exchange.BitMEX, period="1m", col="close",
count=1000, reverse=False)
[@param]
exchange(Exchange(enum)) 処理対象取引所
period(string) 時間足(1m, 3m, 5m, 15m, 30m,
1h, 2h, 3h, 4h, 6h, 8h, 12h, 1d)
col(string) 取得する列名
count(int) 取得行数
reverse(bool) 並び順(True:新->古, False:古->新)
[return]
ndarray
# 始値リスト取得(BitMEX)
ary_open_bm = dtl.get_ohlcv_col(exchange=exc.BitMEX, period="5m", col="open",
count=50, reverse=False)
print("[BitMEX open: %d]" % len(ary_open_bm))
print(ary_open_bm)
# 終値リスト取得(bitFlyer)
ary_close_bf = dtl.get_ohlcv_col(exchange=exc.bitFlyer, period="15m", col="close",
count=50, reverse=True)
print("\n[bitFlyer close: %d]" % len(ary_close_bf))
print(ary_close_bf)
[BitMEX open: 50] [6417.5 6418. 6418. 6417.5 6417.5 6418. 6419.5 6419. 6419. 6419. 6419. 6419. 6419.5 6418. 6417. 6416.5 6415. 6415. 6412.5 6411.5 6411.5 6411. 6411.5 6407. 6409.5 6409.5 6409. 6409. 6409.5 6409. 6409. 6409. 6411.5 6411.5 6409.5 6403.5 6399.5 6398. 6398. 6399. 6399. 6397.5 6397.5 6398. 6394.5 6392.5 6395.5 6395.5 6396. 6396. ] [bitFlyer close: 50] [733579 732688 734167 734419 735000 736152 735750 735975 735808 735791 736443 736405 736783 736637 736241 736400 736500 736420 736463 736796 736618 737826 738253 734165 733834 734145 734213 734252 734395 734288 735025 734969 734658 734913 734449 734366 734534 734812 734444 734042 733465 733500 733744 734471 734437 734868 734459 734196 734698 734724]
get_orderbook(exchange=Exchange.BitMEX, depth=10)
[@param]
exchange(Exchange(enum)) 処理対象取引所
depth(int) bids/asksをそれぞれ取得する件数
[return]
dict{"bids":[["price", "size"]], "asks":[["price", "size"]]}
# BitMEX
dct_ob_bm = dtl.get_orderbook(exchange=exc.BitMEX, depth=10)
print("[BitMEX orderbook asks:%d bids:%d]" %
(len(dct_ob_bm["asks"]), len(dct_ob_bm["bids"])))
display("[asks]", dct_ob_bm["asks"])
display("[bids]", dct_ob_bm["bids"])
# bitFlyer
dct_ob_bf = dtl.get_orderbook(exchange=exc.bitFlyer, depth=10)
print("[bitFlyer orderbook asks:%d bids:%d]" %
(len(dct_ob_bf["asks"]), len(dct_ob_bf["bids"])))
display("[asks]", dct_ob_bf["asks"])
display("[bids]", dct_ob_bf["bids"])
[BitMEX orderbook asks:10 bids:10]
'[asks]'
[[6400.5, 122503], [6400.0, 412642], [6399.5, 171084], [6399.0, 781949], [6398.5, 309483], [6398.0, 411556], [6397.5, 151627], [6397.0, 642472], [6396.5, 114618], [6396.0, 2138323]]
'[bids]'
[[6395.5, 6035872], [6395.0, 713876], [6394.5, 22274], [6394.0, 143074], [6393.5, 51997], [6393.0, 239023], [6392.5, 444565], [6392.0, 1483128], [6391.5, 165218], [6391.0, 1257555]]
[bitFlyer orderbook asks:10 bids:10]
'[asks]'
[[733468.0, 0.01], [733471.0, 0.10048893], [733473.0, 0.1], [733474.0, 0.05], [733475.0, 0.1], [733477.0, 0.47], [733478.0, 1.1029963], [733482.0, 0.01], [733489.0, 0.601], [733490.0, 5.10986665]]
'[bids]'
[[733435.0, 0.09135019], [733431.0, 0.2], [733430.0, 0.14], [733424.0, 0.2], [733423.0, 0.54], [733422.0, 0.561], [733420.0, 0.05], [733419.0, 0.2], [733417.0, 0.05], [733416.0, 0.21]]
get_ticker(exchange=Exchange.BitMEX)
[@param]
exchange(Exchange(enum)) 処理対象取引所
[return]
dict{"last", "bid", "ask"}
# BitMEX
ticker_bm = dtl.get_ticker(exchange=exc.BitMEX)
print("[BitMEX ticker] last:{last} bid:{bid} ask:{ask}".format(**ticker_bm))
# bitFlyer
ticker_bf = dtl.get_ticker(exchange=exc.bitFlyer)
print("[bitFlyer ticker] last:{last} bid:{bid} ask:{ask}".format(**ticker_bf))
[BitMEX ticker] last:6396.0 bid:6395.5 ask:6396.0
/usr/local/lib/python3.6/site-packages/bravado_core/spec.py:271: Warning: JSON format is not registered with bravado-core! category=Warning,
[bitFlyer ticker] last:733435.0 bid:733454.0 ask:733468.0
get_position(exchange=Exchange.BitMEX)
[@param]
exchange(Exchange(enum)) 処理対象取引所
[return]
dict{"size", "avr_entry"}
(size : Buy -> +, Sell -> -)
# BitMEX
pos_bm = dtl.get_position(exchange=exc.BitMEX)
print("[BitMEX position] size:{size} avr_entry:{avr_entry}".format(**pos_bm))
# bitFlyer
pos_bf = dtl.get_position(exchange=exc.bitFlyer)
print("[bitFlyer position] size:{size} avr_entry:{avr_entry}".format(**pos_bf))
[BitMEX position] size:0 avr_entry:0
/usr/local/lib/python3.6/site-packages/bravado_core/spec.py:271: Warning: JSON format is not registered with bravado-core! category=Warning,
[bitFlyer position] size:0.3 avr_entry:733394
get_balance(exchange=Exchange.BitMEX)
[@param]
exchange(Exchange(enum)) 処理対象取引所
[return]
float/int
# BitMEX
balance_bm = dtl.get_balance(exchange=exc.BitMEX)
print("[BitMEX balance] {}".format(balance_bm))
# bitFlyer
balance_bf = dtl.get_balance(exchange=exc.bitFlyer)
print("[bitFlyer balance] {}".format(balance_bf))
[BitMEX balance] 0.01169473 [bitFlyer balance] 47277
trades_to_ohlcv(df, period="1H", label="left")
[@param]
df(DataFrame) 約定履歴("timestamp":Datetimeindexであること)
columns=["timestamp", "price", "size"]
period(string) 指定期間("1S":1秒, "1T":1分, "1H":1時間, "1D":1日)
label(string) 変換後のtimestamp("left":期間開始時刻, "right":期間終了時刻)
[return]
DataFrame
columns=["timestamp", "open", "high", "low", "close", "volume"]
# BitMEX約定履歴取得
to_time = int(time.time()) # 現在時刻UnixTime
from_time = to_time - 10 * 60 # 10分前UnixTime
df_trades = dtl.get_trades(exchange=exc.BitMEX, ut_from=from_time, ut_to=to_time,
reverse=False, tstype="DT")
df_trades.set_index(["timestamp"], inplace=True)
display(df_trades.head())
# 約定履歴から1m足OHLCV生成
df_ohlcv = dtl.trades_to_ohlcv(df_trades, period="1T", label="left")
display(df_ohlcv.head())
/usr/local/lib/python3.6/site-packages/bravado_core/spec.py:271: Warning: guid format is not registered with bravado-core! category=Warning,
Success API request. time: 2018-10-26 14:42:35.904000+00:00 get_trades: 500 RateLimit: 296/300 Reset: 1540565008 Success API request. time: 2018-10-26 14:43:25.640000+00:00 get_trades: 538 RateLimit: 296/300 Reset: 1540565008
side | price | size | |
---|---|---|---|
timestamp | |||
2018-10-26 14:33:27.619000+00:00 | Sell | 6395.5 | 23000 |
2018-10-26 14:33:31.492000+00:00 | Sell | 6395.5 | 1000 |
2018-10-26 14:33:32.941000+00:00 | Sell | 6395.5 | 1000 |
2018-10-26 14:33:34.048000+00:00 | Buy | 6396.0 | 2131 |
2018-10-26 14:33:34.048000+00:00 | Buy | 6396.0 | 200 |
open | high | low | close | volume | |
---|---|---|---|---|---|
timestamp | |||||
2018-10-26 14:33:00+00:00 | 6395.5 | 6396.0 | 6395.5 | 6396.0 | 94363 |
2018-10-26 14:34:00+00:00 | 6395.5 | 6396.0 | 6395.5 | 6396.0 | 138123 |
2018-10-26 14:35:00+00:00 | 6395.5 | 6396.0 | 6395.5 | 6396.0 | 106647 |
2018-10-26 14:36:00+00:00 | 6395.5 | 6396.0 | 6395.5 | 6396.0 | 2994438 |
2018-10-26 14:37:00+00:00 | 6395.5 | 6396.0 | 6395.5 | 6395.5 | 168630 |
resample_ohlcv(df, period="1H", label="left")
[@param]
df(DataFrame) 変換するOHLCV("timestamp":Datetimeindexであること)
columns=["timestamp", "open", "high", "low", "close", "volume"]
period(string) 指定期間("1S":1秒, "1T":1分, "1H":1時間, "1D":1日)
label(string) 変換後のtimestamp("left":期間開始時刻, "right":期間終了時刻)
# BitMEX 1mOHLCV取得
df_ohlcv_1m = dtl.get_ohlcv(exchange=exc.BitMEX, period="1m", count=1000,
reverse=False, tstype="DT")
df_ohlcv_1m.set_index(["timestamp"], inplace=True)
display(df_ohlcv_1m.head())
# 5m足にリサンプリング
df_ohlcv_5m = dtl.resample_ohlcv(df_ohlcv_1m, period="5T", label="left")
display(df_ohlcv_5m.head())
open | high | low | close | volume | |
---|---|---|---|---|---|
timestamp | |||||
2018-10-25 22:04:00+00:00 | 6400.0 | 6400.5 | 6400.0 | 6400.0 | 60108 |
2018-10-25 22:05:00+00:00 | 6400.0 | 6400.5 | 6400.0 | 6400.5 | 86305 |
2018-10-25 22:06:00+00:00 | 6400.5 | 6400.5 | 6400.0 | 6400.0 | 27621 |
2018-10-25 22:07:00+00:00 | 6400.0 | 6400.5 | 6400.0 | 6400.0 | 21739 |
2018-10-25 22:08:00+00:00 | 6400.0 | 6400.5 | 6400.0 | 6400.0 | 88933 |
open | high | low | close | volume | |
---|---|---|---|---|---|
timestamp | |||||
2018-10-25 22:00:00+00:00 | 6400.0 | 6400.5 | 6400.0 | 6400.0 | 60108 |
2018-10-25 22:05:00+00:00 | 6400.0 | 6400.5 | 6400.0 | 6400.5 | 337667 |
2018-10-25 22:10:00+00:00 | 6400.5 | 6400.5 | 6400.0 | 6400.0 | 529709 |
2018-10-25 22:15:00+00:00 | 6400.0 | 6400.5 | 6400.0 | 6400.5 | 1173384 |
2018-10-25 22:20:00+00:00 | 6400.5 | 6403.5 | 6400.0 | 6403.0 | 3438894 |
round_timestamp(df, col="index", period=60)
[@param]
df(DataFrame) 変換するtimestampを持つDataFrame
col(string) 変換するtimestamp列名(indexは"index")
period(int) 丸める期間(秒)
# BitMEX約定履歴取得
to_time = int(time.time()) # 現在時刻UnixTime
from_time = to_time - 10 * 60 # 10分前UnixTime
df_trades = dtl.get_trades(exchange=exc.BitMEX, ut_from=from_time, ut_to=to_time,
reverse=False, tstype="DT")
display(df_trades.head())
# timestamp列の時刻を1m単位に丸める
dtl.round_timestamp(df_trades, col="timestamp", period=60)
display(df_trades.head())
/usr/local/lib/python3.6/site-packages/bravado_core/spec.py:271: Warning: guid format is not registered with bravado-core! category=Warning,
Success API request. time: 2018-10-26 14:42:35.904000+00:00 get_trades: 500 RateLimit: 294/300 Reset: 1540565008 Success API request. time: 2018-10-26 14:43:25.640000+00:00 get_trades: 538 RateLimit: 294/300 Reset: 1540565009
timestamp | side | price | size | |
---|---|---|---|---|
0 | 2018-10-26 14:33:27.619000+00:00 | Sell | 6395.5 | 23000 |
1 | 2018-10-26 14:33:31.492000+00:00 | Sell | 6395.5 | 1000 |
2 | 2018-10-26 14:33:32.941000+00:00 | Sell | 6395.5 | 1000 |
3 | 2018-10-26 14:33:34.048000+00:00 | Buy | 6396.0 | 2131 |
4 | 2018-10-26 14:33:34.048000+00:00 | Buy | 6396.0 | 200 |
timestamp | side | price | size | |
---|---|---|---|---|
0 | 2018-10-26 14:33:00+00:00 | Sell | 6395.5 | 23000 |
1 | 2018-10-26 14:33:00+00:00 | Sell | 6395.5 | 1000 |
2 | 2018-10-26 14:33:00+00:00 | Sell | 6395.5 | 1000 |
3 | 2018-10-26 14:33:00+00:00 | Buy | 6396.0 | 2131 |
4 | 2018-10-26 14:33:00+00:00 | Buy | 6396.0 | 200 |
sort_df(df, by="index", ascending=True, reset_index=False)
[@param]
df(DataFrame) ソートするDataFrame
by(string/list) ソートキー列名(複数はlistで指定)
ascending(bool/list) True:昇順, False:降順(by列数と項目数を合わせる)
reset_index(bool) インデックスリセット
# BitMEX 1mOHLCV取得
df_ohlcv = dtl.get_ohlcv(exchange=exc.BitMEX, period="1m", count=200,
reverse=False, tstype="DT")
display(df_ohlcv.head())
# 出来高が大きい順にソート
dtl.sort_df(df_ohlcv, by="volume", ascending=False, reset_index=False)
display(df_ohlcv.head())
timestamp | open | high | low | close | volume | |
---|---|---|---|---|---|---|
0 | 2018-10-26 11:24:00+00:00 | 6419.5 | 6419.5 | 6419.0 | 6419.0 | 181348 |
1 | 2018-10-26 11:25:00+00:00 | 6419.0 | 6419.5 | 6419.0 | 6419.0 | 128443 |
2 | 2018-10-26 11:26:00+00:00 | 6419.0 | 6419.5 | 6419.0 | 6419.0 | 424200 |
3 | 2018-10-26 11:27:00+00:00 | 6419.0 | 6419.5 | 6419.0 | 6419.0 | 30923 |
4 | 2018-10-26 11:28:00+00:00 | 6419.0 | 6419.5 | 6419.0 | 6419.0 | 233314 |
timestamp | open | high | low | close | volume | |
---|---|---|---|---|---|---|
131 | 2018-10-26 13:35:00+00:00 | 6399.5 | 6400.0 | 6392.0 | 6392.5 | 9848358 |
127 | 2018-10-26 13:31:00+00:00 | 6401.5 | 6401.5 | 6395.0 | 6398.0 | 9091067 |
125 | 2018-10-26 13:29:00+00:00 | 6409.0 | 6409.5 | 6402.0 | 6403.5 | 7367240 |
110 | 2018-10-26 13:14:00+00:00 | 6409.0 | 6411.5 | 6409.0 | 6411.5 | 5876442 |
62 | 2018-10-26 12:26:00+00:00 | 6411.0 | 6411.5 | 6408.5 | 6408.5 | 5747241 |
reverse_df(df, axis="index", reset_index=False)
[@param]
df(DataFrame) 反転するDataFrame
axis(string) "index":行反転, "column":列反転
reset_index(bool) インデックスリセット
# BitMEX 1mOHLCV取得
df_ohlcv = dtl.get_ohlcv(exchange=exc.BitMEX, period="1m", count=200,
reverse=False, tstype="DT")
display(df_ohlcv.head())
# 行反転
df_ohlcv_rev = dtl.reverse_df(df_ohlcv, axis="index", reset_index=False)
display(df_ohlcv_rev.head())
# 列反転
df_ohlcv_rev = dtl.reverse_df(df_ohlcv, axis="column", reset_index=False)
display(df_ohlcv_rev.head())
timestamp | open | high | low | close | volume | |
---|---|---|---|---|---|---|
0 | 2018-10-26 11:24:00+00:00 | 6419.5 | 6419.5 | 6419.0 | 6419.0 | 181348 |
1 | 2018-10-26 11:25:00+00:00 | 6419.0 | 6419.5 | 6419.0 | 6419.0 | 128443 |
2 | 2018-10-26 11:26:00+00:00 | 6419.0 | 6419.5 | 6419.0 | 6419.0 | 424200 |
3 | 2018-10-26 11:27:00+00:00 | 6419.0 | 6419.5 | 6419.0 | 6419.0 | 30923 |
4 | 2018-10-26 11:28:00+00:00 | 6419.0 | 6419.5 | 6419.0 | 6419.0 | 233314 |
timestamp | open | high | low | close | volume | |
---|---|---|---|---|---|---|
199 | 2018-10-26 14:43:00+00:00 | 6396.0 | 6396.0 | 6395.5 | 6396.0 | 51511 |
198 | 2018-10-26 14:42:00+00:00 | 6396.0 | 6396.0 | 6395.5 | 6396.0 | 133946 |
197 | 2018-10-26 14:41:00+00:00 | 6396.0 | 6396.0 | 6395.5 | 6396.0 | 246971 |
196 | 2018-10-26 14:40:00+00:00 | 6396.0 | 6396.0 | 6395.5 | 6396.0 | 169967 |
195 | 2018-10-26 14:39:00+00:00 | 6395.5 | 6396.0 | 6395.5 | 6396.0 | 73251 |
volume | close | low | high | open | timestamp | |
---|---|---|---|---|---|---|
0 | 181348 | 6419.0 | 6419.0 | 6419.5 | 6419.5 | 2018-10-26 11:24:00+00:00 |
1 | 128443 | 6419.0 | 6419.0 | 6419.5 | 6419.0 | 2018-10-26 11:25:00+00:00 |
2 | 424200 | 6419.0 | 6419.0 | 6419.5 | 6419.0 | 2018-10-26 11:26:00+00:00 |
3 | 30923 | 6419.0 | 6419.0 | 6419.5 | 6419.0 | 2018-10-26 11:27:00+00:00 |
4 | 233314 | 6419.0 | 6419.0 | 6419.5 | 6419.0 | 2018-10-26 11:28:00+00:00 |
[datetime -> UnixTime]
dt_to_ut(dt, unit="s")
[@param]
dt(datetime) 変換するdatetime
unit(string) "s"[sec], "ms"[msec]
[return]
int
# datetime現在時刻
dt_now = datetime.now(pytz.utc)
print(dt_now, type(dt_now))
# datetime -> UnixTime
ut_now = dtl.dt_to_ut(dt_now, unit="s")
print("[datetime -> UnixTime]", ut_now, type(ut_now))
# UnixTime -> datetime
dt_now2 = dtl.ut_to_dt(ut_now, unit="s", tz=None)
print("[UnixTime -> datetime]", dt_now2, type(dt_now2))
# datetime -> str
str_now = dtl.dt_to_str(dt_now, fmt="%Y/%m/%d %H:%M:%S")
print("[datetime -> str ]", str_now, type(str_now))
# str -> datetime
dt_now3 = dtl.str_to_dt(str_now, fmt="%Y/%m/%d %H:%M:%S", tz=pytz.utc)
print("[str -> datetime]", dt_now3, type(dt_now3))
2018-10-26 14:43:29.236776+00:00 <class 'datetime.datetime'> [datetime -> UnixTime] 1540565009 <class 'int'> [UnixTime -> datetime] 2018-10-26 14:43:29 <class 'datetime.datetime'> [datetime -> str ] 2018/10/26 14:43:29 <class 'str'> [str -> datetime] 2018-10-26 14:43:29+00:00 <class 'datetime.datetime'>
# BitMEX約定履歴取得
to_time = int(time.time()) # 現在時刻UnixTime
from_time = to_time - 60 # 1分前UnixTime
df_trades = dtl.get_trades(exchange=exc.BitMEX, ut_from=from_time, ut_to=to_time,
reverse=False, tstype="DT")
df = df_trades[["timestamp"]].copy()
display(df.head())
# datetime -> UnixTime
df["dt_to_ut"] = dtl.dt_to_ut(df["timestamp"], unit="s")
display(df.head())
# UnixTime -> datetime
df["ut_to_dt"] = dtl.ut_to_dt(df["dt_to_ut"], unit="s", tz=None)
display(df.head())
# datetime -> str
df["dt_to_str"] = dtl.dt_to_str(df["timestamp"], fmt="%Y/%m/%d %H:%M:%S")
display(df.head())
# str -> datetime
df["str_to_dt"] = dtl.str_to_dt(df["dt_to_str"], fmt="%Y/%m/%d %H:%M:%S", tz=pytz.utc)
display(df.head())
# データ型確認
for i in range(len(df.columns)):
print(df.columns[i], type(df.iloc[0, i]))
Success API request. time: 2018-10-26 14:43:28.381000+00:00 get_trades: 47 RateLimit: 293/300 Reset: 1540565010
/usr/local/lib/python3.6/site-packages/bravado_core/spec.py:271: Warning: guid format is not registered with bravado-core! category=Warning,
timestamp | |
---|---|
0 | 2018-10-26 14:42:31.918000+00:00 |
1 | 2018-10-26 14:42:31.918000+00:00 |
2 | 2018-10-26 14:42:31.918000+00:00 |
3 | 2018-10-26 14:42:31.918000+00:00 |
4 | 2018-10-26 14:42:32.319000+00:00 |
timestamp | dt_to_ut | |
---|---|---|
0 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 |
1 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 |
2 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 |
3 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 |
4 | 2018-10-26 14:42:32.319000+00:00 | 1540564952 |
timestamp | dt_to_ut | ut_to_dt | |
---|---|---|---|
0 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 |
1 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 |
2 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 |
3 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 |
4 | 2018-10-26 14:42:32.319000+00:00 | 1540564952 | 2018-10-26 14:42:32 |
timestamp | dt_to_ut | ut_to_dt | dt_to_str | |
---|---|---|---|---|
0 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 | 2018/10/26 14:42:31 |
1 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 | 2018/10/26 14:42:31 |
2 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 | 2018/10/26 14:42:31 |
3 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 | 2018/10/26 14:42:31 |
4 | 2018-10-26 14:42:32.319000+00:00 | 1540564952 | 2018-10-26 14:42:32 | 2018/10/26 14:42:32 |
timestamp | dt_to_ut | ut_to_dt | dt_to_str | str_to_dt | |
---|---|---|---|---|---|
0 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 | 2018/10/26 14:42:31 | 2018-10-26 14:42:31 |
1 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 | 2018/10/26 14:42:31 | 2018-10-26 14:42:31 |
2 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 | 2018/10/26 14:42:31 | 2018-10-26 14:42:31 |
3 | 2018-10-26 14:42:31.918000+00:00 | 1540564951 | 2018-10-26 14:42:31 | 2018/10/26 14:42:31 | 2018-10-26 14:42:31 |
4 | 2018-10-26 14:42:32.319000+00:00 | 1540564952 | 2018-10-26 14:42:32 | 2018/10/26 14:42:32 | 2018-10-26 14:42:32 |
timestamp <class 'pandas._libs.tslib.Timestamp'> dt_to_ut <class 'numpy.int64'> ut_to_dt <class 'pandas._libs.tslib.Timestamp'> dt_to_str <class 'str'> str_to_dt <class 'pandas._libs.tslib.Timestamp'>