项目作者: stingbo

项目描述 :
Matching Engine For Laravel(基于redis的撮合引擎)
高级语言: PHP
项目地址: git://github.com/stingbo/mengine.git
创建时间: 2019-11-02T10:39:47Z
项目社区:https://github.com/stingbo/mengine

开源协议:

下载


Laravel Package for Matching Engine

Quick Start

  • Install: composer require sting_bo/mengine
  • Copy configuration file: php artisan vendor:publish

Dependencies

  • predis

News

Usage Instructions

  • For existing systems with data, if using this library, you can write an initialization script to first run the data into the queue.

  • Placing an Order

  • After placing an order, store it in the database and then instantiate the order object.

  1. use StingBo\Mengine\Core\Order;
  2. $uuid = 3; // User unique identifier
  3. $oid = 4; // Order unique identifier
  4. $symbol = 'abc2usdt'; // Trading pair
  5. $transaction = 'buy'; // Trading direction, buy/sell
  6. $price = 0.4; // Trading price, will be converted to an integer based on the set precision
  7. $volume = 15; // Trading quantity, will be converted to an integer based on the set precision
  8. $order = new Order($uuid, $oid, $symbol, $transaction, $volume, $price);

Transaction directionandprecisioncan be flexibly set in the configuration file.

  1. return [
  2. 'mengine' => [
  3. // Transaction types, not subject to change.
  4. 'transaction' => [
  5. 'buy',
  6. 'sell',
  7. ],
  8. // Default precision, can be changed.
  9. 'accuracy' => 8,
  10. // If the precision for the trading pair is set, use it; otherwise, take the default accuracy.
  11. 'abc2usdt_accuracy' => 6, // Example of a trading pair
  12. 'test2usdt_accuracy' => 7, // Example of a trading pair
  13. // If strict mode is set to true, it will validate that the decimal places of the transaction volume or price must be less than the configured length, otherwise the order will fail.
  14. // If strict mode is set to false, the data will be truncated to the configured decimal places length.
  15. 'strict_mode' => false,
  16. ],
  17. ];
  • Push to the queue, queue tasks need to be manually started.
    ```php
    use StingBo\Mengine\Services\MengineService;

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

  1. Start the queue task:
  2. `php artisan queue:work --queue=abc2usdt`
  3. You can also use `horizon` and `supervisor` to assist, making your work more efficient!
  4. When the queue is consumed, it will enter the matching program. The general steps are as follows:
  5. 1. Get matching delegated orders.
  6. 2. If there are no matching orders, enter the order pool, triggering the order pool change event, see point 5.
  7. 3. If there are matching orders, the program matches and updates the order pool data.
  8. 4. Successful transactions trigger events. Developers should handle orders with transactions in listeners, such as updating database data, WebSocket notifications, etc.
  9. In EventServiceProvider, register listeners for successful matches:
  10. ```php
  11. // Successful match notification, parameters are: current order, matched order, transaction quantity
  12. event(new MatchEvent($order, $match_order, $match_volume));
  13. // Register listener
  14. protected $listen = [
  15. 'StingBo\Mengine\Events\MatchEvent' => [
  16. 'App\Listeners\YourListener', // Your own listener, should also be implemented asynchronously
  17. ],
  18. ];
  1. If only partially filled, the remaining part enters the order pool, triggering the order pool change event, notifying K-line or depth list changes, etc.
    Register the listener as follows:
    ```php
    // Order pool data change event
    event(new PushQueueEvent($order));

// Register listener
protected $listen = [
‘StingBo\Mengine\Events\PushQueueEvent’ => [
‘App\Listeners\YourListener’, // Your own listener, should also be implemented asynchronously
],
];

  1. * #### Canceling an Order ####
  2. The cancellation process should be to first query the database to confirm if it can be canceled, then successfully delete the data from redis, and finally update the database.
  3. ```php
  4. $order = new Order($uuid, $oid, $symbol, $transaction, $volume, $price);
  5. $ms = new MengineService();
  6. $ms->deleteOrder($order);

This matching engine does not implement locking mechanisms like databases. To prevent a situation where an order is being matched and a cancellation command is issued, both placing and canceling orders use the same queue to ensure order, and each trading pair has an isolated queue. This ensures efficiency, but developers need to implement asynchronous notification functionality. Register the listener as follows:

  1. // Successful cancellation notification
  2. event(new DeleteOrderSuccEvent($order));
  3. // Register listener
  4. protected $listen = [
  5. 'StingBo\Mengine\Events\DeleteOrderSuccEvent' => [
  6. 'App\Listeners\YourListener', // Your own listener, should also be implemented asynchronously
  7. ],
  8. ];
  • Obtaining Buy/Sell Depth List for a Trading Pair

    1. $symbol = 'abc2cny';
    2. $transaction = 'buy';
    3. $ms = new MengineService();
    4. $ms->getDepth($symbol, $transaction);

Summary

Tested on a local, average matching speed for transactions is around 200 per second. Further optimizations for matching speed are planned for the future.

Design of a Matching Engine Based on Redis

Technical Support

contact us detail
QQ Group 871358160
Email sting_bo@163.com