mt4hstcvt
/
main.cpp
127 строк · 2.5 Кб
1#include <hstfmt.h>
2
3#include <chrono>
4#include <cstdlib>
5#include <fstream>
6#include <iostream>
7#include <deque>
8
9
10static std::ostream& operator<<(std::ostream& os, const hst::bar& bar)
11{
12const char del[] = ",";
13
14time_t ctm = static_cast<time_t>(bar.ctm);
15tm bartm{};
16gmtime_s(&bartm, &ctm);
17
18char buff[32];
19sprintf_s(buff, sizeof(buff), "%04d.%02d.%02d,%02d:%02d",
201900 + bartm.tm_year, 1 + bartm.tm_mon, bartm.tm_mday,
21bartm.tm_hour, bartm.tm_min);
22
23os << buff << del << bar.open << del << bar.high << del
24<< bar.low << del << bar.close << del << bar.volume;
25return os;
26}
27
28static int csv(std::ostream& os, hst::dataset& data)
29{
30os << "DATE,TIME,OPEN,HIGH,LOW,CLOSE,VOLUME" << std::endl;
31
32if (!os)
33{
34std::cerr << "failed to write out file" << std::endl;
35return 2;
36}
37
38auto old_prec = os.precision();
39os.precision(5);
40os << std::fixed;
41
42data.each([&os](const auto& bar)
43{
44os << bar << std::endl;
45});
46
47os.precision(old_prec);
48os << std::defaultfloat;
49
50if (!os)
51{
52std::cerr << "failed to write out file" << std::endl;
53return 2;
54}
55
56return 0;
57}
58
59static void help()
60{
61std::cerr << "mt4hstcvt, version 0.1, copyright (c) deep-fx 2024, MIT License" << std::endl
62<< "Usage:" << std::endl
63<< " mt4hstcvt [-o csv-file] hst-file" << std::endl
64;
65}
66
67int main(int argc, char* argv[])
68{
69if (argc < 2)
70{
71help();
72return 1;
73}
74
75std::deque<std::string> args{ argv + 1, argv + argc };
76
77std::string out_path{};
78auto& tmp = args.front();
79
80if (tmp == "-o")
81{
82args.pop_front();
83if (args.empty())
84{
85help();
86return 1;
87}
88out_path = args.front();
89args.pop_front();
90}
91
92if (args.empty())
93{
94help();
95return 1;
96}
97
98std::string in_path = args.front();
99
100try
101{
102hst::dataset d{ in_path };
103
104if (out_path.empty())
105{
106return csv(std::cout, d);
107}
108else
109{
110std::ofstream os{ out_path };
111if (!os)
112{
113std::cerr << "cannot open file: " << out_path << std::endl;
114return 2;
115}
116
117return csv(os, d);
118}
119}
120catch (const hst::dataset_error& ex)
121{
122std::cerr << ex.what() << std::endl;
123return 3;
124}
125
126return 0;
127}
128