├── images
├── buy.png
├── cancel.png
├── edit.png
├── index.png
├── sell.png
├── ticker.png
├── stop_buy.png
├── cancel_all.png
├── credentials.png
├── stop_sell.png
├── get_position.png
└── account_summary.png
├── credentials.py
├── requirements.txt
├── LICENSE
├── deribit_ws.py
└── README.md
/images/buy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/buy.png
--------------------------------------------------------------------------------
/images/cancel.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/cancel.png
--------------------------------------------------------------------------------
/images/edit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/edit.png
--------------------------------------------------------------------------------
/images/index.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/index.png
--------------------------------------------------------------------------------
/images/sell.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/sell.png
--------------------------------------------------------------------------------
/images/ticker.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/ticker.png
--------------------------------------------------------------------------------
/images/stop_buy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/stop_buy.png
--------------------------------------------------------------------------------
/images/cancel_all.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/cancel_all.png
--------------------------------------------------------------------------------
/images/credentials.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/credentials.png
--------------------------------------------------------------------------------
/images/stop_sell.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/stop_sell.png
--------------------------------------------------------------------------------
/images/get_position.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/get_position.png
--------------------------------------------------------------------------------
/images/account_summary.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jimmy-sha256/deribit_websocket_v2/HEAD/images/account_summary.png
--------------------------------------------------------------------------------
/credentials.py:
--------------------------------------------------------------------------------
1 | client_id = 'Enter_Client_ID_Here'
2 | client_secret = 'Enter_Client_Secret_Here'
3 | client_url = 'wss://test.deribit.com/ws/api/v2'
4 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | backcall==0.1.0
2 | decorator==4.4.0
3 | ipython==7.16.3
4 | ipython-genutils==0.2.0
5 | jedi==0.15.1
6 | parso==0.5.1
7 | pexpect==4.7.0
8 | pickleshare==0.7.5
9 | prompt-toolkit==2.0.10
10 | ptyprocess==0.6.0
11 | Pygments==2.7.4
12 | six==1.12.0
13 | traitlets==4.3.3
14 | wcwidth==0.1.7
15 | websockets==9.1
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 James Alexander
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/deribit_ws.py:
--------------------------------------------------------------------------------
1 | import asyncio
2 | import websockets
3 | import json
4 |
5 | from credentials import client_id, client_secret, client_url
6 |
7 | # create a websocket object
8 | class WS_Client(object):
9 | def __init__(self, client_id=None, client_secret=None):
10 | self.client_id = client_id
11 | self.client_secret = client_secret
12 | self.client_url = client_url
13 | self.json = {
14 | "jsonrpc" : "2.0",
15 | "id" : 1,
16 | "method" : None,
17 | }
18 |
19 | # send an authentication request
20 | async def private_api(self, request):
21 | options = {
22 | "grant_type" : "client_credentials",
23 | "client_id" : self.client_id,
24 | "client_secret" : self.client_secret
25 | }
26 |
27 | self.json["method"] = "public/auth"
28 | self.json["params"] = options
29 |
30 | async with websockets.connect(self.client_url) as websocket:
31 | await websocket.send(json.dumps(self.json))
32 | while websocket.open:
33 | response = await websocket.recv()
34 |
35 | # send a private subscription request
36 | if "private/subscribe" in request:
37 | await websocket.send(request)
38 | while websocket.open:
39 | response = await websocket.recv()
40 | response = json.loads(response)
41 | print(response)
42 |
43 | # send a private method request
44 | else:
45 | await websocket.send(request)
46 | response = await websocket.recv()
47 | response = json.loads(response)
48 | break
49 | return response
50 |
51 | # send a public method request
52 | async def public_api(self, request):
53 | async with websockets.connect(self.client_url) as websocket:
54 | await websocket.send(request)
55 | response = await websocket.recv()
56 | response = json.loads(response)
57 | return response
58 |
59 | # send a public subscription request
60 | async def public_sub(self, request):
61 | async with websockets.connect(self.client_url) as websocket:
62 | await websocket.send(request)
63 | while websocket.open:
64 | response = await websocket.recv()
65 | response = json.loads(response)
66 | print(response)
67 |
68 | # create an asyncio event loop
69 | def loop(self, api, request):
70 | response = asyncio.get_event_loop().run_until_complete(
71 | api(json.dumps(request)))
72 | return response
73 |
74 | def index(self, currency):
75 | options = {"currency" : currency}
76 | self.json["method"] = "public/get_index"
77 | self.json["params"] = options
78 | return self.loop(self.public_api, self.json)
79 |
80 | def ticker(self, instrument_name):
81 | options = {"instrument_name" : instrument_name}
82 | self.json["method"] = "public/ticker"
83 | self.json["params"] = options
84 | return self.loop(self.public_api, self.json)
85 |
86 | def buy(self, instrument_name, amount, order_type, reduce_only,
87 | price=None, post_only=None):
88 | options = {
89 | "instrument_name" : instrument_name,
90 | "amount" : amount,
91 | "type" : order_type,
92 | "reduce_only" : reduce_only,
93 | }
94 |
95 | if price:
96 | options["price"] = price
97 | if post_only:
98 | options["post_only"] = post_only
99 |
100 | self.json["method"] = "private/buy"
101 | self.json["params"] = options
102 | return self.loop(self.private_api, self.json)
103 |
104 | def stop_buy(self, instrument_name, trigger, amount, order_type, reduce_only,
105 | stop_price=None, price=None):
106 | options = {
107 | "trigger" : trigger,
108 | "instrument_name" : instrument_name,
109 | "amount" : amount,
110 | "type" : order_type,
111 | "reduce_only": reduce_only,
112 | }
113 |
114 | if stop_price:
115 | options["stop_price"] = stop_price
116 | if price:
117 | options["price"] = price
118 |
119 | self.json["method"] = "private/buy"
120 | self.json["params"] = options
121 | return self.loop(self.private_api, self.json)
122 |
123 | def sell(self, instrument_name, amount, order_type, reduce_only,
124 | price=None, post_only=None):
125 | options = {
126 | "instrument_name" : instrument_name,
127 | "amount" : amount,
128 | "type" : order_type,
129 | "reduce_only" : reduce_only,
130 | }
131 |
132 | if price:
133 | options["price"] = price
134 | if post_only:
135 | options["post_only"] = post_only
136 |
137 | self.json["method"] = "private/sell"
138 | self.json["params"] = options
139 |
140 | return self.loop(self.private_api, self.json)
141 |
142 | def stop_sell(self, instrument_name, trigger, amount, order_type, reduce_only,
143 | stop_price=None, price=None):
144 | options = {
145 | "trigger" : trigger,
146 | "instrument_name" : instrument_name,
147 | "amount" : amount,
148 | "type" : order_type,
149 | "reduce_only" : reduce_only,
150 | }
151 |
152 | if stop_price:
153 | options["stop_price"] = stop_price
154 | if price:
155 | options["price"] = price
156 |
157 | self.json["method"] = "private/sell"
158 | self.json["params"] = options
159 | return self.loop(self.private_api, self.json)
160 |
161 | def edit(self, order_id, amount, price):
162 | options= {
163 | "order_id" : order_id,
164 | "amount" : amount,
165 | "price" : price
166 | }
167 |
168 | self.json["method"] = "private/edit"
169 | self.json["params"] = options
170 | return self.loop(self.private_api, self.json)
171 |
172 | def cancel(self, order_id):
173 | options = {"order_id" : order_id}
174 | self.json["method"] = "private/cancel"
175 | self.json["params"] = options
176 | return self.loop(self.private_api, self.json)
177 |
178 | def cancel_all(self):
179 | self.json["method"] = "private/cancel_all"
180 | return self.loop(self.private_api, self.json)
181 |
182 | def account_summary(self, currency):
183 | options = {"currency" : currency}
184 | self.json["method"] = "private/get_account_summary"
185 | self.json["params"] = options
186 | return self.loop(self.private_api, self.json)
187 |
188 | def get_position(self, instrument_name):
189 | options = {"instrument_name" : instrument_name}
190 | self.json["method"] = "private/get_position"
191 | self.json["params"] = options
192 | return self.loop(self.private_api, self.json)
193 |
194 | def public_trades(self):
195 | options = {"channels" : ["trades.BTC-PERPETUAL.raw"]}
196 | self.json["method"] = "public/subscribe"
197 | self.json["params"] = options
198 | return self.loop(self.public_sub, self.json)
199 |
200 | def chart(self):
201 | options = {"channels" : ["chart.trades.BTC-PERPETUAL.1"]}
202 | self.json["method"] = "public/subscribe"
203 | self.json["params"] = options
204 | return self.loop(self.public_sub, self.json)
205 |
206 | def user_trades(self):
207 | options = {"channels" : ["user.trades.BTC-PERPETUAL.raw"]}
208 | self.json["method"] = "private/subscribe"
209 | self.json["params"] = options
210 | return self.loop(self.private_api, self.json)
211 |
212 | def user_orders(self):
213 | options = {"channels" : ["user.orders.BTC-PERPETUAL.raw"]}
214 | self.json["method"] = "private/subscribe"
215 | self.json["params"] = options
216 | return self.loop(self.private_api, self.json)
217 |
218 | client = WS_Client(client_id, client_secret)
219 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Deribit Websocket API v2
2 |
3 | This is a python wrapper written to make it simple to connect to Deribit's JSON-RPC api v2 using websockets.
4 |
5 | #### Table of Contents
6 | 1. [Installation](#installation)
7 | 2. [Credentials](#credentials)
8 | 3. [Index Price](#index)
9 | 4. [Ticker Data](#ticker)
10 | 5. [Buy](#buy)
11 | 6. [Sell](#sell)
12 | 7. [Stop Buy](#stop_buy)
13 | 8. [Stop Sell](#stop_sell)
14 | 9. [Edit](#edit)
15 | 10. [Cancel](#cancel)
16 | 11. [Cancel All](#cancel_all)
17 | 12. [Account Summary](#account_summary)
18 | 13. [Get Position](#get_position)
19 |
20 |
21 |
22 | #### 1. Installation Ubuntu 18.04
23 |
24 | ```shell
25 | mkdir deribit_ws && cd deribit_ws
26 |
27 | git clone https://github.com/Jimmy-sha256/deribit_ws.git
28 |
29 | virtualenv --python=/usr/bin/python3.10 venv
30 |
31 | source /venv/bin/activate
32 |
33 | pip install -r requirements.txt
34 |
35 | ```
36 |
37 |
38 | #### 2. Credentials
39 |
40 | Add your deribit api access key and access secret to the credentials.py file:
41 | * https://www.deribit.com/main#/account?scrollTo=api
42 |
43 | Add either main-net or test-net websocket url to credentials.py file:
44 | * wss://testapp.deribit.com/ws/api/v2
45 | * wss://deribit.com/ws/api/v2
46 |
47 | 
48 |
49 |
50 |
51 | #### 3. Index Price
52 |
53 | ```shell
54 | index(currency)
55 | ```
56 |
57 | | Paramaters | Type | Description |
58 | |---------------------|------------|-----------------------------------------------------|
59 | | `currency ` | `string` | 'BTC', 'ETH' |
60 |
61 | https://docs.deribit.com/v2/#public-get_index
62 |
63 | 
64 |
65 |
66 |
67 | #### 4. Ticker Data
68 |
69 | `ticker(instrument_name)`
70 |
71 | | Paramaters | Type | Description |
72 | |---------------------|------------|------------------------------------------------------|
73 | | `instrument_name ` | `string` | 'BTC-PERPETUAL', 'ETH-PERPETUAL' |
74 |
75 | https://docs.deribit.com/v2/#public-ticker
76 |
77 | 
78 |
79 |
80 |
81 | #### 5. Buy
82 |
83 | `buy(instrument_name, amont, order_type, price, post_only)`
84 |
85 | | Paramaters | Type | Description |
86 | |---------------------|------------|-----------------------------------------------------|
87 | | `instrument_name ` | `string` | 'BTC-PERPETUAL', 'ETH-PERPETUAL' |
88 | | `amount ` | `int` | number of contracts to purchase |
89 | | `order_type ` | `string` | 'market', 'limit' |
90 | | `price ` | `int` | price to purchase contracts at |
91 | | `post_only ` | boolean | True / False |
92 |
93 | https://docs.deribit.com/v2/#private-buy
94 |
95 | 
96 |
97 |
98 |
99 | #### 6. Sell
100 |
101 | `sell(instrument_name, amont, order_type, price, post_only)`
102 |
103 | | Paramaters | Type | Description |
104 | |---------------------|------------|-----------------------------------------------------|
105 | | `instrument_name ` | `string` | 'BTC-PERPETUAL', 'ETH-PERPETUAL' |
106 | | `amount ` | `int` | number of contracts to purchase |
107 | | `order_type ` | `string` | 'market', 'limit' |
108 | | `price ` | `int` | price to purchase contracts at |
109 | | `post_only ` | boolean | True / False |
110 |
111 | https://docs.deribit.com/v2/#private-sell
112 |
113 | 
114 |
115 |
116 |
117 |
118 | #### 7. Stop Buy
119 |
120 | `stop_buy(instrument_name, trigger, amont, order_type, stop_price, price)`
121 |
122 | | Paramaters | Type | Description |
123 | |---------------------|------------|-----------------------------------------------------|
124 | | `instrument_name ` | `string` | 'BTC-PERPETUAL', 'ETH-PERPETUAL' |
125 | | `trigger ` | `string` | 'index_price', 'mark_price', 'last_price' |
126 | | `amount ` | `int` | number of contracts to purchase |
127 | | `order_type ` | `string` | 'stop_market', 'stop_limit' |
128 | | `stop_price ` | `int` | price at which order is triggered |
129 | | `price ` | `int` | price to purchase contracts at |
130 |
131 | https://docs.deribit.com/v2/#private-buy
132 |
133 | 
134 |
135 |
136 |
137 | #### 8. Stop Sell
138 |
139 | `stop_sell(instrument_name, trigger, amont, order_type, stop_price, price)`
140 |
141 | | Paramaters | Type | Description |
142 | |---------------------|------------|-----------------------------------------------------|
143 | | `instrument_name` | `string` | 'BTC-PERPETUAL', 'ETH-PERPETUAL' |
144 | | `trigger` | `string` | 'index_price', 'mark_price', 'last_price' |
145 | | `amount` | `int` | number of contracts to purchase |
146 | | `order_type` | `string` | 'stop_market', 'stop_limit' |
147 | | `stop_price` | `int` | price at which order is triggered |
148 | | `price` | `int` | price to purchase contracts at |
149 |
150 | https://docs.deribit.com/v2/#private-sell
151 |
152 | 
153 |
154 |
155 |
156 | #### 8. Edit
157 |
158 | `edit(order_id, amount, price)`
159 |
160 | | Paramaters | Type | Description |
161 | |---------------------|------------|-----------------------------------------------------|
162 | | `order_id` | `string` | id of order '3032588463' |
163 | | `amount` | `int` | edit number of contracts |
164 | | `price` | `int` | edit price of contracts |
165 |
166 | https://docs.deribit.com/v2/#private-edit
167 |
168 | 
169 |
170 |
171 |
172 | #### 8. Cancel
173 |
174 | `cancel(order_id)`
175 |
176 | | Paramaters | Type | Description |
177 | |---------------------|------------|-----------------------------------------------------|
178 | | `order_id` | `string` | id of order to be canceled '3032588463' |
179 |
180 | https://docs.deribit.com/v2/#private-cancel
181 |
182 | 
183 |
184 |
185 |
186 | #### 8. Cancel All
187 |
188 | `cancel_all()`
189 |
190 | _This method takes no parameters_
191 |
192 | https://docs.deribit.com/v2/#private-cancel_all
193 |
194 | 
195 |
196 |
197 |
198 | #### 8. Account Summary
199 |
200 | `account_summary(currency)`
201 |
202 | | Paramaters | Type | Description |
203 | |---------------------|------------|-----------------------------------------------------|
204 | | `currency` | `string` | 'BTC', 'ETH' |
205 |
206 | https://docs.deribit.com/v2/#private-get_account_summary
207 |
208 | 
209 |
210 |
211 |
212 | #### 8. Get Position
213 |
214 | `get_position(currency)`
215 |
216 | | Paramaters | Type | Description |
217 | |---------------------|------------|-----------------------------------------------------|
218 | | `instrument_name` | `string` | 'BTC-PERPETUAL', 'ETH-PERPETUAL' |
219 |
220 | https://docs.deribit.com/v2/#private-get_position
221 |
222 | 
223 |
--------------------------------------------------------------------------------