-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
233 lines (172 loc) · 6.75 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0
#include <dxfeed_graal_cpp_api/api.hpp>
#include <chrono>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <unordered_map>
using namespace dxfcpp;
using namespace std::string_literals;
void printUsage() {
std::cout << R"(usage: ScheduleSample <defaults> <profiles> <symbol> [time]
where: <defaults> is a path to Schedule API defaults file
<profiles> is a URL to IPF file
<symbol> is a ticker symbol used for sample
[time] is a time used for sample in a format yyyy-MM-dd-HH:mm:ss
sample: ScheduleSample schedule.properties sample.ipf.zip IBM 2011-05-26-14:15:00)"
<< std::endl;
}
void updateScheduleDefaults(const std::string &url) {
std::ifstream is(url);
if (!is.is_open()) {
std::cerr << "Failed to open " + url << std::endl;
return;
}
std::vector<char> buffer(std::istreambuf_iterator<char>{is}, {});
Schedule::setDefaults(buffer);
}
std::unordered_map<std::string, std::shared_ptr<InstrumentProfile>> loadInstrumentProfiles(const std::string &url) {
std::unordered_map<std::string, std::shared_ptr<InstrumentProfile>> profiles{};
for (auto &&profile : InstrumentProfileReader::create()->readFromFile(url)) {
profiles[profile->getSymbol()] = profile;
}
std::cout << "Loaded " << profiles.size() << " instrument profiles" << std::endl;
return profiles;
}
void checkAllSchedules(auto &&profiles) {
auto successes = 0;
for (auto &&[symbol, profile] : profiles) {
bool error = false;
if (!Schedule::getInstance(profile)) {
error = true;
} else {
for (auto &&venue : Schedule::getTradingVenues(profile)) {
if (!Schedule::getInstance(profile, venue)) {
error = true;
break;
}
}
}
if (error) {
std::cerr << "Error getting schedule for " + profile->getSymbol() + " (" + profile->getTradingHours() +
"): "
<< std::endl;
} else {
successes++;
}
}
std::cout << "Checked " << profiles.size() << " instrument profiles: " << successes << " successes, "
<< (profiles.size() - successes) << " failures" << std::endl;
}
void printNext5Holidays(auto &&profile, auto time) {
auto schedule = Schedule::getInstance(profile);
auto day = schedule->getDayByTime(time);
std::string output = "5 next holidays for " + profile->getSymbol() + ":";
if (!day) {
output += "Could not get day by time";
} else {
for (auto i = 0; i < 5; i++) {
day = day->findNextDay(DayFilter::HOLIDAY);
if (!day) {
break;
}
output += " " + std::to_string(day->getYearMonthDay());
}
}
std::cout << output << std::endl;
}
void printCurrentSession(auto &&profile, auto time) {
auto schedule = Schedule::getInstance(profile);
if (!schedule) {
std::cerr << "Error getting schedule for " + profile->getSymbol() + " (" + profile->getTradingHours() + "): "
<< std::endl;
return;
}
auto session = schedule->getSessionByTime(time);
if (!session) {
std::cerr << "Error getting session for time: " + TimeFormat::DEFAULT_WITH_MILLIS_WITH_TIMEZONE.format(time)
<< std::endl;
return;
}
std::cout << "Current session for " + profile->getSymbol() + ": " + session->toString() + " in " +
session->getDay()->toString()
<< std::endl;
}
void printNextTradingSession(auto &&profile, auto time) {
auto schedule = Schedule::getInstance(profile);
if (!schedule) {
std::cerr << "Error getting schedule for " + profile->getSymbol() + " (" + profile->getTradingHours() + "): "
<< std::endl;
return;
}
auto session = schedule->getSessionByTime(time);
if (!session) {
std::cerr << "Error getting session for time: " + TimeFormat::DEFAULT_WITH_MILLIS_WITH_TIMEZONE.format(time)
<< std::endl;
return;
}
if (!session->isTrading()) {
session = session->getNextSession(SessionFilter::TRADING);
}
if (!session) {
std::cerr << "There is no next trading session for " << profile->getSymbol() << std::endl;
} else {
std::cout << "Next trading session for " + profile->getSymbol() + ": " + session->toString() + " in " +
session->getDay()->toString()
<< std::endl;
}
}
void printNearestTradingSession(auto &&profile, auto time) {
auto schedule = Schedule::getInstance(profile);
if (!schedule) {
std::cerr << "Error getting schedule for " + profile->getSymbol() + " (" + profile->getTradingHours() + "): "
<< std::endl;
return;
}
auto session = schedule->getNearestSessionByTime(time, SessionFilter::TRADING);
if (!session) {
std::cerr << "There is no nearest trading session for " << profile->getSymbol() << std::endl;
} else {
std::cout << "Nearest trading session for " + profile->getSymbol() + ": " + session->toString() + " in " +
session->getDay()->toString()
<< std::endl;
}
}
/**
* A sample program that demonstrates different use cases of Schedule API.
*/
int main(int argc, char *argv[]) {
try {
if (argc > 1 && (argv[1] == "-h"s || argv[1] == "--help"s)) {
printUsage();
return 0;
}
if (argc < 4 || argc > 5) {
std::cerr << "Wrong number of arguments" << std::endl;
printUsage();
return 1;
}
updateScheduleDefaults(argv[1]);
auto profiles = loadInstrumentProfiles(argv[2]);
checkAllSchedules(profiles);
std::string symbol = argv[3];
if (!profiles.contains(symbol)) {
std::cerr << "Could not find profile for " + symbol << std::endl;
return 2;
}
auto profile = profiles[symbol];
std::cout << "Found profile for " + symbol + ": " + profile->getDescription() << std::endl;
auto time = argc < 5 ? dxfcpp::now() : TimeFormat::DEFAULT_WITH_MILLIS_WITH_TIMEZONE.parse(argv[4]);
std::cout << "Using timestamp " + TimeFormat::DEFAULT_WITH_MILLIS_WITH_TIMEZONE.format(time) << std::endl;
printNext5Holidays(profile, time);
printCurrentSession(profile, time);
printNextTradingSession(profile, time);
printNearestTradingSession(profile, time);
} catch (const RuntimeException &e) {
std::cerr << e << '\n';
return 1;
}
return 0;
}