Also moved a few logging deps from lib_spark -> system: * bts_chrono.h * bts_no_copy.h * defer.h * dispatcher.h/cpp * file_utils.h/cpp * type_name.h
178 lines
6.6 KiB
C++
178 lines
6.6 KiB
C++
#include "FTXFuture.h"
|
|
#include "domain/future_security.h"
|
|
#include "domain/protobufs/venue.pb.h"
|
|
#include "domain/security.h"
|
|
#include "logging/logging.h"
|
|
#include "system/json/json_reader.h"
|
|
#include "system/table_formatter.h"
|
|
|
|
#include <ccapi_cpp/ccapi_message.h>
|
|
#include <nlohmann/json.hpp>
|
|
#include <regex>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
namespace bts::md::ftx {
|
|
|
|
using bts::core::TableFormatter;
|
|
using bts::domain::FutureSecurity;
|
|
using bts::domain::Security;
|
|
using bts::io::JsonReader;
|
|
using std::string;
|
|
using std::stringstream;
|
|
using std::vector;
|
|
|
|
#define VENUE bts::domain::protobufs::VENUE_FTX
|
|
|
|
static std::string deduce_sym_from_name(const std::string &name) {
|
|
std::regex word_regex("(\\w+)");
|
|
auto words_begin =
|
|
std::sregex_iterator(name.begin(), name.end(), word_regex);
|
|
auto words_end = std::sregex_iterator();
|
|
if (words_begin != words_end) {
|
|
return words_begin->str();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
static std::string deduce_sym(const FTXFuture &ffut) {
|
|
if (!ffut.underlying.empty()) {
|
|
return ffut.underlying;
|
|
}
|
|
return deduce_sym_from_name(ffut.name);
|
|
}
|
|
|
|
void FTXFuture::parse(const ccapi::Message &msg, vector<Security> &securities) {
|
|
const auto &sec_elements = msg.getElementList();
|
|
for (const auto &sec_el : sec_elements) {
|
|
const auto &sec_props = sec_el.getNameValueMap();
|
|
auto it = sec_props.find(CCAPI_HTTP_BODY);
|
|
if (it == sec_props.end()) {
|
|
continue;
|
|
}
|
|
const auto &body = it->second;
|
|
const auto js = nlohmann::json::parse(body);
|
|
vector<FTXFuture> ftx_futs;
|
|
if (!js["success"].get<bool>()) {
|
|
LOG(md, Error,
|
|
"FTXFutureParser received json element w/ 'success' false");
|
|
continue;
|
|
}
|
|
js["result"].get_to(ftx_futs);
|
|
for (const auto &ftx_fut : ftx_futs) {
|
|
switch (ftx_fut.type.value) {
|
|
case FTXSecurityType::FUTURE:
|
|
case FTXSecurityType::PERPETUAL:
|
|
break;
|
|
case FTXSecurityType::MOVE:
|
|
case FTXSecurityType::PREDICTION:
|
|
// TODO: Add support for 'MOVE' and 'PREDICTION' futs. They may
|
|
// complicate things because they have the same exp dates as
|
|
// 'FUTURE' futs. This translates to duplicate sym/exp pairs since
|
|
// all fall under the umbrella of a uniform 'Future' type in our
|
|
// system.
|
|
default:
|
|
continue;
|
|
}
|
|
auto &sec = securities.emplace_back();
|
|
auto &scomm = sec.security_common;
|
|
scomm.venue = VENUE;
|
|
|
|
scomm.exchange_sec_id = ftx_fut.name;
|
|
scomm.desc = ftx_fut.name;
|
|
scomm.symbol = deduce_sym(ftx_fut);
|
|
scomm.asset = scomm.symbol;
|
|
scomm.group = scomm.symbol;
|
|
|
|
scomm.min_price_increment = ftx_fut.price_increment;
|
|
scomm.min_quantity = ftx_fut.size_increment;
|
|
scomm.min_quantity_increment = ftx_fut.size_increment;
|
|
scomm.currency = "USD";
|
|
|
|
auto &fut = sec.security.emplace<FutureSecurity>();
|
|
fut.expiration = ftx_fut.expiry;
|
|
LOG(md, Diag, "Converted FTXFuture:\n{}to domain::Security:\n{}",
|
|
ftx_fut, sec.to_string(true));
|
|
}
|
|
}
|
|
}
|
|
|
|
void from_json(const nlohmann::json &j, FTXFuture &fut) {
|
|
const string js_str = j.dump(4);
|
|
LOG(md, Diag, "Parsing FTXFuture from json:\n{}\n", js_str);
|
|
|
|
JsonReader jr{&j};
|
|
jr.read("ask", fut.ask);
|
|
jr.read("bid", fut.bid);
|
|
jr.read("change1h", fut.change_1h);
|
|
jr.read("change24h", fut.change_24h);
|
|
jr.read("changeBod", fut.change_bod);
|
|
jr.read("volumeUsd24h", fut.volume_usd_24h);
|
|
jr.read("volume", fut.volume);
|
|
jr.read("description", fut.description);
|
|
jr.read("enabled", fut.enabled);
|
|
jr.read("expired", fut.expired);
|
|
jr.read("expiry", fut.expiry);
|
|
jr.read("index", fut.index);
|
|
jr.read("imfFactor", fut.imf_factor);
|
|
jr.read("last", fut.last);
|
|
jr.read("lowerBound", fut.lower_bound);
|
|
jr.read("mark", fut.mark);
|
|
jr.read("name", fut.name);
|
|
jr.read("openInterest", fut.open_interest);
|
|
jr.read("openInterestUsd", fut.open_interest_usd);
|
|
jr.read("perpetual", fut.perpetual);
|
|
jr.read("positionLimitWeight", fut.position_limit_weight);
|
|
jr.read("postOnly", fut.post_only);
|
|
jr.read("priceIncrement", fut.price_increment);
|
|
jr.read("sizeIncrement", fut.size_increment);
|
|
jr.read("underlying", fut.underlying);
|
|
jr.read("upperBound", fut.upper_bound);
|
|
jr.read("type", fut.type);
|
|
}
|
|
|
|
string FTXFuture::to_string_pretty() const {
|
|
stringstream out;
|
|
TableFormatter::SchemaBuilder<FTXFuture> schema;
|
|
schema.add("ask", [](const auto &f) { return f.ask; });
|
|
schema.add("bid", [](const auto &f) { return f.bid; });
|
|
schema.add("change_1h", [](const auto &f) { return f.change_1h; });
|
|
schema.add("change_24h", [](const auto &f) { return f.change_24h; });
|
|
schema.add("change_bod", [](const auto &f) { return f.change_bod; });
|
|
schema.add("volume_usd_24h", [](const auto &f) { return f.volume_usd_24h; });
|
|
schema.add("volume", [](const auto &f) { return f.volume; });
|
|
schema.add("description", [](const auto &f) { return f.description; });
|
|
schema.add("enabled", [](const auto &f) { return f.enabled; });
|
|
schema.add("expired", [](const auto &f) { return f.expired; });
|
|
schema.add("expiry", [](const auto &f) { return f.expiry; });
|
|
schema.add("index", [](const auto &f) { return f.index; });
|
|
schema.add("imf_factor", [](const auto &f) { return f.imf_factor; });
|
|
schema.add("last", [](const auto &f) { return f.last; });
|
|
schema.add("lower_bound", [](const auto &f) { return f.lower_bound; });
|
|
schema.add("mark", [](const auto &f) { return f.mark; });
|
|
schema.add("name", [](const auto &f) { return f.name; });
|
|
schema.add("open_interest", [](const auto &f) { return f.open_interest; });
|
|
schema.add(
|
|
"open_interest_usd", [](const auto &f) { return f.open_interest_usd; });
|
|
schema.add("perpetual", [](const auto &f) { return f.perpetual; });
|
|
schema.add("position_limit_weight", [](const auto &f) {
|
|
return f.position_limit_weight;
|
|
});
|
|
schema.add("post_only", [](const auto &f) { return f.post_only; });
|
|
schema.add(
|
|
"price_increment", [](const auto &f) { return f.price_increment; });
|
|
schema.add("size_increment", [](const auto &f) { return f.size_increment; });
|
|
schema.add("underlying", [](const auto &f) { return f.underlying; });
|
|
schema.add("upper_bound", [](const auto &f) { return f.upper_bound; });
|
|
schema.add("type", [](const auto &f) { return f.type; });
|
|
TableFormatter::format_record(out, *this, schema.result);
|
|
return out.str();
|
|
}
|
|
|
|
std::ostream &operator<<(std::ostream &out, const FTXFuture &fut) noexcept {
|
|
out << fut.name;
|
|
return out;
|
|
}
|
|
|
|
} // namespace bts::md::ftx
|