matching

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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.


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:

  1. Gap Segment (prevClose → open): Every resting trigger crossed between the prior close and the new open executes at open without advancing the cursor past subsequent resting orders in the same gap.
  2. Intrabar Path:
    • Bullish bar (close >= open): open → low → high → close
    • Bearish bar (close < open): open → high → low → close
  3. 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);

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)

Documentation

Overview

Package matching implements a minimalist deterministic intrabar OHLC matching engine for historical candlestick replay, limit/stop orders, inter-bar opening gaps, take-profit/stop-loss execution priority, and isolated/cross forced liquidation.

Live interactive historical candlestick replay trainers:

Index

Constants

View Source
const (
	MakerFee = 0.0002
	TakerFee = 0.0005
	MMR      = 0.005
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	Wallet   float64
	Leverage float64
	Mode     string // "isolated" or "cross"
	Pos      *Position
	Orders   []Order
	Fills    []Fill
}

Account holds wallet balance, margin mode, open position, resting orders, and fill history.

func NewAccount

func NewAccount(wallet, leverage float64, mode string) *Account

NewAccount initializes a paper-trading perpetual account.

func (*Account) ExecuteFill

func (a *Account) ExecuteFill(side string, qty, price float64, maker bool)

ExecuteFill applies a maker or taker execution against the account's single-way position.

func (*Account) LiqPrice

func (a *Account) LiqPrice() float64

LiqPrice computes the estimated forced-liquidation price of the active position, or 0 if none.

func (*Account) ProcessBar

func (a *Account) ProcessBar(bar Bar, prevClose float64)

ProcessBar advances the matching engine across one OHLC bar with deterministic intrabar pathing.

type Bar

type Bar struct {
	O, H, L, C float64
}

Bar represents an OHLC candlestick.

type Fill

type Fill struct {
	Side  string
	Qty   float64
	Price float64
	Fee   float64
	Kind  string
}

Fill records an executed trade or forced liquidation.

type Order

type Order struct {
	ID      int
	Type    string // "limit" or "stop"
	Side    string // "buy" or "sell"
	Qty     float64
	Price   float64
	Trigger float64
}

Order represents a resting limit or stop order.

type Position

type Position struct {
	Side   string // "long" or "short"
	Qty    float64
	Entry  float64
	Margin float64
	TP     *float64
	SL     *float64
}

Position represents an open single-way perpetual position.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL