ohlc-bar-matching-engine
Minimalist, zero-dependency Python & JavaScript intrabar matching engine (~100 LOC) for historical OHLC candlestick replay and paper-trading simulators: handles limit/stop orders, take-profit / stop-loss priority, opening price gaps (prevClose → open), and isolated/cross forced liquidation.
- Play Live in Browser (English): Historical Kline Candlesticks Trainer (
holdwins.com/en/kline/) - 在线盲盘实战训练(中文): K 线盘感训练器 (
holdwins.com/kline/)
Python Installation & Quick Start
pip install ohlc-bar-matching-engine
from ohlc_bar_matching_engine import create_account, fill, process_bar
acct = create_account(wallet=10000, leverage=10, mode="isolated")
fill(acct, side="buy", qty=0.1, price=100000)
acct["pos"]["tp"] = 105000
process_bar(acct, {"o": 100500, "l": 99800, "h": 105500, "c": 104000}, prev_close=100000)
The Intrabar Ordering Problem
Historical candlestick APIs provide only [open, high, low, close] without tick timestamps. When a single candle spans both a long position's Take-Profit and Stop-Loss (or crosses multiple resting orders across an opening gap), a deterministic path and priority rule is required:
- Gap Segment (
prevClose → open): Every resting trigger crossed between the prior close and the new open executes atopenwithout advancing the cursor past subsequent resting orders in the same gap. - Intrabar Path:
- Bullish bar (
close >= open):open → low → high → close - Bearish bar (
close < open):open → high → low → close
- Bullish bar (
- Trigger Priority: Distance-first along each segment; ties place Stop-Loss ahead of Liquidation so valid protective stops fire before forced liquidation.
Quick Start
import { createAccount, fill, processBar } from './src/engine.mjs';
const acct = createAccount({ wallet: 10000, leverage: 10, mode: 'isolated' });
fill(acct, { side: 'buy', qty: 0.1, price: 100000, liquidity: 'taker' });
acct.pos.tp = 103000;
acct.pos.sl = 96000;
processBar(acct, { o: 100000, h: 104200, l: 98500, c: 103500 }, 100000);
console.log(acct);
Related Open Tools & Guides
- Kline Candlesticks Trainer (EN) · K 线盘感训练器(中文)
- Crypto Volume Profile & Chip Distribution
- Perpetual Futures Liquidation Calculator · 合约计算器
- Stop-Loss & Take-Profit Guide · 止盈止损设置教程
License
MIT © HoldWins
Go Module Installation (pkg.go.dev)
go get github.com/dalangdalang934/ohlc-bar-matching-engine@latest
import "matching" // github.com/dalangdalang934/ohlc-bar-matching-engine
acct := matching.NewAccount(10000, 20, "isolated")
acct.ProcessBar(matching.Bar{O: 95000, H: 96200, L: 94500, C: 95800}, 95000)
- Go Module (
pkg.go.dev): https://pkg.go.dev/github.com/dalangdalang934/ohlc-bar-matching-engine - Liquidation & Margin Modes: Isolated vs Cross Margin Guide · What Is Liquidation?
Metadata
Release files for ohlc-bar-matching-engine 1.1.0
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 |
|---|---|---|---|---|
| ohlc_bar_matching_engine-1.1.0.tar.gz | 5.0 kB | Without provenance | Details for ohlc_bar_matching_engine-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ohlc_bar_matching_engine-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for ohlc_bar_matching_engine-1.1.0-py3-none-any.whl |
Total release size: 11.3 kB
Release files / ohlc_bar_matching_engine-1.1.0.tar.gz
| Download URL | ohlc_bar_matching_engine-1.1.0.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ce6f953426714fd3c50eee88987868b143bfcd33ddd631bcef7a4545103b46a3
|
|
BLAKE2b-256 checksum How to use checksums |
166fd4d68d48cbbf0e5693bee36368f4d7f138f52c37dc8cf9e145558ba6aff3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / ohlc_bar_matching_engine-1.1.0-py3-none-any.whl
| Download URL | ohlc_bar_matching_engine-1.1.0-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
32f36b7b5d8ff7bc58645d41b1a90da3fb24eeb30ab2b0c906ca6178e8a85f4d
|
|
BLAKE2b-256 checksum How to use checksums |
18295eb23eaae04dc933053f35fc321d54dbb566b50a00ea34e17c93ed445b06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for ohlc-bar-matching-engine 1.1.0
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.