Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cmath>
5#include <limits>
6#include <string>
7#include <vector>
10
11namespace slick::trader {
12
13// Fixed-point representation for prices and quantities
14// price_t: int64_t with 8 decimal places (1e-8 precision)
15// qty_t: uint64_t with 8 decimal places (1e-8 precision)
16using price_t = int64_t;
17using qty_t = int64_t;
18
19constexpr price_t NULL_PRICE = std::numeric_limits<price_t>::max();
20constexpr int64_t PRICE_SCALE = 100'000'000; // 1e8
21constexpr int64_t QTY_SCALE = 100'000'000; // 1e8
22
23// Conversion functions
24inline price_t to_price_t(double value) {
25 if (std::isnan(value)) return NULL_PRICE;
26 constexpr double MAX_SAFE = static_cast<double>(std::numeric_limits<price_t>::max()) / PRICE_SCALE;
27 constexpr double MIN_SAFE = static_cast<double>(std::numeric_limits<price_t>::min()) / PRICE_SCALE;
28 if (value > MAX_SAFE || value < MIN_SAFE) return NULL_PRICE;
29 return static_cast<price_t>(std::round(value * PRICE_SCALE));
30}
31
32inline qty_t to_qty_t(double value) {
33 return static_cast<qty_t>(std::round(value * QTY_SCALE));
34}
35
36inline double from_price_t(price_t value) {
37 return value == NULL_PRICE ? std::numeric_limits<double>::quiet_NaN() : static_cast<double>(value) / PRICE_SCALE;
38}
39
40inline double from_qty_t(qty_t value) {
41 return static_cast<double>(value) / QTY_SCALE;
42}
43
44enum class SubscriptionType : uint8_t {
49 MBO, // market by order: every order of the book, for brokers whose feed carries it
50};
51
52// LEVEL2 and ORDER_BOOK both stream the level 2 updates books are built from. Coinbase serves the
53// two on one channel.
54constexpr bool streams_book(SubscriptionType type) noexcept {
56}
57
58// Subscription request from a strategy
60 BrokerId broker_id; // Which broker to subscribe to
61 SubscriptionType type; // Trade, Quote, Level2, OrderBook or MBO
62 std::vector<std::string> instruments; // List of instruments
63 int depth = 10; // Order book depth (for orderbook subscriptions)
64
66 const std::vector<std::string>& instrs, int d = 10)
67 : broker_id(broker), type(sub_type), instruments(instrs), depth(d) {}
68};
69
70} // namespace slick::trader
double from_price_t(price_t value)
Definition types.hpp:36
price_t to_price_t(double value)
Definition types.hpp:24
constexpr int64_t PRICE_SCALE
Definition types.hpp:20
constexpr bool streams_book(SubscriptionType type) noexcept
Definition types.hpp:54
constexpr price_t NULL_PRICE
Definition types.hpp:19
int64_t qty_t
Definition types.hpp:17
int64_t price_t
Definition types.hpp:16
double from_qty_t(qty_t value)
Definition types.hpp:40
constexpr int64_t QTY_SCALE
Definition types.hpp:21
qty_t to_qty_t(double value)
Definition types.hpp:32
SubscriptionRequest(BrokerId broker, SubscriptionType sub_type, const std::vector< std::string > &instrs, int d=10)
Definition types.hpp:65
std::vector< std::string > instruments
Definition types.hpp:62