forked from symphonyfintech/xts-pythonclient-api-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InteractiveSocketExample.py
97 lines (68 loc) · 2.26 KB
/
InteractiveSocketExample.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
from Connect import XTSConnect
from InteractiveSocketClient import OrderSocket_io
# Interactive API Credentials
API_KEY = "cccc0397aefe029ce61392"
API_SECRET = "Aoxf623#aa"
source = "WEBAPI"
# Initialise
xt = XTSConnect(API_KEY, API_SECRET, source)
# Login for authorization token
response = xt.interactive_login()
# Store the token and userid
set_interactiveToken = response['result']['token']
set_iuserID = response['result']['userID']
print("Login: ", response)
# Connecting to Interactive socket
soc = OrderSocket_io(set_interactiveToken, set_iuserID)
# Callback for connection
def on_connect():
"""Connect from the socket."""
print('Interactive socket connected successfully!')
# Callback for receiving message
def on_message():
print('I received a message!')
# Callback for joined event
def on_joined(data):
print('Interactive socket joined successfully!' + data)
# Callback for error
def on_error(data):
print('Interactive socket error!' + data)
# Callback for order
def on_order(data):
print("Order placed!" + data)
# Callback for trade
def on_trade(data):
print("Trade Received!" + data)
# Callback for position
def on_position(data):
print("Position Retrieved!" + data)
# Callback for trade conversion event
def on_tradeconversion(data):
print("Trade Conversion Received!" + data)
# Callback for message logout
def on_messagelogout(data):
print("User logged out!" + data)
# Callback for disconnection
def on_disconnect():
print('Interactive Socket disconnected!')
# Assign the callbacks.
soc.on_connect = on_connect
soc.on_message = on_message
soc.on_joined = on_joined
soc.on_error = on_error
soc.on_order = on_order
soc.on_trade = on_trade
soc.on_position = on_position
soc.on_tradeconversion = on_tradeconversion
soc.on_messagelogout = on_messagelogout
soc.on_disconnect = on_disconnect
# Event listener
el = soc.get_emitter()
el.on('connect', on_connect)
el.on('order', on_order)
el.on('trade', on_trade)
el.on('position', on_position)
el.on('tradeConversion', on_tradeconversion)
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
soc.connect()