A very light matching engine in Python.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist Added distribution and elaborated README Feb 8, 2017
lightmatchingengine Resolve the missing delete of order id during cancellation Dec 27, 2018
.gitignore Updated gitignore Feb 7, 2017
.travis.yml Update travis Feb 8, 2017
LICENSE.txt
MANIFEST Added distribution and elaborated README Feb 8, 2017
MANIFEST.in Added packaging resource Feb 7, 2017
README.md Supported market order and resolve #1 Feb 8, 2017
setup.cfg Added packaging resource Feb 7, 2017
setup.py Supported market order and resolve #1 Feb 8, 2017

README.md

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, order.side)

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, order.side)
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)

Contact

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