310 lines
8.6 KiB
C++
310 lines
8.6 KiB
C++
#pragma once
|
|
|
|
#include "lib/utils/datetime.h"
|
|
#include <cmath>
|
|
#include <memory>
|
|
|
|
#include "lib/domain_model/Security.pb.h"
|
|
#include "lib/domain_model/common.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
|
|
namespace boost {
|
|
namespace gregorian {
|
|
class date;
|
|
}
|
|
} // namespace boost
|
|
|
|
namespace bts {
|
|
|
|
namespace api {
|
|
class Security;
|
|
class ICEExchangeData;
|
|
} // namespace api
|
|
|
|
namespace securities {
|
|
SecurityKey security_key(api::Security const &);
|
|
std::string to_str(api::Security const &);
|
|
std::pair<bool, api::Security> from_str(std::string const &);
|
|
std::string descriptor(api::Security const &);
|
|
api::SecurityType security_type(api::Security const &);
|
|
|
|
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_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));
|
|
}
|
|
|
|
boost::gregorian::date expiration(api::Security const &sec);
|
|
bool is_expired(api::Security const &security);
|
|
// for testing
|
|
bool is_expired(api::Security const &security, datetime::PTime const &now);
|
|
bool is_expired(
|
|
api::Security const &security,
|
|
datetime::PTime const &now,
|
|
uint32_t sod_hh,
|
|
uint32_t sod_mm);
|
|
|
|
inline Price tick_size(api::Security const &sec) {
|
|
return Price(sec.contract_spec().low_tick());
|
|
}
|
|
|
|
// 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
|