Skip to main content

Python native Exchange engine

WebsitePyPIPyPI - LicenseGitHub issues

Beige

Why Beige?

Because it's not particularly attractive, there's probably a better color out there, but it kinda works. I was surprised that there is no package existing with the name beige yet!

A simple, slow, exchange library

Beige is a pretty simple library that provides the full functionality of a matching engine, with features for tracking account balances and ongoing quote.

Example

Import the right bits, and spin up the engine

from beige import Engine, Order, Market, Limit, Side

# Create the engine
eng = Engine()

Account balances can be initialized at the start of the market session

# Start a person off with some given account balance
eng.accounts.init('person-a', {'$':100.01, 'TSLA':1})

# Person is missing 1 dollar, and owns 10 'what?'
eng.accounts.init('person-b', {'$':-1, 'what?':10})

Then orders can be submitted in a fairly intuitive way:

# Limit buy order for 'what?' security, 1
# Store the ID, and we can query its status later
oid = eng.submit(
  Order(
    instrument = 'what?',
    type_ = Limit(10),
    side = Side.Sell,
    quantity = 5,
    entity = 'person-b',
  )
)

# Submit a market order that
eng.submit(
  Order(
    'WHAT?',
    Market(),
    Side.Buy,
    1,
    'person-a',
  )
)

Once the ID for an order has been retrieved, we can see the full information on the order at any time:

o = eng.status(oid)
# type(o) == dict
# o.keys() == ['sym','price','side','qty','acct','id','submitted','filled','averagepx','filledtime','status']

A full chronological transaction dataset is available

df = eng.txs
# df.columns = ['qty','px','time','sym']

At any point, a quote can be requested. Note that None will be returned for any value that isn't available, for example, last will be empty if no transactions have taken place yet.

bid, ask, last, bidsize, asksize, lastsize = eng.quote('TSLA')

Limitations

  • Speed. The software hasn't been fully optimized and many of the data structures could really benefit from a bit of love and care.
  • Integer-only quantities
  • All account entities and securities are case insensitive, and will be displayed in all caps
  • the "$" security encodes cash, and is the base unit for all security transactions. Sorry ex-USA users!

Testing

  • Correctness => python3 tests.py
  • Speed => python3 tests.py speed
  • Profiler => python3 tests.py profile

I'm averaging about 0.15 ms per order execution. This works in the sequential market I'm working on, but this may be orders of magnitude slower than needed in your use case. Feel free to improve the speed, but please pull request back to me.

Metadata

Release files for beige 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for beige 0.1.2
File Size Uploaded
beige-0.1.2.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for beige 0.1.2
File Interpreter ABI Platform
beige-0.1.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 8.2 kB

Release files / beige-0.1.2.tar.gz

Download URL beige-0.1.2.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b0f0e2e83f40a40afc94d19f94841fb11634a6d85b08f0e4e4bfc4e49266753d
BLAKE2b-256 checksum
How to use checksums
e5f3bc4b2581ac8fa15c8d8fa74e2e3cf026a44d01de095e9929bb0bf151373a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2

Release files / beige-0.1.2-py2.py3-none-any.whl

Download URL beige-0.1.2-py2.py3-none-any.whl
Size 3.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2811dd2f3c4b5bf90541a7d818c40ebd166280920f90a286d60a2a5e29482efe
BLAKE2b-256 checksum
How to use checksums
35d265edea711ccccc85ee6977fbf85ca106f9122e1f3b49979cb5c72df2a7cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page