-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheti_client.py
executable file
·125 lines (94 loc) · 3.23 KB
/
eti_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
#!/usr/bin/env python3
# Dummy ETI demo client
#
# SPDX-FileCopyrightText: © 2021 Georg Sauthoff <mail@gms.tf>
# SPDX-License-Identifier: GPL-3.0-or-later
import asyncio
import logging
import struct
import sys
import eti.v9_1 as eti
from dressup import pformat
log = logging.getLogger(__name__)
len_st = struct.Struct('<I')
async def read_one(stream):
bs = await stream.readexactly(4)
n = len_st.unpack(bs)[0]
log.info(f'next message size: {n}')
rest = await stream.readexactly(n - 4)
bs += rest
m = eti.unpack_from(bs)
m.rstrip()
log.info(f'Received: {pformat(m, width=45)}')
async def read_everything(stream):
try:
while True:
await read_one(stream)
except asyncio.IncompleteReadError:
log.info('Got EOF on read end')
async def client(host, port):
rstream, wstream = await asyncio.open_connection(host, port)
bs = bytearray(1024)
x = eti.LogonRequest()
x.HeartBtInt = 2300000 # ms
x.ApplUsageOrders = eti.ApplUsageOrders.AUTOMATED
x.ApplUsageQuotes = eti.ApplUsageQuotes.AUTOMATED
x.OrderRoutingIndicator = eti.OrderRoutingIndicator.NO
x.ApplicationSystemName = b'myRoboTrader'
x.ApplicationSystemVersion = b'1.666'
x.ApplicationSystemVendor = b'ACME Inc.'
x.PartyIDSessionID = 471142
x.Password = b'einsfueralles'
x.RequestHeader.MsgSeqNum = 1
n = x.pack_into(bs)
wstream.write(memoryview(bs)[:n])
log.info('Sending Logon')
await wstream.drain()
await read_one(rstream)
x = eti.UserLoginRequest()
x.RequestHeader.MsgSeqNum = 2
x.Username = 23
x.Password = b'P4s7w0rd'
n = x.pack_into(bs)
wstream.write(memoryview(bs)[:n])
log.info('Sending Login')
await wstream.drain()
await read_one(rstream)
reader = asyncio.create_task(read_everything(rstream))
# ^ Python 3.7, prior:
# reader = asyncio.ensure_future(read_everything(rstream))
x = eti.NewOrderSingleShortRequest()
x.RequestHeader.SenderSubID = 23 # logged in user name
x.ExecutingTrader = 1337
x.EnrichmentRuleID = 1
x.ApplSeqIndicator = eti.ApplSeqIndicator.NO_RECOVERY_REQUIRED
x.PriceValidityCheckType = eti.PriceValidityCheckType.NONE
x.ValueCheckTypeValue = eti.ValueCheckTypeValue.DO_NOT_CHECK
x.OrderAttributeLiquidityProvision = eti.OrderAttributeLiquidityProvision.N
x.TimeInForce = eti.TimeInForce.IOC
x.ExecInst = eti.ExecInst.Q # non-persistant order
x.TradingCapacity = eti.TradingCapacity.MARKET_MAKER
x.PartyIdInvestmentDecisionMakerQualifier = eti.PartyIdInvestmentDecisionMakerQualifier.ALGO
x.PartyIdInvestmentDecisionMaker = 0
x.ExecutingTraderQualifier = eti.ExecutingTraderQualifier.ALGO
x.RequestHeader.MsgSeqNum = 3
x.Side = eti.Side.BUY
x.OrderQty = 42 * 10**4
x.SimpleSecurityID = 23
x.Price = 404 * 10**8
x.ClOrdID = 666
n = x.pack_into(bs)
wstream.write(memoryview(bs)[:n])
log.info('Sending IOC')
await wstream.drain()
await asyncio.sleep(3)
wc = wstream.close()
await wstream.wait_closed()
await reader
def main():
host = sys.argv[1]
port = int(sys.argv[2])
logging.basicConfig(level=logging.INFO)
asyncio.run(client(host, port))
if __name__ == '__main__':
sys.exit(main())