Skip to main content

Stock exchange with a matching engine and webserver

Project description

Chives Exchange

chives-exchange is an implementation of an electronic stock exchange that contains a horizontally scalable matching engine and a web UI.

Update 0.2.1: chives.webserver.create_app and chives.matchingengine.start_engine now takes configuration dictionary instead of individual arguments

Getting started

Chives exchange can be installed from pypi:

pip install chives-exchange

There are four main components of this implementation of stock exchange:

  • Message queue
  • SQL database
  • Matching engine(s)
  • Webserver (optional)

Message queue

Begin by starting a message queue server, which is the channel through which stock orders are picked up by matching engine(s) to match with resting orders in the orderbook and produce trades. The current implementation only supports RabbitMQ, which can be most easily run using a container:

docker run -d --rm \
    --name "rabbitmq" \
    -p 5672:5672 \
    -p 15672:15672 \
    rabbitmq:3-management

Database

Chives exchange supports SQLite and MySQL as the backend database (other SQL databases might work; I just have not tested them). SQLite is a good choice for development, but won't allow multiple matching engines to be running at the same time; therefore, it is highly recommended that MySQL be used, which can be most easily accomplished through a container.

The docker run command below can be modified to use the username/password/database combination of your choice, just note that you will need them for the URI string later for the various commands.

docker run -d --rm \
    --name chives-mysql \
    -e MYSQL_RANDOM_ROOT_PASSWORD="yes" \
    -e MYSQL_DATABASE="chives" \
    -e MYSQL_USER="chives_u" \
    -e MYSQL_PASSWORD="chives_password" \
    -p 3307:3306 \
    -p 33061:33060 \
    mysql:8.0

After MySQL finished initializing, create the databases schemas by running the initdb command from the chives CLI:

python -m chives -v initdb \
    --sql-uri "mysql+pymysql://chives_u:chives_password@localhost:3307/chives"

The -v flag will set the SQLAlchemy engine to echo MySQL's response. It is optional but helpful as a first time to see what tables are created.

Matching engine and webserver

Start an instance of a matching engine by calling start_engine and passing in the same SQL URI string as before:

python -m chives start_engine \
    --sql-uri "mysql+pymysql://chives_u:chives_password@localhost:3307/chives"

The matching engine will try to connect to the database and the message queue as soon as it starts; if the message queue or the database is not available, the matching engine will crash immediately. If the database is available, but the table schemas are not initialized, then the matching engine will crash when it starts processing stock orders.

The webserver offers a GUI for users to submit stock orders and do other things. It runs as a Flask application, and is started by calling webserver:

python -m chives webserver \
    --sql-uri "mysql+pymysql://chives_u:chives_password@localhost:3307/chives"

Further readings

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chives-exchange-0.2.3.tar.gz (340.1 kB view details)

Uploaded Source

Built Distribution

chives_exchange-0.2.3-py3-none-any.whl (346.6 kB view details)

Uploaded Python 3

File details

Details for the file chives-exchange-0.2.3.tar.gz.

File metadata

  • Download URL: chives-exchange-0.2.3.tar.gz
  • Upload date:
  • Size: 340.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5

File hashes

Hashes for chives-exchange-0.2.3.tar.gz
Algorithm Hash digest
SHA256 1b04b3c16e63ac6de15d195a8a2c8c19e5b6703b8ca7364ebeae52a3a63f0b8e
MD5 d15e54cece2c81874589642b053c25ee
BLAKE2b-256 496add359922936a6d457e3d40dbb49fd372e81c68bec77dad218f6cd0d37665

See more details on using hashes here.

File details

Details for the file chives_exchange-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: chives_exchange-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 346.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5

File hashes

Hashes for chives_exchange-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 139453a50ddbfd34d5cacad81abee69ce620a7e0b0d56d358fec8a6b8760d49a
MD5 bb54d3a0d92f8ec46c565c5c7c98bd01
BLAKE2b-256 435ed14c584cdb3ce4f69467a77fa9e9e581d5bcc270f8ad4bdc646166368701

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page