-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
bitstamp.cpp
205 lines (170 loc) · 6.2 KB
/
bitstamp.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
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
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <vector>
#include <sstream>
#include <sys/time.h>
#include "base64.h"
#include <iomanip>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <jansson.h>
#include "bitstamp.h"
#include "curl_fun.h"
namespace Bitstamp {
double getQuote(CURL *curl, bool isBid) {
double quote;
json_t *root = getJsonFromUrl(curl, "https://www.bitstamp.net/api/ticker/", "");
if (isBid) {
quote = atof(json_string_value(json_object_get(root, "bid")));
} else {
quote = atof(json_string_value(json_object_get(root, "ask")));
}
json_decref(root);
return quote;
}
double getAvail(CURL *curl, Parameters params, std::string currency) {
double availability = 0.0;
json_t *root = authRequest(curl, params, "https://www.bitstamp.net/api/balance/", "");
while (json_object_get(root, "message") != NULL) {
std::cout << "<Bitstamp> Error with JSON in getAvail: " << json_dumps(root, 0) << ". Retrying..." << std::endl;
root = authRequest(curl, params, "https://www.bitstamp.net/api/balance/", "");
}
if (currency.compare("btc") == 0) {
availability = atof(json_string_value(json_object_get(root, "btc_balance")));
}
else if (currency.compare("usd") == 0) {
availability = atof(json_string_value(json_object_get(root, "usd_balance")));
}
json_decref(root);
return availability;
}
int sendOrder(CURL *curl, Parameters params, std::string direction, double quantity, double price) {
// define limit price to be sure to be executed
double limPrice;
if (direction.compare("buy") == 0) {
limPrice = getLimitPrice(curl, quantity, false);
}
else if (direction.compare("sell") == 0) {
limPrice = getLimitPrice(curl, quantity, true);
}
std::cout << "<Bitstamp> Trying to send a \"" << direction << "\" limit order: " << quantity << "@$" << limPrice << "..." << std::endl;
std::ostringstream oss;
oss << "https://www.bitstamp.net/api/" << direction << "/";
std::string url = oss.str();
oss.clear();
oss.str("");
oss << "amount=" << quantity << "&price=" << std::fixed << std::setprecision(2) << limPrice;
std::string options = oss.str();
json_t *root = authRequest(curl, params, url, options);
int orderId = json_integer_value(json_object_get(root, "id"));
if (orderId == 0) {
std::cout << "<Bitstamp> Order ID = 0. Message: " << json_dumps(root, 0) << std::endl;
}
std::cout << "<Bitstamp> Done (order ID: " << orderId << ")\n" << std::endl;
json_decref(root);
return orderId;
}
bool isOrderComplete(CURL *curl, Parameters params, int orderId) {
if (orderId == 0) {
return true;
}
std::ostringstream oss;
oss << "id=" << orderId;
std::string options = oss.str();
json_t *root = authRequest(curl, params, "https://www.bitstamp.net/api/order_status/", options);
std::string status = json_string_value(json_object_get(root, "status"));
json_decref(root);
if (status.compare("Finished") == 0) {
return true;
} else {
return false;
}
}
double getActivePos(CURL *curl, Parameters params) {
return getAvail(curl, params, "btc");
}
double getLimitPrice(CURL *curl, double volume, bool isBid) {
double limPrice;
json_t *root;
if (isBid) {
root = json_object_get(getJsonFromUrl(curl, "https://www.bitstamp.net/api/order_book/", ""), "bids");
} else {
root = json_object_get(getJsonFromUrl(curl, "https://www.bitstamp.net/api/order_book/", ""), "asks");
}
// loop on volume
double tmpVol = 0.0;
int i = 0;
while (tmpVol < volume) {
// volumes are added up until the requested volume is reached
tmpVol += atof(json_string_value(json_array_get(json_array_get(root, i), 1)));
i++;
}
// return the second next offer
limPrice = atof(json_string_value(json_array_get(json_array_get(root, i+1), 0)));
json_decref(root);
return limPrice;
}
json_t* authRequest(CURL *curl, Parameters params, std::string url, std::string options) {
std::string readBuffer;
// nonce
struct timeval tv;
gettimeofday(&tv, NULL);
long nonce = (tv.tv_sec * 1000) + (tv.tv_usec / 1000.0) + 0.5;
std::ostringstream oss;
oss << nonce << params.bitstampClientId << params.bitstampApi;
unsigned char* digest;
// Using sha256 hash engine
digest = HMAC(EVP_sha256(), params.bitstampSecret, strlen(params.bitstampSecret), (unsigned char*)oss.str().c_str(), strlen(oss.str().c_str()), NULL, NULL);
char mdString[SHA256_DIGEST_LENGTH+100]; // FIXME +100
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
sprintf(&mdString[i*2], "%02X", (unsigned int)digest[i]);
}
oss.clear();
oss.str("");
oss << "key=" << params.bitstampApi << "&signature=" << mdString << "&nonce=" << nonce << "&" << options;
std::string postParams = oss.str().c_str();
// cURL request
CURLcode resCurl;
// curl = curl_easy_init();
if (curl) {
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POST,1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
resCurl = curl_easy_perform(curl);
json_t *root;
json_error_t error;
while (resCurl != CURLE_OK) {
std::cout << "<Bitstamp> Error with cURL. Retry in 2 sec...\n" << std::endl;
sleep(2.0);
readBuffer = "";
resCurl = curl_easy_perform(curl);
}
root = json_loads(readBuffer.c_str(), 0, &error);
while (!root) {
std::cout << "<Bitstamp> Error with JSON in authRequest:\n" << "Error: : " << error.text << ". Retrying..." << std::endl;
readBuffer = "";
resCurl = curl_easy_perform(curl);
while (resCurl != CURLE_OK) {
std::cout << "<Bitstamp> Error with cURL. Retry in 2 sec...\n" << std::endl;
sleep(2.0);
readBuffer = "";
resCurl = curl_easy_perform(curl);
}
root = json_loads(readBuffer.c_str(), 0, &error);
}
curl_easy_reset(curl);
return root;
}
else {
std::cout << "<Bitstamp> Error with cURL init." << std::endl;
return NULL;
}
}
}