Files
spark/src/lib/domain_model/Security.h
T
2017-10-06 14:47:12 -04:00

71 lines
1.9 KiB
C++

#pragma once
#include <memory>
#include <cmath>
#include "lib/domain_model/common.h"
#include "lib/domain_model/Security.pb.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(std::round (in.value()/mult));
else
return in;
}
inline double raw_round_to_quote_price (double raw_price, double tick_size, api::Side side)
{
double factor = 1000000;
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 Price round_to_quote_price(api::Security const&sec, Price in, api::Side side) {
auto p = in.value();
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));
}
}}