PHPackages                             mehdi9324/mengine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. mehdi9324/mengine

ActiveLibrary[Framework](/categories/framework)

mehdi9324/mengine
=================

Matching Engine For Laravel

v1.0.0(4y ago)010MITPHP

Since Dec 22Pushed 4y agoCompare

[ Source](https://github.com/mehdi9324/mengine)[ Packagist](https://packagist.org/packages/mehdi9324/mengine)[ RSS](/packages/mehdi9324-mengine/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Laravel Package for Matching Engine
===================================

[](#laravel-package-for-matching-engine)

快速开始
----

[](#快速开始)

- 安装: `composer require sting_bo/mengine`
- 复制配置文件: `php artisan vendor:publish`

### 依赖

[](#依赖)

- predis

### 号外

[](#号外)

- **[已经出Golang微服务撮合啦](https://github.com/stingbo/gome)**，欢迎使用并提issue

### 使用说明

[](#使用说明)

- 已有数据的系统如果使用此库，可以自己写一个初始化脚本，先把数据跑入队列
- #### 用户下单

    [](#用户下单)
- 下单后，先存入数据库，然后才开始下面步骤，实例化单据对象

```
use StingBo\Mengine\Core\Order;

$uuid = 3; // 用户唯一标识
$oid = 4; // 订单唯一标识
$symbol = 'abc2usdt'; // 交易对
$transaction = 'buy'; // 交易方向，buy/sell
$price = 0.4; // 交易价格，会根据设置精度转化为整数
$volume = 15; // 交易数量，会根据设置精度转化为整数

$order = new Order($uuid, $oid, $symbol, $transaction, $volume, $price);
```

`交易方向`与`交易精度`可在配置文件灵活设置

```
return [
    'mengine' => [
        // 交易类型，不可更改
        'transaction' => [
            'buy',
            'sell',
        ],
        // 精度，可更改
        'accuracy' => 8, //default
        'test2usdt_accuracy' => 4, //设置交易对精度则使用，没有则取accuracy
    ],
];
```

- push到队列，队列任务需要手动开启

```
use StingBo\Mengine\Services\MengineService;

$ms = new MengineService();
$ms->pushQueue($order);
```

开启队列任务： `php artisan queue:work --queue=abc2usdt`也可以使用`horizon`与`supervisor`来辅助，事半功倍！

队列消费时会进入撮合程序，大概的步骤如下:

1. 获取匹配委托订单
2. 如果没有匹配的订单，则进入委托池，触发委托池变更事件，详见第5点
3. 如果有匹配的委托，程序撮合，更新委托池数据
4. 交易成功会触发事件，开发者要在监听器里处理有交易的委托单，比如更新数据库数据，WebSocket通知等 在EventServiceProvider里为撮合成功的事件注册监听器:

```
// 撮合成功通知，参数分别是：当前订单，被匹配的单据，交易数量
event(new MatchEvent($order, $match_order, $match_volume));

// 注册监听器
protected $listen = [
    'StingBo\Mengine\Events\MatchEvent' => [
        'App\Listeners\YourListener', // 你自己的监听器，应该也使用异步来实现
    ],
];
```

5. 如果只是部分成交，则剩余部分进入委托池，触发委托池变更事件，K线或者深度列表变更通知等， 注册监听器如下：

```
// 委托池数据变更事件
event(new PushQueueEvent($order));

// 注册监听器
protected $listen = [
    'StingBo\Mengine\Events\PushQueueEvent' => [
        'App\Listeners\YourListener', // 你自己的监听器，应该也使用异步来实现
    ],
];
```

- #### 用户撤单

    [](#用户撤单)

撤单流程应该是先查询数据库确认是否可撤销，再从redis里删除数据成功，最后更新回数据库

```
$order = new Order($uuid, $oid, $symbol, $transaction, $volume, $price);
$ms = new MengineService();
$ms->deleteOrder($order);
```

此撮合引擎没有实现像数据库那样的锁机制，为了防止有单子在被撮合时又有撤销的命令出现，所以下单与撤单都走的同一个队列，保证了顺序性，每个交易对是隔离的队列，效率也有一定的保证，但开发需要实现异步通知用户功能，注册监听器如下：

```
// 撤单成功通知
event(new DeleteOrderSuccEvent($order));

// 注册监听器
protected $listen = [
    'StingBo\Mengine\Events\DeleteOrderSuccEvent' => [
        'App\Listeners\YourListener', // 你自己的监听器，应该也使用异步来实现
    ],
];
```

- #### 获取某个交易对买/卖深度列表

    [](#获取某个交易对买卖深度列表)

```
$symbol = 'abc2cny';
$transaction = 'buy';
$ms = new MengineService();
$ms->getDepth($symbol, $transaction);
```

### 总结

[](#总结)

本地垃圾笔记本上测试，交易对撮合速度平均在200笔/s，后续将继续优化撮合速度

[![基于redis的撮合引擎设计](https://raw.githubusercontent.com/stingbo/image/master/%E6%95%B0%E5%AD%97%E8%B4%A7%E5%B8%81%E4%BA%A4%E6%98%93%E6%89%80-%E5%9F%BA%E4%BA%8Eredis%E7%9A%84%E7%AE%80%E5%8D%95%E6%92%AE%E5%8D%95%E5%BC%95%E6%93%8E.png)](https://raw.githubusercontent.com/stingbo/image/master/%E6%95%B0%E5%AD%97%E8%B4%A7%E5%B8%81%E4%BA%A4%E6%98%93%E6%89%80-%E5%9F%BA%E4%BA%8Eredis%E7%9A%84%E7%AE%80%E5%8D%95%E6%92%AE%E5%8D%95%E5%BC%95%E6%93%8E.png)

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 69.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1604d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/aeff91d9cbc617ebd3a1fc087f0f20291d7ae8228ffa498d636c4c30a3e1f38c?d=identicon)[mehdi9324](/maintainers/mehdi9324)

---

Top Contributors

[![stingbo](https://avatars.githubusercontent.com/u/12377271?v=4)](https://github.com/stingbo "stingbo (9 commits)")[![mehdi9324](https://avatars.githubusercontent.com/u/22196673?v=4)](https://github.com/mehdi9324 "mehdi9324 (4 commits)")

### Embed Badge

![Health badge](/badges/mehdi9324-mengine/health.svg)

```
[![Health](https://phpackages.com/badges/mehdi9324-mengine/health.svg)](https://phpackages.com/packages/mehdi9324-mengine)
```

###  Alternatives

[bagisto/bagisto

Bagisto Laravel E-Commerce

26.2k161.6k7](/packages/bagisto-bagisto)[yiisoft/yii2-redis

Redis Cache, Session and ActiveRecord for the Yii framework

48011.7M245](/packages/yiisoft-yii2-redis)[unopim/unopim

UnoPim Laravel PIM

9.4k1.8k](/packages/unopim-unopim)[themosis/framework

The Themosis framework.

676307.9k18](/packages/themosis-framework)[oro/platform

Business Application Platform (BAP)

644134.8k84](/packages/oro-platform)[jorenvanhocht/blogify

Laravel 5 blog package

1821.1k](/packages/jorenvanhocht-blogify)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
