Files
spark/src/lib/orderman/orderman_utils/Formatter.h
T

330 lines
9.9 KiB
C++

#pragma once
#include "domain/domain.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/md/PriceSize.h"
#include "lib/orderman/orderman_utils/Book.h"
#include "lib/orderman/orders/algo/IAlgoOrder.h"
#include "lib/utils/PBUtils.h"
#include <fmt/format.h>
#include <functional>
#include <iostream>
#include <map>
#include <range/v3/view.hpp>
#include <sstream>
namespace bts {
namespace orderman {
struct Formatter {
using Side = bts::api::Side;
// this is a placeholder for other book formats down the road.
enum class BookFormat { BBO = 1 };
enum class Tense { Present, Preterite };
template <typename T>
inline static void concat(std::ostream &ostr, const T &v) {
ostr << v;
}
template <typename T, typename... Args>
inline static void concat(std::ostream &ostr, const T &first, Args... args) {
ostr << first;
(concat(ostr, args), ...);
}
template <typename T> inline static std::string fmt(const T &curr) {
std::stringstream str;
str << curr;
return str.str();
}
template <typename T, typename... Args>
inline static std::string fmt(const T &curr, Args... next) {
std::stringstream str;
concat(str, curr, next...);
return str.str();
}
template <class Iter>
static std::string fmt_values(
Iter first,
Iter last,
const char *delim = ", ",
const char *if_none = "None") {
if (first == last)
return std::string(if_none);
std::stringstream str;
str << *first;
while (++first != last)
str << delim << *first;
return str.str();
}
// convenient overloads, so callers need only pass a container param 'foo'
// instead of 'foo.begin(), foo.end()'
template <class T>
static std::string fmt_values(const std::vector<T> &values) {
return fmt_values(values.begin(), values.end());
}
template <class T>
static std::string fmt_values(const std::unordered_set<T> &values) {
return fmt_values(values.begin(), values.end());
}
template <class TKey, class TValue>
static std::string fmt_values(const std::map<TKey, TValue> &map) {
auto values = map | ranges::views::transform(
[](const auto &p) { return p.second; });
return fmt_values(values.begin(), values.end());
}
static std::string fmt_fill(Side side, uint32_t qty, double price) {
std::stringstream ostr;
ostr << to_string(side, Tense::Preterite) << " " << qty << " @ " << price;
return ostr.str();
}
static std::string fmt_order(Side side, uint32_t qty, double price) {
std::stringstream ostr;
ostr << to_string(side) << " " << qty << " @ " << price;
return ostr.str();
}
static std::string fmt_order(
OrderKey key,
uint32_t qty,
double price,
const api::orderman::OrderDefinition &def) {
std::stringstream ostr;
ostr << key << " " << to_string(def.side()) << " " << qty << " @ "
<< price << std::endl
<< "[Def]" << utils::pb::to_string(def);
return ostr.str();
}
static std::string fmt_order(
OrderKey key, Side side, uint32_t qty, double price, ChildState state) {
std::stringstream ostr;
ostr << key << " " << to_string(side) << " " << qty << " @ " << price
<< " " << to_string(state);
return ostr.str();
}
private:
template <typename T, typename... Args>
inline static void join_next(std::ostream &, const std::string &) {}
template <typename T, typename... Args>
inline static void join_next(std::ostream &, std::function<std::string()>) {}
template <typename T, typename... Args>
inline static void
join_next(std::ostream &str, const std::string &separator, const T &curr) {
str << separator << curr;
}
template <typename T, typename... Args>
inline static void join_next(
std::ostream &str,
std::function<std::string()> get_separator,
const T &curr) {
str << get_separator() << curr;
}
template <typename T, typename... Args>
inline static void join_next(
std::ostream &str,
const std::string &separator,
const T &curr,
Args... next) {
str << separator << curr;
(join_next(str, separator, next), ...);
}
template <typename T, typename... Args>
inline static void join_next(
std::ostream &str,
std::function<std::string()> get_separator,
const T &curr,
Args... next) {
str << get_separator() << curr;
(join_next(str, get_separator, next), ...);
}
public:
template <typename T, typename... Next>
inline static std::string join(const std::string &, const T &curr) {
std::stringstream str;
str << curr;
return str.str();
}
template <typename T, typename... Next>
inline static std::string
join(const std::string &separator, const T &curr, Next... next) {
std::stringstream str;
str << curr;
join_next(str, separator, next...);
return str.str();
}
template <typename T, typename... Next>
inline static std::ostream &
join(std::ostream &out, const char *separator, const T &curr, Next... next) {
out << curr;
join_next(out, separator, next...);
return out;
}
template <typename T, typename... Next>
inline static std::string join(
std::function<std::string()> get_separator,
const T &curr,
Next... next) {
std::stringstream str;
str << curr;
join_next(str, get_separator, next...);
return str.str();
}
template <class T> static std::string to_string(const T &value) {
std::stringstream str;
str << value;
return str.str();
}
static std::string to_string(bool value) { return value ? "true" : "false"; }
static std::string to_string(Side side, Tense tense = Tense::Present) {
switch (side) {
case bts::api::SIDE_BUY:
switch (tense) {
case Tense::Preterite:
return "Bought";
case Tense::Present:
default:
return "Buy";
}
case bts::api::SIDE_SELL:
switch (tense) {
case Tense::Preterite:
return "Sold";
case Tense::Present:
default:
return "Sell";
}
default:
return fmt("Side_", static_cast<int>(side));
}
}
static std::string to_string(ChildStatus value) {
switch (value) {
case ChildStatus::WORKING:
return "Working";
case ChildStatus::COMPLETED:
return "Completed";
case ChildStatus::CANCELED:
return "Canceled";
default:
return fmt("ChildStatus_", static_cast<int>(value));
}
}
static std::string to_string(ChildState value) {
std::stringstream ostr;
ostr << "[" << to_string(value.status) << "] " << value.quantity.value()
<< " @ " << value.price.value() << "(total filled "
<< value.total_fills.value() << " @[avg] " << value.avg_price.value()
<< ", last filled " << value.last_fills.value() << " @ "
<< value.last_price.value() << ")";
return ostr.str();
}
static std::string to_string(const Book &book) {
return to_string(book, BookFormat::BBO);
}
static std::string to_string(const Book &book, BookFormat format) {
// add other impls as needed
std::stringstream ostr;
switch (format) {
case BookFormat::BBO:
if (book.bid().size() > 0 && !book.bid().at(0).is_nan()) {
auto best_bid = book.bid().at(0);
ostr << best_bid.size() << "@" << best_bid.price();
} else {
ostr << "N/A";
}
ostr << " <> ";
if (book.ask().size() > 0 && !book.ask().at(0).is_nan()) {
auto best_ask = book.ask().at(0);
ostr << best_ask.size() << "@" << best_ask.price();
} else {
ostr << "N/A";
}
break;
default:
break;
}
return ostr.str();
}
static std::string to_string(api::orderman::OrderError error) {
return api::orderman::OrderError_Name(error);
}
static std::string
to_string(api::orderman::CustomOrderDefinition::CustomCase algo_type) {
switch (algo_type) {
case api::orderman::CustomOrderDefinition::CustomCase::kLimit:
return "Limit";
case api::orderman::CustomOrderDefinition::CustomCase::kOffset:
return "Offset";
case api::orderman::CustomOrderDefinition::CustomCase::kCover:
return "Cover";
case api::orderman::CustomOrderDefinition::CustomCase::kIceberg:
return "Iceberg";
default:
return fmt("AlgoType_", static_cast<int>(algo_type));
}
}
};
// << operators
inline std::ostream &operator<<(std::ostream &ostr, const api::Side &side) {
ostr << Formatter::to_string(side);
return ostr;
}
inline std::ostream &
operator<<(std::ostream &ostr, const ChildStatus &child_status) {
ostr << Formatter::to_string(child_status);
return ostr;
}
inline std::ostream &
operator<<(std::ostream &ostr, const ChildState &child_state) {
ostr << "[" << child_state.status << "] " << child_state.quantity.value()
<< " @ " << child_state.price.value() << "(filled "
<< child_state.total_fills.value() << " @avg "
<< child_state.avg_price.value() << ")";
return ostr;
}
inline std::ostream &operator<<(std::ostream &ostr, const bts::Price &price) {
ostr << price.value();
return ostr;
}
inline std::ostream &
operator<<(std::ostream &ostr, const bts::Quantity &quantity) {
ostr << quantity.value();
return ostr;
}
} // namespace orderman
} // namespace bts