/
krt.cpp
175 lines (150 loc) · 4.51 KB
/
krt.cpp
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
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <ctime>
#include <chrono>
#include <thread>
#include "kraken/kclient.hpp"
#include "libjson/libjson.h"
using namespace std;
using namespace Kraken;
//------------------------------------------------------------------------------
// deals with Kraken assets:
struct KAsset {
string name;
string altname;
string aclass;
int decimals;
int display_decimals;
// construct a KAssets from an JSONNode:
KAsset(const JSONNode& node) {
name = libjson::to_std_string(node.name());
altname = node["altname"].as_string();
aclass = node["aclass"].as_string();
decimals = node["decimals"].as_int();
display_decimals = node["display_decimals"].as_int();
}
};
//------------------------------------------------------------------------------
// prints a KAsset:
ostream& operator<<(ostream& os, const KAsset& a)
{
return os << '"'
<< a.name << "\",\""
<< a.altname << "\",\""
<< a.aclass << "\",\""
<< a.decimals << "\",\""
<< a.display_decimals
<< '"';
}
//------------------------------------------------------------------------------
// deals with Kraken assets:
struct KAsset_pair {
string name;
string altname;
string aclass_base;
string base;
string aclass_quote;
string quote;
string lot;
int pair_decimals;
int lot_decimals;
int lot_multiplier;
// leverage ...
string fees;
string fee_volume_currency;
int margin_call;
int margin_stop;
// construct a KAssets from an JSONNode:
KAsset_pair(const JSONNode& node) {
name = libjson::to_std_string( node.name() );
altname = libjson::to_std_string( node["altname"].as_string() );
aclass_base = libjson::to_std_string( node["aclass_base"].as_string() );
base = libjson::to_std_string( node["base"].as_string() );
aclass_quote = libjson::to_std_string( node["aclass_quote"].as_string() );
quote = libjson::to_std_string( node["quote"].as_string() );
lot = libjson::to_std_string( node["lot"].as_string() );
pair_decimals = node["pair_decimals"].as_int();
lot_decimals = node["lot_decimals"].as_int();
lot_multiplier = node["lot_multiplier"].as_int();
fees = libjson::to_std_string( node["fees"].as_string() );
fee_volume_currency
= libjson::to_std_string( node["fee_volume_currency"].as_string() );
margin_call = node["margin_call"].as_int();
margin_stop = node["margin_stop"].as_int();
}
};
//------------------------------------------------------------------------------
// prints a KAsset_pair:
ostream& operator<<(ostream& os, const KAsset_pair& a)
{
return os << '"'
<< a.name << "\",\""
<< a.altname << "\",\""
<< a.aclass_base << "\",\""
<< a.base << "\",\""
<< a.aclass_quote << "\",\""
<< a.quote << "\",\""
<< a.lot << "\",\""
<< a.pair_decimals << "\",\""
<< a.lot_decimals << "\",\""
<< a.lot_multiplier << "\",\""
<< a.fees << "\",\""
<< a.fee_volume_currency << "\",\""
<< a.margin_call << "\",\""
<< a.margin_stop
<< '"';
}
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
try {
// initialize kraken lib's resources:
Kraken::initialize();
//
// command line argument handling:
//
// usage:
// krt <pair> [interval] [since]
//
string pair;
string last = "0"; // by default: the oldest possible trade data
int interval = 0; // by default: krt exits after download trade data
switch (argc) {
case 4:
last = string(argv[3]);
case 3:
istringstream(argv[2]) >> interval;
case 2:
pair = string(argv[1]);
break;
default:
throw runtime_error("wrong number of arguments");
};
chrono::seconds dura(interval);
KClient kc;
vector<KTrade> vt;
while (true) {
// store and print trades
last = kc.trades(pair, last, vt);
for (int i = 0; i < vt.size(); ++i)
cout << vt[i] << endl;
// exit from the loop if interval is 0
if (interval == 0) break;
// sleep
this_thread::sleep_for(dura);
}
// terminate kraken lib's resources
Kraken::terminate();
}
catch(exception& e) {
cerr << "Error: " << e.what() << endl;
exit(EXIT_FAILURE);
}
catch(...) {
cerr << "Unknown exception." << endl;
exit(EXIT_FAILURE);
}
return 0;
}