106 lines
3.3 KiB
C++
106 lines
3.3 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <cmath>
|
|
|
|
|
|
#include "lib/domain_model/common.h"
|
|
#include "lib/domain_model/Security.pb.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 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));
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}}
|