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

182 lines
5.2 KiB
C++

#pragma once
#include <cmath>
#include <memory>
#include "lib/domain_model/Security.pb.h"
#include "lib/domain_model/common.h"
#include "lib/utils/bts_assert.h"
namespace bts {
namespace api {
class Security;
}
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 &);
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;
}
}
inline double market_epislon() { return 1e-10; }
inline bool is_better(api::Side side, Price p1, Price p2) {
switch (side) {
case api::SIDE_BUY:
return p1.value() - p2.value() > market_epislon();
case api::SIDE_SELL:
return p2.value() - p1.value() > market_epislon();
default:
return false;
}
}
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_epislon();
}
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;
}
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));
}
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 the difference between 2 prices in ticks.
// the result is an a signed int so the order of args
// matter, its second price - first price
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));
}
// 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;
}
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);
}
} // namespace securities
} // namespace bts