-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnect.py
367 lines (308 loc) · 15.1 KB
/
Connect.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
"""
Connect.py
API wrapper for XTS Connect REST APIs.
:copyright:
:license: see LICENSE for details.
"""
import configparser
import json
import logging
from urllib.parse import urljoin
import requests
from requests.adapters import HTTPAdapter
import Exception as ex
log = logging.getLogger(__name__)
class XTSCommon:
"""
Base variables class
"""
def __init__(self, token=None, userID=None, isInvestorClient=None):
"""Initialize the common variables."""
self.token = token
self.userID = userID
#self.isInvestorClient = isInvestorClient
class XTSConnect(XTSCommon):
"""
The XTS Connect API wrapper class.
In production, you may initialise a single instance of this class per `api_key`.
"""
"""Get the configurations from config.ini"""
cfg = configparser.ConfigParser()
cfg.read('config.ini')
# Default root API endpoint. It's possible to
# override this by passing the `root` parameter during initialisation.
_default_root_uri = cfg.get('root_url', 'root')
_default_login_uri = _default_root_uri + "/user/session"
_default_timeout = 7 # In seconds
# SSL Flag
_ssl_flag = cfg.get('SSL', 'disable_ssl')
# URIs to various calls
_routes = {
# Market API endpoints
"marketdata.prefix": "apimarketdata",
"market.login": "/apibinarymarketdata/auth/login",
"market.logout": "/apibinarymarketdata/auth/logout",
"market.config": "/apibinarymarketdata/config/clientConfig",
"market.instruments.master": "/apibinarymarketdata/instruments/master",
"market.instruments.subscription": "/apibinarymarketdata/instruments/subscription",
"market.instruments.unsubscription": "/apibinarymarketdata/instruments/subscription",
"market.instruments.ohlc": "/apibinarymarketdata/instruments/ohlc",
"market.instruments.indexlist": "/apibinarymarketdata/instruments/indexlist",
"market.instruments.quotes": "/apibinarymarketdata/instruments/quotes",
"market.search.instrumentsbyid": '/apibinarymarketdata/search/instrumentsbyid',
"market.search.instrumentsbystring": '/apibinarymarketdata/search/instruments',
"market.instruments.instrument.series": "/apibinarymarketdata/instruments/instrument/series",
"market.instruments.instrument.equitysymbol": "/apibinarymarketdata/instruments/instrument/symbol",
"market.instruments.instrument.futuresymbol": "/apibinarymarketdata/instruments/instrument/futureSymbol",
"market.instruments.instrument.optionsymbol": "/apibinarymarketdata/instruments/instrument/optionsymbol",
"market.instruments.instrument.optiontype": "/apibinarymarketdata/instruments/instrument/optionType",
"market.instruments.instrument.expirydate": "/apibinarymarketdata/instruments/instrument/expiryDate"
}
def __init__(self,
apiKey,
secretKey,
source,
userID,
password,
root=None,
debug=False,
timeout=None,
pool=None,
disable_ssl=_ssl_flag):
"""
Initialise a new XTS Connect client instance.
- `api_key` is the key issued to you
- `token` is the token obtained after the login flow. Pre-login, this will default to None,
but once you have obtained it, you should persist it in a database or session to pass
to the XTS Connect class initialisation for subsequent requests.
- `root` is the API end point root. Unless you explicitly
want to send API requests to a non-default endpoint, this
can be ignored.
- `debug`, if set to True, will serialise and print requests
and responses to stdout.
- `timeout` is the time (seconds) for which the API client will wait for
a request to complete before it fails. Defaults to 7 seconds
- `pool` is manages request pools. It takes a dict of params accepted by HTTPAdapter
- `disable_ssl` disables the SSL verification while making a request.
If set requests won't throw SSLError if its set to custom `root` url without SSL.
"""
self.debug = debug
self.apiKey = apiKey
self.secretKey = secretKey
self.source = source
self.disable_ssl = disable_ssl
self.root = root or self._default_root_uri
self.timeout = timeout or self._default_timeout
self.password = password
self.userID= userID
super().__init__()
# Create requests session only if pool exists. Reuse session
# for every request. Otherwise create session for each request
if pool:
self.reqsession = requests.Session()
reqadapter = requests.adapters.HTTPAdapter(**pool)
self.reqsession.mount("https://", reqadapter)
else:
self.reqsession = requests
# disable requests SSL warning
requests.packages.urllib3.disable_warnings()
def _set_common_variables(self, access_token, userID, isInvestorClient):
"""Set the `access_token` received after a successful authentication."""
super().__init__(access_token,userID, isInvestorClient)
def _login_url(self):
"""Get the remote login url to which a user should be redirected to initiate the login flow."""
return self._default_login_uri
########################################################################################################
# Market data API
########################################################################################################
def marketdata_login(self):
try:
#self._set_common_variables(token, userid,False)
params = {
"appKey": self.apiKey,
"secretKey": self.secretKey,
"source": self.source
}
response = self._post("market.login", params)
print(response['result'])
if "token" in response['result']:
print("Token exist")
self._set_common_variables(response['result']['token'], response['result']['userID'],False)
return response
except Exception as e:
print("In exce")
# return response
def get_config(self):
try:
params = {}
response = self._get('market.config', params)
return response
except Exception as e:
return response['description']
def get_quote(self, Instruments, xtsMessageCode, publishFormat):
try:
params = {'instruments': Instruments, 'xtsMessageCode': xtsMessageCode, 'publishFormat': publishFormat}
response = self._post('market.instruments.quotes', json.dumps(params))
return response
except Exception as e:
return response['description']
def send_subscription(self, Instruments, xtsMessageCode):
try:
params = {'instruments': Instruments, 'xtsMessageCode': xtsMessageCode}
response = self._post('market.instruments.subscription', json.dumps(params))
return response
except Exception as e:
return response['description']
def send_unsubscription(self, Instruments, xtsMessageCode):
try:
params = {'instruments': Instruments, 'xtsMessageCode': xtsMessageCode}
response = self._put('market.instruments.unsubscription', json.dumps(params))
return response
except Exception as e:
return response['description']
def get_master(self, exchangeSegmentList):
try:
params = {"exchangeSegmentList": exchangeSegmentList}
response = self._post('market.instruments.master', json.dumps(params))
return response
except Exception as e:
return response['description']
def get_ohlc(self, exchangeSegment, exchangeInstrumentID, startTime, endTime, compressionValue):
try:
params = {
'exchangeSegment': exchangeSegment,
'exchangeInstrumentID': exchangeInstrumentID,
'startTime': startTime,
'endTime': endTime,
'compressionValue': compressionValue}
response = self._get('market.instruments.ohlc', params)
return response
except Exception as e:
return response['description']
def get_series(self, exchangeSegment):
try:
params = {'exchangeSegment': exchangeSegment}
response = self._get('market.instruments.instrument.series', params)
return response
except Exception as e:
return response['description']
def get_equity_symbol(self, exchangeSegment, series, symbol):
try:
params = {'exchangeSegment': exchangeSegment, 'series': series, 'symbol': symbol}
response = self._get('market.instruments.instrument.equitysymbol', params)
return response
except Exception as e:
return response['description']
def get_expiry_date(self, exchangeSegment, series, symbol):
try:
params = {'exchangeSegment': exchangeSegment, 'series': series, 'symbol': symbol}
response = self._get('market.instruments.instrument.expirydate', params)
return response
except Exception as e:
return response['description']
def get_future_symbol(self, exchangeSegment, series, symbol, expiryDate):
try:
params = {'exchangeSegment': exchangeSegment, 'series': series, 'symbol': symbol, 'expiryDate': expiryDate}
response = self._get('market.instruments.instrument.futuresymbol', params)
return response
except Exception as e:
return response['description']
def get_option_symbol(self, exchangeSegment, series, symbol, expiryDate, optionType, strikePrice):
try:
params = {'exchangeSegment': exchangeSegment, 'series': series, 'symbol': symbol, 'expiryDate': expiryDate,
'optionType': optionType, 'strikePrice': strikePrice}
response = self._get('market.instruments.instrument.optionsymbol', params)
return response
except Exception as e:
return response['description']
def get_option_type(self, exchangeSegment, series, symbol, expiryDate):
try:
params = {'exchangeSegment': exchangeSegment, 'series': series, 'symbol': symbol, 'expiryDate': expiryDate}
response = self._get('market.instruments.instrument.optiontype', params)
return response
except Exception as e:
return response['description']
def get_index_list(self, exchangeSegment):
try:
params = {'exchangeSegment': exchangeSegment}
response = self._get('market.instruments.indexlist', params)
return response
except Exception as e:
return response['description']
def search_by_instrumentid(self, Instruments):
try:
params = {'source': self.source, 'instruments': Instruments}
response = self._post('market.search.instrumentsbyid', json.dumps(params))
return response
except Exception as e:
return response['description']
def search_by_scriptname(self, searchString):
try:
params = {'searchString': searchString}
response = self._get('market.search.instrumentsbystring', params)
return response
except Exception as e:
return response['description']
def marketdata_logout(self):
try:
params = {}
response = self._delete('market.logout', params)
return response
except Exception as e:
return response['description']
########################################################################################################
# Common Methods
########################################################################################################
def _get(self, route, params=None):
"""Alias for sending a GET request."""
return self._request(route, "GET", params)
def _post(self, route, params=None):
"""Alias for sending a POST request."""
return self._request(route, "POST", params)
def _put(self, route, params=None):
"""Alias for sending a PUT request."""
return self._request(route, "PUT", params)
def _delete(self, route, params=None):
"""Alias for sending a DELETE request."""
return self._request(route, "DELETE", params)
def _request(self, route, method, parameters=None):
"""Make an HTTP request."""
params = parameters if parameters else {}
# Form a restful URL
uri = self._routes[route].format(params)
url = urljoin(self.root, uri)
headers = {}
print(self.token)
if self.token:
# set authorization header
headers.update({'Content-Type': 'application/json', 'Authorization': self.token})
try:
r = self.reqsession.request(method,
url,
data=params if method in ["POST", "PUT"] else None,
params=params if method in ["GET", "DELETE"] else None,
headers=headers,
verify=not self.disable_ssl)
except Exception as e:
raise e
if self.debug:
log.debug("Response: {code} {content}".format(code=r.status_code, content=r.content))
# Validate the content type.
if "json" in r.headers["content-type"]:
try:
data = json.loads(r.content.decode("utf8"))
except ValueError:
raise ex.XTSDataException("Couldn't parse the JSON response received from the server: {content}".format(
content=r.content))
# api error
if data.get("type"):
if r.status_code == 400 and data["type"] == "error" and data["description"] == "Invalid Token":
raise ex.XTSTokenException(data["description"])
if r.status_code == 400 and data["type"] == "error" and data["description"] == "Bad Request":
message = "Description: " + data["description"] + " errors: " + str(data['result']["errors"])
raise ex.XTSInputException(str(message))
return data
else:
raise ex.XTSDataException("Unknown Content-Type ({content_type}) with response: ({content})".format(
content_type=r.headers["content-type"],
content=r.content))