-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.py
202 lines (178 loc) · 7.18 KB
/
client.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
import datetime
import logging
import typing as t
import requests
from config import configuration
log = logging.getLogger(__name__)
class OrderPlaceError(Exception):
pass
class SmarketsClient:
def __init__(self):
self.auth_token = None
@classmethod
def _format_datetime_for_api_request(cls, timestamp: datetime.datetime) -> str:
return timestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
def _auth_headers(self):
return {'Authorization': 'Session-Token ' + self.auth_token}
def init_session(self):
log.info('initiating session')
self.auth_token = configuration['auth'].get('auth_token')
if not self.auth_token:
response = requests.post(
f'{configuration["api"]["base_url"]}sessions/',
json={
'username': configuration['auth']['login'],
'password': configuration['auth']['password'],
},
).json()
self.auth_token = response.get('token')
log.info('auth token: %s', self.auth_token)
def place_order(
self,
market_id,
contract_id,
price,
quantity,
side,
):
log.info(
'placing order: m_id %s: c_id %s\t %s %s @ %s',
market_id,
contract_id,
side,
quantity,
price,
)
response = requests.post(
f'{configuration["api"]["base_url"]}orders/',
json={
'market_id': market_id,
'contract_id': contract_id,
'price': price,
'quantity': quantity,
'reference_id': str(int(datetime.datetime.utcnow().timestamp())),
'side': side,
},
headers=self._auth_headers(),
)
response_body = response.json()
if response.status_code != 200:
raise OrderPlaceError(response_body.get('error_type'))
log.info(
f'''order placed: m_id {market_id}: c_id {contract_id} \t {side} {quantity} @ {price}|'''
f'''balance:{response_body["available_balance"]} executed:{response_body["total_executed_quantity"]}'''
f''' exposure:{response_body["exposure"]}'''
)
def cancel_order(self, order_id):
requests.delete(
f'{configuration["api"]["base_url"]}orders/{order_id}/',
headers=self._auth_headers(),
)
def cancel_all_orders(self, market_id):
requests.delete(
f'{configuration["api"]["base_url"]}orders/?market_id={market_id}',
headers=self._auth_headers(),
)
def get_orders(self, states):
orders = []
states_to_fetch = '&'.join([f'states={state}' for state in states])
next_page = f'?limit={configuration["api"]["chunk_size"]}&{states_to_fetch}'
while next_page:
response = requests.get(
f'{configuration["api"]["base_url"]}orders/{next_page}',
headers=self._auth_headers(),
).json()
log.info(response)
orders += response['orders']
next_page = response['pagination']['next_page']
return orders
def get_available_events(
self,
states: t.List[str],
types: t.List[str],
start_datetime_max: datetime.datetime,
limit: int,
*,
start_datetime_min: t.Optional[datetime.datetime] = None,
):
start_datetime_max = self._format_datetime_for_api_request(start_datetime_max)
if start_datetime_min:
start_datetime_min = self._format_datetime_for_api_request(start_datetime_min)
states_filter = '&'.join([f'states={state}' for state in states])
types_filter = '&'.join([f'types={type_}' for type_ in types])
page_filter = (
f'?{states_filter}&{types_filter}&sort=id' +
f'&limit={limit}&start_datetime_max={start_datetime_max}' +
(f'&start_datetime_min={start_datetime_min}' if start_datetime_min else '')
)
events = []
while page_filter:
request_url = f'{configuration["api"]["base_url"]}events/{page_filter}'
current_page = self._client_wrapper(request_url)
events += current_page['events']
page_filter = current_page['pagination']['next_page']
return events
def get_events(self, event_ids: t.List[str]):
event_ids_filter = '&'.join([f'ids={event_id}' for event_id in event_ids])
request_url = f'{configuration["api"]["base_url"]}events/?{event_ids_filter}'
return self._client_wrapper(request_url).get('events')
def get_markets(self, market_ids: t.List[str], with_volumes: bool=False):
markets = ','.join(market_ids)
request_url = f'{configuration["api"]["base_url"]}markets/{markets}/?with_volumes={with_volumes}'
return self._client_wrapper(request_url).get('markets')
def get_related_markets(self, events):
markets = []
event_ids = [event['id'] for event in events]
i = 0
chunk_size = configuration["api"]["chunk_size"]
while i * chunk_size < len(event_ids):
events_to_fetch = ','.join(
event_ids[i * chunk_size:(i + 1) * chunk_size]
)
request_url = (
f'''{configuration["api"]["base_url"]}events/{events_to_fetch}/markets/'''
f'''?sort=event_id,display_order&with_volumes=true'''
)
markets += self._client_wrapper(request_url)['markets']
i += 1
return markets
def get_related_contracts(self, markets):
contracts = []
market_ids = [market['id'] for market in markets]
i = 0
chunk_size = configuration["api"]["chunk_size"]
while i * chunk_size < len(market_ids):
markets_to_fetch = ','.join(
market_ids[i * chunk_size:(i + 1) * chunk_size]
)
request_url = (
f'''{configuration["api"]["base_url"]}markets/{markets_to_fetch}/contracts/'''
)
contracts += self._client_wrapper(request_url)['contracts']
i += 1
return contracts
def get_quotes(self, market_ids: t.List[str]):
quotes = []
i = 0
chunk_size = configuration["api"]["chunk_size"]
while i * chunk_size < len(market_ids):
markets_to_fetch = ','.join(
market_ids[i * chunk_size:(i + 1) * chunk_size]
)
request_url = f'{configuration["api"]["base_url"]}markets/{markets_to_fetch}/quotes/'
quotes += [self._client_wrapper(request_url)]
i += 1
quotes_result = {}
for quote_entry in quotes:
for contract_id, order_book in quote_entry.items():
quotes_result[contract_id] = order_book
return quotes_result
def get_accounts(self):
response = requests.get(
f'{configuration["api"]["base_url"]}accounts/', headers=self._auth_headers()
).json()
return response
@classmethod
def _client_wrapper(cls, url: str) -> t.Dict[str, t.Any]:
log.info(f'calling url: {url}')
return requests.get(url).json()