ランキングに戻る

Crypto-toolbox/HFT-Orderbook

C

Limit Order Book for high-frequency trading (HFT), as described by WK Selph, implemented in Python3 and C

python3high-frequency-tradingorder-managementorderbookbstavl-treedoubly-linked-listself-balancing-treeslimit-order-bookc
スター成長
スター
1.4k
フォーク
311
週間成長
Issue
6
5001k
2017年7月2020年7月2023年7月2026年7月
README

HFT-Orderbook

Limit Order Book for high-frequency trading (HFT), as described by WK Selph, implemented in C.

Based on WK Selph's Blogpost:

http://howtohft.wordpress.com/2011/02/15/how-to-build-a-fast-limit-order-book/

Available at Archive.org's WayBackMachine:

https://goo.gl/KF1SRm

"There are three main operations that a limit order book (LOB) has to
implement: add, cancel, and execute.  The goal is to implement these
operations in O(1) time while making it possible for the trading model to
efficiently ask questions like “what are the best bid and offer?”, “how much
volume is there between prices A and B?” or “what is order X’s current
position in the book?”.

The vast majority of the activity in a book is usually made up of add and
cancel operations as market makers jockey for position, with executions a
distant third (in fact I would argue that the bulk of the useful information
on many stocks, particularly in the morning, is in the pattern of adds and
cancels, not executions, but that is a topic for another post).  An add
operation places an order at the end of a list of orders to be executed at
a particular limit price, a cancel operation removes an order from anywhere
in the book, and an execution removes an order from the inside of the book
(the inside of the book is defined as the oldest buy order at the highest
buying price and the oldest sell order at the lowest selling price).  Each
of these operations is keyed off an id number (Order.idNumber in the
pseudo-code below), making a hash table a natural structure for tracking
them.

Depending on the expected sparsity of the book (sparsity being the
average distance in cents between limits that have volume, which is
generally positively correlated with the instrument price), there are a
number of slightly different implementations I’ve used.  First it will help
to define a few objects:

    Order
      int idNumber;
      bool buyOrSell;
      int shares; // order size
      int limit;
      int entryTime;
      int eventTime;
      Order *nextOrder;
      Order *prevOrder;
      Limit *parentLimit;

    Limit  // representing a single limit price
      int limitPrice;
      int size;
      int totalVolume;
      Limit *parent;
      Limit *leftChild;
      Limit *rightChild;
      Order *headOrder;
      Order *tailOrder;

    Book
      Limit *buyTree;
      Limit *sellTree;
      Limit *lowestSell;
      Limit *highestBuy;

The idea is to have a binary tree of Limit objects sorted by limitPrice,
each of which is itself a doubly linked list of Order objects.  Each side
of the book, the buy Limits and the sell Limits, should be in separate trees
so that the inside of the book corresponds to the end and beginning of the
buy Limit tree and sell Limit tree, respectively.  Each order is also an
entry in a map keyed off idNumber, and each Limit is also an entry in a
map keyed off limitPrice.

With this structure you can easily implement these key operations with
good performance:

Add – O(log M) for the first order at a limit, O(1) for all others
Cancel – O(1)
Execute – O(1)
GetVolumeAtLimit – O(1)
GetBestBid/Offer – O(1)

where M is the number of price Limits (generally << N the number of orders).
Some strategy for keeping the limit tree balanced should be used because the
nature of markets is such that orders will be being removed from one side
of the tree as they’re being added to the other.  Keep in mind, though,
that it is important to be able to update Book.lowestSell/highestBuy
in O(1) time when a limit is deleted (which is why each Limit has a Limit
*parent) so that GetBestBid/Offer can remain O(1)."
関連リポジトリ
fastapi/fastapi

FastAPI framework, high performance, easy to learn, fast to code, ready for production

PythonPyPIMIT Licensepythonjson
fastapi.tiangolo.com
100.8k9.7k
sherlock-project/sherlock

Hunt down social media accounts by username across social networks

PythonPyPIMIT Licenseosintreconnaissance
sherlockproject.xyz
86.9k10.2k
666ghj/MiroFish

A Simple and Universal Swarm Intelligence Engine, Predicting Anything. 简洁通用的群体智能引擎,预测万物

PythonPyPIGNU Affero General Public License v3.0agent-memoryfinancial-forecasting
mirofish.ai
69.1k10.8k
Asabeneh/30-Days-Of-Python

The 30 Days of Python programming challenge is a step-by-step guide to learn the Python programming language in 30 days. This challenge may take more than 100 days. Follow your own pace. These videos may help too: https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw

PythonPyPI30-days-of-pythonpython
68.8k12.8k
Textualize/rich

Rich is a Python library for rich text and beautiful formatting in the terminal.

PythonPyPIMIT Licensepythonpython3
rich.readthedocs.io/en/latest/
56.9k2.3k
astral-sh/ruff

An extremely fast Python linter and code formatter, written in Rust.

Rustcrates.ioMIT Licenselinterpep8
docs.astral.sh/ruff
48.8k2.3k
666ghj/BettaFish

微舆:人人可用的多Agent舆情分析助手,打破信息茧房,还原舆情原貌,预测未来走向,辅助决策!从0实现,不依赖任何框架。

PythonPyPIGNU General Public License v2.0agent-frameworkdata-analysis
deepwiki.com/666ghj/BettaFish
41.8k7.6k
mouredev/Hello-Python

Curso para aprender el lenguaje de programación Python desde cero y para principiantes. 100 clases, 44 horas en vídeo, código, proyectos y grupo de chat. Fundamentos, frontend, backend, testing, IA...

PythonPyPIApache License 2.0fastapimongodb
mouredev.link/python
36.5k2.3k
doocs/leetcode

🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解

JavaMavenCreative Commons Attribution Share Alike 4.0 Internationalalgorithmscpp
leetcode.doocs.org
36.3k9.4k
soxoj/maigret

🕵️‍♂️ Collect a dossier on a person by username from 3000+ sites

PythonPyPIMIT Licenseosintsocial-network
maigret.app
35.6k2.7k
zhayujie/chatgpt-on-wechat

基于大模型搭建的聊天机器人,同时支持 微信公众号、企业微信应用、飞书、钉钉 等接入,可选择GPT3.5/GPT-4o/GPT-o1/ DeepSeek/Claude/文心一言/讯飞星火/通义千问/ Gemini/GLM-4/Claude/Kimi/LinkAI,能处理文本、语音和图片,访问操作系统和互联网,支持基于自有知识库进行定制企业智能客服。

PythonPyPIMIT Licensechatgptwechat
docs.link-ai.tech/cow
35.5k9k
celery/celery

Distributed Task Queue (development branch)

PythonPyPIOtherpythontask-manager
docs.celeryq.dev
28.7k5.1k