325 lines
11 KiB
C++
325 lines
11 KiB
C++
#ifndef _WIN32
|
|
#include "catch/catch.hpp"
|
|
#include "../IntPrice.h"
|
|
#include "../MBOProcessor.h"
|
|
#include "Co2TestInstrument.h"
|
|
#include "lib/utils/Shuffle.h"
|
|
|
|
#include <random>
|
|
#include <algorithm>
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
// fwd declare from MBOServiceServer.cpp
|
|
std::optional<uint32_t> search_orders(const ExchangeOrderKey oid, const std::vector<SnapshotOrder>& orders);
|
|
|
|
namespace test {
|
|
int mbo_tests = 0;
|
|
|
|
TEST_CASE("MBO publish snapshot", "[md][mbo]") {
|
|
MBOProcessor p;
|
|
// publish a snapshot
|
|
auto sec = SecurityKey(99);
|
|
{
|
|
auto s = p.writer().allocate_snapshot();
|
|
s->bids.push_back({ ExchangeOrderKey(1), { 100, 0 }, 42 });
|
|
p.writer().publish_snapshot(sec, &s);
|
|
}
|
|
|
|
auto s = p.reader().acquire_snapshot(sec);
|
|
REQUIRE(s);
|
|
REQUIRE(s->bids.size() == 1);
|
|
REQUIRE(s->bids[0].id.value() == 1);
|
|
REQUIRE(s->asks.size() == 0);
|
|
}
|
|
|
|
std::vector<inc::Update> gen_test_orders(uint64_t id=100, inc::UpdateType type=inc::UpdateType::Bid) {
|
|
std::vector<inc::Update> result;
|
|
inc::Priority priority = 10000;
|
|
for (int p = 100; p < 501; p+=100) {
|
|
// 6 sizes
|
|
for (int32_t s = 1; s <= 5; ++s) {
|
|
IntPrice price { p + 5 , -2};
|
|
auto order_id = ExchangeOrderKey(++id);
|
|
auto size = s;
|
|
priority++;
|
|
result.push_back( inc::Update {
|
|
order_id,
|
|
priority,
|
|
price,
|
|
size,
|
|
type,
|
|
inc::UpdateAction::New
|
|
});
|
|
// std::cout << std::endl << order_id.value() << ": " << price.price() << "-" << size;
|
|
}
|
|
}
|
|
utils::Shuffle::shuffle(result.begin(), result.end());
|
|
return result;
|
|
}
|
|
|
|
TEST_CASE("MBO state to snapshot", "[md][mbo]") {
|
|
// convert state to snapshot
|
|
Co2TestInstrument instr { "MBO", api::SECURITYTYPE_FUTURE };
|
|
auto handle = instr.producer();
|
|
MBOWorkingState state(handle);
|
|
|
|
state.updated = utils::TimeStamp::now();
|
|
|
|
auto bids = gen_test_orders(1000, inc::UpdateType::Bid);
|
|
apply_incremental(state, bids);
|
|
auto asks = gen_test_orders(2000, inc::UpdateType::Ask);
|
|
apply_incremental(state, asks);
|
|
|
|
MBOSnapshot result;
|
|
copy_to_snapshot(state, &result);
|
|
|
|
REQUIRE(result.updated == state.updated);
|
|
REQUIRE(result.bids.size() == state.bid.size());
|
|
REQUIRE(result.asks.size() == state.ask.size());
|
|
REQUIRE(bids.size() == result.bids.size());
|
|
REQUIRE(asks.size() == result.asks.size());
|
|
for (size_t x = 0; x < result.bids.size() - 1; ++x) {
|
|
REQUIRE(result.bids[x].price >= result.bids[x+1].price);
|
|
}
|
|
for (size_t x = 0; x < result.asks.size() - 1; ++x) {
|
|
REQUIRE(result.asks[x].price <= result.asks[x+1].price);
|
|
}
|
|
}
|
|
|
|
template <class T>
|
|
std::vector<T> concat(const std::vector<T>& a, const std::vector<T>& b) {
|
|
std::vector<T> res;
|
|
std::copy(a.begin(), a.end(), std::back_inserter(res));
|
|
std::copy(b.begin(), b.end(), std::back_inserter(res));
|
|
return res;
|
|
}
|
|
|
|
TEST_CASE("MBO incremental", "[md][mbo]") {
|
|
// basic incremental test
|
|
Co2TestInstrument instr { "MBO", api::SECURITYTYPE_FUTURE };
|
|
auto handle = instr.producer();
|
|
MBOWorkingState state(handle);
|
|
|
|
auto orders =
|
|
concat( gen_test_orders(128, inc::UpdateType::Bid),
|
|
gen_test_orders(256, inc::UpdateType::Ask));
|
|
|
|
utils::Shuffle::shuffle(orders.begin(), orders.end());
|
|
apply_incremental(state, orders);
|
|
|
|
REQUIRE(state.bid.size() + state.ask.size() == orders.size());
|
|
|
|
// generate some updates/deletes
|
|
std::vector<inc::Update> updates;
|
|
// - change all size=2 to size=6
|
|
// - delete all size=3
|
|
size_t removed = 0;
|
|
for (const auto& e : orders) {
|
|
if (e.size == 2) {
|
|
updates.push_back( inc::Update {
|
|
e.order_id,
|
|
e.priority,
|
|
e.price,
|
|
6,
|
|
e.type,
|
|
inc::UpdateAction::Change
|
|
});
|
|
}
|
|
else if (e.size == 3) {
|
|
++removed;
|
|
updates.push_back( inc::Update {
|
|
e.order_id,
|
|
e.priority,
|
|
e.price,
|
|
e.size,
|
|
e.type,
|
|
inc::UpdateAction::Delete
|
|
});
|
|
}
|
|
}
|
|
apply_incremental(state,updates);
|
|
|
|
REQUIRE(state.bid.size() + state.ask.size() == (orders.size() - removed));
|
|
|
|
MBOSnapshot result;
|
|
copy_to_snapshot(state, &result);
|
|
|
|
auto all = concat (result.bids, result.asks);
|
|
std::map<int,int> counts;
|
|
for (const auto& order : all) {
|
|
REQUIRE(order.size != 3);
|
|
REQUIRE(order.size != 2);
|
|
counts[order.size] ++;
|
|
}
|
|
REQUIRE(counts[6] > 0);
|
|
REQUIRE(counts[6] == counts[1]);
|
|
REQUIRE(counts[5] == counts[4]);
|
|
REQUIRE(counts[6] == counts[5]);
|
|
}
|
|
|
|
TEST_CASE("MBO sort order", "[md][mbo]") {
|
|
Co2TestInstrument instr { "MBO", api::SECURITYTYPE_FUTURE };
|
|
auto handle = instr.producer();
|
|
MBOWorkingState state(handle);
|
|
|
|
std::vector<inc::Update> updates;
|
|
// multiple updates at the same price
|
|
for (unsigned x=0; x < 100; ++x) {
|
|
// bid
|
|
auto p = 1000 + x;
|
|
updates.push_back( inc::Update {
|
|
ExchangeOrderKey(1000 + x),
|
|
1000 + x,
|
|
{ 1000, -1 },
|
|
(int)x+1,
|
|
inc::UpdateType::Bid,
|
|
inc::UpdateAction::New
|
|
});
|
|
// ask
|
|
if (x < 75) { // ensure bid.size != ask.size
|
|
updates.push_back( inc::Update {
|
|
ExchangeOrderKey(2000 + x),
|
|
2000 + x,
|
|
{ 2000, -1 },
|
|
(int)x+1,
|
|
inc::UpdateType::Ask,
|
|
inc::UpdateAction::New
|
|
});
|
|
}
|
|
}
|
|
std::random_device rd;
|
|
std::shuffle(updates.begin(), updates.end(), std::mt19937(rd()));
|
|
|
|
apply_incremental(state, updates);
|
|
|
|
MBOSnapshot result;
|
|
copy_to_snapshot(state, &result);
|
|
|
|
// Since all orders are at the same price we can assert they are in priority order
|
|
// Since priority is not explicit in the shapshot we set order_id to the same value
|
|
|
|
for (size_t x = 0; x < result.bids.size() - 1; ++x) {
|
|
REQUIRE(result.bids[x].id.value() < result.bids[x+1].id.value());
|
|
}
|
|
for (size_t x = 0; x < result.asks.size() - 1; ++x) {
|
|
REQUIRE(result.asks[x].id.value() < result.asks[x+1].id.value());
|
|
}
|
|
}
|
|
|
|
|
|
TEST_CASE("MBO change price and size", "[md][mbo]") {
|
|
// When an order's size goes up it gets a new priority
|
|
Co2TestInstrument instr { "MBO", api::SECURITYTYPE_FUTURE };
|
|
auto handle = instr.producer();
|
|
MBOWorkingState state(handle);
|
|
|
|
constexpr auto bid = inc::UpdateType::Bid;
|
|
constexpr auto ask = inc::UpdateType::Ask;
|
|
constexpr auto add = inc::UpdateAction::New;
|
|
constexpr auto remove = inc::UpdateAction::Delete;
|
|
constexpr auto change = inc::UpdateAction::Change;
|
|
auto p = [](double price) -> IntPrice { return IntPrice(price * 1000, -3); };
|
|
|
|
// snapshot
|
|
std::vector<inc::Update> snapshot = {
|
|
{ ExchangeOrderKey(701267154552), 1160179309, p(2.975), 1, ask, add},
|
|
{ ExchangeOrderKey(701267991871), 1161068569, p(2.925), 9, bid, add},
|
|
{ ExchangeOrderKey(701267963307), 1161037482, p(2.975), 1, ask, add},
|
|
{ ExchangeOrderKey(701267991967), 1161068670, p(2.900), 1, bid, add},
|
|
{ ExchangeOrderKey(701267928346), 1160999982, p(3.000), 1, ask, add},
|
|
{ ExchangeOrderKey(701267993125), 1161069878, p(2.900), 3, bid, add},
|
|
{ ExchangeOrderKey(701267037972), 1160052467, p(2.875), 1, bid, add},
|
|
{ ExchangeOrderKey(701267099137), 1160119020, p(2.875), 5, bid, add},
|
|
{ ExchangeOrderKey(701267299079), 1160330214, p(2.875), 1, bid, add},
|
|
{ ExchangeOrderKey(701267308619), 1160340068, p(2.875), 5, bid, add},
|
|
{ ExchangeOrderKey(701267310436), 1160341952, p(2.875), 3, bid, add},
|
|
{ ExchangeOrderKey(701267872642), 1160939720, p(2.875), 1, bid, add},
|
|
{ ExchangeOrderKey(701267895533), 1160964252, p(2.875), 1, bid, add},
|
|
{ ExchangeOrderKey(701267953989), 1161027476, p(2.875), 3, bid, add},
|
|
{ ExchangeOrderKey(701267993135), 1161069890, p(2.875), 2, bid, add},
|
|
{ ExchangeOrderKey(701266410447), 1159379621, p(2.850), 3, bid, add},
|
|
{ ExchangeOrderKey(701267324626), 1160356597, p(2.850), 3, bid, add},
|
|
{ ExchangeOrderKey(701267992564), 1161069297, p(2.850), 1, bid, add},
|
|
{ ExchangeOrderKey(701266420294), 1159389827, p(2.775), 5, bid, add},
|
|
{ ExchangeOrderKey(701267334462), 1160366796, p(2.775), 5, bid, add},
|
|
{ ExchangeOrderKey(701266411103), 1159380297, p(2.625), 5, bid, add},
|
|
{ ExchangeOrderKey(701267325284), 1160357275, p(2.625), 5, bid, add},
|
|
};
|
|
|
|
apply_incremental(state, snapshot);
|
|
REQUIRE(state.bid.size() == 19);
|
|
REQUIRE(state.ask.size() == 3);
|
|
|
|
std::vector<inc::Update> incremental = {
|
|
{ ExchangeOrderKey(701267993738), 1161070510, p(2.925), 2, bid, add},
|
|
{ ExchangeOrderKey(701267993759), 1161070532, p(2.975), 3, ask, add},
|
|
{ ExchangeOrderKey(701267993884), 1161070662, p(2.950), 3, ask, add},
|
|
{ ExchangeOrderKey(701267994228), 1161071029, p(2.925), 2, bid, add},
|
|
{ ExchangeOrderKey(701267994243), 1161071044, p(2.925), 4, bid, add},
|
|
{ ExchangeOrderKey(701267994228), 1161071029, p(2.925), 2, bid, remove},
|
|
{ ExchangeOrderKey(701267993135), 1161071281, p(2.875), 3, bid, change},
|
|
};
|
|
apply_incremental(state, incremental);
|
|
REQUIRE(state.bid.size() == 21);
|
|
REQUIRE(state.ask.size() == 5);
|
|
|
|
{
|
|
auto iter = state.bid.begin();
|
|
auto next = state.bid.begin();
|
|
next++;
|
|
for (; next != state.bid.end(); ++iter, ++next) {
|
|
auto cur_price = std::get<IntPrice>(iter->first);
|
|
auto cur_prio = std::get<1>(iter->first);
|
|
auto next_price = std::get<IntPrice>(next->first);
|
|
auto next_prio = std::get<1>(next->first);
|
|
REQUIRE(cur_price >= next_price);
|
|
if (cur_price == next_price) {
|
|
REQUIRE(cur_prio < next_prio);
|
|
}
|
|
}
|
|
}
|
|
{
|
|
auto iter = state.ask.begin();
|
|
auto next = state.ask.begin();
|
|
next++;
|
|
for (; next != state.ask.end(); ++iter, ++next) {
|
|
auto cur_price = std::get<IntPrice>(iter->first);
|
|
auto cur_prio = std::get<1>(iter->first);
|
|
auto next_price = std::get<IntPrice>(next->first);
|
|
auto next_prio = std::get<1>(next->first);
|
|
REQUIRE(cur_price <= next_price);
|
|
if (cur_price == next_price) {
|
|
REQUIRE(cur_prio < next_prio);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Do some tests on the snapshot
|
|
MBOSnapshot result;
|
|
copy_to_snapshot(state, &result);
|
|
|
|
auto ask30 = search_orders(ExchangeOrderKey(701267928346), result.asks);
|
|
REQUIRE(ask30);
|
|
REQUIRE(*ask30 == 0);
|
|
|
|
auto bid2875 = search_orders(ExchangeOrderKey(701267993135), result.bids);
|
|
REQUIRE(bid2875);
|
|
REQUIRE(*bid2875 == 20);
|
|
|
|
bid2875 = search_orders(ExchangeOrderKey(701267299079), result.bids);
|
|
REQUIRE(bid2875);
|
|
REQUIRE(*bid2875 == 6);
|
|
|
|
auto not_bid = search_orders(ExchangeOrderKey(701967299879), result.bids);
|
|
auto not_ask = search_orders(ExchangeOrderKey(701967299879), result.asks);
|
|
REQUIRE(!not_bid);
|
|
REQUIRE(!not_ask);
|
|
}
|
|
|
|
} // namespace test
|
|
} // namespace md
|
|
} // namespace bts
|
|
#endif
|