pqueue
A simple priority queue for use cases in computational finance.
Priority queues are one of the most commonly used structures in computational finance. One example are Central Limit Orders Books, or CLOBS, in which the priority of the execution of orders is given by an orders' price. This specific implementation uses a heap structure where the priority is prepended to the search key. For CLOB orders the priority is the price of an order.
The original implementation of this library was done over an afternoon far back in 2016 to support agent simulations in my doctorate thesis.
See LICENSE for important licensing information.
Instalation
pip install jfaleiro.pqueue
Or as a dependency in poetry:
poetry add jfaleiro.pqueue
poetry update
Use
Say for example you have any structure, like this one, to represent an order:
class NewOrder(NamedTuple):
""" based on https://www.fixglobal.com/home/trader-fix-tags-reading/ """
side: OrderSideEnum
symbol: str
quantity: int
price: Decimal
id: str = None
instruction: OrderExecutionInstruction = OrderExecutionInstruction.ALL_OR_NONE
time_in_force: OrderTimeInForceEnum = OrderTimeInForceEnum.GOOD_TIL_CANCEL
type: OrderTypeEnum = OrderTypeEnum.LIMIT
And a book with orders falling on either bid (buy orders) or asks (sell orders) side:
bids = Heap()
asks = Heap(reverse=True)
As you probably know, the reverse=True is used because orders in an "asks" side are reversed, i.e. lower prices cross before higher prices.
You can add new orders in either side by using a push. You need do specify an id and a priority. For example, to book a new buy order the item is of course the order, the priority is the price, and the id is the order's id:
bids.push(id=order.id, priority=order.price, item=order)
Adding a sell order is exactly the same. The reverse=True takes care of the reverse priority:
asks.push(id=order.id, priority=order.price, item=order)
If you have an id of a previously booked order, you can cancel orders as easily with a remove:
bids.remove(id=action.id)
You can verify crosses with a peek, for example, if want to verify if a cross occurred:
if asks.peek().price <= bids.peek().price:
print('a cross happened!!')
After which you might want to cross (execute) orders on the top of each side:
ask_order = asks.pop()
...
bid_order = bids.pop()
And that does it. It is so simple and short that you can see it as just another proof that finding the adequate patterns is 99% of any solution in engineering. Computational finance is of course no exception.
You can check this implementation of an order book for a full example of use of pqueue.
Metadata
Release files for jfaleiro.pqueue 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jfaleiro.pqueue-0.0.2.tar.gz | 4.7 kB | Without provenance | Details for jfaleiro.pqueue-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jfaleiro.pqueue-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for jfaleiro.pqueue-0.0.2-py3-none-any.whl |
Total release size: 9.6 kB
Release files / jfaleiro.pqueue-0.0.2.tar.gz
| Download URL | jfaleiro.pqueue-0.0.2.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
115ca2d26fc10edacdb0c6611a337cd076da90e8d4172a03a7a98fbb11b788f0
|
|
BLAKE2b-256 checksum How to use checksums |
db22885d8918e78d85f7c100e785fafeb16b2305eb6e257f94ce7ac6f03abee3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.6 CPython/3.8.10 Linux/5.10.25-linuxkit
|
Release files / jfaleiro.pqueue-0.0.2-py3-none-any.whl
| Download URL | jfaleiro.pqueue-0.0.2-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3c20fb63da272e6c3a2b3f4364a7175cd1fc7ebc65a48c945386c433aa12e5b0
|
|
BLAKE2b-256 checksum How to use checksums |
c1e9236c7f365079451e9c71b55eecaf4efcd911d62298bc2e00e8f073ef2b54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.6 CPython/3.8.10 Linux/5.10.25-linuxkit
|
Security for jfaleiro.pqueue 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.