-
Notifications
You must be signed in to change notification settings - Fork 2
/
deribit_download.py
125 lines (102 loc) · 3.47 KB
/
deribit_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python3
from deribit_api import RestClient
import websocket
import json
import datetime
from my_model import *
import syslog
import time
import sys
import os
def float_or_m1(thing):
try:
return float(thing)
except:
return -1
def main():
client = RestClient("CSNAPH9dfTzg", "JGDPSFIQFZXNGROTOVTKW3RXBFWPNHA2")
client.i = 0
# get existing instruments
instruments = { i.ticker : i.id for i in Instrument.select() }
# create a loop in case we get disconnected
def on_message(ws, message):
if message == '{"id":-1,"result":"pong"}':
return
client.i = client.i+1
try:
data = json.loads(message)
except:
return
if 'notifications' not in data:
return
ms_out = data['msOut']
for n in data['notifications']:
if n['message'] != 'order_book_event':
continue
result = n['result']
instrument_name = result['instrument']
if instrument_name in instruments:
instrument = instruments[instrument_name]
else:
instrument = Instrument.create(ticker = instrument_name)
instruments[instrument_name] = instrument
update = OrderBookUpdate.create(ms_out=ms_out, instrument = instrument)
summary = Summary.create(
high = float_or_m1(result['high']),
low = float_or_m1(result['low']),
last = float_or_m1(result['last']),
update=update
)
for bid in result['bids']:
ob_entry = OrderBook.create(
is_bid = True,
cm = bid['cm'],
price=bid['price'],
qty = bid['quantity'],
update = update
)
for ask in result['bids']:
ob_entry = OrderBook.create(
is_bid = False,
cm = ask['cm'],
price= ask['price'],
qty = ask['quantity'],
update = update
)
if client.i % 100 == 0:
data = {
'id' :-1,
'action': '/api/v1/public/ping',
'arguments' : {}
}
data['sig'] = client.generate_signature(data['action'], data['arguments'])
ws.send(json.dumps(data))
def on_error(ws, error):
print(error)
def on_close(ws):
print('subscription closed!')
def on_open(ws):
data = {
"id": 5533,
"action": "/api/v1/private/subscribe",
"arguments": {
"instrument": ["options"],
"event": ["order_book"]
}
}
data['sig'] = client.generate_signature(data['action'], data['arguments'])
ws.send(json.dumps(data))
# websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://www.deribit.com/ws/api/v1/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open # not sure why this was on another line, it was like this in the example
ws.run_forever()
if __name__ == "__main__":
for i in range(10):
main()
try:
time.sleep(3)
except:
sys.exit()