-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcandlesticks.cpp
More file actions
249 lines (205 loc) · 6.55 KB
/
candlesticks.cpp
File metadata and controls
249 lines (205 loc) · 6.55 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <ctime>
#include "kapi.hpp"
#include "libjson/libjson.h"
using namespace std;
using namespace Kraken;
//------------------------------------------------------------------------------
// deals with Kraken trades:
struct Trade {
double price, volume;
time_t time;
char order;
Trade(JSONNode node) {
price = node[0].as_float();
volume = node[1].as_float();
time = node[2].as_int();
order = node[3].as_string()[0];
}
};
//------------------------------------------------------------------------------
// prints out a kraken trade:
ostream& operator<<(ostream& os, const Trade& t)
{
return os << '"'
<< t.time << "\",\""
<< t.order << "\",\""
<< fixed
<< setprecision(5) << t.price << "\",\""
<< setprecision(9) << t.volume << '"';
}
//------------------------------------------------------------------------------
// helper types to map time to a group of trades:
typedef vector<Trade> Period;
typedef map<time_t,Period> Period_map;
//------------------------------------------------------------------------------
// deal with candlesticks:
struct Candlestick {
double open, close, low, high;
double volume;
time_t time;
};
//------------------------------------------------------------------------------
struct HA_Candlestick : public Candlestick {
// create a candlestick from current period
HA_Candlestick(const Candlestick& curr)
{
time = curr.time;
volume = curr.volume;
close = (curr.open + curr.close + curr.low + curr.high) / 4;
open = (curr.open + curr.close) / 2;
low = min(curr.low, min(open, close));
high = max(curr.high, max(open, close));
}
// create a HA candlestick from current period
// and WITH a prior HA candlestick
HA_Candlestick(const Candlestick& curr, const HA_Candlestick& prior)
{
time = curr.time;
volume = curr.volume;
close = (curr.open + curr.close + curr.low + curr.high) / 4;
open = (prior.open + prior.close) / 2;
low = min(curr.low, min(open, close));
high = max(curr.high, max(open, close));
}
};
//------------------------------------------------------------------------------
// prints out a Candlestick
ostream& operator<<(ostream& os, const Candlestick& c)
{
struct tm timeinfo;
localtime_r(&c.time, &timeinfo);
return os << c.time << ','
<< fixed << setprecision(5)
<< c.open << ','
<< c.high << ','
<< c.low << ','
<< c.close << ','
<< setprecision(9)
<< c.volume;
}
//------------------------------------------------------------------------------
// downloads recent trades:
string recent_trades(const KAPI& k, const KAPI::Input& i, vector<Trade>& v)
{
string json_data = k.public_method("Trades", i);
JSONNode root = libjson::parse(json_data);
//cout << json_data << endl;
// throw an exception if there are errors in the JSON response
if (!root.at("error").empty()) {
std::ostringstream oss;
oss << "Kraken response contains errors: ";
// append errors to output string stream
for (auto it = root["error"].begin(); it != root["error"].end(); ++it)
oss << endl << " * " << libjson::to_std_string(it->as_string());
throw runtime_error(oss.str());
}
// throw an exception if result is empty
if (root.at("result").empty()) {
throw runtime_error("Kraken response doesn't contain result data");
}
const string& pair = i.at("pair");
JSONNode& result = root["result"];
JSONNode& result_pair = result.at(pair);
vector<Trade> output;
for (JSONNode::iterator it = result_pair.begin();
it != result_pair.end(); ++it)
output.push_back(Trade(*it));
output.swap(v);
return libjson::to_std_string( result.at("last").as_string() );
}
//------------------------------------------------------------------------------
// fills a candlestick vector grouping trades by time:
void group_by_time(const vector<Trade>& trades,
const time_t step,
vector<Candlestick>& candlesticks)
{
vector<Trade>::const_iterator it = trades.begin();
while (it != trades.end()) {
Candlestick period;
period.volume = 0;
period.open = it->price;
period.low = it->price;
period.high = it->price;
// the period time
period.time = it->time - (it->time % step);
while (it != trades.end() && it->time < (period.time+step)) {
// the lowest price
if (it->price < period.low)
period.low = it->price;
// the highest price
if (it->price > period.high)
period.high = it->price;
// sum volumes
period.volume += it->volume;
// last price is close time
period.close = it->price;
// next element
it++;
}
// store period
candlesticks.push_back(period);
// next group
}
}
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
try {
time_t step = 15*60; // by default 15 minutes
time_t last = 24*60*60; // by default last 24 hours
//
// usage: prog <pair> [seconds] [last]
//
// kph prints out the price history of the <pair> in
// the [last] number of seconds. The trade data is
// showed as candlesticks grouped in periods of
// [seconds] seconds.
//
KAPI::Input input;
input["since"] = "0";
switch (argc) {
case 4:
istringstream(argv[3]) >> last;
case 3:
istringstream(argv[2]) >> step;
case 2:
input["pair"] = std::string(argv[1]);
break;
default:
throw std::runtime_error("wrong number of arguments");
};
// initialize kraken lib's resources:
Kraken::initialize();
KAPI kapi;
std::vector<Trade> trades;
std::vector<Candlestick> candlesticks;
recent_trades(kapi, input, trades);
// group trades by time
group_by_time(trades, step, candlesticks);
if (!candlesticks.empty()) {
// print candlestick after this threshold
time_t thresh = candlesticks.back().time - last;
auto it = candlesticks.cbegin();
HA_Candlestick ha(*it);
if (ha.time > thresh)
cout << ha << endl;
for (++it; it != candlesticks.cend(); ++it) {
ha = HA_Candlestick(*it, ha);
if (ha.time >= thresh)
cout << ha << endl;
}
}
}
catch(exception& e) {
cerr << "Error: " << e.what() << endl;
}
catch(...) {
cerr << "Unknow exception." << endl;
}
curl_global_cleanup();
return 0;
}