Python exchange engine
Project description
Python native Exchange engine
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file beige-0.1.2.tar.gz
.
File metadata
- Download URL: beige-0.1.2.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0f0e2e83f40a40afc94d19f94841fb11634a6d85b08f0e4e4bfc4e49266753d |
|
MD5 | 40efe597d93bc959ebe3b5afa2664ad8 |
|
BLAKE2b-256 | e5f3bc4b2581ac8fa15c8d8fa74e2e3cf026a44d01de095e9929bb0bf151373a |
File details
Details for the file beige-0.1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: beige-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2811dd2f3c4b5bf90541a7d818c40ebd166280920f90a286d60a2a5e29482efe |
|
MD5 | c7b0d6a12b845bdba757c5f93a1a9a8d |
|
BLAKE2b-256 | 35d265edea711ccccc85ee6977fbf85ca106f9122e1f3b49979cb5c72df2a7cd |