Exchange-core est un noyau d'échange de marché open source basé sur LMAX Disruptor, Eclipse Collections (ex. Goldman Sachs GS Collections), Real Logic Agrona, OpenHFT Chronicle-Wire, LZ4 Java et Adaptive Radix Trees.
Exchange-core comprend :
Conçu pour une évolutivité élevée et un fonctionnement sans pause 24h/24 et 7j/7 dans des conditions de charge élevée et pour fournir des réponses à faible latence :
La configuration à carnet de commandes unique est capable de traiter 5 millions d'opérations par seconde sur du matériel vieux de 10 ans (Intel® Xeon® X5690) avec une dégradation de latence modérée :
taux | 50,0% | 90,0% | 95,0% | 99,0% | 99,9% | 99,99% | pire |
---|---|---|---|---|---|---|---|
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,3 ms | 1,5 ms | 1,8 ms | 1,9 ms | 1,9 ms | 1,9 ms |
Configuration de référence :
mvn install
pom.xml
de votre projet : <dependency>
<groupId>exchange.core2</groupId>
<artifactId>exchange-core</artifactId>
<version>0.5.3</version>
</dependency>
Vous pouvez également cloner ce référentiel et exécuter l'exemple de test.
Créez et démarrez un noyau d'échange vide :
// 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 ();
Créer un nouveau symbole :
// 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 ));
Créez de nouveaux utilisateurs :
// create user uid=301
future = api . submitCommandAsync ( ApiAddUser . builder ()
. uid ( 301L )
. build ());
// create user uid=302
future = api . submitCommandAsync ( ApiAddUser . builder ()
. uid ( 302L )
. build ());
Effectuer des dépôts :
// 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 ());
Passer des commandes :
// 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 ());
Demander un carnet de commandes :
future = api . requestOrderBookAsync ( symbolXbtLtc , 10 );
Manipulations des commandes 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 ());
Vérifiez le solde de l'utilisateur et les commandes GTC :
Future < SingleUserReportResult > report = api . processReport ( new SingleUserReportQuery ( 301 ), 0 );
Vérifiez le solde du système :
// check fees collected
Future < TotalCurrencyBalanceReportResult > totalsReport = api . processReport ( new TotalCurrencyBalanceReportQuery (), 0 );
System . out . println ( "LTC fees collected: " + totalsReport . get (). getFees (). get ( currencyCodeLtc ));
Exchange-core est un projet open source et les contributions sont les bienvenues !