/PyTrading

Let's trade in Python !

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

PyTrading project

PyTrading is a sandbox stock market project written in Python 3.
This project has an educational purpose to learn how financial markets work.

Continuous Integration and code quality

GNU/Linux Windows Static analysis Code coverage
Build Status Build status Codacy Badge codecov

How to generate .proto files

To generate Protobuf "_pb2.py" files:

$ protoc --python_out=. *.proto`

Lexicon

  • An order is composed of a price, a quantity, a way (buy or sell) and the instrument you want to trade (e.g. Apple stock)
  • A deal is generated when there is a match between two orders on same price and same instrument (order book's last price is updated)
  • An order book hosts every incoming orders waiting to be executed (most interesting price comes first)

High level architecture

  • TradingServer is composed of two servers: a matching engine and a feeder.
    • MatchingEngine accepts orders from clients and try to match them.
    • Feeder streams continuously order books content (full snapshot).
  • TradingClient connects two client sockets: an ordersender and a feedhandler.
    • OrderSender sends order according to algorithm implemented in main_loop_hook
    • FeedHandler receives order book updates (e.g. price modified, executed order etc..)

Communication

TradingServer and TradingClient communicates via TCP/IP serialized messages.
Two serializations are available: plain text (pipe separated) and Protobuf.

Where to start

TradingSandbox module gives a good example to start in local, take a look in main_loop_hook implementation.

Requirements

  • MongoDB
  • Protobuf