Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
price_scale.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <cstdint>
5#include <limits>
6#include <numeric>
7#include <optional>
8
9namespace slick::trader {
10
11// Exact conversion between an exchange's integer wire prices and price_t (1e-8).
12//
13// price_t = wire x num / den
14//
15// The ratio is kept reduced, so a conversion is one integer division and one exact
16// multiplication. A price the target scale cannot hold exactly is refused rather than
17// rounded: it can only be off-tick, and trading it would trade a price nobody quoted.
18class PriceScale {
19public:
20 // Empty unless both terms are positive.
21 static std::optional<PriceScale> from_ratio(int64_t num, int64_t den) noexcept {
22 if (num <= 0 || den <= 0) {
23 return std::nullopt;
24 }
25 const int64_t g = std::gcd(num, den);
26 return PriceScale(num / g, den / g);
27 }
28
29 // The identity: wire prices already are price_t.
30 static PriceScale identity() noexcept { return PriceScale(1, 1); }
31
32 // Wire -> price_t. False for a null (INT64_MAX or INT64_MIN, which is every wire
33 // format's null), an off-scale price, or one too large to hold.
34 [[nodiscard]] bool to_price(int64_t wire, price_t& out) const noexcept {
35 if (wire == WIRE_NULL_MAX || wire == WIRE_NULL_MIN || wire % den_ != 0) {
36 return false;
37 }
38 const int64_t q = wire / den_;
39 if (q > MAX_VALUE / num_ || q < -MAX_VALUE / num_) {
40 return false;
41 }
42 out = q * num_;
43 return true;
44 }
45
46 // price_t -> wire. False for NULL_PRICE, or a price finer than the wire can express.
47 [[nodiscard]] bool to_wire(price_t price, int64_t& out) const noexcept {
48 if (price == NULL_PRICE || price % num_ != 0) {
49 return false;
50 }
51 const int64_t q = price / num_;
52 if (q > MAX_VALUE / den_ || q < -MAX_VALUE / den_) {
53 return false;
54 }
55 out = q * den_;
56 return true;
57 }
58
59 [[nodiscard]] int64_t numerator() const noexcept { return num_; }
60 [[nodiscard]] int64_t denominator() const noexcept { return den_; }
61
62private:
63 PriceScale(int64_t num, int64_t den) noexcept : num_(num), den_(den) {}
64
65 static constexpr int64_t WIRE_NULL_MAX = std::numeric_limits<int64_t>::max();
66 static constexpr int64_t WIRE_NULL_MIN = std::numeric_limits<int64_t>::min();
67 // Keeps every result clear of both nulls, NULL_PRICE included.
68 static constexpr int64_t MAX_VALUE = std::numeric_limits<int64_t>::max() - 1;
69
70 int64_t num_;
71 int64_t den_;
72};
73
74} // namespace slick::trader
int64_t numerator() const noexcept
int64_t denominator() const noexcept
bool to_price(int64_t wire, price_t &out) const noexcept
static std::optional< PriceScale > from_ratio(int64_t num, int64_t den) noexcept
static PriceScale identity() noexcept
bool to_wire(price_t price, int64_t &out) const noexcept
constexpr price_t NULL_PRICE
Definition types.hpp:19
int64_t price_t
Definition types.hpp:16