-
Notifications
You must be signed in to change notification settings - Fork 36
/
MarketdataSocketExample.py
150 lines (114 loc) · 5.01 KB
/
MarketdataSocketExample.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
from datetime import datetime
from Connect import XTSConnect
from MarketDataSocketClient import MDSocket_io
# MarketData API Credentials
API_KEY = "a27e231381266f63c62157"
API_SECRET = "Akko754#C3"
source = "WEBAPI"
# Initialise
xt = XTSConnect(API_KEY, API_SECRET, source)
# Login for authorization token
response = xt.marketdata_login()
# Store the token and userid
set_marketDataToken = response['result']['token']
set_muserID = response['result']['userID']
print("Login: ", response)
# Connecting to Marketdata socket
soc = MDSocket_io(set_marketDataToken, set_muserID)
# Instruments for subscribing
Instruments = [
{'exchangeSegment': 1, 'exchangeInstrumentID': 2885},
{'exchangeSegment': 1, 'exchangeInstrumentID': 26000},
{'exchangeSegment': 2, 'exchangeInstrumentID': 51601}
]
# Callback for connection
def on_connect():
"""Connect from the socket."""
print('Market Data Socket connected successfully!')
# # Subscribe to instruments
print('Sending subscription request for Instruments - \n' + str(Instruments))
response = xt.send_subscription(Instruments, 1501)
print('Sent Subscription request!')
print("Subscription response: ", response)
# Callback on receiving message
def on_message(data):
print('I received a message!')
# Callback for message code 1501 FULL
def on_message1501_json_full(data):
print('I received a 1501 Touchline message!' + data)
# Callback for message code 1502 FULL
def on_message1502_json_full(data):
print('I received a 1502 Market depth message!' + data)
# Callback for message code 1505 FULL
def on_message1505_json_full(data):
print('I received a 1505 Candle data message!' + data)
# Callback for message code 1507 FULL
def on_message1507_json_full(data):
print('I received a 1507 MarketStatus data message!' + data)
# Callback for message code 1510 FULL
def on_message1510_json_full(data):
print('I received a 1510 Open interest message!' + data)
# Callback for message code 1512 FULL
def on_message1512_json_full(data):
print('I received a 1512 Level1,LTP message!' + data)
# Callback for message code 1105 FULL
def on_message1105_json_full(data):
print('I received a 1105, Instrument Property Change Event message!' + data)
# Callback for message code 1105 FULL
def on_message1105_json_partial(data):
print('I received a 1105, Instrument Property Change Event message!' + data)
# Callback for message code 1501 PARTIAL
def on_message1501_json_partial(data):
print('I received a 1501, Touchline Event message!' + data)
# Callback for message code 1502 PARTIAL
def on_message1502_json_partial(data):
print('I received a 1502 Market depth message!' + data)
# Callback for message code 1505 PARTIAL
def on_message1505_json_partial(data):
print('I received a 1505 Candle data message!' + data)
# Callback for message code 1510 PARTIAL
def on_message1510_json_partial(data):
print('I received a 1510 Open interest message!' + data)
# Callback for message code 1512 PARTIAL
def on_message1512_json_partial(data):
print('I received a 1512, LTP Event message!' + data)
# Callback for message code 1105 PARTIAL
def on_message1105_json_partial(data):
print('I received a 1105, Instrument Property Change Event message!' + data)
# Callback for disconnection
def on_disconnect():
print('Market Data Socket disconnected!')
# Callback for error
def on_error(data):
"""Error from the socket."""
print('Market Data Error', data)
# Assign the callbacks.
soc.on_connect = on_connect
soc.on_message = on_message
soc.on_message1502_json_full = on_message1502_json_full
soc.on_message1505_json_full = on_message1505_json_full
soc.on_message1507_json_full = on_message1507_json_full
soc.on_message1510_json_full = on_message1510_json_full
soc.on_message1501_json_full = on_message1501_json_full
soc.on_message1512_json_full = on_message1512_json_full
soc.on_message1105_json_full = on_message1105_json_full
soc.on_message1502_json_partial = on_message1502_json_partial
soc.on_message1505_json_partial = on_message1505_json_partial
soc.on_message1510_json_partial = on_message1510_json_partial
soc.on_message1501_json_partial = on_message1501_json_partial
soc.on_message1512_json_partial = on_message1512_json_partial
soc.on_message1105_json_partial = on_message1105_json_partial
soc.on_disconnect = on_disconnect
soc.on_error = on_error
# Event listener
el = soc.get_emitter()
el.on('connect', on_connect)
el.on('1501-json-full', on_message1501_json_full)
el.on('1502-json-full', on_message1502_json_full)
el.on('1507-json-full', on_message1507_json_full)
el.on('1512-json-full', on_message1512_json_full)
el.on('1105-json-full', on_message1105_json_full)
el.on('1105-json-partial', on_message1105_json_full)
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
soc.connect()