120 lines
4.1 KiB
C++
120 lines
4.1 KiB
C++
#pragma once
|
|
|
|
#include <cmath>
|
|
#ifndef _WIN32
|
|
#include <bits/stdint-uintn.h>
|
|
#endif
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
#include "lib/core/FMT_IMPL.h"
|
|
#include "lib/domain_model/SecurityKey.h"
|
|
#include "lib/utils/ValueWrapper.h"
|
|
|
|
namespace bts {
|
|
struct CQuantity {
|
|
double qty = 0;
|
|
constexpr static double precision_digits = 6;
|
|
constexpr static double err = 0.0000001;
|
|
CQuantity() {}
|
|
CQuantity(uint32_t whole_qty) : qty(whole_qty) {}
|
|
CQuantity(uint32_t int_qty, double fractional_qty)
|
|
: qty(int_qty + fractional_qty) {}
|
|
static CQuantity create_new(double v) {
|
|
CQuantity rv;
|
|
rv.qty = v;
|
|
return rv;
|
|
}
|
|
|
|
static double round_to_decimal_places(double value, int decimal_places) {
|
|
double factor = pow(10, decimal_places);
|
|
return std::round(value * factor) / factor;
|
|
}
|
|
uint32_t value() const { return static_cast<uint32_t>(qty); }
|
|
double full_value() const { return qty; }
|
|
float full_value_float() const { return static_cast<float>(full_value()); }
|
|
uint32_t int_part() { return static_cast<uint32_t>(qty); }
|
|
double fractional_part() { return full_value() - int_part(); }
|
|
static bool equal_d(double d1, double d2) {
|
|
return std::abs(d1 - d2) <= err;
|
|
}
|
|
static bool greater_d(double d1, double d2) { return d1 - d2 > err; }
|
|
static bool greater_eq_d(double d1, double d2) {
|
|
return equal_d(d1, d2) || greater_d(d1, d2);
|
|
}
|
|
static bool less_d(double d1, double d2) { return greater_d(d2, d1); }
|
|
static bool less_eq_d(double d1, double d2) {
|
|
return less_d(d1, d2) || equal_d(d1, d2);
|
|
}
|
|
|
|
static CQuantity u(uint32_t u) { return CQuantity(u); }
|
|
// CQuantity &operator=(uint32_t whole_qty) noexcept {
|
|
// qty = whole_qty;
|
|
// return *this;
|
|
// }
|
|
CQuantity &operator=(double full_qty) noexcept {
|
|
qty = full_qty;
|
|
return *this;
|
|
}
|
|
bool operator==(const CQuantity &rhs) const { return equal_d(qty, rhs.qty); }
|
|
bool operator==(uint32_t rhs) const {
|
|
return this->operator==(CQuantity{rhs});
|
|
}
|
|
bool operator!=(const CQuantity &rhs) const {
|
|
return !this->operator==(rhs);
|
|
}
|
|
bool operator!=(uint32_t &rhs) const { return this->operator!=(u(rhs)); }
|
|
bool operator>(const CQuantity &rhs) const {
|
|
return greater_d(qty, rhs.qty);
|
|
}
|
|
bool operator>(uint32_t rhs) const { return this->operator>(u(rhs)); }
|
|
bool operator>=(const CQuantity &rhs) const {
|
|
return greater_eq_d(qty, rhs.qty);
|
|
}
|
|
bool operator>=(uint32_t rhs) const { return this->operator>=(u(rhs)); }
|
|
bool operator<(const CQuantity &rhs) const { return less_d(qty, rhs.qty); }
|
|
bool operator<(uint32_t rhs) const { return this->operator<(u(rhs)); }
|
|
bool operator<=(const CQuantity &rhs) const {
|
|
return less_eq_d(qty, rhs.qty);
|
|
}
|
|
bool operator<=(uint32_t rhs) const { return this->operator<=(u(rhs)); }
|
|
CQuantity &operator+=(std::uint32_t inc) {
|
|
qty += inc;
|
|
return *this;
|
|
}
|
|
CQuantity &operator+=(const CQuantity inc) {
|
|
qty += inc.qty;
|
|
return *this;
|
|
}
|
|
CQuantity &operator-=(std::uint32_t inc) {
|
|
qty -= inc;
|
|
return *this;
|
|
}
|
|
CQuantity &operator-=(const CQuantity inc) {
|
|
qty -= inc.qty;
|
|
return *this;
|
|
}
|
|
CQuantity operator-(const CQuantity &rhs) const {
|
|
return create_new(qty - rhs.qty);
|
|
}
|
|
CQuantity operator+(const CQuantity &rhs) const {
|
|
return create_new(qty + rhs.qty);
|
|
}
|
|
bool is_zero() const { return *this == create_new(0.0); }
|
|
void make_zero_if_negative() { qty = std::max(0.0, qty); }
|
|
void round() { qty = round_to_decimal_places(qty, 6); }
|
|
};
|
|
|
|
inline std::ostream &operator<<(std::ostream &out, CQuantity const &qty) {
|
|
out << qty.full_value();
|
|
return out;
|
|
}
|
|
using Price = utils::ValueWrapper<struct OrderPriceTag, double>;
|
|
// using Quantity = utils::ValueWrapper<struct QuantityTag, uint32_t>;
|
|
using Quantity = CQuantity;
|
|
using UserLogin = utils::ValueWrapper<struct UserLoginTag, std::string>;
|
|
} // namespace bts
|
|
|
|
FMT_IMPL(bts::Price, p, "{}", p.value());
|
|
FMT_IMPL(bts::Quantity, q, "{}", q.full_value());
|