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를 기반으로 하는 오픈 소스 시장 교환 코어 입니다.
교환 코어에는 다음이 포함됩니다.
고부하 조건에서 높은 확장성과 무중단 작동을 위해 설계되었으며 지연 시간이 짧은 응답을 제공합니다.
단일 주문서 구성은 10년 된 하드웨어(Intel® Xeon® X5690)에서 적당한 지연 시간 저하와 함께 초당 500만 작업을 처리할 수 있습니다.
비율 | 50.0% | 90.0% | 95.0% | 99.0% | 99.9% | 99.99% | 최악의 |
---|---|---|---|---|---|---|---|
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 |
100만 | 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 |
700만 | 60μs | 1.3ms | 1.5ms | 1.8ms | 1.9ms | 1.9ms | 1.9ms |
벤치마크 구성:
mvn install
실행하여 Maven의 로컬 저장소에 라이브러리를 설치합니다.pom.xml
에 다음 Maven 종속성을 추가합니다. <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는 오픈 소스 프로젝트이며 기여를 환영합니다!