SPARK-608, Generating our own book.

This commit is contained in:
Pedro Pinto committed 2018-03-06 14:28:58 -05:00
1 parent fab0388ae1
commit 970bf44751
4 files changed
+143 -88

No files matched your search

@@ -7,6 +7,7 @@
#include "lib/md/get_symbols.h"
#include "lib/utils/datetime.h"
#include "lib/domain_model/Security.h"
#include "lib/md/Shm/MDStructure.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <quickfix/fix42/ExecutionReport.h>
@@ -71,7 +72,7 @@ void ExchangeSimApplication::onMessage(const FIX42::NewOrderSingle &message,
(long)orderQty, convert(moi));
processOrder(order);
m_orderMatcher.display();
update_md(sec_desc);
} catch (std::exception &e) {
rejectOrder(senderCompID, senderSubID, targetCompID, clOrdID, sec_desc,
side, account, e.what());
@@ -114,7 +115,7 @@ void ExchangeSimApplication::onMessage(
fillOrder(orders.front());
orders.pop();
}
m_orderMatcher.display();
update_md(sec_desc);
} catch (std::exception &e) {
rejectOrder(senderCompID, senderSubID, targetCompID, clOrdID, sec_desc,
side, account, e.what());
@@ -134,7 +135,7 @@ void ExchangeSimApplication::onMessage(const FIX42::OrderCancelRequest &message,
LOG(ExSim, Info, "Got order cancellation for: {}", origClOrdID.getString());
try {
processCancel(origClOrdID, sec_desc, convert(side));
m_orderMatcher.display();
update_md(sec_desc);
} catch (std::exception &) {
}
}
@@ -316,20 +317,46 @@ ExchangeSimApplication::ExchangeSimApplication() :
producer(co2::PLATFORM_SIM),
handles()
{
// std::set<md::UserSymbol> symbols;
// md::get_system_md_symbols(symbols);
// md::db::load_securities(symbols, [this](const api::Security &s) {
// if (s.exchange() == api::VENUE_GLBX) {
// auto new_s = s;
// *new_s.mutable_glbx()->mutable_security_desc() = std::string("sim:") + s.glbx().security_desc();
// *new_s.mutable_symbol() = std::string("sim:") + s.symbol();
// md::FixedSecurity fs;
// md::convert_security(new_s, fs, true);
// md::MDProducerHandle handle = producer.add_security(fs);
// auto sec_desc = securities::descriptor(s);
// handles.insert(make_pair(sec_desc,handle));
// }
// });
std::set<md::UserSymbol> symbols;
md::get_system_md_symbols(symbols);
md::db::load_securities(symbols, [this](const api::Security &s) {
if (s.exchange() == api::VENUE_GLBX) {
md::with_security_like(s,[this,s] (auto &fs) {
auto new_s = s;
*new_s.mutable_glbx()->mutable_security_desc() = std::string("sim:") + s.glbx().security_desc();
*new_s.mutable_symbol() = std::string("sim:") + s.symbol();
md::convert_security(new_s, fs, true);
md::MDProducerHandle handle = producer.add_security(fs);
auto sec_desc = securities::descriptor(s);
handles.insert(make_pair(sec_desc,handle));
});
}});
}
void ExchangeSimApplication::update_md(std::string const&sec_desc)
{
auto it = handles.find(sec_desc);
if (it != handles.end()) {
auto &handle = it->second;
auto book = m_orderMatcher.get_book(sec_desc);
auto n_bids = book.bids.size();
for (uint i = 0; i < std::min(md::COCONUT_MAX_DEPTH,(uint32_t) n_bids); i++) {
auto psz = md::PriceSize::from_double<7> (book.bids[i].price, book.bids[i].size);
LOG(ExSim, Info, "Updating market data for {}. Depth: {} : Buy {}@{}",
sec_desc,i,book.bids[i].price,book.bids[i].size);
handle.update_bid (utils::TimeStamp::now(), psz,i);
}
auto n_asks = book.asks.size();
for (uint i = 0; i < std::min(md::COCONUT_MAX_DEPTH,(uint32_t) n_asks); i++) {
auto psz = md::PriceSize::from_double<7> (book.asks[i].price, book.asks[i].size);
LOG(ExSim, Info, "Updating market data for {}. Depth: {} : Sell {}@{}",
sec_desc,i,book.asks[i].price,book.asks[i].size);
handle.update_ask (utils::TimeStamp::now(), psz,i);
}
}
}
GCC_DIAG_RESTORE()
@@ -109,6 +109,7 @@ private:
md::MDProducer producer;
std::map<std::string,md::MDProducerHandle> handles;
public:
void update_md(std::string const&sec_desc);
const OrderMatcher& orderMatcher() { return m_orderMatcher; }
};
}}
+20 -17
View File
@@ -23,32 +23,35 @@
#define ORDERMATCH_MARKET_H
#include "Order.h"
#include <functional>
#include <map>
#include <queue>
#include <string>
#include <functional>
namespace bts {
namespace sim {
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;
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;
typedef std::multimap<double, Order, std::greater<double>> BidOrders;
typedef std::multimap<double, Order, std::less<double>> AskOrders;
void match( Order& bid, Order& ask );
BidOrders bids() const { return m_bidOrders; }
AskOrders asks() const { return m_askOrders; }
std::queue < Order > m_orderUpdates;
BidOrders m_bidOrders;
AskOrders m_askOrders;
private:
void match(Order &bid, Order &ask);
std::queue<Order> m_orderUpdates;
BidOrders m_bidOrders;
AskOrders m_askOrders;
};
}}
}
}
#endif
+78 -54
View File
@@ -23,72 +23,96 @@
#define ORDERMATCH_ORDERMATCHER_H
#include "Market.h"
#include <map>
#include <iostream>
#include <map>
namespace bts {
namespace sim {
class OrderMatcher
{
typedef std::map < std::string, Market > Markets;
public:
bool insert( const Order& order )
{
Markets::iterator i = m_markets.find( order.getSecDesc() );
if ( i == m_markets.end() )
i = m_markets.insert( std::make_pair( order.getSecDesc(), Market() ) ).first;
return i->second.insert( order );
}
class OrderMatcher {
typedef std::map<std::string, Market> Markets;
void erase( const Order& order )
{
Markets::iterator i = m_markets.find( order.getSecDesc() );
if ( i == m_markets.end() ) return ;
i->second.erase( order );
}
public:
bool insert(const Order &order) {
Markets::iterator i = m_markets.find(order.getSecDesc());
if (i == m_markets.end())
i = m_markets.insert(std::make_pair(order.getSecDesc(), Market()))
.first;
return i->second.insert(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 );
}
void erase(const Order &order) {
Markets::iterator i = m_markets.find(order.getSecDesc());
if (i == m_markets.end())
return;
i->second.erase(order);
}
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 );
}
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::queue < Order > & orders )
{
Markets::iterator i;
for ( i = m_markets.begin(); i != m_markets.end(); ++i )
i->second.match( orders );
return orders.size() != 0;
}
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);
}
void display( std::string symbol ) const
{
Markets::const_iterator i = m_markets.find( symbol );
if ( i == m_markets.end() ) return ;
i->second.display();
}
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() const
{
std::cout << "SYMBOLS:" << std::endl;
std::cout << "--------" << std::endl;
void display(std::string symbol) const {
Markets::const_iterator i = m_markets.find(symbol);
if (i == m_markets.end())
return;
i->second.display();
}
Markets::const_iterator i;
for ( i = m_markets.begin(); i != m_markets.end(); ++i )
std::cout << i->first << std::endl;
}
struct BookEntry {
double price;
long size;
};
private:
Markets m_markets;
struct Book {
std::vector<BookEntry> bids;
std::vector<BookEntry> asks;
};
Book get_book(std::string const &sec_desc) const {
Book book;
auto it = m_markets.find(sec_desc);
if (it != m_markets.end()) {
auto &s = it->second;
for (auto &o : s.bids())
book.bids.push_back(BookEntry{o.first, o.second.getQuantity()});
for (auto &o : s.asks())
book.asks.push_back(BookEntry{o.first, o.second.getQuantity()});
}
return book;
}
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;
i->second.display();
}
}
private:
Markets m_markets;
};
}}
}
}
#endif