-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.py
431 lines (363 loc) · 11.8 KB
/
product.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
from dataclasses import dataclass
from typing import Dict, List, Optional, Union, TypeVar, Generic
from enum import Enum
from datetime import datetime, date, timezone
import btnl_client.hmac_utils as hmac_utils
from urllib.parse import urlparse
import requests
BASE_URL = "https://bitnomial.com/exchange/api/v1"
class BaseSymbol(Enum):
# only valid in env="prod"
BUI = "BUI"
BUS = "BUS"
HUP = "HUP"
# only valid in env="sandbox"
ZZZ = "ZZZ"
class ProductStatus(Enum):
Active = "active"
Forthcoming = "forthcoming"
Expired = "expired"
class ProductSpecType(Enum):
Future = "future"
Spread = "spread"
Option = "option"
@dataclass
class SpreadSpecLeg:
product_id: int
weight: int
@dataclass
class BaseProductSpec:
type: ProductSpecType
product_id: int
product_name: str
max_order_quantity: int
min_block_size: int
price_band_variation: int
price_limit_percentage: float
price_increment: int
first_trading_day: str
final_settle_time: str
daily_open_time: str
daily_settle_time: str
symbol: str
cqg_symbol: str
product_status: ProductStatus
base_symbol: BaseSymbol
@dataclass
class ProductFutureSpec(BaseProductSpec):
margin_unit: str
settlement_method: str
contract_size: int
contract_size_unit: str
price_quotation_unit: str
month: int
year: int
@dataclass
class ProductSpreadSpec(BaseProductSpec):
legs: List[SpreadSpecLeg]
@dataclass
class ProductOptionSpec(BaseProductSpec):
underlying_product: int
strike_price: float
option_type: str
ProductSpec = Union[ProductFutureSpec, ProductOptionSpec, ProductSpreadSpec]
class ProductType(Enum):
Future = "Future"
Spread = "Spread"
Option = "Option"
class Side(Enum):
Bid = "Bid"
Ask = "Ask"
class TimeInForce(Enum):
IOC = "IOC"
Day = "Day"
GTC = "GTC"
class OrderStatus(Enum):
Working = "Working"
Closed = "Closed"
class Liquidity(Enum):
Add = "Add"
Remove = "Remove"
SpreadLeg = "SpreadLeg"
@dataclass
class Order:
symbol: str
product_id: int
product_type: ProductType
id: int
connection_id: int
clearing_firm_code: str
account_id: str
open_ack_id: int
side: Side
price: int
quantity_requested: int
quantity_filled: int
status: OrderStatus
time_in_force: TimeInForce
@dataclass
class Fill:
symbol: str
product_id: int
product_type: ProductType
order_id: int
clearing_firm_code: str
time: datetime
connection_id: int
account_id: str
ack_id: int
side: Side
price: int
quantity_requested: int
quantity_filled: int
liquidity: Liquidity
class BlockTradeStatus(Enum):
Pending = "Pending"
Canceled = "Canceled"
Rejected = "Rejected"
Confirmed = "Confirmed"
Accepted = "Accepted"
@dataclass
class BlockTrade:
account_id: str
block_trade_id: int
counterparty_id: str
counterparty_email: Optional[str]
symbol: str
side: Side
price: int
quantity: int
exec_time: datetime
report_time: datetime
status: BlockTradeStatus
status_reason: Optional[str]
status_time: Optional[datetime]
@dataclass
class ProductData:
product_id: int
last_price_time: Optional[str]
last_price: Optional[float]
settlement_time: Optional[str]
settlement_price: Optional[float]
settlement_price_comment: Optional[str]
open_price: Optional[float]
high_price: Optional[float]
low_price: Optional[float]
close_price: Optional[float]
price_change: Optional[float]
volume: Optional[float]
notional_volume: Optional[float]
block_volume: Optional[float]
notional_block_volume: Optional[float]
price_limit_upper: float
price_limit_lower: float
open_interest: Optional[float]
open_interest_change: Optional[float]
class Ordering:
Asc = "asc"
Desc = "desc"
T = TypeVar("T")
P = TypeVar("P")
@dataclass
class Pagination(Generic[T, P]):
data: List[T]
pagination: P
@dataclass
class CursorInfo:
cursor: str
class BitnomialHttpClient:
base_url: str
env: str
def __init__(self, base_url=None, env=None):
self.base_url = base_url or BASE_URL
self.env = env or "prod"
def get_product_spec(
self, product_id, day=None, active=None, base_symbol=None
) -> ProductSpec:
url = self.base_url + f"/{self.env}/product/spec/{product_id}"
params = {"day": day, "active": active, "base_symbol": base_symbol}
response = requests.get(url, params=params)
spec = response.json()
spec_type = ProductSpecType(spec["type"])
product_spec: ProductSpec
if spec_type == ProductSpecType.Future:
product_spec = ProductFutureSpec(**spec)
elif spec_type == ProductSpecType.Spread:
product_spec = ProductSpreadSpec(**spec)
elif spec_type == ProductSpecType.Option:
product_spec = ProductOptionSpec(**spec)
else:
raise ValueError(f"Unexpected product spec type: {spec['type']}")
return product_spec
def get_product_specs(
self, day=None, active=None, base_symbol=None
) -> List[ProductSpec]:
url = self.base_url + f"/{self.env}/product/specs"
params = {"day": day, "active": active, "base_symbol": base_symbol}
response = requests.get(url, params=params)
product_specs: List[ProductSpec] = []
for spec in response.json():
spec_type = ProductSpecType(spec["type"])
if spec_type == ProductSpecType.Future:
product_specs.append(ProductFutureSpec(**spec))
elif spec_type == ProductSpecType.Spread:
product_specs.append(ProductSpreadSpec(**spec))
elif spec_type == ProductSpecType.Option:
product_specs.append(ProductOptionSpec(**spec))
else:
raise ValueError(f"Unexpected product spec type: {spec['type']}")
return product_specs
def get_product_data(
self, day=None, active=None, base_symbol=None
) -> List[ProductData]:
url = self.base_url + f"/{self.env}/product/data"
params = {"day": day, "active": active, "base_symbol": base_symbol}
response = requests.get(url, params=params)
return [ProductData(**data) for data in response.json()]
def get_product_datum(
self, product_id, day=None, active=None, base_symbol=None
) -> ProductData:
url = self.base_url + f"/{self.env}/product/data/{product_id}"
params = {"day": day, "active": active, "base_symbol": base_symbol}
response = requests.get(url, params=params)
product_data = response.json()
return ProductData(**product_data)
class AuthBitnomialHttpClient(BitnomialHttpClient):
connection_id: int
auth_token: str
def __init__(self, connection_id, auth_token, base_url=None, env=None):
self.connection_id = connection_id
self.auth_token = auth_token
super().__init__(base_url, env)
def get_fills(
self,
symbols: Optional[List[str]] = None,
product_ids: Optional[List[int]] = None,
product_types: Optional[List[ProductType]] = None,
clearing_firm_codes: Optional[List[str]] = None,
account_ids: Optional[List[str]] = None,
connection_ids: Optional[List[int]] = None,
day: Optional[date] = None,
limit=None,
begin_time=None,
end_time=None,
order=None,
cursor=None,
) -> Pagination[List[Fill], CursorInfo]:
method = "GET"
path = f"/{self.env}/fills"
url = self.base_url + path
params = {
"symbol": symbols,
"connection_id": connection_ids,
"cursor": cursor,
"order": order,
"begin_time": begin_time,
"end_time": end_time,
"limit": limit,
"day": day,
"account_id": account_ids,
"clearing_firm_code": clearing_firm_codes,
"product_type": product_types,
"product_id": product_ids,
}
headers = self.auth_headers(method, url, params)
response = requests.get(url, params=params, headers=headers)
fills = response.json()
return Pagination(**fills)
def get_orders(
self,
symbols: Optional[List[str]] = None,
product_ids: Optional[List[int]] = None,
product_types: Optional[List[ProductType]] = None,
clearing_firm_codes: Optional[List[str]] = None,
account_ids: Optional[List[str]] = None,
connection_ids: Optional[List[int]] = None,
day: Optional[date] = None,
limit=None,
begin_time=None,
end_time=None,
order=None,
cursor=None,
) -> Pagination[List[Fill], CursorInfo]:
method = "GET"
path = f"/{self.env}/orders"
url = self.base_url + path
params = {
"symbol": symbols,
"connection_id": connection_ids,
"product_id": product_ids,
"account_id": account_ids,
"clearing_firm_code": clearing_firm_codes,
"product_type": product_types,
"order": order,
"begin_time": begin_time,
"end_time": end_time,
"limit": limit,
"day": day,
"cursor": cursor,
}
headers = self.auth_headers(method, url, params)
response = requests.get(url, params=params, headers=headers)
orders = response.json()
return Pagination(**orders)
def get_block_trades(
self,
symbols: Optional[List[str]] = None,
product_ids: Optional[List[int]] = None,
product_types: Optional[List[ProductType]] = None,
clearing_firm_codes: Optional[List[str]] = None,
account_ids: Optional[List[str]] = None,
connection_ids: Optional[List[int]] = None,
status: Optional[List[BlockTradeStatus]] = None,
day: Optional[date] = None,
limit=None,
begin_time=None,
end_time=None,
order=None,
cursor=None,
) -> Pagination[List[Fill], CursorInfo]:
method = "GET"
path = f"/{self.env}/block-trades"
url = self.base_url + path
params = {
"symbol": symbols,
"connection_id": connection_ids,
"product_id": product_ids,
"account_id": account_ids,
"clearing_firm_code": clearing_firm_codes,
"product_type": product_types,
"status": status,
"order": order,
"begin_time": begin_time,
"end_time": end_time,
"limit": limit,
"day": day,
"cursor": cursor,
}
headers = self.auth_headers(method, url, params)
response = requests.get(url, params=params, headers=headers)
block_trades = response.json()
return Pagination(**block_trades)
def auth_headers(self, method: str, url: str, params: Dict):
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")
path = urlparse(url).path
signature = hmac_utils.signature(
method, path, params, timestamp, self.connection_id, self.auth_token
)
return {
hmac_utils.CONNECTION_ID_HEADER: str(self.connection_id),
hmac_utils.TIMESTAMP_HEADER: timestamp,
hmac_utils.SIGNATURE_HEADER: signature,
}
# Example use:
# client = BitnomialHttpClient()
# product_specs = client.get_product_specs(active=True, base_symbol="BUS")
# product_data = client.get_product_data()
# first_product_id = product_specs[0].product_id
# first_spec = client.get_product_spec(first_product_id)
# first_data = client.get_product_datum(first_product_id)
# print(product_specs)
# print(product_data)
# print(first_spec)
# print(first_data)