-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtradingserverclient_test.py
142 lines (121 loc) · 5.85 KB
/
tradingserverclient_test.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
import os
import sys
import time
import socket
import unittest
import logging
import traceback
from database import Database
from orderway import OrderWay, Buy, Sell
from multiprocessing.pool import ThreadPool
from tradingserver import TradingServer
from tradingclient import TradingClient
from protobufserialization import ProtobufSerialization
class LiquidityTaker(TradingClient):
""" LiquidityTaker is looking for all available orders and sends opposite way orders to clear order book"""
def __init__(self, *args, **kwargs):
super(LiquidityTaker, self).__init__(*args, **kwargs)
def main_loop_hook(self):
# Read the order books
for _, order_book in self.feedhandler.order_books.items():
all_orders = order_book.get_all_orders()
# Create opposite orders to get executed on every order
for order in all_orders:
if order.counterparty != self.ordersender.login:
self.ordersender.push_order(way=OrderWay.get_opposite_way(order.way),
price=order.price,
quantity=order.quantity,
instrument_identifier=order.instrument_identifier)
class LiquidityProvider(TradingClient):
""" LiquidityProvider constantly sends orders to get executed """
def __init__(self, *args, **kwargs):
super(LiquidityProvider, self).__init__(*args, **kwargs)
def main_loop_hook(self):
# Get all instruments available
instruments = self.feedhandler.referential.get_instruments()
# Send buy and sell orders on each one (if not empty)
for instrument in instruments:
if instrument.identifier not in self.feedhandler.order_books:
continue
order_book = self.feedhandler.order_books[instrument.identifier]
if order_book.count_bids() == 0:
self.ordersender.push_order(way=Buy(),
price=40.0,
quantity=10.0,
instrument_identifier=instrument.identifier)
if order_book.count_asks() == 0:
self.ordersender.push_order(way=Sell(),
price=42.0,
quantity=10.0,
instrument_identifier=instrument.identifier)
class TestTradingServerClient(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.liquidity_provider_login = 'BNP'
cls.liquidity_taker_login = 'CFM'
cls.client_password = 'whatever'
cls.filename = 'TradingServerClientTest.db'
@classmethod
def tearDownClass(cls):
os.remove(cls.filename)
def test_trading_server_and_client(self):
logging.basicConfig(stream=sys.stdout,
level=logging.INFO,
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
datefmt='%d/%m/%Y %H:%M:%S')
thread_pool = ThreadPool(processes=3)
async_server_result = thread_pool.apply_async(self.start_server)
liquidity_provider_result = thread_pool.apply_async(self.start_liquidity_provider)
liquidity_taker_result = thread_pool.apply_async(self.start_liquidity_taker)
for failure in [async_server_result.get(), liquidity_taker_result.get(), liquidity_provider_result.get()]:
if failure:
self.fail(failure)
def start_server(self):
try:
db = Database(database_filename=self.filename)
db.initialize()
db.insert_user(login=self.liquidity_provider_login, password=self.client_password)
db.insert_user(login=self.liquidity_taker_login, password=self.client_password)
server = TradingServer(storage=db,
marshaller=ProtobufSerialization(),
feeder_port=60000,
matching_engine_port=60001,
uptime_in_seconds=3.0)
server.start()
db.close()
except Exception as exception:
print(traceback.print_exc())
return exception
return None
def start_liquidity_taker(self):
try:
time.sleep(1)
client = LiquidityTaker(login=self.liquidity_taker_login,
password=self.client_password,
marshaller=ProtobufSerialization(),
host=socket.gethostbyname(socket.gethostname()),
feeder_port=60000,
matching_engine_port=60001,
uptime_in_seconds=3.0)
client.start([client.feedhandler, client.ordersender])
except Exception as exception:
print(traceback.print_exc())
return exception
return None
def start_liquidity_provider(self):
try:
time.sleep(1)
client = LiquidityProvider(login=self.liquidity_provider_login,
password=self.client_password,
marshaller=ProtobufSerialization(),
host=socket.gethostbyname(socket.gethostname()),
feeder_port=60000,
matching_engine_port=60001,
uptime_in_seconds=3.0)
client.start([client.feedhandler, client.ordersender])
except Exception as exception:
print(traceback.print_exc())
return exception
return None
if __name__ == '__main__':
unittest.main()