-
Notifications
You must be signed in to change notification settings - Fork 19
/
deribit_ws.py
218 lines (182 loc) · 7.2 KB
/
deribit_ws.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import asyncio
import websockets
import json
from credentials import client_id, client_secret, client_url
# create a websocket object
class WS_Client(object):
def __init__(self, client_id=None, client_secret=None):
self.client_id = client_id
self.client_secret = client_secret
self.client_url = client_url
self.json = {
"jsonrpc" : "2.0",
"id" : 1,
"method" : None,
}
# send an authentication request
async def private_api(self, request):
options = {
"grant_type" : "client_credentials",
"client_id" : self.client_id,
"client_secret" : self.client_secret
}
self.json["method"] = "public/auth"
self.json["params"] = options
async with websockets.connect(self.client_url) as websocket:
await websocket.send(json.dumps(self.json))
while websocket.open:
response = await websocket.recv()
# send a private subscription request
if "private/subscribe" in request:
await websocket.send(request)
while websocket.open:
response = await websocket.recv()
response = json.loads(response)
print(response)
# send a private method request
else:
await websocket.send(request)
response = await websocket.recv()
response = json.loads(response)
break
return response
# send a public method request
async def public_api(self, request):
async with websockets.connect(self.client_url) as websocket:
await websocket.send(request)
response = await websocket.recv()
response = json.loads(response)
return response
# send a public subscription request
async def public_sub(self, request):
async with websockets.connect(self.client_url) as websocket:
await websocket.send(request)
while websocket.open:
response = await websocket.recv()
response = json.loads(response)
print(response)
# create an asyncio event loop
def loop(self, api, request):
response = asyncio.get_event_loop().run_until_complete(
api(json.dumps(request)))
return response
def index(self, currency):
options = {"currency" : currency}
self.json["method"] = "public/get_index"
self.json["params"] = options
return self.loop(self.public_api, self.json)
def ticker(self, instrument_name):
options = {"instrument_name" : instrument_name}
self.json["method"] = "public/ticker"
self.json["params"] = options
return self.loop(self.public_api, self.json)
def buy(self, instrument_name, amount, order_type, reduce_only,
price=None, post_only=None):
options = {
"instrument_name" : instrument_name,
"amount" : amount,
"type" : order_type,
"reduce_only" : reduce_only,
}
if price:
options["price"] = price
if post_only:
options["post_only"] = post_only
self.json["method"] = "private/buy"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def stop_buy(self, instrument_name, trigger, amount, order_type, reduce_only,
stop_price=None, price=None):
options = {
"trigger" : trigger,
"instrument_name" : instrument_name,
"amount" : amount,
"type" : order_type,
"reduce_only": reduce_only,
}
if stop_price:
options["stop_price"] = stop_price
if price:
options["price"] = price
self.json["method"] = "private/buy"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def sell(self, instrument_name, amount, order_type, reduce_only,
price=None, post_only=None):
options = {
"instrument_name" : instrument_name,
"amount" : amount,
"type" : order_type,
"reduce_only" : reduce_only,
}
if price:
options["price"] = price
if post_only:
options["post_only"] = post_only
self.json["method"] = "private/sell"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def stop_sell(self, instrument_name, trigger, amount, order_type, reduce_only,
stop_price=None, price=None):
options = {
"trigger" : trigger,
"instrument_name" : instrument_name,
"amount" : amount,
"type" : order_type,
"reduce_only" : reduce_only,
}
if stop_price:
options["stop_price"] = stop_price
if price:
options["price"] = price
self.json["method"] = "private/sell"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def edit(self, order_id, amount, price):
options= {
"order_id" : order_id,
"amount" : amount,
"price" : price
}
self.json["method"] = "private/edit"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def cancel(self, order_id):
options = {"order_id" : order_id}
self.json["method"] = "private/cancel"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def cancel_all(self):
self.json["method"] = "private/cancel_all"
return self.loop(self.private_api, self.json)
def account_summary(self, currency):
options = {"currency" : currency}
self.json["method"] = "private/get_account_summary"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def get_position(self, instrument_name):
options = {"instrument_name" : instrument_name}
self.json["method"] = "private/get_position"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def public_trades(self):
options = {"channels" : ["trades.BTC-PERPETUAL.raw"]}
self.json["method"] = "public/subscribe"
self.json["params"] = options
return self.loop(self.public_sub, self.json)
def chart(self):
options = {"channels" : ["chart.trades.BTC-PERPETUAL.1"]}
self.json["method"] = "public/subscribe"
self.json["params"] = options
return self.loop(self.public_sub, self.json)
def user_trades(self):
options = {"channels" : ["user.trades.BTC-PERPETUAL.raw"]}
self.json["method"] = "private/subscribe"
self.json["params"] = options
return self.loop(self.private_api, self.json)
def user_orders(self):
options = {"channels" : ["user.orders.BTC-PERPETUAL.raw"]}
self.json["method"] = "private/subscribe"
self.json["params"] = options
return self.loop(self.private_api, self.json)
client = WS_Client(client_id, client_secret)