Add compute_synthetic_book function

This commit is contained in:
Eric Harding committed 2019-02-25 14:26:23 -05:00
1 parent 76a1367510
commit 744797b91c
2 files changed
+83 -10

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "lib/md/IntPrice.h"
#include "lib/md/PriceSize.h"
#include <vector>
namespace bts {
namespace md {
namespace test {
struct PS {
double price;
Size size;
bool operator == (const PS& other) const {
// Convert to IntPrice to avoid double rounding issues
auto p1 = IntPrice_from_double<11>(price);
auto p2 = IntPrice_from_double<11>(other.price);
return p1 == p2 && size == other.size;
}
};
struct Book {
std::vector<PS> bids;
std::vector<PS> asks;
};
Book compute_synthetic_book(const api::Security& security, const std::vector<Book>& leg_books);
std::ostream& operator<< (std::ostream& os, const PS& val);
}}}//namespace
+51 -10
View File
@@ -1,6 +1,7 @@
#ifndef _WIN32
#include "catch/catch.hpp"
#include "../MDConsumerHandle.h"
#include "md_test_utils.h"
#include "Co2TestInstrument.h"
#include "lib/utils/sane_algorithm.h"
#include <memory>
@@ -23,16 +24,6 @@ namespace md {
namespace test {
// std::tuple is hard to read in the debugger
struct PS {
double price;
Size size;
bool operator == (const PS& other) const {
// Convert to IntPrice to avoid double rounding issues
auto p1 = IntPrice_from_double<11>(price);
auto p2 = IntPrice_from_double<11>(other.price);
return p1 == p2 && size == other.size;
}
};
ostream& operator<< (ostream& os, const PS& val) {
os << "{ " << val.price << "," << val.size << " }";
@@ -149,6 +140,56 @@ void set_leg_params(SyntheticStrategy& strat, uint32_t security_key, const spark
map->insert({ security_key, params });
}
Book compute_synthetic_book(const api::Security& security, const std::vector<Book>& leg_books) {
auto synthetic = std::make_shared<SyntheticStrategy>();
synthetic->security.CopyFrom(security);
vector<Co2TestInstrument> leg_instruments;
for (const auto& leg : synthetic->security.synthetic().legs()) {
const auto& security = leg.security();
Co2TestInstrument instr(security.symbol(), securitytype_of(security));
convert_security(security, instr.cold().security, true);
leg_instruments.push_back(instr);
synthetic->legs.push_back(instr.consumer());
}
// Write leg books
for (size_t x = 0; x < leg_books.size(); ++x) {
std::array<PriceSize,COCONUT_MAX_DEPTH> bids = {}, asks = {};
std::transform(
leg_books[x].bids.begin(),
leg_books[x].bids.end(),
bids.begin(),
[](const PS& ps) {
return PriceSize_from_double<7>(ps.price, ps.size);
});
std::transform(
leg_books[x].asks.begin(),
leg_books[x].asks.end(),
asks.begin(),
[](const PS& ps) {
return PriceSize_from_double<7>(ps.price, ps.size);
});
auto ts = TimeStamp::now();
leg_instruments[x].producer().update_direct(ts, bids, asks);
}
// Read synthetic book back
MDConsumerHandle synthetic_handle { synthetic };
std::array<PriceSize,COCONUT_MAX_DEPTH> raw_bids, raw_asks;
TimeStamp t;
synthetic_handle.get_direct_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
Book result {};
sane::map(raw_bids, result.bids, [](PriceSize ps) -> PS { return {ps.price(), ps.size()}; });
sane::map(raw_asks, result.asks, [](PriceSize ps) -> PS { return {ps.price(), ps.size()}; });
return result;
}
///////////////////////
// Test Cases
///////////////////////
TEST_CASE("SyntheticBookMerge", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;