tzutrader
A composable C++ backtesting library for trading strategies (experimental)
Loading...
Searching...
No Matches
streamers.h
Go to the documentation of this file.
1
14
15#ifndef STREAMERS_H
16#define STREAMERS_H
17
18#include <istream>
19#include <iostream>
20#include <array>
21#include <cstring>
22#include "defs.h"
23
24namespace tzu {
25
26// trade-off: we work with a fixed-size buffer for line parsing
27// to avoid dynamic memory allocation. This should be sufficient for
28// typical CSV lines in financial data.
29constexpr size_t MAX_BUFFER_SIZE = 2048;
30
36template<typename T>
38
43template<>
45 static bool parse(const char* line_buffer, Ohlcv& out) {
46 char* end;
47 int64_t ts = std::strtol(line_buffer, &end, 10);
48 double o = std::strtod(end, &end);
49 double h = std::strtod(end, &end);
50 double l = std::strtod(end, &end);
51 double c = std::strtod(end, &end);
52 double v = std::strtod(end, &end);
53 if (*end != '\0' && *end != '\n') return false;
54 out = Ohlcv(ts, o, h, l, c, v);
55 return true;
56 }
57};
58
63template<>
65 static bool parse(const char* line_buffer, Tick& out) {
66 char* end;
67 int64_t ts = std::strtol(line_buffer, &end, 10);
68 double price = std::strtod(end, &end);
69 double volume = std::strtod(end, &end);
70 Side side = static_cast<Side>(std::strtol(end, &end, 10));
71 if (*end != '\0' && *end != '\n') return false;
72 out = Tick{ts, price, volume, static_cast<Side>(side)};
73 return true;
74 }
75};
76
81template<>
83 static bool parse(const char* line_buffer, SingleValue& out) {
84 char* end;
85 int64_t ts = std::strtol(line_buffer, &end, 10);
86 double value = std::strtod(end, &end);
87 if (*end != '\0' && *end != '\n') return false;
88 out = SingleValue{ts, value};
89 return true;
90 }
91};
92
97template<typename T, typename Parser>
99 std::istream* input_;
100 T current_;
101 bool end_ = false;
102public:
107 ParseIterator(std::istream* input, bool end = false)
108 : input_(input), end_(end) {
109 if (!end_) { ++(*this); }
110 }
111
118 std::array<char, MAX_BUFFER_SIZE> line_buffer;
119 while (!end_ &&
120 input_->getline(
121 line_buffer.data(),
122 static_cast<std::streamsize>(line_buffer.size()))) {
123 char* buf = line_buffer.data();
124 size_t len = std::strlen(buf);
125 while (len > 0 && (buf[len - 1] == '\r' || buf[len - 1] == '\n')) {
126 buf[--len] = '\0';
127 }
128 // Replace commas with spaces so numeric parsers using
129 // strtod/strtol work as expected
130 for (size_t i = 0; i < len; ++i) {
131 if (buf[i] == ',') buf[i] = ' ';
132 }
133 if (Parser::parse(buf, current_)) {
134 return *this;
135 }
136 }
137 end_ = true;
138 return *this;
139 }
140
145 const T& operator*() const { return this->current_; }
146
151 const T* operator->() const { return &(this->current_); }
152
158 bool operator==(const ParseIterator& other) const {
159 return end_ == other.end_;
160 }
161
165 bool operator!=(const ParseIterator& other) const {
166 return !(*this == other);
167 }
168};
169
175template<typename T>
176class Csv {
177 std::istream& input_;
178 bool has_headers_;
179public:
180 explicit Csv(std::istream& input, bool has_headers = true)
181 : input_(input), has_headers_(has_headers) {
182 if (has_headers_) {
183 std::string header_line;
184 std::getline(input_, header_line);
185 }
186 }
188 Iterator begin() { return Iterator(&input_); }
189 Iterator end() { return Iterator(&input_, true); }
190};
191
192} // namespace tzu
193
194#endif // STREAMERS_H
Csv(std::istream &input, bool has_headers=true)
Definition streamers.h:180
Iterator begin()
Definition streamers.h:188
ParseIterator< T, CsvParseTraits< T > > Iterator
Definition streamers.h:187
Iterator end()
Definition streamers.h:189
Definition streamers.h:98
const T & operator*() const
Definition streamers.h:145
ParseIterator(std::istream *input, bool end=false)
Definition streamers.h:107
bool operator==(const ParseIterator &other) const
Definition streamers.h:158
ParseIterator & operator++()
Definition streamers.h:117
bool operator!=(const ParseIterator &other) const
Definition streamers.h:165
const T * operator->() const
Definition streamers.h:151
Core data structures and types for the tzutrader library.
Definition defs.h:20
constexpr size_t MAX_BUFFER_SIZE
Definition streamers.h:29
Side
Trade direction enumeration.
Definition defs.h:28
static bool parse(const char *line_buffer, Ohlcv &out)
Definition streamers.h:45
static bool parse(const char *line_buffer, SingleValue &out)
Definition streamers.h:83
static bool parse(const char *line_buffer, Tick &out)
Definition streamers.h:65
Definition streamers.h:37
Open-High-Low-Close-Volume candlestick data.
Definition defs.h:84
Definition defs.h:134
Definition defs.h:119