Files
spark/src/lib/domain_model/Security.h
T

369 lines
10 KiB
C++

#pragma once
#include "lib/core/FMT_IMPL.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/domain_model/SecurityType.h"
#include "lib/domain_model/Side.h"
#include "lib/domain_model/Venue.h"
#include "lib/domain_model/common.h"
#include "lib/utils/bts_assert.h"
#include <cmath>
namespace boost {
namespace gregorian {
class date;
}
} // namespace boost
namespace bts {
namespace api {
class Security;
class ICEExchangeData;
} // namespace api
namespace securities {
/**
* Gets a descriptor string for a security (should be unique within a particular exchange).
*/
const std::string &descriptor(
const api::Security &sec, const std::string &default_rv = "UNDEFINED");
/**
* Gets a descriptor string for a security that is unique within the entire
* system (not just exchange).
*/
std::string unique_descriptor(const api::Security &security);
api::SecurityType security_type(api::Security const &);
api::SecurityType security_type(api::Security::ContractCase contract_case);
void write_date_string(
std::uint32_t year,
std::uint32_t month,
std::uint32_t day,
std::ostringstream &result);
inline std::string const &side_name(api::Side side) {
return api::Side_Name(side);
}
inline api::Side other_side(api::Side side) {
switch (side) {
case api::SIDE_BUY:
return api::SIDE_SELL;
case api::SIDE_SELL:
return api::SIDE_BUY;
default:
return side;
}
}
template <class V> class TwoSided {
public:
TwoSided() : buy(), sell() {}
TwoSided(V const &buy, V const &sell) : buy(buy), sell(sell) {}
V const &get(api::Side side) const {
if (side == api::SIDE_BUY)
return buy;
else
return sell;
}
void set(api::Side side, V const &v) {
if (side == api::SIDE_BUY)
buy = v;
else
sell = v;
}
private:
V buy;
V sell;
};
inline double market_epsilon() { return 1e-10; }
// better == p1 more aggressive than p1 ie for a buy order p1 is higher
// than p2, and for a sell order p1 is lower than p2
inline bool is_better(api::Side side, Price p1, Price p2) {
switch (side) {
case api::SIDE_BUY:
return p1.value() - p2.value() > market_epsilon();
case api::SIDE_SELL:
return p2.value() - p1.value() > market_epsilon();
default:
return false;
}
}
inline bool is_worse(api::Side s, Price p1, Price p2) {
return is_better(s, p2, p1);
}
template <class C> void apply_to_both_sides(C c) {
c(api::SIDE_BUY);
c(api::SIDE_SELL);
}
// like the above function
inline Price improve_price(api::Side side, Price p, Price offset) {
switch (side) {
case api::SIDE_BUY:
return Price(p.value() + offset.value());
case api::SIDE_SELL:
return Price(p.value() - offset.value());
default:
bts_assert(false);
break;
}
bts_assert(false);
return Price(0);
}
inline Price worsen_price(api::Side side, Price p, Price offset) {
return improve_price(other_side(side), p, offset);
}
inline bool is_same(Price p1, Price p2) {
return std::abs(p1.value() - p2.value()) < market_epsilon();
}
inline bool is_same(double d1, double d2) {
return std::abs(d1 - d2) < market_epsilon();
}
inline bool is_equal_or_better(api::Side side, Price p1, Price p2) {
return is_same(p1, p2) || is_better(side, p1, p2);
}
// returns the better price out of p1, p2, if p1==p2,
// return p1
inline Price better_price(api::Side side, Price p1, Price p2) {
return is_equal_or_better(side, p1, p2) ? p1 : p2;
}
// Returns the worst possible price representable in the Price data type for the
// side in question
inline Price worst_price(api::Side side) {
switch (side) {
case api::SIDE_BUY:
return Price{std::numeric_limits<double>::min()};
case api::SIDE_SELL:
return Price{std::numeric_limits<double>::max()};
default:
bts_assert(false);
break;
}
bts_assert(false);
return Price(0);
}
inline Price best_price(api::Side side) {
return worst_price(other_side(side));
}
inline Price decode_exchange_price(api::Security const &sec, Price in) {
return Price(in.value() * sec.contract_spec().price_multiplier());
}
inline Price encode_exchange_price(api::Security const &sec, Price in) {
auto mult = sec.contract_spec().price_multiplier();
if (mult != 1.0)
return Price(in.value() / mult);
else
return in;
}
inline double
raw_round_to_quote_price(double raw_price, double tick_size, api::Side side) {
double factor = 1000000000;
auto i_price = llround(factor * raw_price);
auto i_tick_size = llround(tick_size * factor);
long new_i_price = 0;
long mod = i_price % i_tick_size;
if (mod == 0)
// price is already a valid tick
new_i_price = i_price;
else if (side == api::SIDE_BUY) {
// buy, round down
if (i_price > 0)
new_i_price = i_price - mod;
else
new_i_price = i_price - mod - i_tick_size;
} else if (side == api::SIDE_SELL) {
// sell, round up
if (i_price > 0)
new_i_price = i_price - mod + i_tick_size;
else
new_i_price = i_price - mod;
}
return new_i_price / factor;
}
inline double checked_round_to_quote_price(
const api::ContractSpecification &spec, double price, api::Side side) {
if (std::isnan(price))
return price;
if (price < -spec.price_break() || price > spec.price_break()) {
if (spec.high_tick() == 0)
return price;
return raw_round_to_quote_price(price, spec.high_tick(), side);
} else {
if (spec.low_tick() == 0)
return price;
return raw_round_to_quote_price(price, spec.low_tick(), side);
}
}
inline Price
round_to_quote_price(api::Security const &sec, Price in, api::Side side) {
auto p = in.value();
bts_assert_d(!std::isnan(p));
if (p < -sec.contract_spec().price_break() ||
p > sec.contract_spec().price_break())
return Price(
raw_round_to_quote_price(p, sec.contract_spec().high_tick(), side));
else
return Price(
raw_round_to_quote_price(p, sec.contract_spec().low_tick(), side));
}
std::optional<core::TimeStamp> expiration(api::Security const &sec);
bool is_expired(api::Security const &security);
// for testing
bool is_expired(api::Security const &security, core::TimeStamp now);
bool is_expired(
api::Security const &security,
core::TimeStamp now,
uint32_t sod_hh,
uint32_t sod_mm);
inline Price tick_size(api::Security const &sec) {
return Price(sec.contract_spec().low_tick());
}
inline const std::string &descriptor(
const api::Security &sec, const std::string &default_rv) {
switch (sec.ExchangeData_case()) {
case api::Security::kGlbx:
return sec.glbx().security_desc();
case api::Security::kCboe:
return sec.cboe().security_desc();
case api::Security::kCfe:
return sec.cfe().security_desc();
case api::Security::kIce:
return sec.ice().security_desc();
case api::Security::kDeribit:
return sec.deribit().security_desc();
case api::Security::kFtx:
return sec.ftx().security_desc();
case api::Security::kBinance:
return sec.binance().security_desc();
case api::Security::kBinanceUsdsFutures:
return sec.binance_usds_futures().security_desc();
case api::Security::kBinanceUs:
return sec.binance_us().security_desc();
case api::Security::kCoinbase:
return sec.coinbase().security_desc();
case api::Security::kOkx:
return sec.okx().security_desc();
case api::Security::kTaifex:
return sec.taifex().security_desc();
default: {
if (sec.has_synthetic()) {
return sec.synthetic().name();
}
return default_rv;
}
}
}
// Looking to round md::IntPrice? Look in IntPrice.h
// computes price_2 - price_1 in ticks
inline int
price_diff_to_ticks(double price_1, double price_2, double tick_size) {
return static_cast<int>(std::round((price_2 - price_1) / tick_size));
}
// returns the difference (in ticks) bw and order and a market.
// if the order has crossed the market, returns -ve ticks
// (ie the buy order price > market_price or
// sell_order_price < market_price)
inline int order_diff_market(
double order_price, double market_price, api::Side side, double tick_size) {
switch (side) {
case api::SIDE_BUY:
return price_diff_to_ticks(order_price, market_price, tick_size);
case api::SIDE_SELL:
return price_diff_to_ticks(market_price, order_price, tick_size);
default:
bts_assert(false);
break;
}
return 0;
}
// because of prices not being completely rounded to the tick,
// simple double equality can not be used to compare prices.
// this function deems 2 prices to be equal if they are within
// 1 % of the tick size
inline bool
test_price_equality(double price_1, double price_2, double tick_size) {
return std::fabs(price_2 - price_1) < 0.01 * tick_size;
}
inline bool gr_than_eq(double p1, double p2, double tick_size) {
return test_price_equality(p1, p2, tick_size) || p1 >= p2;
}
// p1 >= p2
inline bool gr_than_eq(double p1, double p2) {
return test_price_equality(p1, p2, market_epsilon()) || p1 >= p2;
}
inline bool gr_than(double p1, double p2, double tick_size) {
return gr_than_eq(p1, p2, tick_size) &&
!test_price_equality(p1, p2, tick_size);
}
inline bool gr_than(double p1, double p2) {
auto tick_size = market_epsilon();
return gr_than_eq(p1, p2, tick_size) &&
!test_price_equality(p1, p2, tick_size);
}
inline bool test_price_equality(double price_1, double price_2) {
return std::fabs(price_2 - price_1) < market_epsilon();
}
inline bool eq(double price_1, double price_2) {
return std::fabs(price_2 - price_1) < market_epsilon();
}
// ice specialization /////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
inline bool is_ice_spread(const bts::api::ICEExchangeData &ice_data) {
const auto &strip = ice_data.strip_name();
return strip.find("/") != std::string::npos;
}
inline bool is_strip(const bts::api::Security &sec) {
return sec.has_ice() && sec.has_spread() && !is_ice_spread(sec.ice());
}
inline bool is_spread_of_strips(const bts::api::Security &sec) {
if (sec.has_spread())
if (sec.has_ice()) {
const auto &spr = sec.spread();
for (const auto &l : spr.legs())
// assumption : either all legs are strips or none
return is_strip(l.security());
}
return false;
}
} // namespace securities
} // namespace bts
FMT_IMPL(bts::api::Security, s, "{}", bts::securities::descriptor(s));