algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-rate-limit-stats.cpp
1// Shows how a request budget is spent, and what the trading reserve protects.
2//
3// Entirely offline: it drives the pacer with its own clock and issues no broker
4// request, so it needs no credentials and touches no network. The point is to
5// make a budget's behaviour inspectable before a strategy depends on it.
6//
7// example-rate-limit-stats [burst] [per-second] [trading-reserve]
8
9#include <algotradelib/src/network/rate_limit.hpp>
10
11#include <chrono>
12#include <cstdlib>
13#include <exception>
14#include <iomanip>
15#include <iostream>
16#include <string>
17
18namespace {
19
24
26struct WalkClock {
27 using duration = std::chrono::nanoseconds;
28 using rep = duration::rep;
29 using period = duration::period;
30 using time_point = std::chrono::time_point<WalkClock, duration>;
31
33 static time_point now() {
34 return time_point{};
35 }
36};
37
39
41std::chrono::nanoseconds ms(long long count) {
42 return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::milliseconds(count));
43}
44
46long long as_ms(std::chrono::nanoseconds value) {
47 return std::chrono::duration_cast<std::chrono::milliseconds>(value).count();
48}
49
51int argument(int argc, char** argv, int index, int fallback) {
52 if (argc <= index) {
53 return fallback;
54 }
55 const int value = std::atoi(argv[index]);
56 return value >= 0 ? value : fallback;
57}
58
60void print_allowances(const RateLimitBudget& budget) {
61 std::cout
62 << "budget: burst=" << budget.burst_ << " per_second=" << budget.per_second_
63 << " trading_reserve=" << budget.trading_reserve_ << '\n';
64 std::cout << " interval between admissions: " << as_ms(Bucket(budget).interval()) << "ms\n";
65 for (
66 const auto group : {
67 RateLimitGroup::AUTH,
68 RateLimitGroup::TRADING,
69 RateLimitGroup::ACCOUNT,
70 RateLimitGroup::MARKET_DATA,
71 RateLimitGroup::REFERENCE,
72 }
73 ) {
74 std::cout
75 << " " << std::left << std::setw(12) << rate_limit_group_name(group) << " burst allowance "
76 << usable_burst(budget, group) << '\n';
77 }
78}
79
81void print_reserve(const RateLimitBudget& budget) {
82 Bucket bucket(budget);
83 const auto now = WalkClock::now();
84 const int polling = usable_burst(budget, RateLimitGroup::MARKET_DATA);
85 const int trading = usable_burst(budget, RateLimitGroup::TRADING);
86
87 int spent = 0;
88 while (bucket.try_acquire(now, polling)) {
89 ++spent;
90 }
91
92 std::cout << "\na saturated polling loop spends " << spent << " request(s) immediately\n";
93 std::cout << " its next request waits " << as_ms(bucket.delay_until(now, polling)) << "ms\n";
94 std::cout << " a cancellation waits " << as_ms(bucket.delay_until(now, trading)) << "ms";
95 std::cout << " with " << bucket.available(now, trading) << " reserved slot(s) left\n";
96}
97
99void print_corrections(const RateLimitBudget& budget) {
100 std::cout << "\nreactions to a broker refusal\n";
101
103 shared.hold(std::chrono::seconds(1));
104 std::cout << " shared cap reached: allowance unchanged at " << shared.budget().per_second_ << "/s, admission held\n";
105
107 reported.shrink(2, 2, std::chrono::seconds(1));
108 std::cout << " broker reported its allowance: adopted " << reported.budget().per_second_ << "/s\n";
109
110 std::cout << " broker reported no allowance: allowance unchanged, admission held\n";
111 std::cout
112 << " (KIS states none, so its budget stays as configured and its"
113 << " refusals are counted)\n";
114}
115
117void print_sustained(const RateLimitBudget& budget) {
118 Bucket bucket(budget);
119 const auto start = WalkClock::now();
120 const int polling = usable_burst(budget, RateLimitGroup::MARKET_DATA);
121
122 int admitted = 0;
123 for (long long step = 0; step < 1000; step += 5) {
124 if (bucket.try_acquire(start + ms(step), polling)) {
125 ++admitted;
126 }
127 }
128 std::cout << "\none second of polling as fast as allowed: " << admitted << " request(s)\n";
129}
130
131} // namespace
132
133int main(int argc, char** argv) {
134 try {
135 RateLimitBudget budget;
136 budget.burst_ = argument(argc, argv, 1, budget.burst_);
137 budget.per_second_ = argument(argc, argv, 2, budget.per_second_);
138 budget.trading_reserve_ = argument(argc, argv, 3, budget.trading_reserve_);
139
140 print_allowances(budget);
141 print_reserve(budget);
142 print_sustained(budget);
143 print_corrections(budget);
144 return 0;
145 } catch (const std::exception& exception) {
146 std::cerr << "example failed: " << exception.what() << '\n';
147 return 1;
148 }
149}
A rate limiter (the generic cell rate algorithm) that computes when a request may go; it neither wait...
The rate limiter of one connection (one app key): a RateLimitBucket on the steady clock,...
RateLimitGroup
The traffic class of a request, for rate limiting and queue priority. The broker adapter that builds ...
constexpr int usable_burst(const RateLimitBudget &budget, RateLimitGroup group) noexcept
Returns how much of the burst a traffic class may use.
constexpr std::string_view rate_limit_group_name(RateLimitGroup group) noexcept
Returns a lowercase name for a traffic class, such as "market_data", for logs.
void start(S &strategy, Ctx &context)
Calls strategy.on_start(context) if the strategy declares on_start; otherwise does nothing.
Definition concept.hpp:240
The request rate a connection may use.