当前位置: 首页 > 软件库 > Web3 > 开源货币/比特币 >

exchange-core

授权协议 Apache-2.0 License
开发语言 Python
所属分类 Web3、 开源货币/比特币
软件类型 开源软件
地区 不详
投 递 者 裴良弼
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

exchange-core

Exchange-core is an open source market exchange core based onLMAX Disruptor,Eclipse Collections (ex. Goldman Sachs GS Collections),Real Logic Agrona,OpenHFT Chronicle-Wire,LZ4 Java,and Adaptive Radix Trees.

Exchange-core includes:

  • orders matching engine
  • risk control and accounting module
  • disk journaling and snapshots module
  • trading, admin and reports API

Designed for high scalability and pauseless 24/7 operation under high-load conditions and providing low-latency responses:

  • 3M users having 10M accounts in total
  • 100K order books (symbols) having 4M pending orders in total
  • less than 1ms worst wire-to-wire target latency for 1M+ operations per second throughput
  • 150ns per matching for large market orders

Single order book configuration is capable to process 5M operations per second on 10-years old hardware (Intel® Xeon® X5690) with moderate latency degradation:

rate 50.0% 90.0% 95.0% 99.0% 99.9% 99.99% worst
125K 0.6µs 0.9µs 1.0µs 1.4µs 4µs 24µs 41µs
250K 0.6µs 0.9µs 1.0µs 1.4µs 9µs 27µs 41µs
500K 0.6µs 0.9µs 1.0µs 1.6µs 14µs 29µs 42µs
1M 0.5µs 0.9µs 1.2µs 4µs 22µs 31µs 45µs
2M 0.5µs 1.2µs 3.9µs 10µs 30µs 39µs 60µs
3M 0.7µs 3.6µs 6.2µs 15µs 36µs 45µs 60µs
4M 1.0µs 6.0µs 9µs 25µs 45µs 55µs 70µs
5M 1.5µs 9.5µs 16µs 42µs 150µs 170µs 190µs
6M 5µs 30µs 45µs 300µs 500µs 520µs 540µs
7M 60µs 1.3ms 1.5ms 1.8ms 1.9ms 1.9ms 1.9ms

Benchmark configuration:

  • Single symbol order book.
  • 3,000,000 inbound messages are distributed as follows: 9% GTC orders, 3% IOC orders, 6% cancel commands, 82% move commands. About 6% of all messages are triggering one or more trades.
  • 1,000 active user accounts.
  • In average ~1,000 limit orders are active, placed in ~750 different price slots.
  • Latency results are only for risk processing and orders matching. Other stuff like network interface latency, IPC, journaling is not included.
  • Test data is not bursty, meaning constant interval between commands (0.2~8µs depending on target throughput).
  • BBO prices are not changing significantly throughout the test. No avalanche orders.
  • No coordinated omission effect for latency benchmark. Any processing delay affects measurements for next following messages.
  • GC is triggered prior/after running every benchmark cycle (3,000,000 messages).
  • RHEL 7.5, network-latency tuned-adm profile, dual X5690 6 cores 3.47GHz, one socket isolated and tickless, spectre/meltdown protection disabled.
  • Java version 8u192, newer Java 8 versions can have a performance bug

Features

  • HFT optimized. Priority is a limit-order-move operation mean latency (currently ~0.5µs). Cancel operation takes ~0.7µs, placing new order ~1.0µs;
  • In-memory working state for accounting data and order books.
  • Event-sourcing - disk journaling and journal replay support, state snapshots (serialization) and restore operations, LZ4 compression.
  • Lock-free and contention-free orders matching and risk control algorithms.
  • No floating-point arithmetic, no loss of significance is possible.
  • Matching engine and risk control operations are atomic and deterministic.
  • Pipelined multi-core processing (based on LMAX Disruptor): each CPU core is responsible for certain processing stage, user accounts shard, or symbol order books shard.
  • Two different risk processing modes (specified per symbol): direct-exchange and margin-trade.
  • Maker/taker fees (defined in quote currency units).
  • Two order books implementations: simple implementation ("Naive") and performance implementation ("Direct").
  • Order types: Immediate-or-Cancel (IOC), Good-till-Cancel (GTC), Fill-or-Kill Budget (FOK-B)
  • Testing - unit-tests, integration tests, stress tests, integrity/consistency tests.
  • Low GC pressure, objects pooling, single ring-buffer.
  • Threads affinity (requires JNA).
  • User suspend/resume operation (reduces memory consumption).
  • Core reports API (user balances, open interest).

Installation

  1. Install library into your Maven's local repository by running mvn install
  2. Add the following Maven dependency to your project's pom.xml:
<dependency>
    <groupId>exchange.core2</groupId>
    <artifactId>exchange-core</artifactId>
    <version>0.5.0</version>
</dependency>

Alternatively, you can clone this repository and run the example test.

Usage examples

Create and start empty exchange core:

// simple async events handler
SimpleEventsProcessor eventsProcessor = new SimpleEventsProcessor(new IEventsHandler() {
    @Override
    public void tradeEvent(TradeEvent tradeEvent) {
        System.out.println("Trade event: " + tradeEvent);
    }

    @Override
    public void reduceEvent(ReduceEvent reduceEvent) {
        System.out.println("Reduce event: " + reduceEvent);
    }

    @Override
    public void rejectEvent(RejectEvent rejectEvent) {
        System.out.println("Reject event: " + rejectEvent);
    }

    @Override
    public void commandResult(ApiCommandResult commandResult) {
        System.out.println("Command result: " + commandResult);
    }

    @Override
    public void orderBook(OrderBook orderBook) {
        System.out.println("OrderBook event: " + orderBook);
    }
});

// default exchange configuration
ExchangeConfiguration conf = ExchangeConfiguration.defaultBuilder().build();

// no serialization
Supplier<ISerializationProcessor> serializationProcessorFactory = () -> DummySerializationProcessor.INSTANCE;

// build exchange core
ExchangeCore exchangeCore = ExchangeCore.builder()
        .resultsConsumer(eventsProcessor)
        .serializationProcessorFactory(serializationProcessorFactory)
        .exchangeConfiguration(conf)
        .build();

// start up disruptor threads
exchangeCore.startup();

// get exchange API for publishing commands
ExchangeApi api = exchangeCore.getApi();

Create new symbol:

// currency code constants
final int currencyCodeXbt = 11;
final int currencyCodeLtc = 15;

// symbol constants
final int symbolXbtLtc = 241;

// create symbol specification and publish it
CoreSymbolSpecification symbolSpecXbtLtc = CoreSymbolSpecification.builder()
        .symbolId(symbolXbtLtc)         // symbol id
        .type(SymbolType.CURRENCY_EXCHANGE_PAIR)
        .baseCurrency(currencyCodeXbt)    // base = satoshi (1E-8)
        .quoteCurrency(currencyCodeLtc)   // quote = litoshi (1E-8)
        .baseScaleK(1_000_000L) // 1 lot = 1M satoshi (0.01 BTC)
        .quoteScaleK(10_000L)   // 1 price step = 10K litoshi
        .takerFee(1900L)        // taker fee 1900 litoshi per 1 lot
        .makerFee(700L)         // maker fee 700 litoshi per 1 lot
        .build();

future = api.submitBinaryDataAsync(new BatchAddSymbolsCommand(symbolSpecXbtLtc));

Create new users:

// create user uid=301
future = api.submitCommandAsync(ApiAddUser.builder()
        .uid(301L)
        .build());

// create user uid=302
future = api.submitCommandAsync(ApiAddUser.builder()
        .uid(302L)
        .build());

Perform deposits:

// first user deposits 20 LTC
future = api.submitCommandAsync(ApiAdjustUserBalance.builder()
        .uid(301L)
        .currency(currencyCodeLtc)
        .amount(2_000_000_000L)
        .transactionId(1L)
        .build());

// second user deposits 0.10 BTC
future = api.submitCommandAsync(ApiAdjustUserBalance.builder()
        .uid(302L)
        .currency(currencyCodeXbt)
        .amount(10_000_000L)
        .transactionId(2L)
        .build());

Place orders:

// first user places Good-till-Cancel Bid order
// he assumes BTCLTC exchange rate 154 LTC for 1 BTC
// bid price for 1 lot (0.01BTC) is 1.54 LTC => 1_5400_0000 litoshi => 10K * 15_400 (in price steps)
future = api.submitCommandAsync(ApiPlaceOrder.builder()
        .uid(301L)
        .orderId(5001L)
        .price(15_400L)
        .reservePrice(15_600L) // can move bid order up to the 1.56 LTC, without replacing it
        .size(12L) // order size is 12 lots
        .action(OrderAction.BID)
        .orderType(OrderType.GTC) // Good-till-Cancel
        .symbol(symbolXbtLtc)
        .build());

// second user places Immediate-or-Cancel Ask (Sell) order
// he assumes wost rate to sell 152.5 LTC for 1 BTC
future = api.submitCommandAsync(ApiPlaceOrder.builder()
        .uid(302L)
        .orderId(5002L)
        .price(15_250L)
        .size(10L) // order size is 10 lots
        .action(OrderAction.ASK)
        .orderType(OrderType.IOC) // Immediate-or-Cancel
        .symbol(symbolXbtLtc)
        .build());

Request order book:

future = api.requestOrderBookAsync(symbolXbtLtc, 10);

GtC orders manipulations:

// first user moves remaining order to price 1.53 LTC
future = api.submitCommandAsync(ApiMoveOrder.builder()
        .uid(301L)
        .orderId(5001L)
        .newPrice(15_300L)
        .symbol(symbolXbtLtc)
        .build());
        
// first user cancel remaining order
future = api.submitCommandAsync(ApiCancelOrder.builder()
        .uid(301L)
        .orderId(5001L)
        .symbol(symbolXbtLtc)
        .build());

Check user balance and GtC orders:

Future<SingleUserReportResult> report = api.processReport(new SingleUserReportQuery(301), 0);

Check system balance:

// check fees collected
Future<TotalCurrencyBalanceReportResult> totalsReport = api.processReport(new TotalCurrencyBalanceReportQuery(), 0);
System.out.println("LTC fees collected: " + totalsReport.get().getFees().get(currencyCodeLtc));

Testing

  • latency test: mvn -Dtest=PerfLatency#testLatencyMargin test
  • throughput test: mvn -Dtest=PerfThroughput#testThroughputMargin test
  • hiccups test: mvn -Dtest=PerfHiccups#testHiccups test
  • serialization test: mvn -Dtest=PerfPersistence#testPersistenceMargin test

TODOs

  • market data feeds (full order log, L2 market data, BBO, trades)
  • clearing and settlement
  • reporting
  • clustering
  • FIX and REST API gateways
  • cryptocurrency payment gateway
  • more tests and benchmarks
  • NUMA-aware and CPU layout custom configuration

Contributing

Exchange-core is an open-source project and contributions are welcome!

Support

  • 在RabbitMQ中,生产者发送消息不会直接将消息投递到队列中,而是先将消息投递到交换机中,在由交换机转发到具体的队列,    队列再将消息以推送或者拉取方式给消费者进行消费    创建消息              路由键              pull/push    生产者------------>交换机------------>队列------------>消费者 Exchange(交

  • 使用Exchange服务发送邮件服务,主要是发附件,和嵌入式图片 首先NuGet   . .NetFrameWork   Microsoft.Exchange.WebServices .net Core Microsoft.Exchange.WebServices.NETStandard   新建一个消息体类 public class SendMailContent { /

  • 1. 官方指导文章 官方文章:https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide Creating a Recurring Appointment To schedule a recurring appointment, create an appointment for the first meeting ti

  • 1、将Spring与Rabbitmq整合,报错,如下所示。 org.springframework.amqp.AmqpIOException: java.io.IOException at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExc

  • exchange邮件 正常发送邮件只需要集成javax.mail就可以了 javamail如果发送邮件自动保存到已发送邮件里面,是需要自己实现的。but pop3协议是实现不了的,必须要实现imap协议。 但在工作中遇到了使用的是微软的邮箱,只有pop3,没有imap。我又不能让他们去开启imap,只能自己解决。 然后看到了exchange,就尝试了下。 1、引入jar包 compile "com

  • 问题描述: 在6月7日的时候,我负责系统中的邮件审批任务突然无法执行,并且提示{"The request failed. 基础连接已经关闭: 发送时发生错误。"}的错误信息。导致邮件审批功能无法正常使用。并且在本地调试时,也会出现同样的提示描述。 检查过程: 1、Exchange的EWS在6月份的时候,禁用了TLS1.0的默认安全协议。原代码中使用的是TLS1.0的默认协议,需进行协议更新 2、T

  • 用java mail 实现Exchange发邮件的功能 1. 需要用到 microsoft.exchange.webservices 导入ews-java-api-2.0.jar, 在pom.xml文件里加入以下代码: com.microsoft.ews-java-api ews-java-api 2.0 2. 新建一个MailUtil.java类: package com.spacex.util

 相关资料
  • crypto-exchange Pulls together list of crypto exchanges to interact with their API's in a uniform fashion. The goal of this project is to be able to interact with a number of different cryptocurrency

  • Dark Exchange 是一个分布式的比特币的 P2P 交换项目,使用 Dart 语言编写。

  • 我试图理解RabbitMQ对发布的保证——到目前为止没有成功——我想知道是否有人可以帮助我解决这个问题: 绑定 交易所 A - 交易所 A - 交易所B- 交易所C- 属性 B1和C1被声明为持久队列 B1和C1都是镜像队列 所有交换机均声明为扇出 我正在运行一个具有多个节点的RabbitMQ集群,因此队列可以在不同的节点上主控 如果我发布到Exchange A,那么我是将消息发布到所有队列还是不

  • Here we outline the design of the AddressBook and how it used by the Peer Exchange Reactor (PEX) to ensure we are connected to good peers and to gossip peers to others. Peer Types Certain peers are sp

  • 我尝试在回调中使用exchange名称。我在Pika中使用SelectConnection适配器处理异步请求。 我的想法是首先在我的程序中构建交换、队列和绑定的列表。然后,我将多个交换和队列声明分派给Pika,并在每个绑定所需的队列和交换收到DeclareOK消息后立即创建绑定。 这意味着我需要知道回调中交换和队列的名称,以便我可以将它们与要创建的绑定相关联。 类似这样: 这将给出以下输出,清楚地

  • 任何人都可以帮助我理解如何代表用户对服务器应用程序的交换服务器进行身份验证?我的用例是同步用户邮箱的邮件,并与我们的服务器更新已读状态和已删除状态。Exchange Online有rest API和Oauth进行身份验证,但不适用于On Premise帐户?是否有一种方法可以对所有不同的部署进行身份验证[在线和内部交换]并访问Outlook邮件? 我们需要一种方法来验证用户,而不需要尝试获取任何凭