exchange core
0.5.3
Exchange-core 是一個基於 LMAX Disruptor、Eclipse Collections(例如 Goldman Sachs GS Collections)、Real Logic Agrona、OpenHFT Chronicle-Wire、LZ4 Java 和 Adaptive Radix Trees的開源市場交易核心。
Exchange 核心包括:
專為高負載條件下的高可擴展性和不間斷 24/7 操作而設計,並提供低延遲響應:
單一訂單簿配置能夠在 10 年舊硬體(Intel® 至強® X5690)上每秒處理 500 萬次操作,且延遲程度適中:
速度 | 50.0% | 90.0% | 95.0% | 99.0% | 99.9% | 99.99% | 最糟糕的 |
---|---|---|---|---|---|---|---|
125K | 0.6微秒 | 0.9微秒 | 1.0微秒 | 1.4微秒 | 4微秒 | 24微秒 | 41微秒 |
25萬 | 0.6微秒 | 0.9微秒 | 1.0微秒 | 1.4微秒 | 9微秒 | 27微秒 | 41微秒 |
50萬 | 0.6微秒 | 0.9微秒 | 1.0微秒 | 1.6微秒 | 14微秒 | 29微秒 | 42微秒 |
1M | 0.5微秒 | 0.9微秒 | 1.2微秒 | 4微秒 | 22微秒 | 31微秒 | 45微秒 |
2M | 0.5微秒 | 1.2微秒 | 3.9微秒 | 10微秒 | 30微秒 | 39微秒 | 60微秒 |
3M | 0.7微秒 | 3.6微秒 | 6.2微秒 | 15微秒 | 36微秒 | 45微秒 | 60微秒 |
4M | 1.0微秒 | 6.0微秒 | 9微秒 | 25微秒 | 45微秒 | 55微秒 | 70微秒 |
5M | 1.5微秒 | 9.5微秒 | 16微秒 | 42微秒 | 150微秒 | 170微秒 | 190微秒 |
6M | 5微秒 | 30微秒 | 45微秒 | 300微秒 | 500微秒 | 520微秒 | 540微秒 |
7M | 60微秒 | 1.3毫秒 | 1.5毫秒 | 1.8毫秒 | 1.9毫秒 | 1.9毫秒 | 1.9毫秒 |
基準配置:
mvn install
將庫安裝到 Maven 的本機儲存庫中pom.xml
: <dependency>
<groupId>exchange.core2</groupId>
<artifactId>exchange-core</artifactId>
<version>0.5.3</version>
</dependency>
或者,您可以克隆此存儲庫並運行範例測試。
創建並啟動空交換核心:
// 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 ();
建立新符號:
// 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 user uid=301
future = api . submitCommandAsync ( ApiAddUser . builder ()
. uid ( 301L )
. build ());
// create user uid=302
future = api . submitCommandAsync ( ApiAddUser . builder ()
. uid ( 302L )
. build ());
執行存款:
// 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 ());
下訂單:
// 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 ());
索取訂單:
future = api . requestOrderBookAsync ( symbolXbtLtc , 10 );
GTC 指令操作:
// 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 ());
查看用戶餘額和GtC訂單:
Future < SingleUserReportResult > report = api . processReport ( new SingleUserReportQuery ( 301 ), 0 );
檢查系統平衡:
// check fees collected
Future < TotalCurrencyBalanceReportResult > totalsReport = api . processReport ( new TotalCurrencyBalanceReportQuery (), 0 );
System . out . println ( "LTC fees collected: " + totalsReport . get (). getFees (). get ( currencyCodeLtc ));
Exchange-core 是一個開源項目,歡迎貢獻!