New sell order for $50 Market Maker 1ms latency 10ms latency 1) MM receives the update before the buyer 2) MM buys the stock for $50 and immediately enters a sell order for $100; happens with very high frequency! 3) Buyer buys at $100, MM pockets $50 profit. (Automated trading program, also referred to as a bot)
AbstractBaseBot BotClient OrderManagementSystem Human clients via webpage Assigner (Experiment Coordinator) ØMQ Transport BotClientAPI After… BotClient ZMQ or exchange_zmq.c Bots in C
experiment • Any original systems are used as controls 2. Generate hypotheses based on observation 3. Implement hypotheses 4. Go to 2 5. Generalize “We should forget about small efficiencies, say about 97% of the time: Premature optimization is the root of all evil, yet we should not pass up our opportunities in that critical 3%.” (Knuth, 1974)
AbstractBaseBot BotClient OrderManagementSystem Human clients via webpage Assigner (Experiment Coordinator) ØMQ Endpoint BotClientAPI After… BotClient ZMQ or exchange_zmq.c Bots in C
receives assignments from the Assigner Exchange communication thread Thread which accesses the OMS, executes some trading logic, and decides whether to send a response or not .interrupt() .interrupt() .sendNewMessage()
110101010101…. Between the Bot and the Exchange Type: LimitShoutMessage shoutMessageFields { assignID: 67 shoutID: 231870732 isBuy: false stockName: "MSFT" Size savings up to 40% with very large order book updates (artificially constructed up to 20 levels of depth), while nice on paper won’t be very significant with the message sizes encountered
obtain fine-grained timing information; disadvantage: will skew methods which are very quick Sampling: Collects information at fixed intervals And now for two concrete improvements made.. https://visualvm.java.net/
to know if a message is ready is to call poll() in a loop. OwnShoutUnfulfilledMessage (Order still on the books) callback OwnShoutMessage (Trade Executed) callback Thread ... other callbacks… What is the problem?
Bot To Server Inside the Exchange Server To Bot Packets in transit Breakdown of Communication Time Building Shout Message String Blocking sendString() Decoding Shout Message Order Matching Return sendString Decode received order book On the wire Total: ~2.4ms 4x Before…
0.7 0.8 Bot To Server Inside the Exchange Server To Bot Packets in transit Breakdown of Communication Time Encoding Shout Message Nonblocking ZMQ.Socket.send() Decoding Shout Message Order Matching Encoding protobuf return message from POJO Nonblocking return send Decoding Orderbook On the wire Total: ~1.594ms 4x After…
Write and run a ØMQ bot in C which attempts to make $1 profit given the assignment Core deliverables of this project: • ØMQ library in Java • ØMQ library in C • Library which allows bots to be run inside the CommServer using reflection • Sample bots (here’s one in front of you now) • Benchmarking stuff
optimization. • Measuring at different granularity levels can be helpful, lots of tools at our disposal. • Measure after finished with fixing a code hotspot. • Programming to interfaces instead of implementations makes things maintainable and easy to use.
optimization. • Measuring at different granularity levels can be helpful, lots of tools at our disposal. • Measure after finished with fixing a code hotspot. • Programming to interfaces instead of implementations makes things maintainable and easy to use. Question Time