Git Product home page Git Product logo

lightmatchingengine's Introduction

LightMatchingEngine

A light matching engine written in Python.

The engine is a trivial object to support

  • Add order - Returns the order and filled trades
  • Cancel order - Returns the original order

The objective is to provide a easy interface for users on the standard price-time priority matching algorithm among different instruments.

Installation

The package can be installed by:

pip install lightmatchingengine

Usage

Create a matching engine instance.

from lightmatchingengine.lightmatchingengine import LightMatchingEngine, Side

lme = LightMatchingEngine()

Place an order.

order, trades = lme.add_order("EUR/USD", 1.10, 1000, Side.BUY)

Cancel an order.

del_order = lme.cancel_order(order.order_id, order.instmt)

Fill an order.

buy_order, trades = lme.add_order("EUR/USD", 1.10, 1000, Side.BUY)
print("Number of trades = %d" % len(trades))                # Number of trades = 0
print("Buy order quantity = %d" % buy_order.qty)            # Buy order quantity = 1000
print("Buy order filled = %d" % buy_order.cum_qty)          # Buy order filled = 0
print("Buy order leaves = %d" % buy_order.leaves_qty)       # Buy order leaves = 1000

sell_order, trades = lme.add_order("EUR/USD", 1.10, 1000, Side.SELL)
print("Number of trades = %d" % len(trades))                # Number of trades = 2
print("Buy order quantity = %d" % buy_order.qty)            # Buy order quantity = 1000
print("Buy order filled = %d" % buy_order.cum_qty)          # Buy order filled = 1000
print("Buy order leaves = %d" % buy_order.leaves_qty)       # Buy order leaves = 0
print("Trade price = %.2f" % trades[0].trade_price)         # Trade price = 1.10
print("Trade quantity = %d" % trades[0].trade_qty)          # Trade quantity = 1000
print("Trade side = %d" % trades[0].trade_side)             # Trade side = 2

Failing to delete an order returns a None value.

del_order = lme.cancel_order(9999, order.instmt)
print("Is order deleted = %d" % (del_order is not None))    # Is order deleted = 0

Supported version

Python 2.x and 3.x are both supported.

Order

The order object contains the following information:

  • Exchange order ID (order_id)
  • Instrument name (instmt)
  • Price (price)
  • Quantity (qty)
  • Side (Buy/Sell) (side)
  • Cumulated filled quantity (cum_qty)
  • Leaves quantity (leaves_qty)

Trade

The trade object contains the following information:

  • Trade ID (trade_id)
  • Instrument name (instmt)
  • Exchange order ID (order_id)
  • Trade price (trade_price)
  • Trade quantity (trade_qty)
  • Trade side (trade_side)

Performance

To run the performance test, run the commands

pip install lightmatchingengine[performance]
python tests/performance/performance_test.py --freq 20

It returns the latency in nanosecond like below

add cancel add (trade > 0) add (trade > 2.0)
count 100 61 27 6
mean 107.954 50.3532 164.412 205.437
std 58.1438 16.3396 36.412 24.176
min 17.1661 11.4441 74.1482 183.105
25% 81.3007 51.9753 141.382 188.47
50% 92.5064 58.4126 152.349 200.748
75% 140.19 59.3662 190.496 211.239
max 445.604 71.0487 248.909 248.909

Contact

For any inquiries, please feel free to contact me by gavincyi at gmail dot com.

lightmatchingengine's People

Contributors

dikkechill avatar gavincyi avatar hajoscher avatar laffini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lightmatchingengine's Issues

No possibility of viewing the state of the book + doesn't seem to consume orders

See attached code. I wrote some simple code that displays the prices and quantities currently available on the book. When I submit a new order that should consume the other side (I buy 500 at the price of the ask), it doesn't seem to do so, although the resulting trade execution is properly recorded. That is, when I view the book after that, I still have 1500 offered on the ask, even though in theory I should have only 1000 left.

testBook.TXT

Support more order types

Hi,
I've been using the project and have found it to be a valuable tool for learning & even real projects. However, I noticed that the current version does not support most order types such as:

  • Stop-Limit Order
  • Stop Market order
  • Trailing Stop Order
  • Post Only Order
  • Limit TP/SL Order (Strategy Order)

I believe that incorporating these order types would significantly increase the usefulness of the project.
Are there any plans to add these features?

Any material on simulating the order matching

Hi, thanks for sharing this cool Repo, I found it quite useful in practice.

I would like to ask that whether there is any good material on introducing the order matching algorithms publicly available? I find details lying behind a matching engine quite complex, and I want to have a better understanding on order matching to facilitate my research.

Looking forward to your reply, thanks !

Bid orders with leaves_qty < 1e-09 not removed from book, results in unexpected behavior

In some unexpected cases LightMatchingEngine gives the following error:

"Match quantity must be larger than zero"

The problem occurs sometimes due to float arithmetic with small quantities.
Because of this the leaves_qty of an order can become smaller than the 1e-9 threshold.
As the checking code in lightmatchingengine.pyx is different for bids than for asks, trying to clear the order with a leaves_qty < 1e-9 is impossible.

To fix this, the leaves_qty and match_qty bid side code in lightmatchingengine.pyx should be made consistent with the ask side code.

110    assert match_qty > 0, "Match quantity must be larger than zero"

Should become

110    assert match_qty >= 1e-9, "Match quantity must be larger than zero"

And

144    if order.leaves_qty > 0.0:

Should become

144    if order.leaves_qty >= 1e-9:

[Question] How to persist the Orderbook state between the API requests?

Totally new to Python, but having read the code I've got the impression it's just the simple matching engine which can be run by long-running process which keeps the order book in-memory and reads the orders from the queue. Is there any way to persist its state in-memory between subsequent API requests? Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.