forked from cqg/WebAPIPythonSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9orders.py
110 lines (94 loc) · 4.24 KB
/
9orders.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
from WebAPI.webapi_2_pb2 import *
from WebAPI import webapi_client
from threading import Thread
host_name = 'wss://demoapi.cqg.com:443'
user_name = ''
password = ''
resolveSymbolName = 'ZUC'
trade_subscription_id = 1
request_id = 1 # request id must have unique value per trader per day
account_id = 12345678 # change the value according to your account_id
contract_id = 1
cl_order_id = '2' # every order must have unique cl_order_id per trader per day
order_type = 1 # 1 means MKT 2 means LMT 3 means STP 4 means STL
duration = 1
side = 1 # 1 means buy and 2 means sell
qty_significant = 1
qty_exponent = 0
is_manual = False
def logon(user_name, password,
client_app_id='WebApiTest', client_version='python-client-test-2-87',
protocol_version_major=2,protocol_version_minor = 87):
client_msg = ClientMsg()
logon = client_msg.logon
logon.user_name = user_name
logon.password = password
logon.client_app_id = client_app_id
logon.client_version = client_version
logon.protocol_version_major = protocol_version_major
logon.protocol_version_minor = protocol_version_minor
client.send_client_message(client_msg)
server_msg = client.receive_server_message()
if server_msg.logon_result.result_code == 0:
return server_msg.logon_result.base_time
else:
raise Exception("Can't login: " + server_msg.logon_result.text_message)
def resolve_symbol(symbol_name, msg_id=1, subscribe=None):
client_msg = ClientMsg()
information_request = client_msg.information_requests.add()
information_request.id = msg_id
if subscribe is not None:
information_request.subscribe = subscribe
information_request.symbol_resolution_request.symbol = symbol_name
client.send_client_message(client_msg)
server_msg = client.receive_server_message()
return server_msg.information_reports[0].symbol_resolution_report.contract_metadata
def request_trade_subscription(trade_subscription_id):
client_msg = ClientMsg()
trade_sub_request = client_msg.trade_subscriptions.add()
trade_sub_request.id = trade_subscription_id
trade_sub_request.subscribe = True
trade_sub_request.subscription_scopes.append(1)
#trade_sub_request.subscription_scopes.append(2)
#trade_sub_request.subscription_scopes.append(3)
client.send_client_message(client_msg)
while True:
server_msg = client.receive_server_message()
if server_msg.trade_snapshot_completions is not None:
server_msg = client.receive_server_message()
break
def new_order_request(request_id, account_id, contract_id,
cl_order_id, order_type, duration, side,
qty_significant, qty_exponent, is_manual):
client_msg = ClientMsg()
order_request = client_msg.order_requests.add()
order_request.request_id = request_id
order_request.new_order.order.account_id = account_id
order_request.new_order.order.when_utc_time = 0
order_request.new_order.order.contract_id = contract_id
order_request.new_order.order.cl_order_id = cl_order_id
order_request.new_order.order.order_type = order_type
order_request.new_order.order.duration = duration
order_request.new_order.order.side = side
order_request.new_order.order.qty.significand = qty_significant
order_request.new_order.order.qty.exponent = qty_exponent
order_request.new_order.order.is_manual = is_manual
# add the limit_price when order_type is LIMIT
# order_request.new_order.order.limit_price = xxxx
client.send_client_message(client_msg)
while True:
server_msg = client.receive_server_message()
if server_msg.trade_snapshot_completions is not None:
server_msg = client.receive_server_message()
if __name__ == "__main__":
client = webapi_client.WebApiClient()
client.connect(host_name)
baseTime = logon(user_name, password)
contract_metadata = resolve_symbol(resolveSymbolName)
t1 = Thread(target = request_trade_subscription(trade_subscription_id))
t1.setDaemon(True)
t1.start()
t2 = Thread(target = new_order_request(request_id, account_id, contract_id, cl_order_id, order_type, duration, side, qty_significant, qty_exponent, is_manual))
t2.setDaemon(True)
t2.start()
client.disconnect()