Crypto backtesting
Loading...
Searching...
No Matches
utils.h File Reference
#include "fx/constants.h"
#include <string>
#include <string_view>
#include <vector>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::string epoch_to_utc (const size_t)
 Routine to convert epoch seconds to a UTC time string.
std::string file_read (const std::string_view)
 Open a file a return a string of the contents.
std::vector< std::vector< double > > to_series (const std::string)
 Open a CSV file and return a vector of floating points for each row.
std::vector< std::vector< double > > to_series3 (const std::string &)
 Open a CSV file and return a vector of floating points for each row.
std::vector< std::vector< double > > to_series4 (const std::string &)
 Open a CSV file and return a vector of floating points for each row.

Function Documentation

◆ epoch_to_utc()

std::string epoch_to_utc ( const size_t epoch)

Routine to convert epoch seconds to a UTC time string.

Definition at line 7 of file time.cxx.

7 {
8 std::time_t t = epoch;
9 std::tm tm = *std::gmtime(&t);
10 std::ostringstream oss;
11 oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
12 return oss.str();
13}
Here is the caller graph for this function:

◆ file_read()

std::string file_read ( const std::string_view file_name)

Open a file a return a string of the contents.

Open file and return the contents as a string

Definition at line 12 of file file.cxx.

12 {
13
14 /// Open file and return the contents as a string
15 std::ifstream in{std::string{file_name}};
16
17 if (not in.good())
18 return {};
19
20 // Run to the end
21 in.seekg(0, std::ios::end);
22
23 // Reserve enough storage for the file contents
24 std::string out(in.tellg(), '\0');
25
26 // Back to the start
27 in.seekg(0, std::ios::beg);
28
29 // Copy the file contents
30 in.read(out.data(), out.size());
31
32 return out;
33}
Here is the caller graph for this function:

◆ to_series()

std::vector< std::vector< double > > to_series ( const std::string csv)

Open a CSV file and return a vector of floating points for each row.

Definition at line 78 of file file.cxx.

78 {
79
80 // Read line by line
81 std::istringstream in{csv};
82 std::ranges::istream_view<std::string> rows(in);
83
84 // Read prices, drop the heading, and split each row
85 return rows | std::views::drop(1) | std::views::transform(split_on_comma)
86 | std::ranges::to<std::vector<std::vector<double>>>();
87};
const auto split_on_comma
Take a row of data, split on delimiter and return a row of floating points.
Definition file.cxx:36
Here is the caller graph for this function:

◆ to_series3()

std::vector< std::vector< double > > to_series3 ( const std::string & csv)

Open a CSV file and return a vector of floating points for each row.

Definition at line 90 of file file.cxx.

90 {
91
92 // Read line by line
93 std::istringstream in{csv};
94 std::ranges::istream_view<std::string> rows(in);
95
96 // Read prices, drop the heading, and split each row
97 auto &&px =
98 rows | std::views::drop(1) | std::views::transform(split_on_comma2);
99
100 // Create a default row to populate
101 auto &&xs = std::vector<double>(fx::cells);
102
103 // Evaluate the view by copying into a vector
104 auto &&series = std::vector<std::vector<double>>{};
105
106 for (auto v : px) {
107
108 // Copy the individual prices
109 std::ranges::copy(v, std::begin(xs));
110
111 // And store the full complement
112 series.emplace_back(xs);
113 }
114
115 return std::move(series);
116};
const auto split_on_comma2
Take a row of data, split on delimiter and return a row of floating points.
Definition file.cxx:57
const std::vector< std::vector< double > > xs
An example of a full set of price data.
Definition main.cxx:13
constexpr auto cells
Number of cells in a row of price data.
Definition constants.h:23
Here is the caller graph for this function:

◆ to_series4()

std::vector< std::vector< double > > to_series4 ( const std::string & csv)

Open a CSV file and return a vector of floating points for each row.

Definition at line 119 of file file.cxx.

119 {
120
121 // Read line by line
122 std::istringstream in{csv};
123 std::ranges::istream_view<std::string> rows(in);
124
125 // Read prices, drop the heading, and split each row
126 auto &&px =
127 rows | std::views::drop(1) | std::views::transform(split_on_comma2);
128
129 // Evaluate the view by copying into a vector
130 auto &&series = std::vector<std::vector<double>>{};
131 series.reserve(std::ranges::distance(px));
132
133 std::ranges::transform(px, std::back_inserter(series), [&](auto &&v) {
134 // Copy the individual prices
135 auto &&xs = std::vector<double>(fx::cells);
136 std::ranges::copy(v, std::begin(xs));
137 return xs;
138 });
139
140 return series;
141};
Here is the caller graph for this function: