diff --git a/docker/linux b/docker/linux index 8ff762140..d4a32d325 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit 8ff7621408e9be2dcfe53704625859c20db811f3 +Subproject commit d4a32d32592c688c3a1f042750fd3323bcdfd38a diff --git a/externals b/externals index 1884a761e..2ddd46715 160000 --- a/externals +++ b/externals @@ -1 +1 @@ -Subproject commit 1884a761e4a0296b59c6801ec7e62acca4d22997 +Subproject commit 2ddd4671513460faf006cd89a851937575cbc9c3 diff --git a/src/server/sparkd/CMakeDeps.cmake b/src/server/sparkd/CMakeDeps.cmake index 107661a30..554c743b3 100644 --- a/src/server/sparkd/CMakeDeps.cmake +++ b/src/server/sparkd/CMakeDeps.cmake @@ -1 +1 @@ -set(sources ;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/Help.cpp;cmds/Md.cpp;cmds/Run.cpp;cmds/Test.cpp;cmds/TestCeloxica.cpp;cmds/TestOrderman.cpp;event_grid/EventNode.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/store_tests.cpp;main.cpp;md/platforms/celoxica/CXListener.cpp;md/platforms/celoxica/CXMain.cpp;md/platforms/celoxica/tests/cx_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/MDP3Utils.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;orderman/components/FillRecorder.cpp;orderman/components/Gateway.cpp;orderman/components/MDSource.cpp;orderman/components/NativeOrderMan.cpp;orderman/components/OrderRecorder.cpp;orderman/components/SpreaderOrderMan.cpp;orderman/events/ExecutionReport.cpp;orderman/events/NativeOrderStateChange.cpp;orderman/events/SpreaderOrderStateChange.cpp;orderman/events/TimeChange.cpp;orderman/native_orders/NativeOrder.cpp;orderman/native_orders/NativeOrderBuilder.cpp;orderman/native_orders/NativeOrderFactory.cpp;orderman/persistence/FillDB.cpp;orderman/persistence/OrderDB.cpp;orderman/persistence/gen_order_id.cpp;orderman/service/OrderServiceServer.cpp;orderman/spreader_orders/SpreaderOrder.cpp;orderman/spreader_orders/SpreaderOrderBase.cpp;orderman/tests/spreader_order_tests.cpp;orderman/utils/fix_order_codec.cpp;orderman/utils/orderman_utils.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/tests/PersistenceServiceTests.cpp;risk_limits/IRiskLimitObserver.cpp;risk_limits/RiskLimitManager.cpp;risk_limits/RiskLimiter.cpp;risk_limits/SharedRiskLimiter.cpp;risk_limits/SymbolKey.cpp;risk_limits/tests/RiskLimitsTests.cpp;securities/SecurityDB.cpp;spreader/QuoteLegPriceQuantityState.cpp;spreader/SingleFillHedger.cpp;spreader/SingleLegQuoter.cpp;spreader/SpreaderBase.cpp;testing/catch_test_runner.cpp;user/UserServiceServer.cpp) \ No newline at end of file +set(sources ;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/Help.cpp;cmds/Md.cpp;cmds/Run.cpp;cmds/Test.cpp;cmds/TestCeloxica.cpp;cmds/TestOrderman.cpp;event_grid/EventNode.cpp;exchange_sim/Application.cpp;exchange_sim/Market.cpp;exchange_sim/ordermatch.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/store_tests.cpp;main.cpp;md/platforms/celoxica/CXListener.cpp;md/platforms/celoxica/CXMain.cpp;md/platforms/celoxica/tests/cx_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/MDP3Utils.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;orderman/components/FillRecorder.cpp;orderman/components/Gateway.cpp;orderman/components/MDSource.cpp;orderman/components/NativeOrderMan.cpp;orderman/components/OrderRecorder.cpp;orderman/components/SpreaderOrderMan.cpp;orderman/events/ExecutionReport.cpp;orderman/events/NativeOrderStateChange.cpp;orderman/events/SpreaderOrderStateChange.cpp;orderman/events/TimeChange.cpp;orderman/native_orders/NativeOrder.cpp;orderman/native_orders/NativeOrderBuilder.cpp;orderman/native_orders/NativeOrderFactory.cpp;orderman/persistence/FillDB.cpp;orderman/persistence/OrderDB.cpp;orderman/persistence/gen_order_id.cpp;orderman/service/OrderServiceServer.cpp;orderman/spreader_orders/SpreaderOrder.cpp;orderman/spreader_orders/SpreaderOrderBase.cpp;orderman/tests/spreader_order_tests.cpp;orderman/utils/fix_order_codec.cpp;orderman/utils/orderman_utils.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/tests/PersistenceServiceTests.cpp;risk_limits/IRiskLimitObserver.cpp;risk_limits/RiskLimitManager.cpp;risk_limits/RiskLimiter.cpp;risk_limits/SharedRiskLimiter.cpp;risk_limits/SymbolKey.cpp;risk_limits/tests/RiskLimitsTests.cpp;securities/SecurityDB.cpp;spreader/QuoteLegPriceQuantityState.cpp;spreader/SingleFillHedger.cpp;spreader/SingleLegQuoter.cpp;spreader/SpreaderBase.cpp;testing/catch_test_runner.cpp;user/UserServiceServer.cpp) \ No newline at end of file diff --git a/src/server/sparkd/CMakeLists.txt b/src/server/sparkd/CMakeLists.txt index 2969443c9..54f29873a 100644 --- a/src/server/sparkd/CMakeLists.txt +++ b/src/server/sparkd/CMakeLists.txt @@ -33,6 +33,7 @@ target_link_libraries( sparkd double-conversion pugixml onixs-cmemdh-all-4.3.0 + quickfix -static-libgcc -static-libstdc++ -pthread) @@ -41,4 +42,4 @@ target_link_libraries( sparkd file(COPY jetfix/tests/sample_fix.dat DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test_data) - \ No newline at end of file + diff --git a/src/server/sparkd/exchange_sim/Application.cpp b/src/server/sparkd/exchange_sim/Application.cpp new file mode 100644 index 000000000..ce2e17da5 --- /dev/null +++ b/src/server/sparkd/exchange_sim/Application.cpp @@ -0,0 +1,223 @@ +#include "Application.h" +#include + +#include + +void Application::onLogon( const FIX::SessionID& ) {} + +void Application::onLogout( const FIX::SessionID& ) {} + +void Application::fromApp( const FIX::Message& message, + const FIX::SessionID& sessionID ) +throw( FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType ) +{ + crack( message, sessionID ); +} + +void Application::onMessage( const FIX42::NewOrderSingle& message, const FIX::SessionID& ) +{ + FIX::SenderCompID senderCompID; + FIX::TargetCompID targetCompID; + FIX::ClOrdID clOrdID; + FIX::Symbol symbol; + FIX::Side side; + FIX::OrdType ordType; + FIX::Price price; + FIX::OrderQty orderQty; + FIX::TimeInForce timeInForce( FIX::TimeInForce_DAY ); + + message.getHeader().get( senderCompID ); + message.getHeader().get( targetCompID ); + message.get( clOrdID ); + message.get( symbol ); + message.get( side ); + message.get( ordType ); + if ( ordType == FIX::OrdType_LIMIT ) + message.get( price ); + message.get( orderQty ); + message.getFieldIfSet( timeInForce ); + + try + { + if ( timeInForce != FIX::TimeInForce_DAY ) + throw std::logic_error( "Unsupported TIF, use Day" ); + + Order order( clOrdID, symbol, senderCompID, targetCompID, + convert( side ), convert( ordType ), + price, (long)orderQty ); + + processOrder( order ); + } + catch ( std::exception & e ) + { + rejectOrder( senderCompID, targetCompID, clOrdID, symbol, side, e.what() ); + } +} + +void Application::onMessage( const FIX42::OrderCancelRequest& message, const FIX::SessionID& ) +{ + FIX::OrigClOrdID origClOrdID; + FIX::Symbol symbol; + FIX::Side side; + + message.get( origClOrdID ); + message.get( symbol ); + message.get( side ); + + try + { + processCancel( origClOrdID, symbol, convert( side ) ); + } + catch ( std::exception& ) {}} + +void Application::onMessage( const FIX42::MarketDataRequest& message, const FIX::SessionID& ) +{ + FIX::MDReqID mdReqID; + FIX::SubscriptionRequestType subscriptionRequestType; + FIX::MarketDepth marketDepth; + FIX::NoRelatedSym noRelatedSym; + FIX42::MarketDataRequest::NoRelatedSym noRelatedSymGroup; + + message.get( mdReqID ); + message.get( subscriptionRequestType ); + if ( subscriptionRequestType != FIX::SubscriptionRequestType_SNAPSHOT ) + throw( FIX::IncorrectTagValue( subscriptionRequestType.getField() ) ); + message.get( marketDepth ); + message.get( noRelatedSym ); + + for ( int i = 1; i <= noRelatedSym; ++i ) + { + FIX::Symbol symbol; + message.getGroup( i, noRelatedSymGroup ); + noRelatedSymGroup.get( symbol ); + } +} + +void Application::onMessage( const FIX43::MarketDataRequest& message, const FIX::SessionID& ) +{ + std::cout << message.toXML() << std::endl; +} + +void Application::updateOrder( const Order& order, char status ) +{ + FIX::TargetCompID targetCompID( order.getOwner() ); + FIX::SenderCompID senderCompID( order.getTarget() ); + + FIX42::ExecutionReport fixOrder + ( FIX::OrderID ( order.getClientID() ), + FIX::ExecID ( m_generator.genExecutionID() ), + FIX::ExecTransType ( FIX::ExecTransType_NEW ), + FIX::ExecType ( status ), + FIX::OrdStatus ( status ), + FIX::Symbol ( order.getSymbol() ), + FIX::Side ( convert( order.getSide() ) ), + FIX::LeavesQty ( order.getOpenQuantity() ), + FIX::CumQty ( order.getExecutedQuantity() ), + FIX::AvgPx ( order.getAvgExecutedPrice() ) ); + + fixOrder.set( FIX::ClOrdID( order.getClientID() ) ); + fixOrder.set( FIX::OrderQty( order.getQuantity() ) ); + + if ( status == FIX::OrdStatus_FILLED || + status == FIX::OrdStatus_PARTIALLY_FILLED ) + { + fixOrder.set( FIX::LastShares( order.getLastExecutedQuantity() ) ); + fixOrder.set( FIX::LastPx( order.getLastExecutedPrice() ) ); + } + + try + { + FIX::Session::sendToTarget( fixOrder, senderCompID, targetCompID ); + } + catch ( FIX::SessionNotFound& ) {}} + +void Application::rejectOrder +( const FIX::SenderCompID& sender, const FIX::TargetCompID& target, + const FIX::ClOrdID& clOrdID, const FIX::Symbol& symbol, + const FIX::Side& side, const std::string& message ) +{ + FIX::TargetCompID targetCompID( sender.getValue() ); + FIX::SenderCompID senderCompID( target.getValue() ); + + FIX42::ExecutionReport fixOrder + ( FIX::OrderID ( clOrdID.getValue() ), + FIX::ExecID ( m_generator.genExecutionID() ), + FIX::ExecTransType ( FIX::ExecTransType_NEW ), + FIX::ExecType ( FIX::ExecType_REJECTED ), + FIX::OrdStatus ( FIX::ExecType_REJECTED ), + symbol, side, FIX::LeavesQty( 0 ), FIX::CumQty( 0 ), FIX::AvgPx( 0 ) ); + + fixOrder.set( clOrdID ); + fixOrder.set( FIX::Text( message ) ); + + try + { + FIX::Session::sendToTarget( fixOrder, senderCompID, targetCompID ); + } + catch ( FIX::SessionNotFound& ) {}} + +void Application::processOrder( const Order& order ) +{ + if ( m_orderMatcher.insert( order ) ) + { + acceptOrder( order ); + + std::queue < Order > orders; + m_orderMatcher.match( order.getSymbol(), orders ); + + while ( orders.size() ) + { + fillOrder( orders.front() ); + orders.pop(); + } + } + else + rejectOrder( order ); +} + +void Application::processCancel( const std::string& id, + const std::string& symbol, Order::Side side ) +{ + Order & order = m_orderMatcher.find( symbol, side, id ); + order.cancel(); + cancelOrder( order ); + m_orderMatcher.erase( order ); +} + +Order::Side Application::convert( const FIX::Side& side ) +{ + switch ( side ) + { + case FIX::Side_BUY: return Order::buy; + case FIX::Side_SELL: return Order::sell; + default: throw std::logic_error( "Unsupported Side, use buy or sell" ); + } +} + +Order::Type Application::convert( const FIX::OrdType& ordType ) +{ + switch ( ordType ) + { + case FIX::OrdType_LIMIT: return Order::limit; + default: throw std::logic_error( "Unsupported Order Type, use limit" ); + } +} + +FIX::Side Application::convert( Order::Side side ) +{ + switch ( side ) + { + case Order::buy: return FIX::Side( FIX::Side_BUY ); + case Order::sell: return FIX::Side( FIX::Side_SELL ); + default: throw std::logic_error( "Unsupported Side, use buy or sell" ); + } +} + +FIX::OrdType Application::convert( Order::Type type ) +{ + switch ( type ) + { + case Order::limit: return FIX::OrdType( FIX::OrdType_LIMIT ); + default: throw std::logic_error( "Unsupported Order Type, use limit" ); + } +} diff --git a/src/server/sparkd/exchange_sim/Application.h b/src/server/sparkd/exchange_sim/Application.h new file mode 100644 index 000000000..28664129d --- /dev/null +++ b/src/server/sparkd/exchange_sim/Application.h @@ -0,0 +1,102 @@ +/* -*- C++ -*- */ + +/**************************************************************************** +** Copyright (c) 2001-2014 +** +** This file is part of the QuickFIX FIX Engine +** +** This file may be distributed under the terms of the quickfixengine.org +** license as defined by quickfixengine.org and appearing in the file +** LICENSE included in the packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.quickfixengine.org/LICENSE for licensing information. +** +** Contact ask@quickfixengine.org if any conditions of this licensing are +** not clear to you. +** +****************************************************************************/ + +#ifndef ORDERMATCH_APPLICATION_H +#define ORDERMATCH_APPLICATION_H + +#include "IDGenerator.h" +#include "OrderMatcher.h" +#include "Order.h" +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include "lib/utils/warn_ctrl.h" + +GCC_DIAG_OFF("-Woverloaded-virtual") +class Application + : public FIX::Application, + public FIX::MessageCracker +{ + // Application overloads + void onCreate( const FIX::SessionID& ) {} + void onLogon( const FIX::SessionID& sessionID ); + void onLogout( const FIX::SessionID& sessionID ); + void toAdmin( FIX::Message&, const FIX::SessionID& ) {} + void toApp( FIX::Message&, const FIX::SessionID& ) + throw( FIX::DoNotSend ) {} + void fromAdmin( const FIX::Message&, const FIX::SessionID& ) + throw( FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon ) {} + void fromApp( const FIX::Message& message, const FIX::SessionID& sessionID ) + throw( FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType ); + + // MessageCracker overloads + void onMessage( const FIX42::NewOrderSingle&, const FIX::SessionID& ); + void onMessage( const FIX42::OrderCancelRequest&, const FIX::SessionID& ); + void onMessage( const FIX42::MarketDataRequest&, const FIX::SessionID& ); + void onMessage( const FIX43::MarketDataRequest&, const FIX::SessionID& ); + + // Order functionality + void processOrder( const Order& ); + void processCancel( const std::string& id, const std::string& symbol, Order::Side ); + + void updateOrder( const Order&, char status ); + void rejectOrder( const Order& order ) + { updateOrder( order, FIX::OrdStatus_REJECTED ); } + void acceptOrder( const Order& order ) + { updateOrder( order, FIX::OrdStatus_NEW ); } + void fillOrder( const Order& order ) + { + updateOrder( order, + order.isFilled() ? FIX::OrdStatus_FILLED + : FIX::OrdStatus_PARTIALLY_FILLED ); + } + void cancelOrder( const Order& order ) + { updateOrder( order, FIX::OrdStatus_CANCELED ); } + + void rejectOrder( const FIX::SenderCompID&, const FIX::TargetCompID&, + const FIX::ClOrdID& clOrdID, const FIX::Symbol& symbol, + const FIX::Side& side, const std::string& message ); + + // Type conversions + Order::Side convert( const FIX::Side& ); + Order::Type convert( const FIX::OrdType& ); + FIX::Side convert( Order::Side ); + FIX::OrdType convert( Order::Type ); + + OrderMatcher m_orderMatcher; + IDGenerator m_generator; + +public: + const OrderMatcher& orderMatcher() { return m_orderMatcher; } +}; +GCC_DIAG_RESTORE() + +#endif diff --git a/src/server/sparkd/exchange_sim/IDGenerator.h b/src/server/sparkd/exchange_sim/IDGenerator.h new file mode 100644 index 000000000..2d40c5178 --- /dev/null +++ b/src/server/sparkd/exchange_sim/IDGenerator.h @@ -0,0 +1,52 @@ +/* -*- C++ -*- */ + +/**************************************************************************** +** Copyright (c) 2001-2014 +** +** This file is part of the QuickFIX FIX Engine +** +** This file may be distributed under the terms of the quickfixengine.org +** license as defined by quickfixengine.org and appearing in the file +** LICENSE included in the packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.quickfixengine.org/LICENSE for licensing information. +** +** Contact ask@quickfixengine.org if any conditions of this licensing are +** not clear to you. +** +****************************************************************************/ + +#ifndef ORDERMATCH_IDGENERATOR_H +#define ORDERMATCH_IDGENERATOR_H + +#include +#include + +class IDGenerator +{ +public: +IDGenerator() : m_orderID( 0 ), m_executionID( 0 ) {} + + std::string genOrderID() + { + std::stringstream stream; + stream << ++m_orderID; + return stream.str(); + } + + std::string genExecutionID() + { + std::stringstream stream; + stream << ++m_executionID; + return stream.str(); + } + +private: + long m_orderID; + long m_executionID; +}; + +#endif diff --git a/src/server/sparkd/exchange_sim/Market.cpp b/src/server/sparkd/exchange_sim/Market.cpp new file mode 100644 index 000000000..029450b4e --- /dev/null +++ b/src/server/sparkd/exchange_sim/Market.cpp @@ -0,0 +1,137 @@ +/* -*- C++ -*- */ + +/**************************************************************************** +** Copyright (c) 2001-2014 +** +** This file is part of the QuickFIX FIX Engine +** +** This file may be distributed under the terms of the quickfixengine.org +** license as defined by quickfixengine.org and appearing in the file +** LICENSE included in the packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.quickfixengine.org/LICENSE for licensing information. +** +** Contact ask@quickfixengine.org if any conditions of this licensing are +** not clear to you. +** +****************************************************************************/ + +#ifdef _MSC_VER +#pragma warning( disable : 4786 ) +#endif + +#include "Market.h" +#include + +bool Market::insert( const Order& order ) +{ + if ( order.getSide() == Order::buy ) + m_bidOrders.insert( BidOrders::value_type( order.getPrice(), order ) ); + else + m_askOrders.insert( AskOrders::value_type( order.getPrice(), order ) ); + return true; +} + +void Market::erase( const Order& order ) +{ + std::string id = order.getClientID(); + if ( order.getSide() == Order::buy ) + { + BidOrders::iterator i; + for ( i = m_bidOrders.begin(); i != m_bidOrders.end(); ++i ) + if ( i->second.getClientID() == id ) + { + m_bidOrders.erase( i ); + return ; + } + } + else if ( order.getSide() == Order::sell ) + { + AskOrders::iterator i; + for ( i = m_askOrders.begin(); i != m_askOrders.end(); ++i ) + if ( i->second.getClientID() == id ) + { + m_askOrders.erase( i ); + return ; + } + } +} + +bool Market::match( std::queue < Order > & orders ) +{ + while ( true ) + { + if ( !m_bidOrders.size() || !m_askOrders.size() ) + return orders.size() != 0; + + BidOrders::iterator iBid = m_bidOrders.begin(); + AskOrders::iterator iAsk = m_askOrders.begin(); + + if ( iBid->second.getPrice() >= iAsk->second.getPrice() ) + { + Order & bid = iBid->second; + Order& ask = iAsk->second; + + match( bid, ask ); + orders.push( bid ); + orders.push( ask ); + + if ( bid.isClosed() ) m_bidOrders.erase( iBid ); + if ( ask.isClosed() ) m_askOrders.erase( iAsk ); + } + else + return orders.size() != 0; + } +} + +Order& Market::find( Order::Side side, std::string id ) +{ + if ( side == Order::buy ) + { + BidOrders::iterator i; + for ( i = m_bidOrders.begin(); i != m_bidOrders.end(); ++i ) + if ( i->second.getClientID() == id ) return i->second; + } + else if ( side == Order::sell ) + { + AskOrders::iterator i; + for ( i = m_askOrders.begin(); i != m_askOrders.end(); ++i ) + if ( i->second.getClientID() == id ) return i->second; + } + throw std::exception(); +} + +void Market::match( Order& bid, Order& ask ) +{ + double price = ask.getPrice(); + long quantity = 0; + + if ( bid.getOpenQuantity() > ask.getOpenQuantity() ) + quantity = ask.getOpenQuantity(); + else + quantity = bid.getOpenQuantity(); + + bid.execute( price, quantity ); + ask.execute( price, quantity ); +} + +void Market::display() const +{ + BidOrders::const_iterator iBid; + AskOrders::const_iterator iAsk; + + std::cout << "BIDS:" << std::endl; + std::cout << "-----" << std::endl << std::endl; + for ( iBid = m_bidOrders.begin(); iBid != m_bidOrders.end(); ++iBid ) + std::cout << iBid->second << std::endl; + + std::cout << std::endl << std::endl; + + std::cout << "ASKS:" << std::endl; + std::cout << "-----" << std::endl << std::endl; + for ( iAsk = m_askOrders.begin(); iAsk != m_askOrders.end(); ++iAsk ) + std::cout << iAsk->second << std::endl; +} diff --git a/src/server/sparkd/exchange_sim/Market.h b/src/server/sparkd/exchange_sim/Market.h new file mode 100644 index 000000000..bbe09f9e2 --- /dev/null +++ b/src/server/sparkd/exchange_sim/Market.h @@ -0,0 +1,51 @@ +/* -*- C++ -*- */ + +/**************************************************************************** +** Copyright (c) 2001-2014 +** +** This file is part of the QuickFIX FIX Engine +** +** This file may be distributed under the terms of the quickfixengine.org +** license as defined by quickfixengine.org and appearing in the file +** LICENSE included in the packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.quickfixengine.org/LICENSE for licensing information. +** +** Contact ask@quickfixengine.org if any conditions of this licensing are +** not clear to you. +** +****************************************************************************/ + +#ifndef ORDERMATCH_MARKET_H +#define ORDERMATCH_MARKET_H + +#include "Order.h" +#include +#include +#include +#include + +class Market +{ +public: + bool insert( const Order& order ); + void erase( const Order& order ); + Order& find( Order::Side side, std::string id ); + bool match( std::queue < Order > & ); + void display() const; + +private: + typedef std::multimap < double, Order, std::greater < double > > BidOrders; + typedef std::multimap < double, Order, std::less < double > > AskOrders; + + void match( Order& bid, Order& ask ); + + std::queue < Order > m_orderUpdates; + BidOrders m_bidOrders; + AskOrders m_askOrders; +}; + +#endif diff --git a/src/server/sparkd/exchange_sim/Order.h b/src/server/sparkd/exchange_sim/Order.h new file mode 100644 index 000000000..6df5ce296 --- /dev/null +++ b/src/server/sparkd/exchange_sim/Order.h @@ -0,0 +1,111 @@ +/* -*- C++ -*- */ + +/**************************************************************************** +** Copyright (c) 2001-2014 +** +** This file is part of the QuickFIX FIX Engine +** +** This file may be distributed under the terms of the quickfixengine.org +** license as defined by quickfixengine.org and appearing in the file +** LICENSE included in the packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.quickfixengine.org/LICENSE for licensing information. +** +** Contact ask@quickfixengine.org if any conditions of this licensing are +** not clear to you. +** +****************************************************************************/ + +#ifndef ORDERMATCH_ORDER_H +#define ORDERMATCH_ORDER_H + +#include +#include +#include + +class Order +{ + friend std::ostream& operator<<( std::ostream&, const Order& ); + +public: + enum Side { buy, sell }; + enum Type { market, limit }; + + Order( const std::string& clientId, const std::string& symbol, + const std::string& owner, const std::string& target, + Side side, Type type, double price, long quantity ) +: m_clientId( clientId ), m_symbol( symbol ), m_owner( owner ), + m_target( target ), m_side( side ), m_type( type ), m_price( price ), + m_quantity( quantity ) + { + m_openQuantity = m_quantity; + m_executedQuantity = 0; + m_avgExecutedPrice = 0; + m_lastExecutedPrice = 0; + m_lastExecutedQuantity = 0; + } + + const std::string& getClientID() const { return m_clientId; } + const std::string& getSymbol() const { return m_symbol; } + const std::string& getOwner() const { return m_owner; } + const std::string& getTarget() const { return m_target; } + Side getSide() const { return m_side; } + Type getType() const { return m_type; } + double getPrice() const { return m_price; } + long getQuantity() const { return m_quantity; } + + long getOpenQuantity() const { return m_openQuantity; } + long getExecutedQuantity() const { return m_executedQuantity; } + double getAvgExecutedPrice() const { return m_avgExecutedPrice; } + double getLastExecutedPrice() const { return m_lastExecutedPrice; } + long getLastExecutedQuantity() const { return m_lastExecutedQuantity; } + + bool isFilled() const { return m_quantity == m_executedQuantity; } + bool isClosed() const { return m_openQuantity == 0; } + + void execute( double price, long quantity ) + { + m_avgExecutedPrice = + ( ( quantity * price ) + ( m_avgExecutedPrice * m_executedQuantity ) ) + / ( quantity + m_executedQuantity ); + + m_openQuantity -= quantity; + m_executedQuantity += quantity; + m_lastExecutedPrice = price; + m_lastExecutedQuantity = quantity; + } + void cancel() + { + m_openQuantity = 0; + } + +private: + std::string m_clientId; + std::string m_symbol; + std::string m_owner; + std::string m_target; + Side m_side; + Type m_type; + double m_price; + long m_quantity; + + long m_openQuantity; + long m_executedQuantity; + double m_avgExecutedPrice; + double m_lastExecutedPrice; + long m_lastExecutedQuantity; +}; + +inline std::ostream& operator<<( std::ostream& ostream, const Order& order ) +{ + return ostream + << "ID: " << std::setw( 10 ) << "," << order.getClientID() + << " OWNER: " << std::setw( 10 ) << "," << order.getOwner() + << " PRICE: " << std::setw( 10 ) << "," << order.getPrice() + << " QUANTITY: " << std::setw( 10 ) << "," << order.getQuantity(); +} + +#endif diff --git a/src/server/sparkd/exchange_sim/OrderMatcher.h b/src/server/sparkd/exchange_sim/OrderMatcher.h new file mode 100644 index 000000000..2b2bd41fe --- /dev/null +++ b/src/server/sparkd/exchange_sim/OrderMatcher.h @@ -0,0 +1,91 @@ +/* -*- C++ -*- */ + +/**************************************************************************** +** Copyright (c) 2001-2014 +** +** This file is part of the QuickFIX FIX Engine +** +** This file may be distributed under the terms of the quickfixengine.org +** license as defined by quickfixengine.org and appearing in the file +** LICENSE included in the packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.quickfixengine.org/LICENSE for licensing information. +** +** Contact ask@quickfixengine.org if any conditions of this licensing are +** not clear to you. +** +****************************************************************************/ + +#ifndef ORDERMATCH_ORDERMATCHER_H +#define ORDERMATCH_ORDERMATCHER_H + +#include "Market.h" +#include +#include + +class OrderMatcher +{ + typedef std::map < std::string, Market > Markets; +public: + bool insert( const Order& order ) + { + Markets::iterator i = m_markets.find( order.getSymbol() ); + if ( i == m_markets.end() ) + i = m_markets.insert( std::make_pair( order.getSymbol(), Market() ) ).first; + return i->second.insert( order ); + } + + void erase( const Order& order ) + { + Markets::iterator i = m_markets.find( order.getSymbol() ); + if ( i == m_markets.end() ) return ; + i->second.erase( order ); + } + + Order& find( std::string symbol, Order::Side side, std::string id ) + { + Markets::iterator i = m_markets.find( symbol ); + if ( i == m_markets.end() ) throw std::exception(); + return i->second.find( side, id ); + } + + bool match( std::string symbol, std::queue < Order > & orders ) + { + Markets::iterator i = m_markets.find( symbol ); + if ( i == m_markets.end() ) return false; + return i->second.match( orders ); + } + + bool match( std::queue < Order > & orders ) + { + Markets::iterator i; + for ( i = m_markets.begin(); i != m_markets.end(); ++i ) + i->second.match( orders ); + return orders.size() != 0; + } + + void display( std::string symbol ) const + { + Markets::const_iterator i = m_markets.find( symbol ); + if ( i == m_markets.end() ) return ; + i->second.display(); + } + + void display() const + { + std::cout << "SYMBOLS:" << std::endl; + std::cout << "--------" << std::endl; + + Markets::const_iterator i; + for ( i = m_markets.begin(); i != m_markets.end(); ++i ) + std::cout << i->first << std::endl; + } + +private: + Markets m_markets; +}; + +#endif diff --git a/src/server/sparkd/exchange_sim/ordermatch.cpp b/src/server/sparkd/exchange_sim/ordermatch.cpp new file mode 100644 index 000000000..1c0ff03c7 --- /dev/null +++ b/src/server/sparkd/exchange_sim/ordermatch.cpp @@ -0,0 +1,53 @@ +#include "Application.h" + +#include +#include +#include + +#include +#include +#include + +int start( int argc, char** argv ) +{ + if ( argc != 2 ) + { + std::cout << "usage: " << argv[ 0 ] + << " FILE." << std::endl; + return 0; + } + std::string file = argv[ 1 ]; + + try + { + FIX::SessionSettings settings( file ); + + Application application; + FIX::FileStoreFactory storeFactory( settings ); + FIX::ScreenLogFactory logFactory( settings ); + FIX::SocketAcceptor acceptor( application, storeFactory, settings, logFactory ); + + acceptor.start(); + while ( true ) + { + std::string value; + std::cin >> value; + + if ( value == "#symbols" ) + application.orderMatcher().display(); + else if( value == "#quit" ) + break; + else + application.orderMatcher().display( value ); + + std::cout << std::endl; + } + acceptor.stop(); + return 0; + } + catch ( std::exception & e ) + { + std::cout << e.what() << std::endl; + return 1; + } +}