Files
spark/src/lib/md/test/md2_tests.cpp
T

2518 lines
97 KiB
C++

#ifndef _WIN32
#include "../MDClient.h"
#include "../SecurityCache.h"
#include "../SettlementRecorder.h"
#include "../coconut2.h"
#include "../relay/QueryServer.h"
#include "Co2TestInstrument.h"
#include "catch/catch.hpp"
#include "lib/config/config.h"
#include "lib/metrics/metrics.h"
#include "lib/os/os.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/defer.h"
#include "lib/utils/warn_ctrl.h"
#include "lib/utils/Shuffle.h"
#include "lib/config/protos/MarketData.pb.h"
#include "lib/utils/Sqlite_utils.h"
#include "md_test_utils.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iterator>
#include <random>
#include <string>
#include <tbb/parallel_for.h>
#include <tbb/task_scheduler_init.h>
#include <thread>
#include <tuple>
#include <vector>
using namespace std;
namespace bts {
#ifdef _WIN32
namespace metrics {
struct MetricCollection {};
MetricsContainer::MetricsContainer() {}
MetricsContainer::~MetricsContainer() {}
} // namespace metrics
#endif
int md_tests = 0;
namespace md {
// import this from RelayClient.cpp
PriceSize
book_at(const google::protobuf::RepeatedPtrField<co2::PriceSizeMessage> &side,
int index);
namespace test {
using namespace api;
using namespace co2;
Platform TEST_PLATFORM = PLATFORM_SIM;
//
// Helper functions for Producer/Consumer tests
//
std::vector<MDProducerHandle> create_test_securities(MDProducer &producer,
const char *postfix = "");
set<UserSymbol> get_test_symbols(const char *postfix = "") {
set<UserSymbol> symbols;
UserSymbol future_symbol(string("FOO") + postfix, VENUE_BTS,
SECURITYTYPE_FUTURE);
symbols.insert(future_symbol);
UserSymbol option_symbol(string("FOOX") + postfix, VENUE_BTS,
SECURITYTYPE_OPTION);
symbols.insert(option_symbol);
return symbols;
}
Security mkfuture(string symbol, security_key key, int year, int month,
int day) {
Security result;
result.set_symbol(symbol);
result.set_exchange(VENUE_BTS);
result.set_id(key);
result.mutable_future()->set_expiration_day(day);
result.mutable_future()->set_expiration_month(month);
result.mutable_future()->set_expiration_year(year);
return result;
}
Security mkoption(string symbol, security_key key, int year, int month, int day,
double strike) {
Security result;
result.set_symbol(symbol);
result.set_exchange(VENUE_BTS);
result.set_id(key);
result.mutable_option()->set_expiration_day(day);
result.mutable_option()->set_expiration_month(month);
result.mutable_option()->set_expiration_year(year);
result.mutable_option()->set_strike(strike);
result.mutable_option()->set_call_put(PUTORCALL_PUT);
return result;
}
unique_ptr<FixedFutureSecurity> mkfixedfuture(string symbol, security_key key,
int year, int month, int day) {
unique_ptr<FixedFutureSecurity> ffs(new FixedFutureSecurity());
ffs->type = SECURITYTYPE_FUTURE;
ffs->exchange = VENUE_BTS;
ffs->key = key;
ffs->expiration_day = day;
ffs->expiration_month = month;
ffs->expiration_year = year;
ffs->symbol(symbol);
return ffs;
}
Security mkspread(string symbol, security_key key,
const vector<Security> &legs) {
Security result;
result.set_symbol(symbol);
result.set_exchange(VENUE_BTS);
result.set_id(key);
for (const auto &leg : legs) {
auto *pleg = result.mutable_spread()->add_legs();
pleg->mutable_security()->CopyFrom(leg);
}
return result;
}
Security mkspread(string symbol, security_key key, Security leg1,
Security leg2) {
vector<Security> vlegs;
vlegs.push_back(leg1);
vlegs.push_back(leg2);
return mkspread(symbol, key, vlegs);
}
Security mkspread(string symbol, security_key key, Security leg1, Security leg2,
Security leg3) {
vector<Security> vlegs;
vlegs.push_back(leg1);
vlegs.push_back(leg2);
vlegs.push_back(leg3);
return mkspread(symbol, key, vlegs);
}
class Co2Tests {
public:
void setup() {
config::internal::initialize_for_test(
"PlatformMap {"
" value {"
" glbx_platform: 'PLATFORM_SIM'"
" }"
"}"
"PublisherOptions {"
" value {"
" heartbeat_ms: 100"
" heartbeat_boot_seconds: 1"
" }"
"}");
}
#ifndef _WIN32
void test_security_id_recorder(const std::string &db_name) {
// Verify callbacks happen after work complete
auto db1 = sqlite::open_db(db_name.c_str());
REQUIRE(db1);
auto db2 = sqlite::open_db(db_name.c_str());
REQUIRE(db2);
SecurityIDRecorder idsvc{{db1}}, idsvc2{{db2}};
security_key result = invalid_security_key;
Security s1 = mkoption("FOOX", invalid_security_key, 2018, 1, 20, 1850);
Security f1 = mkfuture("FOO", invalid_security_key, 2018, 1, 20);
idsvc.assign_key(f1, [](const api::Security &s) {
REQUIRE(s.id() != invalid_security_key);
});
idsvc.assign_key(s1,
[&result](const api::Security &s) { result = s.id(); });
idsvc.do_work();
REQUIRE(s1.id() == invalid_security_key);
REQUIRE(result != invalid_security_key);
security_key result2 = invalid_security_key;
idsvc2.assign_key(
s1, [&result2](const api::Security &s) { result2 = s.id(); });
idsvc2.do_work();
if (db_name == ":memory:") {
// This is a meta-test to make sure we don't accidently generate the
// same key sequence when writing to different databases.
REQUIRE(result != result2);
} else {
REQUIRE(result == result2);
}
}
void test_security_id_recorder_spreads() {
// Test that security id recorder can resolve spread legs
// Set up "shared memory"
auto cold_header = std::make_shared<ColdHeader>();
auto hot_header = std::make_shared<HotHeader>();
// Create a spread with 2 legs
auto f1 = mkfuture("FO1", invalid_security_key, 2018, 1, 17);
f1.mutable_glbx()->set_exchange_id(100);
REQUIRE(f1.id() == invalid_security_key);
auto f1handle =
std::make_unique<Co2TestInstrument>(f1, cold_header, hot_header);
auto f2 = mkfuture("FO2", invalid_security_key, 2018, 1, 20);
f2.mutable_glbx()->set_exchange_id(200);
auto f2handle =
std::make_unique<Co2TestInstrument>(f2, cold_header, hot_header);
auto s1 = mkspread("FOO", invalid_security_key, {f1, f2});
auto s1handle =
std::make_unique<Co2TestInstrument>(s1, cold_header, hot_header);
// Add spread to SecurityIDRecorder and work it
auto db = sqlite::open_db(":memory:");
REQUIRE(db);
SecurityIDRecorder idsvc{
{db, [&](ExchangeKey k) -> SecurityKey {
for (const auto &fut :
{f1handle->producer(), f2handle->producer()}) {
if (k.value() == fut.security().numeric_id)
return SecurityKey(fut.key());
}
return SecurityKey(invalid_security_key);
}}};
auto workwork = [&]() {
idsvc.do_work();
idsvc.do_work();
};
idsvc.ensure_key(s1handle->producer());
workwork();
// Expect no key
REQUIRE(s1handle->consumer().key() == invalid_security_key);
// Add leg spreads
idsvc.ensure_key(f1handle->producer());
idsvc.ensure_key(f2handle->producer());
workwork();
// Expect legs to have keys
REQUIRE(f1handle->consumer().key() != invalid_security_key);
REQUIRE(f2handle->consumer().key() != invalid_security_key);
// Expect spread to have keys
REQUIRE(s1handle->consumer().key() != invalid_security_key);
}
#endif
void test_price_size() {
// PriceSize violates some type punning rules
// So it is important to have this test work correctly on the target
// platform
PriceSize p2;
auto bid = PriceSize_from_double<2>(4.5, 14);
auto ask = PriceSize_from_double<9>(4.5, 14);
PriceLevel memory;
memory = PriceLevel(bid, ask);
REQUIRE(bid.price() == memory.bid().price());
REQUIRE(bid.size() == memory.bid().size());
REQUIRE(ask.price() == memory.ask().price());
REQUIRE(ask.size() == memory.ask().size());
for (auto p : {memory.bid(), memory.ask()}) {
REQUIRE(p.price() == 4.5);
REQUIRE(p.size() == 14);
}
auto p = PriceSize_from_double<2>(4.0, 5);
memory = PriceLevel(p, p);
for (auto p2 : {memory.bid(), memory.ask()}) {
REQUIRE(p2.price() == 4.0);
REQUIRE(p2.size() == 5);
}
p = PriceSize_from_double<2>(-4.2, 6);
memory = PriceLevel(p, p);
for (auto p2 : {memory.bid(), memory.ask()}) {
REQUIRE(p2.price() == -4.2);
REQUIRE(p2.size() == 6);
}
p = PriceSize({4, -1}, 2);
memory = PriceLevel(p, p);
for (auto p2 : {memory.bid(), memory.ask()}) {
REQUIRE(p2.price() == 0.4);
REQUIRE(p2.size() == 2);
}
// Test requirement for more than 32 bit mantissa
double bigval = -9999.9921875;
p = PriceSize_from_double<7>(bigval, 42);
memory = PriceLevel(p, p);
for (auto p2 : {memory.bid(), memory.ask()}) {
REQUIRE(p2.price() == bigval);
REQUIRE(p2.size() == 42);
}
}
void test_price_size_required_precision() {
auto min_tick_2017 = 9999.0078125;
auto p17 = PriceSize_from_double<7>(min_tick_2017, 2017);
PriceLevel m17(p17, p17);
for (auto p : {m17.bid(), m17.ask()}) {
REQUIRE(min_tick_2017 == p.price());
REQUIRE(p.size() == 2017);
}
auto min_tick_2018 = 1999.00390625;
auto p18 = PriceSize_from_double<8>(min_tick_2018, 2018);
PriceLevel m18(p18, p18);
for (auto p : {m18.bid(), m18.ask()}) {
REQUIRE(min_tick_2018 == p.price());
REQUIRE(p.size() == 2018);
}
}
void test_slow_double_conversion() {
double values[] = {1.5, 0.5, 0.2155, 0.216, 42.1};
int expected[] = {-1, -1, -4, -3, -1};
static_assert(BTS_ARRAYSIZE(values) == BTS_ARRAYSIZE(expected), "");
for (size_t x = 0; x < BTS_ARRAYSIZE(values); ++x) {
REQUIRE(to_exponent(values[x]) == expected[x]);
}
// Tests powers of 10. Important for display_factor conversion
for (int x = 8; x > -8; --x) {
double d = pow10(x);
REQUIRE(to_exponent(d) == x);
}
}
struct ExchangeDecimal {
int64_t mantissa;
int32_t exponent;
};
bool close_enough(double a, double b) {
// We need at least 7 places but we're testing with 10 just in case
const double acceptable_error = 1E-12;
double delta = abs(a - b);
// Equal if the delta is less than error
return delta < acceptable_error;
}
void test_producer_consumer() {
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers = create_test_securities(producer);
// Add bbo data for those expirations
auto write_time = TimeStamp::now();
for (auto &p : producers) {
p.update_bbo(write_time, PriceSize_from_double<2>(5.0, 5),
PriceSize_from_double<2>(5.0, 5));
}
// Symbol list for the consumer
set<UserSymbol> symbols;
UserSymbol future_symbol("FOO", VENUE_BTS, SECURITYTYPE_FUTURE);
symbols.insert(future_symbol);
UserSymbol option_symbol("FOOX", VENUE_BTS, SECURITYTYPE_OPTION);
symbols.insert(option_symbol);
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &symbols, true,
TEST_PLATFORM);
vector<Security> consumer_securities;
// Poll for security list - none visible until they have keys
size_t count = consumer.walk_security_list(
[&](const SecurityPtr &security, MDConsumerHandle) {
consumer_securities.push_back(*security);
});
// Since no security has an id none are "public"
REQUIRE(consumer_securities.size() == count);
REQUIRE(count == 0);
// Assign a unique ID to each security (from the producer)
assign_ids(producers.begin(), producers.end());
// Now securities can be enumerated by the client
count = consumer.walk_security_list(
[&](const SecurityPtr &security, MDConsumerHandle) {
consumer_securities.push_back(*security);
});
REQUIRE(consumer_securities.size() == count);
REQUIRE(count == 24);
vector<MDConsumerHandle> consumers;
for (const auto &security : consumer_securities) {
consumers.push_back(consumer.get_handle(security.id()));
}
for (const auto &c : consumers) {
PriceSize bid, ask;
TimeStamp read_time;
// get_boo returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, 1, &bid, &ask) ==
HandleReadResult::SUCCESS);
REQUIRE((read_time - TimeStamp()).seconds() > 0);
REQUIRE(bid.price() == 5.0);
REQUIRE(ask.price() == 5.0);
REQUIRE(bid.size() == 5);
REQUIRE(ask.size() == 5);
}
// Second query before a write has happend finds no updates
auto read_time = TimeStamp::now();
for (const auto &c : consumers) {
PriceSize bid, ask;
REQUIRE(c.get_direct_book(read_time, 1, &bid, &ask) !=
HandleReadResult::SUCCESS);
}
// update everything once more
write_time = TimeStamp::now();
for (auto &p : producers) {
// Update bid/ask with one call
p.update_bbo(write_time, PriceSize_from_double<2>(6.55, 6),
PriceSize_from_double<2>(6.55, 6));
}
// Now read_time is < write_time
for (const auto &c : consumers) {
TimeStamp modified_time = read_time;
PriceSize bid, ask;
// get_direct_book returns true becuase this time is after the change
REQUIRE(c.get_direct_book(modified_time, 1, &bid, &ask) ==
HandleReadResult::SUCCESS);
REQUIRE((modified_time - read_time).seconds() > 0);
REQUIRE(bid.price() == 6.55);
REQUIRE(ask.price() == 6.55);
REQUIRE(bid.size() == 6);
REQUIRE(ask.size() == 6);
}
}
// When a security is added the consumer should provide callback
void test_consumer_callback_on_security_added() {
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers = create_test_securities(producer);
// Symbol list for the consumer
set<UserSymbol> symbols;
UserSymbol future_symbol("FOO", VENUE_BTS, SECURITYTYPE_FUTURE);
symbols.insert(future_symbol);
UserSymbol option_symbol("FOOX", VENUE_BTS, SECURITYTYPE_OPTION);
symbols.insert(option_symbol);
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &symbols, true,
TEST_PLATFORM);
// Add the on_security callback
vector<Security> securities;
consumer.on_security(
[&](const SecurityPtr &security, MDConsumerHandle handle) {
REQUIRE(security->id() != invalid_security_key);
REQUIRE(security->id() == handle.key());
securities.push_back(*security);
});
// No securities have ids yet
REQUIRE(securities.size() == 0);
// Check for new securities
consumer.scan_securities();
// Still none
REQUIRE(securities.size() == 0);
producers.front().set_key(1001);
consumer.scan_securities();
REQUIRE(securities.size() == 1);
// Assign the rest of the ids
assign_ids(producers.begin() + 1, producers.end(),
producers.front().security().key);
consumer.scan_securities();
REQUIRE(securities.size() == producers.size());
}
// If an MDConsumer is asked for a handle to a security which does not exist
// in the system The consumer should return a handle which always returns
// false on all calls but wakes up when the security is added.
// * Case 1 - the shared memory has been created but the security has not
// been discovered
// * Case 2 - The producer has never been created
void test_consumer_defer_handle1() {
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers = create_test_securities(producer);
set<UserSymbol> symbols;
UserSymbol future_symbol("FOO", VENUE_BTS, SECURITYTYPE_FUTURE);
symbols.insert(future_symbol);
UserSymbol option_symbol("FOOX", VENUE_BTS, SECURITYTYPE_OPTION);
symbols.insert(option_symbol);
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &symbols, true,
TEST_PLATFORM);
// Get a handle to 24 consumers which haven't been created yet
vector<MDConsumerHandle> consumers;
vector<shared_ptr<MDConsumerHandle>> managed_consumers;
security_key key = 1;
for (; key < 24; ++key) {
consumers.push_back(consumer.get_handle(key + 1000));
managed_consumers.push_back(consumer.get_managed_handle(key + 1000));
}
// Set bbo on the producers - still not public
TimeStamp write_time = TimeStamp::now();
for (auto &p : producers) {
p.update_bbo(write_time, PriceSize_from_double<3>(4.5, 5),
PriceSize_from_double<3>(6.75, 7));
}
// Even though bbo is available it is not given out because the producing
// security does not have an id
PriceSize bid, ask;
for (auto &c : consumers) {
TimeStamp zerotime;
REQUIRE(c.get_direct_book(zerotime, 1, &bid, &ask) !=
HandleReadResult::SUCCESS);
}
for (auto &mc : managed_consumers) {
TimeStamp zerotime;
REQUIRE(mc->get_direct_book(zerotime, 1, &bid, &ask) !=
HandleReadResult::SUCCESS);
}
assign_ids(producers.begin(), producers.end());
consumer.scan_securities();
size_t bbo_count = 0;
// Existing old handles remain invalid
for (auto &c : consumers) {
TimeStamp zerotime;
if (c.get_direct_book(zerotime, 1, &bid, &ask) ==
HandleReadResult::SUCCESS) {
bbo_count++;
}
}
REQUIRE(bbo_count == 0);
// Now, magically, the existing (managed) handles are valid
for (auto &mc : managed_consumers) {
TimeStamp zerotime;
if (mc->get_direct_book(zerotime, 1, &bid, &ask) ==
HandleReadResult::SUCCESS) {
bbo_count++;
}
}
REQUIRE(bbo_count > 0);
}
// It is legal and valid to create a consumer before the producer process has
// started Handles given out by that consumer will become valid once the
// securities are discovered
void test_consumer_defer_handle2() {
set<UserSymbol> symbols;
UserSymbol future_symbol("FOO", VENUE_BTS, SECURITYTYPE_FUTURE);
symbols.insert(future_symbol);
UserSymbol option_symbol("FOOX", VENUE_BTS, SECURITYTYPE_OPTION);
symbols.insert(option_symbol);
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &symbols, true,
TEST_PLATFORM);
// Get a handle to 24 consumers which haven't been created yet
vector<MDConsumerHandle> consumers;
vector<shared_ptr<MDConsumerHandle>> managed_consumers;
security_key key = 1;
for (; key < 24; ++key) {
consumers.push_back(consumer.get_handle(key + 1000));
managed_consumers.push_back(consumer.get_managed_handle(key + 1000));
}
// Even though bbo is available it is not given out because the producing
// security does not have an id
PriceSize bid, ask;
for (auto &c : consumers) {
TimeStamp zerotime;
REQUIRE(c.get_direct_book(zerotime, 1, &bid, &ask) !=
HandleReadResult::SUCCESS);
}
for (auto &mc : managed_consumers) {
TimeStamp zerotime;
REQUIRE(mc->get_direct_book(zerotime, 1, &bid, &ask) !=
HandleReadResult::SUCCESS);
}
// Only NOW create the producers (and the shared memory)
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers = create_test_securities(producer);
// Set bbo on the producers - still not public
TimeStamp write_time = TimeStamp::now();
for (auto &p : producers) {
p.update_bbo(write_time, PriceSize_from_double<3>(4.5, 5),
PriceSize_from_double<3>(6.75, 7));
}
assign_ids(producers.begin(), producers.end());
consumer.scan_securities();
size_t bbo_count = 0;
// Existing old handles remain invalid
for (auto &c : consumers) {
TimeStamp zerotime;
if (c.get_direct_book(zerotime, 1, &bid, &ask) ==
HandleReadResult::SUCCESS) {
bbo_count++;
}
}
REQUIRE(bbo_count == 0);
// Now, magically, the existing (managed) handles are valid
for (auto &mc : managed_consumers) {
TimeStamp zerotime;
if (mc->get_direct_book(zerotime, 1, &bid, &ask) ==
HandleReadResult::SUCCESS) {
bbo_count++;
}
}
REQUIRE(bbo_count > 0);
}
void test_no_double_security_callback() {
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers = create_test_securities(producer);
// Assign ids so securities are already public in the producer
assign_ids(producers.begin(), producers.end());
// Symbol list for the consumer
auto symbols = get_test_symbols();
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &symbols, true,
TEST_PLATFORM);
// Add the on_security callback - callback fires for existing securities
vector<Security> securities;
consumer.on_security(
[&](const SecurityPtr &security, MDConsumerHandle handle) {
REQUIRE(security->id() != invalid_security_key);
REQUIRE(security->id() == handle.key());
securities.push_back(*security);
});
// Found all securities
REQUIRE(securities.size() == producers.size());
// Check for new securities
consumer.scan_securities();
// Expect no duplicates
REQUIRE(securities.size() == producers.size());
}
void test_recovery() {
auto producer1 = make_shared<MDProducer>(TEST_PLATFORM);
vector<MDProducerHandle> producers = create_test_securities(*producer1);
assign_ids(producers.begin(), producers.end());
producer1->get_channel("FOO", SECURITYTYPE_FUTURE, VENUE_BTS).heartbeat();
producer1->get_channel("FOOX", SECURITYTYPE_OPTION, VENUE_BTS)
.heartbeat();
for (auto &pro : producers) {
pro.update_bbo(TimeStamp::now(), PriceSize({1, 0}, 1),
PriceSize({2, 0}, 2));
}
// Symbol list for the consumer
SecurityCache cache;
auto symbols = get_test_symbols();
MDClient client(nullptr, nullptr, nullptr, &symbols);
MDConsumer consumer = client.current_consumer();
map<security_key, MDConsumerHandle> products;
consumer.on_security(
[&](const SecurityPtr &security, MDConsumerHandle handle) {
REQUIRE(security->id() == handle.key());
products.insert(make_pair(security->id(), handle));
});
client.scan_securities();
REQUIRE(products.size() == producers.size());
// Check handles
for (auto &pair : products) {
auto &handle = pair.second;
REQUIRE(handle.is_initialized());
REQUIRE(handle.is_heart_beating());
}
// Delete the producer
try {
producers.clear();
producer1.reset();
} catch (std::exception &) {
LOG(md, Error, "Failed test trying to destroy producer.");
REQUIRE(false);
}
// Wait out the heartbeat
auto options = config::get<md::PublisherOptions>();
auto ms = std::max(options.heartbeat_ms(),
options.heartbeat_boot_seconds() * 1000) +
10;
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(ms));
// Make sure heartbeat is done
try {
for (auto &pair : products) {
auto &handle = pair.second;
REQUIRE(handle.is_initialized());
REQUIRE(handle.is_heart_beating() == false);
}
} catch (std::exception &) {
LOG(md, Error, "Failed to validate heartbeat while producer is down.");
REQUIRE(false);
}
// Create a new producer and attempt to recover
shared_ptr<MDProducer> producer2;
try {
producer2 = make_shared<MDProducer>(TEST_PLATFORM);
} catch (std::exception &) {
LOG(md, Error, "Failed to create new producer");
REQUIRE(false);
}
producers = create_test_securities(*producer2);
REQUIRE(products.size() == producers.size());
assign_ids(producers.begin(), producers.end());
producer2->get_channel("FOO", SECURITYTYPE_FUTURE, VENUE_BTS).heartbeat();
producer2->get_channel("FOOX", SECURITYTYPE_OPTION, VENUE_BTS)
.heartbeat();
// set bbo to a different value
for (auto &pro : producers) {
pro.update_bbo(TimeStamp::now(), PriceSize({4, 0}, 5),
PriceSize({6, 0}, 7));
}
// Attempt to recover the managed handles
int on_consumer_count = 0;
auto handle = client.on_consumer().subscribe([&](const MDConsumer &c) {
++on_consumer_count;
consumer = c;
products.clear();
});
client.check_heartbeats();
REQUIRE(on_consumer_count == 1);
REQUIRE(products.empty());
// Consumer has been updated
consumer.on_security(
[&](const SecurityPtr &security, MDConsumerHandle handle) {
REQUIRE(security->id() == handle.key());
products.insert(make_pair(security->id(), handle));
});
client.on_consumer().unsubscribe(handle);
// Check that the new product handles have been updated
for (auto &pair : products) {
auto &handle = pair.second;
REQUIRE(handle.is_initialized());
REQUIRE(handle.is_heart_beating());
TimeStamp tzero;
PriceSize bid, ask;
REQUIRE(handle.get_direct_book(tzero, 1, &bid, &ask) ==
HandleReadResult::SUCCESS);
REQUIRE(bid.price() == 4.0);
REQUIRE(bid.size() == 5);
REQUIRE(ask.price() == 6.0);
REQUIRE(ask.size() == 7);
}
}
void test_no_block_forever_on_publisher_crash() {
#ifdef _WIN32
#pragma warning(push, 0)
#endif
// OK if this isn't aligned for testing
auto src =
std::make_unique<Co2TestInstrument>("Foo", SECURITYTYPE_FUTURE);
#ifdef _WIN32
#pragma warning(pop)
#endif
TimeStamp read = TimeStamp::now();
src->producer().update_bbo(TimeStamp::invalid(),
PriceSize_from_double<3>(100.0, 100),
PriceSize_from_double<3>(101.0, 101));
PriceSize bid, ask;
TimeStamp start = TimeStamp::now();
auto result = src->consumer().get_direct_book(read, 1, &bid, &ask);
TimeStamp end = TimeStamp::now();
REQUIRE(result != HandleReadResult::SUCCESS);
REQUIRE((end - start).seconds() < 5);
}
void test_get_handles() {
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers = create_test_securities(producer);
// Add entries to the shared memory that we aren't looking for
vector<MDProducerHandle> p2_no_id = create_test_securities(producer, "2");
vector<MDProducerHandle> p3 = create_test_securities(producer, "3");
for (auto &p : producers) {
p.update_bbo(TimeStamp::now(), PriceSize_from_double<3>(5.0, 5),
PriceSize_from_double<3>(5.0, 5));
}
assign_ids(producers.begin(), producers.end());
assign_ids(p3.begin(), p3.end());
// Symbol list for the consumer - only search half of the symbols
set<UserSymbol> symbols;
UserSymbol future_symbol("FOO", VENUE_BTS, SECURITYTYPE_FUTURE);
symbols.insert(future_symbol);
UserSymbol option_symbol("FOOX", VENUE_BTS, SECURITYTYPE_OPTION);
symbols.insert(option_symbol);
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &symbols, true,
TEST_PLATFORM);
set<security_key> keys;
consumer.walk_security_list(
[&](const SecurityPtr &security, MDConsumerHandle) {
keys.insert(security->id());
});
REQUIRE(keys.size() == producers.size());
auto map = consumer.get_handles(keys);
REQUIRE(map.size() == keys.size());
for (const auto &entry : map) {
REQUIRE(entry.second.is_initialized());
}
}
void test_denormalization() {
// By using a different symbol for each security
// we put them all on separate shared mem sections (co2 channels)
security_key key = 999;
Security f1 = mkfuture("F1", ++key, 2016, 5, 1);
Security o1 = mkoption("O1", ++key, 2016, 5, 1, 100);
Security o2 = mkoption("O2", ++key, 2016, 5, 2, 200);
Security o3 = mkoption("O3", ++key, 2016, 5, 3, 300);
Security s4 = mkspread("S3", ++key, o2, o3);
Security s3 = mkspread("S4", ++key, o1, s4);
Security s2 = mkspread("S2", ++key, o1, s4, s3);
Security s1 = mkspread("S1", ++key, f1, s2);
Security all_secs_array[] = {f1, o1, o2, o3, s1, s2, s3, s4};
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers;
for (auto &sec : all_secs_array) {
with_security_like(sec, [&](FixedSecurity &fsec) {
convert_security(sec, fsec, true);
producers.push_back(producer.add_security(fsec));
});
}
set<UserSymbol> csymbols;
csymbols.insert(UserSymbol("F1", VENUE_BTS, SECURITYTYPE_FUTURE));
csymbols.insert(UserSymbol("O1", VENUE_BTS, SECURITYTYPE_OPTION));
csymbols.insert(UserSymbol("O2", VENUE_BTS, SECURITYTYPE_OPTION));
csymbols.insert(UserSymbol("O3", VENUE_BTS, SECURITYTYPE_OPTION));
csymbols.insert(UserSymbol("S1", VENUE_BTS, SECURITYTYPE_SPREAD));
csymbols.insert(UserSymbol("S2", VENUE_BTS, SECURITYTYPE_SPREAD));
csymbols.insert(UserSymbol("S3", VENUE_BTS, SECURITYTYPE_SPREAD));
csymbols.insert(UserSymbol("S4", VENUE_BTS, SECURITYTYPE_SPREAD));
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &csymbols, true,
TEST_PLATFORM);
auto check_sec = [&](const SecurityPtr &security,
MDConsumerHandle handle) {
REQUIRE(security->id() == handle.key());
auto iter = find_if(
std::begin(all_secs_array), std::end(all_secs_array),
[&](const Security &sec) { return sec.id() == security->id(); });
REQUIRE(iter != end(all_secs_array));
REQUIRE(iter->symbol() == (security->symbol()));
if (security->Contract_case() == Security::kSpread) {
REQUIRE(security->spread().normalized() == false);
REQUIRE(security->spread().legs_size() ==
iter->spread().legs_size());
for (int x = 0; x < security->spread().legs_size(); ++x) {
REQUIRE((security->spread().legs(x).security().id()) ==
(iter->spread().legs(x).security().id()));
}
}
};
consumer.walk_security_list(check_sec);
consumer.on_security(check_sec);
consumer.scan_securities();
}
void test_denormalization2() {
// Just like test_denormailzation but with a different memory layout
security_key key = 999;
Security f1 = mkfuture("FF", ++key, 2016, 5, 1);
Security o1 = mkoption("OO", ++key, 2016, 5, 1, 100);
Security o2 = mkoption("OO", ++key, 2016, 5, 2, 200);
Security o3 = mkoption("OO", ++key, 2016, 5, 3, 300);
Security s4 = mkspread("SS", ++key, o2, o3);
Security s3 = mkspread("SS", ++key, o1, s4);
Security s2 = mkspread("SS", ++key, o1, s4, s3);
Security s1 = mkspread("SS", ++key, f1, s2);
Security all_secs_array[] = {f1, o1, o2, o3, s1, s2, s3, s4};
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers;
for (auto &sec : all_secs_array) {
with_security_like(sec, [&](FixedSecurity &fsec) {
convert_security(sec, fsec, true);
producers.push_back(producer.add_security(fsec));
});
}
set<UserSymbol> csymbols;
csymbols.insert(UserSymbol("FF", VENUE_BTS, SECURITYTYPE_FUTURE));
csymbols.insert(UserSymbol("OO", VENUE_BTS, SECURITYTYPE_OPTION));
csymbols.insert(UserSymbol("SS", VENUE_BTS, SECURITYTYPE_SPREAD));
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &csymbols, true,
TEST_PLATFORM);
auto check_sec = [&](const SecurityPtr &security,
MDConsumerHandle handle) {
REQUIRE(security->id() == handle.key());
auto iter = find_if(
begin(all_secs_array), end(all_secs_array),
[&](const Security &sec) { return sec.id() == security->id(); });
REQUIRE(iter != end(all_secs_array));
REQUIRE((iter->id()) == (security->id()));
if (security->Contract_case() == Security::kSpread) {
REQUIRE(security->spread().normalized() == false);
REQUIRE(security->spread().legs_size() ==
iter->spread().legs_size());
for (int x = 0; x < security->spread().legs_size(); ++x) {
REQUIRE((security->spread().legs(x).security().id()) ==
(iter->spread().legs(x).security().id()));
}
}
};
consumer.walk_security_list(check_sec);
consumer.on_security(check_sec);
consumer.scan_securities();
}
void test_retry_unresolved_spread() {
// Make 2 spreads with option legs
// Leave out one leg for hte other spread
// The other spread only shows up once its option leg is produced
Security o1 = mkoption("OO", 400, 2016, 5, 1, 100);
Security o2 = mkoption("OO", 401, 2016, 5, 2, 200);
Security o3 = mkoption("OO", 402, 2016, 5, 3, 300);
Security s1 = mkspread("SS", 500, o1, o2);
Security s2 = mkspread("SS", 501, o2, o3);
MDProducer producer(TEST_PLATFORM);
map<security_key, MDProducerHandle> handles;
auto add_security = [&](const Security &sec) {
with_security_like(sec, [&](FixedSecurity &fsec) {
convert_security(sec, fsec, true);
handles.insert(make_pair(fsec.key, producer.add_security(fsec)));
});
};
// "options publisher has not started yet"
// add_security(o1);
// add_security(o2);
// add_security(o3);
add_security(s1);
add_security(s2);
// Create consumer
set<UserSymbol> consumer_symbols;
consumer_symbols.insert(UserSymbol("OO", VENUE_BTS, SECURITYTYPE_OPTION));
consumer_symbols.insert(UserSymbol("SS", VENUE_BTS, SECURITYTYPE_SPREAD));
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &consumer_symbols, true,
TEST_PLATFORM);
size_t security_count = 0;
// Indirection for check function
function<void(const Security &)> check_func;
check_func = [&](const Security &) { REQUIRE(false); };
consumer.on_security([&](const SecurityPtr &sec, MDConsumerHandle) {
++security_count;
check_func(*sec);
});
consumer.scan_securities();
REQUIRE(security_count == 0);
// OK, now add enough options to resolve s1 but not s2
add_security(o1);
add_security(o2);
check_func = [&](const Security &security) {
if (security.Contract_case() == Security::kSpread) {
set<security_key> legs;
for (const auto &leg : security.spread().legs()) {
legs.insert(leg.security().id());
}
REQUIRE(security.id() == 500);
REQUIRE(legs.size() == 2);
REQUIRE(legs.count(400) == 1);
REQUIRE(legs.count(401) == 1);
}
};
consumer.scan_securities(); // finds o1,o2,s1
consumer.scan_securities(); // finds nothing
REQUIRE(security_count == 3);
// Then add the last security and we should see s2
add_security(o3);
check_func = [&](const Security &security) {
if (security.Contract_case() == Security::kSpread) {
set<security_key> legs;
for (const auto &leg : security.spread().legs()) {
legs.insert(leg.security().id());
}
REQUIRE(security.id() == 501);
REQUIRE(legs.size() == 2);
REQUIRE(legs.count(401) == 1);
REQUIRE(legs.count(402) == 1);
}
};
consumer.scan_securities();
REQUIRE(security_count == 5);
}
#ifndef _WIN32
void test_settlement_recorder() {
using namespace datetime;
security_key k1 = 401, k2 = 402;
Security o1 = mkoption("OO", k1, 2016, 5, 1, 100);
Security o2 = mkoption("OO", k2, 2016, 5, 2, 200);
MDProducer producer(TEST_PLATFORM);
auto add_security = [&](const Security &sec) {
unique_ptr<MDProducerHandle> phandle;
with_security_like(sec, [&](FixedSecurity &fsec) {
convert_security(sec, fsec, true);
phandle.reset(new MDProducerHandle(producer.add_security(fsec)));
});
return phandle;
};
auto ph1 = *add_security(o1);
auto ph2 = *add_security(o2);
set<UserSymbol> consumer_symbols;
consumer_symbols.insert(UserSymbol("OO", VENUE_BTS, SECURITYTYPE_OPTION));
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &consumer_symbols, true,
TEST_PLATFORM);
auto h1 = consumer.get_handle(k1);
REQUIRE(h1.is_initialized());
auto h2 = consumer.get_handle(k2);
// Pick 2 valid dates in the future
auto today = datetime::today();
today.year += 1;
auto yesterday = datetime::add_days(today, -1);
{
co2::SettlementRecorder recorder(PLATFORM_UNSET);
recorder.add_settlement(ph1, {101, 0}, yesterday);
recorder.add_settlement(ph1, {102, 0}, today);
recorder.add_settlement(ph2, {199, 0}, yesterday);
recorder.add_settlement(ph2, {188, 0}, today);
recorder.do_work();
recorder.do_work();
}
REQUIRE(h1.settlement_price().price() == 102);
REQUIRE(h1.misc().settlement_date == today);
REQUIRE(h1.misc().previous_settlement_price.price() == 101);
REQUIRE(h1.misc().previous_settlement_date == yesterday);
REQUIRE(h2.settlement_price().price() == 188);
REQUIRE(h2.misc().settlement_date == today);
REQUIRE(h2.misc().previous_settlement_price.price() == 199);
REQUIRE(h2.misc().previous_settlement_date == yesterday);
// Update but skip persist
auto tomorrow = datetime::add_days(today, 1);
ph1.update_settlement(TimeStamp::now(), {103, 0}, tomorrow);
REQUIRE(h1.settlement_price().price() == 103);
REQUIRE(h1.misc().settlement_date == tomorrow);
REQUIRE(h1.misc().previous_settlement_price.price() == 102);
REQUIRE(h1.misc().previous_settlement_date == today);
ph1.update_previous_settlement(TimeStamp::now(), {0, 0}, Date());
REQUIRE(h1.settlement_price().price() == 103);
REQUIRE(h1.misc().settlement_date == tomorrow);
ph1.update_settlement(TimeStamp::now(), {0, 0}, Date());
REQUIRE(h1.settlement_price().price() == 0);
REQUIRE(h1.misc().settlement_date.day == 0);
// pushing a new settlement for today doesn't invalidate yesterday
ph2.update_settlement(TimeStamp::now(), {201, 0}, today);
REQUIRE(h2.settlement_price().price() == 201);
REQUIRE(h2.misc().settlement_date == today);
REQUIRE(h2.misc().previous_settlement_price.price() == 199);
REQUIRE(h2.misc().previous_settlement_date == yesterday);
// Restore persisted state
{
co2::SettlementRecorder recorder(PLATFORM_UNSET);
recorder.restore_settlement(ph1);
recorder.restore_settlement(ph2);
recorder.do_work();
}
REQUIRE(h1.settlement_price().price() == 102);
REQUIRE(h1.misc().settlement_date == today);
REQUIRE(h1.misc().previous_settlement_price.price() == 101);
REQUIRE(h1.misc().previous_settlement_date == yesterday);
REQUIRE(h2.settlement_price().price() == 188);
REQUIRE(h2.misc().settlement_date == today);
REQUIRE(h2.misc().previous_settlement_price.price() == 199);
REQUIRE(h2.misc().previous_settlement_date == yesterday);
}
#endif
void teardown() {}
};
std::unique_ptr<Co2Tests> co2_tests;
TEST_CASE("CO2Tests", "[md]") {
// Lazy created this for config
if (!co2_tests)
co2_tests = std::make_unique<Co2Tests>();
co2_tests->setup();
#ifndef _WIN32
SECTION("test_security_id_recorder") {
co2_tests->test_security_id_recorder(":memory:");
co2_tests->test_security_id_recorder("file::memory:?cache=shared");
}
SECTION("test_security_id_recorder_spreads") {
co2_tests->test_security_id_recorder_spreads();
}
#endif
SECTION("test_price_size") { co2_tests->test_price_size(); }
SECTION("test_price_size_required_precision") {
co2_tests->test_price_size_required_precision();
}
SECTION("test_slow_double_conversion") {
co2_tests->test_slow_double_conversion();
}
SECTION("test_producer_consumer") { co2_tests->test_producer_consumer(); }
SECTION("test_consumer_callback_on_security_added") {
co2_tests->test_consumer_callback_on_security_added();
}
SECTION("test_consumer_defer_handle1") {
co2_tests->test_consumer_defer_handle1();
}
SECTION("test_consumer_defer_handle2") {
co2_tests->test_consumer_defer_handle2();
}
SECTION("test_no_double_security_callback") {
co2_tests->test_no_double_security_callback();
}
SECTION("test_recovery") { co2_tests->test_recovery(); }
SECTION("test_no_block_forever_on_publisher_crash") {
co2_tests->test_no_block_forever_on_publisher_crash();
}
SECTION("test_get_handles") { co2_tests->test_get_handles(); }
SECTION("test_denormalization") { co2_tests->test_denormalization(); }
SECTION("test_denormalization2") { co2_tests->test_denormalization2(); }
SECTION("test_retry_unresolved_spread") {
co2_tests->test_retry_unresolved_spread();
}
#ifndef _WIN32
SECTION("test_settlement_recorder") {
co2_tests->test_settlement_recorder();
}
#endif
}
struct Co2IndexTests {
vector<uint64_t> random_ids;
set<uint64_t> used_ids;
const int iterations;
tbb::task_scheduler_init _tbb;
Co2IndexTests()
: random_ids(), used_ids(), iterations(50),
_tbb(tbb::task_scheduler_init::automatic) {}
void setup() {
used_ids.clear();
random_ids.clear();
std::uniform_int_distribution<int> rand_distribution(1, 0x7FFFFFFF);
std::mt19937 random_number_engine;
auto genrandom = std::bind(rand_distribution, random_number_engine);
set<uint64_t> temp_ids;
for (int x = 0; x < 9999; ++x) {
auto r = genrandom();
if (x < 1000) {
used_ids.insert(r);
}
temp_ids.insert(r);
}
for (uint64_t id : temp_ids) {
random_ids.push_back(id);
}
REQUIRE(random_ids.size() > 1000);
}
struct Handle {
Handle() : _key(0) {}
uint64_t _key;
security_key key() const { return _key; }
void key(security_key key) { _key = key; }
};
struct SecurityData {
uint64_t exchange_id;
Security security;
Handle handle;
};
template <class Index> void gen_securities(Index &index) {
unique_ptr<SecurityData> temp;
uint32_t count = 0;
for (auto &id : used_ids) {
++count;
temp.reset(new SecurityData());
temp->security.Clear();
temp->security.set_symbol("FOOX");
temp->security.mutable_option();
temp->security.mutable_glbx()->set_exchange_id(id);
temp->exchange_id = id;
temp->handle.key(count);
index.add_security(id, move(temp));
}
}
template <class Index> void test_random_search(Index &index) {
vector<uint64_t> temp(random_ids);
utils::Shuffle::shuffle(temp.begin(), temp.end());
for (auto &id : temp) {
if (used_ids.find(id) != used_ids.end()) {
REQUIRE(index.has_security(id));
} else {
REQUIRE(index.get_security(id) == nullptr);
}
}
}
template <class Index> void test_walk(Index &index) {
int count = 0;
index.walk_security_list([&count](const SecurityData &) { ++count; });
REQUIRE(count == (int)used_ids.size());
}
typedef SimpleSecurityIndex<int, SecurityData> MySimpleIndex;
void test_simple_index_search() {
MySimpleIndex index;
gen_securities(index);
for (int x = 0; x < iterations; ++x) {
test_random_search(index);
}
}
void test_simple_index_parallel_search() {
MySimpleIndex index;
gen_securities(index);
tbb::parallel_for(0, iterations, [&](int) { test_random_search(index); });
}
void test_simple_index_iterate() {
MySimpleIndex index;
gen_securities(index);
test_walk(index);
}
typedef ConcurrentSecurityIndex<int, SecurityData> MyConcurrentIndex;
void test_concurrent_index_search() {
MyConcurrentIndex index;
gen_securities(index);
for (int x = 0; x < iterations; ++x) {
test_random_search(index);
}
}
void test_concurrent_index_parallel_search() {
MyConcurrentIndex index;
gen_securities(index);
tbb::parallel_for(0, iterations, [&](int) { test_random_search(index); });
}
void test_concurrent_index_iterate() {
MyConcurrentIndex index;
gen_securities(index);
test_walk(index);
}
};
struct Co2RelayTests {
SecurityCache cache;
Co2RelayTests() : _stats() {}
void setup() {
config::internal::initialize_for_test(
"PlatformMap {"
" value {"
" glbx_platform: 'PLATFORM_SIM'"
" }"
"}");
}
void teardown() {}
MDConsumer create_consumer() {
set<UserSymbol> symbols;
UserSymbol future_symbol("FOO", VENUE_BTS, SECURITYTYPE_FUTURE);
symbols.insert(future_symbol);
UserSymbol option_symbol("FOOX", VENUE_BTS, SECURITYTYPE_OPTION);
symbols.insert(option_symbol);
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &symbols);
return consumer;
}
template <class T> T simulate_wire_transfer(const T &request) {
T temp;
std::string buffer;
request.SerializeToString(&buffer);
temp.ParseFromString(buffer);
return temp;
}
metrics::MetricsContainer _stats;
const std::set<UserSymbol> setup_symbols() {
std::set<UserSymbol> result;
for (const auto &symbol : {"FOO", "FOOF"}) {
result.insert({symbol, VENUE_BTS, SECURITYTYPE_FUTURE});
}
return result;
}
std::unique_ptr<IQueryServer>
setup_query_server(MDClient &client, MDProducer &producer,
Entitlements *out_entitlements,
vector<MDProducerHandle> *out_producers) {
// Create a bunch of securities
for (size_t x = 0; x < 100; ++x) {
// That's right. Expirations daily.
out_producers->push_back(producer.add_security(*mkfixedfuture(
"FOO", invalid_security_key, 2017, 1 + (x / 28), x % 27 + 1)));
out_producers->push_back(producer.add_security(*mkfixedfuture(
"FOOF", invalid_security_key, 2017, 1 + (x / 28), x % 27 + 1)));
}
assign_ids(out_producers->begin(), out_producers->end(), 1000);
// set query server
set<UserSymbol> symbols;
symbols.insert({"FOO", VENUE_BTS, SECURITYTYPE_FUTURE});
symbols.insert({"FOOF", VENUE_BTS, SECURITYTYPE_FUTURE});
for (const auto &s : symbols) {
out_entitlements->insert(s.symbol());
}
auto server = create_query_server(client, _stats);
REQUIRE(server);
client.scan_securities();
return server;
}
void relay_nans() {
MDProducer producer(TEST_PLATFORM);
Entitlements entitlements;
// initialize data
vector<MDProducerHandle> producers;
//
// todo: setup_query_server should use client.symbols() instead of it's
// own list
//
const auto symbols = setup_symbols();
MDClient client(nullptr, nullptr, nullptr, &symbols);
auto server =
setup_query_server(client, producer, &entitlements, &producers);
// assign prices
PriceSize price35 = PriceSize_from_double<2>(3.5, 5);
PriceSize price5 = PriceSize_from_double<2>(5, 5);
for (auto p : producers) {
p.update_bbo(TimeStamp::now(), price35, price35);
}
// Request via query server
co2::MDRequest request;
for (const auto &symbol : symbols) {
auto *chan = request.add_channels();
chan->set_type(symbol.type());
chan->set_symbol(symbol.symbol());
chan->set_exchange(symbol.exchange());
chan->set_flags(co2::MDRequest::BBO | co2::MDRequest::SECURITY);
chan->set_entitled(true);
}
// This request should include everything
co2::MDReply reply;
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
REQUIRE((size_t)reply.channels().size() == 2);
// update the request to match the server time
for (int ch = 0; ch < 2; ++ch) {
REQUIRE((size_t)reply.channels(ch).updates().size() ==
producers.size() / 2);
for (auto &update : reply.channels(ch).updates()) {
REQUIRE(update.has_security());
REQUIRE(update.has_direct());
REQUIRE(update.direct().asks_size() > 0);
REQUIRE(update.direct().bids_size() > 0);
REQUIRE(book_at(update.direct().bids(), 0) == price35);
REQUIRE(book_at(update.direct().asks(), 0) == price35);
}
}
// Second request with updated time should include nothing
request.mutable_channels(0)->set_time(reply.channels(0).channel().time());
request.mutable_channels(1)->set_time(reply.channels(1).channel().time());
reply.Clear();
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
REQUIRE(reply.channels().size() == 2);
REQUIRE(reply.channels(0).updates().size() == 0);
REQUIRE(reply.channels(1).updates().size() == 0);
// Update a single price and make sure we get that back
producers[0].update_bbo(TimeStamp::now(), price5, price5, 1);
request.mutable_channels(0)->set_time(reply.channels(0).channel().time());
reply.Clear();
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
REQUIRE(reply.channels().size() == 2);
REQUIRE(reply.channels(0).updates().size() == 1);
REQUIRE(reply.channels(1).updates().size() == 0);
REQUIRE(reply.channels(0).updates(0).has_direct());
REQUIRE(book_at(reply.channels(0).updates(0).direct().bids(), 0) ==
price5);
REQUIRE(book_at(reply.channels(0).updates(0).direct().asks(), 0) ==
price5);
// NaN out all the prices - expect to get a full set of updates
for (auto p : producers) {
p.update_bbo(TimeStamp::now(), NO_MARKET, NO_MARKET, 2);
}
request.mutable_channels(0)->set_time(reply.channels(0).channel().time());
reply.Clear();
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
REQUIRE(reply.channels().size() == 2);
REQUIRE((size_t)reply.channels(0).updates().size() ==
producers.size() / 2);
REQUIRE((size_t)reply.channels(1).updates().size() ==
producers.size() / 2);
for (auto &channel : reply.channels()) {
for (auto &update : channel.updates()) {
REQUIRE(update.has_direct());
// We need to have a book to signal that it was updated but it can
// have no rows. This signifies an empty book and will be written
// as such by the relay client.
REQUIRE(book_at(update.direct().bids(), 0) == NO_MARKET);
REQUIRE(book_at(update.direct().asks(), 0) == NO_MARKET);
}
}
// Fill up the markets again
for (auto p : producers) {
p.update_bbo(TimeStamp::now(), price35, price35, 3);
}
request.mutable_channels(0)->set_time(reply.channels(0).channel().time());
request.mutable_channels(1)->set_time(reply.channels(1).channel().time());
reply.Clear();
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
REQUIRE(reply.channels().size() == 2);
REQUIRE((size_t)reply.channels(0).updates().size() ==
producers.size() / 2);
REQUIRE((size_t)reply.channels(1).updates().size() ==
producers.size() / 2);
for (auto &channel : reply.channels()) {
for (auto &update : channel.updates()) {
REQUIRE(update.has_direct());
REQUIRE(update.direct().bids_size() > 0);
REQUIRE(update.direct().asks_size() > 0);
REQUIRE(book_at(update.direct().bids(), 0) == price35);
REQUIRE(book_at(update.direct().asks(), 0) == price35);
}
}
// NaN out the bids
for (auto p : producers) {
p.update_bbo(TimeStamp::now(), NO_MARKET, price35, 4);
}
request.mutable_channels(0)->set_time(reply.channels(0).channel().time());
request.mutable_channels(1)->set_time(reply.channels(1).channel().time());
reply.Clear();
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
REQUIRE(reply.channels().size() == 2);
REQUIRE((size_t)reply.channels(0).updates().size() ==
producers.size() / 2);
REQUIRE((size_t)reply.channels(1).updates().size() ==
producers.size() / 2);
for (auto &channel : reply.channels()) {
for (auto &update : channel.updates()) {
REQUIRE(update.has_direct());
REQUIRE(book_at(update.direct().bids(), 0) == NO_MARKET);
REQUIRE(book_at(update.direct().asks(), 0) == price35);
}
}
}
void relay_implied() {
MDProducer producer(TEST_PLATFORM);
// initialize data
vector<MDProducerHandle> producers;
Entitlements entitlements;
const auto symbols = setup_symbols();
MDClient client(nullptr, nullptr, nullptr, &symbols);
auto server =
setup_query_server(client, producer, &entitlements, &producers);
// assign prices to all levels
auto set_direct = [&](PriceSize bid, PriceSize ask) {
for (auto p : producers) {
std::array<PriceSize, COCONUT_MAX_DEPTH> bids, asks;
for (auto &p : bids)
p = bid;
for (auto &p : asks)
p = ask;
p.update_direct(TimeStamp::now(), bids, asks);
}
};
auto set_implied = [&](PriceSize bid, PriceSize ask) {
for (auto p : producers) {
std::array<PriceSize, COCONUT_MAX_IMPLIED_DEPTH> implied_bids,
implied_asks;
for (auto &p : implied_bids)
p = bid;
for (auto &p : implied_asks)
p = ask;
p.update_implied(TimeStamp::now(), implied_bids, implied_asks);
}
};
PriceSize price35 = PriceSize_from_double<7>(3.5, 5);
PriceSize price5 = PriceSize_from_double<7>(5, 5);
set_direct(price35, price5);
set_implied(price35, price5);
// Form the request
co2::MDRequest request;
auto *chan = request.add_channels();
chan->set_type(SECURITYTYPE_FUTURE);
chan->set_symbol("FOO");
chan->set_exchange(VENUE_BTS);
chan->set_flags(co2::MDRequest::DIRECT | co2::MDRequest::IMPLIED);
// Send the request
co2::MDReply reply;
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
// check price levels
REQUIRE(reply.channels_size() > 0);
for (auto &channel : reply.channels()) {
for (auto &update : channel.updates()) {
REQUIRE(update.has_direct());
REQUIRE(update.has_implied());
REQUIRE(update.direct().bids_size() == COCONUT_MAX_DEPTH);
REQUIRE(update.direct().asks_size() == COCONUT_MAX_DEPTH);
REQUIRE(update.implied().bids_size() == COCONUT_MAX_IMPLIED_DEPTH);
REQUIRE(update.implied().asks_size() == COCONUT_MAX_IMPLIED_DEPTH);
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
REQUIRE(book_at(update.direct().bids(), x) == price35);
REQUIRE(book_at(update.direct().asks(), x) == price5);
}
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(book_at(update.implied().bids(), x) == price35);
REQUIRE(book_at(update.implied().asks(), x) == price5);
}
}
}
//
// Nan out implied
request.mutable_channels(0)->set_time(reply.channels(0).channel().time());
set_implied(NO_MARKET, NO_MARKET);
reply.Clear();
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
for (auto &channel : reply.channels()) {
for (auto &update : channel.updates()) {
REQUIRE(update.has_direct());
REQUIRE(update.has_implied());
REQUIRE(update.direct().bids_size() == COCONUT_MAX_DEPTH);
REQUIRE(update.direct().asks_size() == COCONUT_MAX_DEPTH);
REQUIRE(update.implied().bids_size() == 0);
REQUIRE(update.implied().asks_size() == 0);
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
REQUIRE(book_at(update.direct().bids(), x) == price35);
REQUIRE(book_at(update.direct().asks(), x) == price5);
}
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(book_at(update.implied().bids(), x) == NO_MARKET);
REQUIRE(book_at(update.implied().asks(), x) == NO_MARKET);
}
}
}
//
// Nan out direct
request.mutable_channels(0)->set_time(reply.channels(0).channel().time());
set_direct(NO_MARKET, NO_MARKET);
reply.Clear();
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
for (auto &channel : reply.channels()) {
for (auto &update : channel.updates()) {
REQUIRE(update.has_direct());
REQUIRE(update.has_implied());
REQUIRE(update.direct().bids_size() == 0);
REQUIRE(update.direct().asks_size() == 0);
REQUIRE(update.implied().bids_size() == 0);
REQUIRE(update.implied().asks_size() == 0);
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
REQUIRE(book_at(update.direct().bids(), x) == NO_MARKET);
REQUIRE(book_at(update.direct().asks(), x) == NO_MARKET);
}
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(book_at(update.implied().bids(), x) == NO_MARKET);
REQUIRE(book_at(update.implied().asks(), x) == NO_MARKET);
}
}
}
//
// restore values
request.mutable_channels(0)->set_time(reply.channels(0).channel().time());
set_direct(price5, price35);
set_implied(price5, price35);
reply.Clear();
server->query(request, &reply, &entitlements, false);
reply = simulate_wire_transfer(reply);
for (auto &channel : reply.channels()) {
for (auto &update : channel.updates()) {
REQUIRE(update.has_direct());
REQUIRE(update.has_implied());
REQUIRE(update.direct().bids_size() == COCONUT_MAX_DEPTH);
REQUIRE(update.direct().asks_size() == COCONUT_MAX_DEPTH);
REQUIRE(update.implied().bids_size() == COCONUT_MAX_IMPLIED_DEPTH);
REQUIRE(update.implied().asks_size() == COCONUT_MAX_IMPLIED_DEPTH);
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
REQUIRE(book_at(update.direct().bids(), x) == price5);
REQUIRE(book_at(update.direct().asks(), x) == price35);
}
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(book_at(update.implied().bids(), x) == price5);
REQUIRE(book_at(update.implied().asks(), x) == price35);
}
}
}
}
};
TEST_CASE("Co2RelayTests", "[md]") {
Co2RelayTests co2_relay_tests;
co2_relay_tests.setup();
SECTION("relay_nans") { co2_relay_tests.relay_nans(); }
SECTION("relay_implied") { co2_relay_tests.relay_implied(); }
}
namespace ImpliedTests {
void test_merge(
const std::array<PriceSize, COCONUT_MAX_DEPTH> &direct_bid,
const std::array<PriceSize, COCONUT_MAX_IMPLIED_DEPTH> &implied_bid,
const std::array<PriceSize, COCONUT_MAX_DEPTH> &direct_ask,
const std::array<PriceSize, COCONUT_MAX_IMPLIED_DEPTH> &implied_ask,
const std::function<void(const vector<MDConsumerHandle> &)> &validate) {
MDProducer producer(TEST_PLATFORM);
vector<MDProducerHandle> producers = create_test_securities(producer);
auto write_time = TimeStamp::now();
// update both direct and implied
for (auto &p : producers) {
p.update_direct(write_time, direct_bid, direct_ask, 0);
p.update_implied(write_time, implied_bid, implied_ask, 1);
}
// read from both
set<UserSymbol> symbols;
UserSymbol future_symbol("FOO", VENUE_BTS, SECURITYTYPE_FUTURE);
symbols.insert(future_symbol);
UserSymbol option_symbol("FOOX", VENUE_BTS, SECURITYTYPE_OPTION);
symbols.insert(option_symbol);
SecurityCache cache;
MDConsumer consumer(cache, nullptr, nullptr, &symbols);
vector<Security> consumer_securities;
// Poll for security list - none visible until they have keys
size_t count = consumer.walk_security_list(
[&](const SecurityPtr &security, MDConsumerHandle) {
consumer_securities.push_back(*security);
});
// Since no security has an id none are "public"
REQUIRE(consumer_securities.size() == count);
REQUIRE(count == 0);
assign_ids(producers.begin(), producers.end());
// Now securities can be enumerated by the client
count = consumer.walk_security_list(
[&](const SecurityPtr &security, MDConsumerHandle) {
consumer_securities.push_back(*security);
});
REQUIRE(consumer_securities.size() == count);
REQUIRE(count == 24);
vector<MDConsumerHandle> consumers;
for (const auto &security : consumer_securities) {
consumers.push_back(consumer.get_handle(security.id()));
}
validate(consumers);
}
} // namespace ImpliedTests
TEST_CASE("MergeImpliedTests", "[md]") {
config::internal::initialize_for_test(
"PlatformMap {"
" value {"
" glbx_platform: 'PLATFORM_SIM'"
" }"
"}");
// create a handle with implied and direct
std::array<PriceSize, COCONUT_MAX_DEPTH> direct_bid, direct_ask;
std::array<PriceSize, COCONUT_MAX_IMPLIED_DEPTH> implied_bid, implied_ask;
SECTION("equal_impiled_deep") {
for (size_t x = 0; x < direct_bid.size(); ++x) {
direct_bid[x] = PriceSize_from_double<7>(x * 100, 2);
if (x < implied_bid.size()) {
implied_bid[x] = PriceSize_from_double<7>(x * 100, 1);
}
}
direct_ask = direct_bid;
implied_ask = implied_bid;
ImpliedTests::test_merge(
direct_bid, implied_bid, direct_ask, implied_ask,
[](const vector<MDConsumerHandle> &consumers) {
for (const auto &c : consumers) {
std::array<PriceSize, COCONUT_MAX_DEPTH> bids, asks;
TimeStamp read_time;
// get_book returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, COCONUT_MAX_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
REQUIRE(bids[x].size() == 2);
REQUIRE(bids[x].direct_size() == 2);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].size() == 2);
REQUIRE(asks[x].direct_size() == 2);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 1);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 1);
}
read_time = TimeStamp();
REQUIRE(c.get_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0],
&asks[0]) == HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
if (x < COCONUT_MAX_IMPLIED_DEPTH) {
REQUIRE(bids[x].size() == 3);
REQUIRE(asks[x].size() == 3);
REQUIRE(bids[x].direct_size() == 2);
REQUIRE(bids[x].implied_size() == 1);
REQUIRE(asks[x].direct_size() == 2);
REQUIRE(asks[x].implied_size() == 1);
} else {
REQUIRE(bids[x].size() == 2);
REQUIRE(asks[x].size() == 2);
REQUIRE(bids[x].direct_size() == 2);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 2);
REQUIRE(asks[x].implied_size() == 0);
}
}
}
});
}
SECTION("partial_Read") {
direct_bid[0] = {{700, 1}, 1};
direct_bid[1] = {{600, 1}, 1};
direct_bid[2] = {{500, 1}, 1};
direct_bid[3] = {{400, 1}, 1};
direct_ask[0] = {{800, 1}, 1};
direct_ask[1] = {{900, 1}, 1};
direct_ask[2] = {{1000, 1}, 1};
direct_ask[3] = {{1100, 1}, 1};
implied_bid[0] = {{700, 1}, 2};
implied_bid[1] = {{600, 1}, 2};
implied_ask[0] = {{800, 1}, 2};
implied_ask[1] = {{900, 1}, 2};
ImpliedTests::test_merge(
direct_bid, implied_bid, direct_ask, implied_ask,
[](const vector<MDConsumerHandle> &consumers) {
const size_t TEST_DEPTH = 4;
for (const auto &c : consumers) {
std::array<PriceSize, TEST_DEPTH> bids, asks;
int canary = 0;
TimeStamp read_time;
// get_boo returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0],
&asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < TEST_DEPTH; ++x) {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(bids[x].size() == 2);
REQUIRE(asks[x].size() == 2);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 2);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 2);
}
read_time = TimeStamp();
REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < TEST_DEPTH; ++x) {
if (x < COCONUT_MAX_IMPLIED_DEPTH) {
REQUIRE(bids[x].size() == 3);
REQUIRE(asks[x].size() == 3);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 2);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 2);
} else {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 0);
}
}
REQUIRE(canary == 0);
}
});
}
SECTION("better_implied") {
direct_bid[0] = {{500, 1}, 1};
direct_bid[1] = {{400, 1}, 1};
direct_bid[2] = {{300, 1}, 1};
direct_bid[3] = {{200, 1}, 1};
direct_ask[0] = {{1000, 1}, 1};
direct_ask[1] = {{1100, 1}, 1};
direct_ask[2] = {{1200, 1}, 1};
direct_ask[3] = {{1300, 1}, 1};
implied_bid[0] = {{600, 1}, 2};
implied_bid[1] = {{500, 1}, 2};
implied_ask[0] = {{900, 1}, 2};
implied_ask[1] = {{1000, 1}, 2};
ImpliedTests::test_merge(
direct_bid, implied_bid, direct_ask, implied_ask,
[=](const vector<MDConsumerHandle> &consumers) {
const size_t TEST_DEPTH = 4;
for (const auto &c : consumers) {
std::array<PriceSize, TEST_DEPTH> bids, asks;
int canary = 0;
TimeStamp read_time;
// get_boo returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0],
&asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < TEST_DEPTH; ++x) {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(bids[x].size() == 2);
REQUIRE(asks[x].size() == 2);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 2);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 2);
}
read_time = TimeStamp();
REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
REQUIRE(bids[0].size() == 2);
REQUIRE(bids[0].mantissa() == 600);
REQUIRE(asks[0].size() == 2);
REQUIRE(asks[0].mantissa() == 900);
REQUIRE(bids[1].size() == 3);
REQUIRE(asks[1].size() == 3);
REQUIRE(bids[1].direct_size() == 1);
REQUIRE(bids[1].implied_size() == 2);
REQUIRE(asks[1].direct_size() == 1);
REQUIRE(asks[1].implied_size() == 2);
for (size_t x = 2; x < TEST_DEPTH; ++x) {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 0);
}
REQUIRE(canary == 0);
}
});
}
SECTION("worse_implied") {
direct_bid[0] = {{500, 1}, 1};
direct_bid[1] = {{400, 1}, 1};
direct_bid[2] = {{300, 1}, 1};
direct_bid[3] = {{200, 1}, 1};
direct_ask[0] = {{1000, 1}, 1};
direct_ask[1] = {{1100, 1}, 1};
direct_ask[2] = {{1200, 1}, 1};
direct_ask[3] = {{1300, 1}, 1};
implied_bid[0] = {{400, 1}, 2};
implied_bid[1] = {{300, 1}, 2};
implied_ask[0] = {{1100, 1}, 2};
implied_ask[1] = {{1200, 1}, 2};
ImpliedTests::test_merge(
direct_bid, implied_bid, direct_ask, implied_ask,
[=](const vector<MDConsumerHandle> &consumers) {
const size_t TEST_DEPTH = 4;
for (const auto &c : consumers) {
std::array<PriceSize, TEST_DEPTH> bids, asks;
int canary = 0;
TimeStamp read_time;
// get_boo returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0],
&asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < TEST_DEPTH; ++x) {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(bids[x].size() == 2);
REQUIRE(asks[x].size() == 2);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 2);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 2);
}
read_time = TimeStamp();
REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
REQUIRE(bids[0].mantissa() == 500);
REQUIRE(bids[0].size() == 1);
REQUIRE(asks[0].mantissa() == 1000);
REQUIRE(asks[0].size() == 1);
REQUIRE(bids[0].direct_size() == 1);
REQUIRE(bids[0].implied_size() == 0);
REQUIRE(asks[0].direct_size() == 1);
REQUIRE(asks[0].implied_size() == 0);
REQUIRE(bids[1].mantissa() == 400);
REQUIRE(bids[1].size() == 3);
REQUIRE(asks[1].mantissa() == 1100);
REQUIRE(asks[1].size() == 3);
REQUIRE(bids[1].direct_size() == 1);
REQUIRE(bids[1].implied_size() == 2);
REQUIRE(asks[1].direct_size() == 1);
REQUIRE(asks[1].implied_size() == 2);
REQUIRE(bids[2].mantissa() == 300);
REQUIRE(bids[2].size() == 3);
REQUIRE(asks[2].mantissa() == 1200);
REQUIRE(asks[2].size() == 3);
REQUIRE(bids[2].direct_size() == 1);
REQUIRE(bids[2].implied_size() == 2);
REQUIRE(asks[2].direct_size() == 1);
REQUIRE(asks[2].implied_size() == 2);
REQUIRE(bids[3].mantissa() == 200);
REQUIRE(bids[3].size() == 1);
REQUIRE(asks[3].mantissa() == 1300);
REQUIRE(asks[3].size() == 1);
REQUIRE(bids[3].direct_size() == 1);
REQUIRE(bids[3].implied_size() == 0);
REQUIRE(asks[3].direct_size() == 1);
REQUIRE(asks[3].implied_size() == 0);
REQUIRE(canary == 0);
}
});
}
SECTION("worse_implied_bid") {
direct_bid[0] = {{500, 1}, 1};
direct_bid[1] = {{400, 1}, 1};
direct_bid[2] = {{300, 1}, 1};
direct_bid[3] = {{200, 1}, 1};
direct_ask[0] = {{1000, 1}, 1};
direct_ask[1] = {{1100, 1}, 1};
direct_ask[2] = {{1200, 1}, 1};
direct_ask[3] = {{1300, 1}, 1};
implied_bid[0] = {{400, 1}, 2};
implied_bid[1] = {{300, 1}, 2};
implied_ask[0] = {{1000, 1}, 2};
implied_ask[1] = {{1100, 1}, 2};
ImpliedTests::test_merge(
direct_bid, implied_bid, direct_ask, implied_ask,
[=](const vector<MDConsumerHandle> &consumers) {
const size_t TEST_DEPTH = 4;
for (const auto &c : consumers) {
std::array<PriceSize, TEST_DEPTH> bids, asks;
int canary = 0;
TimeStamp read_time;
// get_boo returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0],
&asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < TEST_DEPTH; ++x) {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(bids[x].size() == 2);
REQUIRE(asks[x].size() == 2);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 2);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 2);
}
read_time = TimeStamp();
REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
REQUIRE(bids[0].mantissa() == 500);
REQUIRE(bids[0].size() == 1);
REQUIRE(asks[0].mantissa() == 1000);
REQUIRE(asks[0].size() == 3);
REQUIRE(bids[0].direct_size() == 1);
REQUIRE(bids[0].implied_size() == 0);
REQUIRE(asks[0].direct_size() == 1);
REQUIRE(asks[0].implied_size() == 2);
REQUIRE(bids[1].mantissa() == 400);
REQUIRE(bids[1].size() == 3);
REQUIRE(asks[1].mantissa() == 1100);
REQUIRE(asks[1].size() == 3);
REQUIRE(bids[1].direct_size() == 1);
REQUIRE(bids[1].implied_size() == 2);
REQUIRE(asks[1].direct_size() == 1);
REQUIRE(asks[1].implied_size() == 2);
REQUIRE(bids[2].mantissa() == 300);
REQUIRE(bids[2].size() == 3);
REQUIRE(asks[2].mantissa() == 1200);
REQUIRE(asks[2].size() == 1);
REQUIRE(bids[2].direct_size() == 1);
REQUIRE(bids[2].implied_size() == 2);
REQUIRE(asks[2].direct_size() == 1);
REQUIRE(asks[2].implied_size() == 0);
REQUIRE(bids[3].mantissa() == 200);
REQUIRE(bids[3].size() == 1);
REQUIRE(asks[3].mantissa() == 1300);
REQUIRE(asks[3].size() == 1);
REQUIRE(bids[3].direct_size() == 1);
REQUIRE(bids[3].implied_size() == 0);
REQUIRE(asks[3].direct_size() == 1);
REQUIRE(asks[3].implied_size() == 0);
REQUIRE(canary == 0);
}
});
}
SECTION("worse_implied_ask") {
direct_bid[0] = {{500, 1}, 1};
direct_bid[1] = {{400, 1}, 1};
direct_bid[2] = {{300, 1}, 1};
direct_bid[3] = {{200, 1}, 1};
direct_ask[0] = {{1000, 1}, 1};
direct_ask[1] = {{1100, 1}, 1};
direct_ask[2] = {{1200, 1}, 1};
direct_ask[3] = {{1300, 1}, 1};
implied_bid[0] = {{500, 1}, 2};
implied_bid[1] = {{400, 1}, 2};
implied_ask[0] = {{1300, 1}, 2};
implied_ask[1] = {{1400, 1}, 2};
ImpliedTests::test_merge(
direct_bid, implied_bid, direct_ask, implied_ask,
[=](const vector<MDConsumerHandle> &consumers) {
const size_t TEST_DEPTH = 4;
for (const auto &c : consumers) {
std::array<PriceSize, TEST_DEPTH + 1> bids, asks;
int canary = 0;
TimeStamp read_time;
// get_boo returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0],
&asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < TEST_DEPTH; ++x) {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(bids[x].size() == 2);
REQUIRE(asks[x].size() == 2);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 2);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 2);
}
read_time = TimeStamp();
REQUIRE(c.get_book(read_time, TEST_DEPTH + 1, &bids[0],
&asks[0]) == HandleReadResult::SUCCESS);
REQUIRE(bids[0].mantissa() == 500);
REQUIRE(bids[0].size() == 3);
REQUIRE(asks[0].mantissa() == 1000);
REQUIRE(asks[0].size() == 1);
REQUIRE(bids[0].direct_size() == 1);
REQUIRE(bids[0].implied_size() == 2);
REQUIRE(asks[0].direct_size() == 1);
REQUIRE(asks[0].implied_size() == 0);
REQUIRE(bids[1].mantissa() == 400);
REQUIRE(bids[1].size() == 3);
REQUIRE(asks[1].mantissa() == 1100);
REQUIRE(asks[1].size() == 1);
REQUIRE(bids[1].direct_size() == 1);
REQUIRE(bids[1].implied_size() == 2);
REQUIRE(asks[1].direct_size() == 1);
REQUIRE(asks[1].implied_size() == 0);
REQUIRE(bids[2].mantissa() == 300);
REQUIRE(bids[2].size() == 1);
REQUIRE(asks[2].mantissa() == 1200);
REQUIRE(asks[2].size() == 1);
REQUIRE(bids[2].direct_size() == 1);
REQUIRE(bids[2].implied_size() == 0);
REQUIRE(asks[2].direct_size() == 1);
REQUIRE(asks[2].implied_size() == 0);
REQUIRE(bids[3].mantissa() == 200);
REQUIRE(bids[3].size() == 1);
REQUIRE(asks[3].mantissa() == 1300);
REQUIRE(asks[3].size() == 3);
REQUIRE(bids[3].direct_size() == 1);
REQUIRE(bids[3].implied_size() == 0);
REQUIRE(asks[3].direct_size() == 1);
REQUIRE(asks[3].implied_size() == 2);
REQUIRE(bids[4].mantissa() == 0);
REQUIRE(bids[4].size() == 0);
REQUIRE(asks[4].mantissa() == 1400);
REQUIRE(asks[4].size() == 2);
REQUIRE(bids[4].direct_size() == 0);
REQUIRE(bids[4].implied_size() == 0);
REQUIRE(asks[4].direct_size() == 0);
REQUIRE(asks[4].implied_size() == 2);
REQUIRE(canary == 0);
}
});
}
SECTION("nan_implied") {
direct_bid[0] = {{500, 1}, 1};
direct_bid[1] = {{400, 1}, 1};
direct_bid[2] = {{300, 1}, 1};
direct_bid[3] = {{200, 1}, 1};
direct_ask[0] = {{1000, 1}, 1};
direct_ask[1] = {{1100, 1}, 1};
direct_ask[2] = {{1200, 1}, 1};
direct_ask[3] = {{1300, 1}, 1};
implied_bid[0] = {INT_NAN, 0};
implied_bid[1] = {INT_NAN, 0};
implied_ask[0] = {INT_NAN, 0};
implied_ask[1] = {INT_NAN, 0};
ImpliedTests::test_merge(
direct_bid, implied_bid, direct_ask, implied_ask,
[=](const vector<MDConsumerHandle> &consumers) {
const size_t TEST_DEPTH = 4;
for (const auto &c : consumers) {
std::array<PriceSize, TEST_DEPTH> bids, asks;
int canary = 0;
TimeStamp read_time;
// get_boo returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0],
&asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < TEST_DEPTH; ++x) {
REQUIRE(bids[x].size() == 1);
REQUIRE(asks[x].size() == 1);
REQUIRE(bids[x].direct_size() == 1);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 1);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(bids[x].size() == 0);
REQUIRE(asks[x].size() == 0);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
REQUIRE(bids[0].mantissa() == 500);
REQUIRE(bids[0].size() == 1);
REQUIRE(asks[0].mantissa() == 1000);
REQUIRE(asks[0].size() == 1);
REQUIRE(bids[0].direct_size() == 1);
REQUIRE(bids[0].implied_size() == 0);
REQUIRE(asks[0].direct_size() == 1);
REQUIRE(asks[0].implied_size() == 0);
REQUIRE(bids[1].mantissa() == 400);
REQUIRE(bids[1].size() == 1);
REQUIRE(asks[1].mantissa() == 1100);
REQUIRE(asks[1].size() == 1);
REQUIRE(bids[1].direct_size() == 1);
REQUIRE(bids[1].implied_size() == 0);
REQUIRE(asks[1].direct_size() == 1);
REQUIRE(asks[1].implied_size() == 0);
REQUIRE(bids[2].mantissa() == 300);
REQUIRE(bids[2].size() == 1);
REQUIRE(asks[2].mantissa() == 1200);
REQUIRE(asks[2].size() == 1);
REQUIRE(bids[2].direct_size() == 1);
REQUIRE(bids[2].implied_size() == 0);
REQUIRE(asks[2].direct_size() == 1);
REQUIRE(asks[2].implied_size() == 0);
REQUIRE(bids[3].mantissa() == 200);
REQUIRE(bids[3].size() == 1);
REQUIRE(asks[3].mantissa() == 1300);
REQUIRE(asks[3].size() == 1);
REQUIRE(bids[3].direct_size() == 1);
REQUIRE(bids[3].implied_size() == 0);
REQUIRE(asks[3].direct_size() == 1);
REQUIRE(asks[3].implied_size() == 0);
REQUIRE(canary == 0);
}
});
}
SECTION("nan_direct") {
direct_bid[0] = {INT_NAN, 0};
direct_bid[1] = {INT_NAN, 0};
direct_bid[2] = {INT_NAN, 0};
direct_bid[3] = {INT_NAN, 0};
direct_ask[0] = {INT_NAN, 0};
direct_ask[1] = {INT_NAN, 0};
direct_ask[2] = {INT_NAN, 0};
direct_ask[3] = {INT_NAN, 0};
implied_bid[0] = {{500, 1}, 2};
implied_bid[1] = {{400, 1}, 2};
implied_ask[0] = {{1000, 1}, 2};
implied_ask[1] = {{1100, 1}, 2};
ImpliedTests::test_merge(
direct_bid, implied_bid, direct_ask, implied_ask,
[=](const vector<MDConsumerHandle> &consumers) {
const size_t TEST_DEPTH = 4;
for (const auto &c : consumers) {
std::array<PriceSize, TEST_DEPTH> bids, asks;
int canary = 0;
TimeStamp read_time;
// get_boo returns true becuase this time is after the change
REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0],
&asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < TEST_DEPTH; ++x) {
REQUIRE(bids[x].size() == 0);
REQUIRE(asks[x].size() == 0);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 0);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 0);
}
read_time = TimeStamp();
REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH,
&bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
REQUIRE(bids[x].size() == 2);
REQUIRE(asks[x].size() == 2);
REQUIRE(bids[x].direct_size() == 0);
REQUIRE(bids[x].implied_size() == 2);
REQUIRE(asks[x].direct_size() == 0);
REQUIRE(asks[x].implied_size() == 2);
}
read_time = TimeStamp();
REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) ==
HandleReadResult::SUCCESS);
REQUIRE(bids[0].mantissa() == 500);
REQUIRE(bids[0].size() == 2);
REQUIRE(asks[0].mantissa() == 1000);
REQUIRE(asks[0].size() == 2);
REQUIRE(bids[0].direct_size() == 0);
REQUIRE(bids[0].implied_size() == 2);
REQUIRE(asks[0].direct_size() == 0);
REQUIRE(asks[0].implied_size() == 2);
REQUIRE(bids[1].mantissa() == 400);
REQUIRE(bids[1].size() == 2);
REQUIRE(asks[1].mantissa() == 1100);
REQUIRE(asks[1].size() == 2);
REQUIRE(bids[1].direct_size() == 0);
REQUIRE(bids[1].implied_size() == 2);
REQUIRE(asks[1].direct_size() == 0);
REQUIRE(asks[1].implied_size() == 2);
REQUIRE(bids[2].size() == 0);
REQUIRE(asks[2].size() == 0);
REQUIRE(bids[2].direct_size() == 0);
REQUIRE(bids[2].implied_size() == 0);
REQUIRE(asks[2].direct_size() == 0);
REQUIRE(asks[2].implied_size() == 0);
REQUIRE(bids[3].size() == 0);
REQUIRE(asks[3].size() == 0);
REQUIRE(bids[3].direct_size() == 0);
REQUIRE(bids[3].implied_size() == 0);
REQUIRE(asks[3].direct_size() == 0);
REQUIRE(asks[3].implied_size() == 0);
REQUIRE(canary == 0);
}
});
}
}
TEST_CASE("IntPrice", "[md]") {
SECTION("sign extend") {
auto a = IntPrice_from_double<4>(101.111);
REQUIRE(std::to_string(a.price()) == "101.111000");
REQUIRE(a.mantissa() == 1011110);
a = a.sign_extend(-5);
REQUIRE(std::to_string(a.price()) == "101.111000");
REQUIRE(a.mantissa() == 10111100);
a = a.sign_extend(-9);
REQUIRE(std::to_string(a.price()) == "101.111000");
REQUIRE(a.mantissa() == 101111000000);
}
SECTION("< equal exponent") {
auto a = IntPrice_from_double<4>(101.111);
auto b = IntPrice_from_double<4>(100.111);
REQUIRE((int)a.exponent() == (int)b.exponent());
REQUIRE(b < a);
REQUIRE(!(a < b));
}
SECTION("< different exponent") {
auto a = IntPrice_from_double<4>(101.111); // -4
auto b = IntPrice_from_double<0>(100); // 0
REQUIRE((int)a.exponent() < (int)b.exponent());
REQUIRE(b < a);
REQUIRE(!(a < b));
auto c = std::max(a, b);
REQUIRE(c == a);
}
// subtract with equal exponent
SECTION("subtract equal exponent") {
IntPrice a = IntPrice_from_double<4>(100.333);
IntPrice b = IntPrice_from_double<4>(100.222);
IntPrice delta = a - b;
REQUIRE(delta == IntPrice_from_double<4>(0.111));
}
// subtract smaller from greater
SECTION("subtract smaller from greater") {
IntPrice a = IntPrice_from_double<4>(100.333);
IntPrice b = IntPrice_from_double<5>(100.222);
IntPrice delta = a - b;
REQUIRE(delta == IntPrice_from_double<5>(0.111));
}
// subtract greater from smaller
SECTION("subtract greater from smaller") {
IntPrice a = IntPrice_from_double<4>(100.333);
IntPrice b = IntPrice_from_double<3>(100.111);
IntPrice delta = a - b;
REQUIRE(delta == IntPrice_from_double<4>(0.222));
}
SECTION("subtract NAN from something") {
IntPrice a = IntPrice_from_double<4>(100.333);
IntPrice delta = a - INT_NAN;
REQUIRE(delta == INT_NAN);
}
SECTION("subtract something from NAN") {
IntPrice a = IntPrice_from_double<4>(100.333);
IntPrice delta = INT_NAN - a;
REQUIRE(delta == INT_NAN);
}
SECTION("round equal exponents") {
auto price = IntPrice_from_double<4>(100.333);
auto tick = IntPrice_from_double<4>(0.25);
// buy - round down
auto buy = round_to_quote_price(tick, price, api::SIDE_BUY);
REQUIRE(buy == IntPrice_from_double<4>(100.25));
// sell - round up
auto sell = round_to_quote_price(tick, price, api::SIDE_SELL);
REQUIRE(sell == IntPrice_from_double<4>(100.50));
}
SECTION("round smaller tick exponent") {
auto price = IntPrice_from_double<4>(100.333);
auto tick = IntPrice_from_double<3>(0.25);
// buy - round down
auto buy = round_to_quote_price(tick, price, api::SIDE_BUY);
REQUIRE(buy == IntPrice_from_double<4>(100.25));
// sell - round up
auto sell = round_to_quote_price(tick, price, api::SIDE_SELL);
REQUIRE(sell == IntPrice_from_double<4>(100.50));
}
SECTION("round smaller price exponent") {
auto price = IntPrice_from_double<4>(100.333);
auto tick = IntPrice_from_double<7>(0.25);
// buy - round down
auto buy = round_to_quote_price(tick, price, api::SIDE_BUY);
REQUIRE(buy == IntPrice_from_double<7>(100.25));
// sell - round up
auto sell = round_to_quote_price(tick, price, api::SIDE_SELL);
REQUIRE(sell == IntPrice_from_double<4>(100.50));
}
SECTION("round 0.005") {
IntPrice price = {-43595000, -7};
IntPrice tick = {50000, -7};
auto buy = round_to_quote_price(tick, price, api::SIDE_BUY);
REQUIRE(buy == IntPrice_from_double<7>(-4.360));
}
SECTION("division by double") {
IntPrice price = {-43595000, -7};
IntPrice price_div10 = {-43595000, -8};
REQUIRE(price / 10.0 == price_div10);
}
}
} // namespace test
} // namespace md
} // namespace bts
#endif