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
def start_server(self): try: db = SqliteStorage(database_filename=self.filename) db.initialize() server = TradingServer(storage=db, client_authentication=False, marshaller=ProtobufSerialization(), feeder_port=60000, matching_engine_port=60001, uptime_in_seconds=3.0) server.start() db.close() except Exception as exception: logger.exception(exception) return exception return None
def start_liquidity_taker(self): try: # Let the server starts properly... 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: logger.exception(exception) return exception return None
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
if __name__ == '__main__': import sys logging.basicConfig( stream=sys.stdout, level=logging.INFO, format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%d/%m/%Y %H:%M:%S %p') try: from protobufserialization import ProtobufSerialization except ImportError as error: ProtobufSerialization = None print('Unable to start trading client. Reason [{}]'.format(error)) else: class BasicClient(TradingClient): def __init__(self, *args, **kwargs): super(BasicClient, self).__init__(*args, **kwargs) def main_loop_hook(self): pass client = BasicClient(login='', password='', marshaller=ProtobufSerialization(), host=socket.gethostbyname(socket.gethostname()), feeder_port=50000, matching_engine_port=50001, uptime_in_seconds=None) client.start([client.feedhandler, client.ordersender])
def setUp(self): self.instrument_identifier = 0 self.marshaller = ProtobufSerialization()
class TestProtobufSerialization(unittest.TestCase): def setUp(self): self.instrument_identifier = 0 self.marshaller = ProtobufSerialization() def test_logon(self): logon = Logon(login='******', password='******') encoded_logon = self.marshaller.encode_logon(logon=logon) message_type, body, _ = self.marshaller.decode_header(encoded_logon) decoded_logon = self.marshaller.decode_logon(body) self.assertEqual(message_type, MessageTypes.Logon.value) self.assertEqual(logon.__dict__, decoded_logon.__dict__) def test_empty_referential(self): empty_referential = Referential() encoded_referential = self.marshaller.encode_referential( empty_referential) message_type, body, _ = self.marshaller.decode_header( encoded_referential) decoded_referential = self.marshaller.decode_referential(body) self.assertEqual(message_type, MessageTypes.Referential.value) self.assertEqual(empty_referential.__dict__, decoded_referential.__dict__) def test_default_referential(self): referential = StaticData.get_default_referential() encoded_referential = self.marshaller.encode_referential(referential) message_type, body, _ = self.marshaller.decode_header( encoded_referential) decoded_referential = self.marshaller.decode_referential(body) self.assertEqual(message_type, MessageTypes.Referential.value) self.assertEqual(referential.__dict__, decoded_referential.__dict__) def test_empty_order_book(self): empty_order_book = OrderBook(StaticData.get_instrument(1).identifier) encoded_order_book = self.marshaller.encode_order_book( empty_order_book) message_type, body, _ = self.marshaller.decode_header( encoded_order_book) decoded_order_book = self.marshaller.decode_order_book(body) self.assertEqual(message_type, MessageTypes.OrderBook.value) self.assertEqual(empty_order_book.__dict__, decoded_order_book.__dict__) def test_one_buy_order_book(self): simple_order_book = OrderBook(self.instrument_identifier) buy_order = ServerOrder(Buy(), self.instrument_identifier, quantity=100.0, price=10.0, counterparty='Trader1') simple_order_book.add_order(buy_order) encoded_order_book = self.marshaller.encode_order_book( simple_order_book) message_type, body, _ = self.marshaller.decode_header( encoded_order_book) decoded_order_book = self.marshaller.decode_order_book(body) self.assertEqual(message_type, MessageTypes.OrderBook.value) self.assertEqual(encoded_order_book, self.marshaller.encode_order_book(decoded_order_book)) def test_two_opposite_orders_in_order_book(self): order_book = OrderBook(self.instrument_identifier) orders = [ ServerOrder(Buy(), self.instrument_identifier, quantity=100.0, price=9.0, counterparty='Trader1'), ServerOrder(Sell(), self.instrument_identifier, quantity=100.0, price=10.0, counterparty='Trader2') ] for order in orders: order_book.add_order(order) encoded_order_book = self.marshaller.encode_order_book(order_book) message_type, body, _ = self.marshaller.decode_header( encoded_order_book) decoded_order_book = self.marshaller.decode_order_book(body) self.assertEqual(message_type, MessageTypes.OrderBook.value) self.assertEqual(encoded_order_book, self.marshaller.encode_order_book(decoded_order_book)) def test_simple_create_order(self): create_order = CreateOrder(way=Buy(), price=42.0, quantity=10.0, instrument_identifier=1) encoded_create_order = self.marshaller.encode_create_order( create_order=create_order) message_type, body, _ = self.marshaller.decode_header( encoded_create_order) decoded_create_order = self.marshaller.decode_create_order(body) self.assertEqual(message_type, MessageTypes.CreateOrder.value) self.assertEqual(create_order.__dict__, decoded_create_order.__dict__)
from referential import Referential from staticdata import StaticData from staticdata import MessageTypes from orderbook import OrderBook from serverorder import ServerOrder from createorder import CreateOrder from logon import Logon from toolbox import random_string from simpleserialization import SimpleSerialization from protobufserialization import ProtobufSerialization @pytest.mark.parametrize("instrument_identifier", [1]) @pytest.mark.parametrize( 'marshaller', [SimpleSerialization(), ProtobufSerialization()]) class TestSerialization: @staticmethod def test_logon(instrument_identifier, marshaller): logon = Logon(login=random_string(length=5), password=random_string(length=5)) encoded_logon = marshaller.encode_logon(logon=logon) message_type, body, _ = marshaller.decode_header(encoded_logon) decoded_logon = marshaller.decode_logon(body) assert message_type == MessageTypes.Logon.value assert logon.__dict__ == decoded_logon.__dict__ @staticmethod def test_empty_referential(instrument_identifier, marshaller): empty_referential = Referential() encoded_referential = marshaller.encode_referential(empty_referential)