-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTickParser.cpp
More file actions
114 lines (96 loc) · 2.66 KB
/
TickParser.cpp
File metadata and controls
114 lines (96 loc) · 2.66 KB
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
module;
#include <string>
#include <sstream>
#include <vector>
#include <filesystem>
#include <format>
#include <chrono>
export module TickParser;
import AlgoTrading;
import Utils;
using namespace std;
using namespace utils;
/**
* @brief Represents a parser for ticks in the specified custom format.
*/
export class TickParser {
public:
TickParser() {
_intermediate_tick.timestamp = std::chrono::system_clock::now();
_intermediate_tick.bid = 0.0;
_intermediate_tick.ask = 0.0;
_intermediate_tick.volume = 0.0;
_intermediate_tick.flags = ChangeFlag::ASK_AND_BID;
}
/**
* @brief Parses the ticks from the specified file.
* @param path The path to the file with ticks.
* @return The parsed ticks.
*/
Ticks getTicks(const std::string& path) {
Ticks ticks;
try {
CSVParser parser(path, 6, '\t', true);
RowStream row_stream = parser.getRowStream();
Row row;
while (row_stream) {
row_stream >> row;
ticks.emplace_back(createTick(row));
}
}
catch (const std::runtime_error&) {
return ticks;
}
return ticks;
}
private:
Tick _intermediate_tick;
ChangeFlag getFlags(Cell cell) const {
int flag_value;
if (!cell.toNumber(flag_value))
{
throw format_error("Flag is not parsable - either empty or malformatted.");
}
switch (flag_value)
{
case ChangeFlag::ASK_AND_BID:
return ChangeFlag::ASK_AND_BID;
case ChangeFlag::ASK:
return ChangeFlag::ASK;
case ChangeFlag::BID:
return ChangeFlag::BID;
case ChangeFlag::VOLUME:
return ChangeFlag::VOLUME;
default:
throw format_error("Unexpected flag value");
}
}
Tick createTick(Row& row) {
CellStream cell_stream = row.getCellStream();
// format of the csv file: <DATE> <TIME> <BID> <ASK> <LAST> <VOLUME> <FLAGS>
{
std::string timestamp = "";
timestamp += cell_stream.next().toView();
timestamp += " ";
timestamp += cell_stream.next().toView();
_intermediate_tick.timestamp = convertToTime(timestamp);
}
_intermediate_tick.bid = cell_stream.next().toNumberOrDefault(_intermediate_tick.bid);
_intermediate_tick.ask = cell_stream.next().toNumberOrDefault(_intermediate_tick.ask);
cell_stream.next(); // skip column <LAST>
Cell cell = cell_stream.next();
_intermediate_tick.volume = cell.toNumberOrDefault(_intermediate_tick.volume);
_intermediate_tick.flags = getFlags(cell_stream.next());
return _intermediate_tick;
}
TimePoint convertToTime(std::string& timestamp) {
static string format = "%Y.%m.%d %H:%M:%S";
std::istringstream iss(timestamp);
TimePoint tp;
iss >> std::chrono::parse(format.c_str(), tp);
if (iss.fail()) {
throw format_error("Enexpected format of timestamps - conversion failed.");
}
return tp;
}
};