Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
environment.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <nlohmann/json.hpp>
4#include <algorithm>
5#include <cctype>
6#include <cstdint>
7#include <string>
8#include <string_view>
9
10namespace slick::trader {
11
12// The venue environment the config's top-level "environment" names. Venues don't say which one
13// a session is on, so the config does, and the dashboard flags anything that isn't production.
14inline constexpr std::string_view PRODUCTION_ENVIRONMENT = "Production";
15
16// What a Sim environment is while its slick-sim has a live feed or, with none, a pcap feed. Found out
17// over the control link, never configured.
18inline constexpr std::string_view LIVE_SIM_ENVIRONMENT = "Live Sim";
19inline constexpr std::string_view HISTORICAL_SIM_ENVIRONMENT = "Historical Sim";
20
21// `env` is `word`, given in lower case, in any case.
22inline bool environment_is(std::string_view env, std::string_view word) noexcept {
23 return env.size() == word.size() &&
24 std::equal(env.begin(), env.end(), word.begin(), [](char a, char b) {
25 return std::tolower(static_cast<unsigned char>(a)) == b;
26 });
27}
28
29// Empty, "production" or "prod", in any case. Anything else, a misspelling included, is not:
30// the dashboard then shows its bar rather than hiding a sim or cert session.
31inline bool is_production_environment(std::string_view env) noexcept {
32 return env.empty() || environment_is(env, "production") || environment_is(env, "prod");
33}
34
35// "Sim", in any case: the venues are slick-sim's, and the engine keeps a control link to it.
36inline bool is_sim_environment(std::string_view env) noexcept {
37 return environment_is(env, "sim");
38}
39
40// The config's "environment" into `env`, PRODUCTION_ENVIRONMENT when it has none. False when it is
41// there but not a string: a mistake, and reading it as production would hide the bar.
42inline bool read_environment(const nlohmann::json& config, std::string& env) {
43 const auto it = config.find("environment");
44 if (it == config.end()) {
46 return true;
47 }
48 if (!it->is_string()) {
49 return false;
50 }
51 env = it->get<std::string>();
52 return true;
53}
54
55// Where slick-sim's CLI port is. Port 0 is no control link.
57 static constexpr const char* ADDR_KEY = "sim_control_addr";
58 static constexpr const char* PORT_KEY = "sim_control_port";
59
60 std::string addr;
61 uint16_t port{0};
62
63 bool enabled() const noexcept { return port != 0; }
64};
65
66// The config's "sim_control_addr" and "sim_control_port", both required in Sim, into `sim`. False,
67// with `error` naming the key, when one is missing or wrong.
68inline bool read_sim_control(const nlohmann::json& config, SimControlConfig& sim, std::string& error) {
69 const auto addr = config.find(SimControlConfig::ADDR_KEY);
70 if (addr == config.end() || !addr->is_string() || addr->get_ref<const std::string&>().empty()) {
71 error = std::string("'") + SimControlConfig::ADDR_KEY + "' must be slick-sim's address, such as \"127.0.0.1\"";
72 return false;
73 }
74 const auto port = config.find(SimControlConfig::PORT_KEY);
75 const int64_t number = port != config.end() && port->is_number_integer() ? port->get<int64_t>() : 0;
76 if (number < 1 || number > UINT16_MAX) {
77 error = std::string("'") + SimControlConfig::PORT_KEY + "' must be slick-sim's cli.port, 1 to 65535";
78 return false;
79 }
80 sim.addr = addr->get<std::string>();
81 sim.port = static_cast<uint16_t>(number);
82 return true;
83}
84
85} // namespace slick::trader
bool environment_is(std::string_view env, std::string_view word) noexcept
constexpr std::string_view PRODUCTION_ENVIRONMENT
bool is_sim_environment(std::string_view env) noexcept
constexpr std::string_view LIVE_SIM_ENVIRONMENT
bool is_production_environment(std::string_view env) noexcept
bool read_sim_control(const nlohmann::json &config, SimControlConfig &sim, std::string &error)
constexpr std::string_view HISTORICAL_SIM_ENVIRONMENT
bool read_environment(const nlohmann::json &config, std::string &env)
static constexpr const char * PORT_KEY
bool enabled() const noexcept
static constexpr const char * ADDR_KEY