-
Notifications
You must be signed in to change notification settings - Fork 20
/
BinanceBot.cpp
287 lines (252 loc) · 11.3 KB
/
BinanceBot.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "BinanceBot.h"
void init();
auto main(int argc, char* argv[]) -> int
{
cout << endl << "Welcome to the C++ Binance Bot. WARNING --> This is still an extremly early alpha build. Use at your own risk." << endl;
init();
return 0;
}
void init(){
string input;
cout << endl << "Choose bot mode: 1 = trading" << endl;
cin >> input;
(input == "1") ? bot.tradingBot() : bot.algoBot(); //TEMPORARY
}
void botData::setPair(){
cout << "Select trading pair (ex: LTCBTC, TRXETH, BTCUSDT, etc): " << endl;
cin >> bot.pair;
}
void botData::setPrice(double foundPrice){
price = foundPrice;
}
void botData::setSellPercent(){
cout << "Enter target gains percentage" << endl;
cin >> sellPercent;
}
void botData::tradingBot(){
string choice, amount;
bot.setPair();
bot.getPrice(pair);
cout << "Selected " << pair << ". Last known price of " << pair << ": " << price;
cout << "Buying or selling? (1 for buying, 0 for selling)" << endl;
cin >> choice;
(choice == "0") ? choice = "SELL" : choice = "BUY";
cout << "Enter the price you would like to buy/sell at." << endl;
cin >> price;
cout << "Enter amount you would like to buy/sell." << endl;
cin >> amount;
string curlTest = "curl -H \"X-MBX-APIKEY: ";
// string command = "\" -X POST 'https://api.binance.com/api/v3/order?symbol="+pair+"&side="+choice+"&type=LIMIT&timeInForce=GTC&quantity="+amount+"&price="+price+"&recvWindow=5000×tamp=";
getTime();
//string message = "symbol="+pair+"&side="+choice+"&type=LIMIT&timeInForce=GTC&quantity="+amount+"&price="+price+"&recvWindow=5000×tamp="+epochTime;
// HMACsha256(message, bot.secretKey);
//curlTest = curlTest+APIKEY+command+time+"&signature="+signature+"'";
cout << endl << endl << curlTest << endl << endl;
// system(curlTest.c_str());
}
void botData::algoBot(){
algoCheck = false;
algoBuy = true;
bot.setPair();
bot.setSellPercent();
cout << "Gathering data on pair...This may take some time..." << endl;
getPrice(bot.pair);
cout << "PRICE: " << setprecision(8) << fixed << bot.price << endl;
pastPrice = price;
while(algoCheck == false)
(algoBuy == true) ? checkBuy() : checkSell();
}
void botData::checkBuy(){
bot.getHistoricalPrices();
bot.calcRSI();
}
void botData::calcRSI()
{
vector<long double> gain, loss, change, avgGain, avgLoss, RS, RSI;
vector<long double> currentPeriod(close.end()-33, close.end());
double sumGain = 0, sumLoss = 0;
for(int i = 1; i < currentPeriod.size(); i++){
change.push_back(currentPeriod[i] - currentPeriod[i-1]);
cout << endl << currentPeriod[i] << endl;
}
cout << endl << "******* " << close.back() << endl;
for(int i = 0; i < change.size(); i++){
change[i] > 0 ? gain.push_back(change[i]) : gain.push_back(0);
change[i] < 0 ? loss.push_back(abs(change[i])) : loss.push_back(0);
}
for(int i = 0; i < 14; i++){
sumGain += gain[i];
sumLoss += loss[i];
}
avgGain.push_back(sumGain/14);
avgLoss.push_back(sumLoss/14);
cout << endl << "gain: " << avgGain[0] << " loss: " << avgLoss[0] << endl;
for(int i = 14, j = 1; i < gain.size(); i++){
avgGain.push_back(((avgGain[j-1] * 13)+ gain[i])/14);
avgLoss.push_back(((avgLoss[j-1] * 13)+ loss[i])/14);
j++;
}
for(int i = 0; i < avgGain.size(); i++)
RS.push_back(avgGain[i]/avgLoss[i]);
for(int i = 0; i < RS.size(); i++)
avgLoss[i] == 0 ? RSI.push_back(100) : RSI.push_back(100 - (100/(1+RS[i])));
cout << endl << "RSI: " << RSI.back() << endl;
}
void botData::formatHistoricalPrices(json::value const &value){ //temp
if(!value.is_null()){
json::value historicalData = value;
ofstream outputFile("test.txt"); //output to file
outputFile << historicalData; //store JSON into file
outputFile.close();
ifstream inputFile("test.txt"); //input from file
string historicalDataString="";
string appendTemp;
while(!inputFile.eof()){
getline(inputFile,appendTemp); //store value from file into string
historicalDataString.append(appendTemp);
}
boost::erase_first(historicalDataString, "["); //formatting for parsing
historicalDataString.append("\"]"); //formatting for parsing
int count = 0;
cout << endl << endl << "------ " << historicalDataString << endl << endl;
string tempHolder;
for (string::iterator it = historicalDataString.begin(); it < historicalDataString.end(); it++){
if(*it != '[' && *it != '"' && *it !=']'){
if(*it != ',')
tempHolder.append(1,*it);
else{
switch (count){
case 0:
openTime.push_back(stof(tempHolder.c_str()));
break;
case 1:
open.push_back(stof(tempHolder.c_str()));
break;
case 2:
high.push_back(stof(tempHolder.c_str()));
break;
case 3:
low.push_back(stof(tempHolder.c_str()));
break;
case 4:
close.push_back(stof(tempHolder.c_str()));
break;
case 5:
volume.push_back(stof(tempHolder.c_str()));
break;
case 6:
closeTime.push_back(stof(tempHolder.c_str()));
break;
case 7:
quoteAssetVolume.push_back(stof(tempHolder.c_str()));
break;
case 8:
numTrades.push_back(stof(tempHolder.c_str()));
break;
case 9:
takerBuyAssetVol.push_back(stof(tempHolder.c_str()));
break;
case 10:
takerBuyQuoteAssetVol.push_back(stof(tempHolder.c_str()));
break;
case 11:
Ignore.push_back(stof(tempHolder.c_str()));
break;
default:
break;
}//end of switch
(count == 11) ? count = 0 : count++;
tempHolder = "";
}//end of else
}//end of 2nd if
} //end of for loop
}//end of first if
}//End of function
void botData::getHistoricalPrices(){ //Get all price data since 1/1/2017 over an interval
//https://api.binance.com/api/v1/klines?symbol=ETHBTC&interval=1h&startTime=1523059200
http_client client(U(RESTfulHost));
string interval, timestamp = "1483243199000";
cout << endl << "Enter interval for data collection (Ex: 1h, 2h, 4h, 5m, etc)" << endl;
cin >> interval;
string full_request = "/api/v1/klines?symbol="+pair+"&interval="+interval;
client.request(methods::GET, full_request).then([](http_response response)-> //Do a RESTful GET request on the full URL for the price
pplx::task<json::value>{ //Result will be in JSON
if(response.status_code() == status_codes::OK){ //If everything went okay, return the JSON (lambda function)
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask){
try{
json::value const & v = previousTask.get(); //Take the JSON gathered by the GET request and store it into CPP JSON object
bot.formatHistoricalPrices(v);
}
catch (http_exception const & e){ //This is just here to output an error if one occurs.
wcout << e.what() << endl;
}
}).wait(); //Wait for all to finish
}
void botData::checkSell(){
getPrice(bot.pair);
double difference;
cout << endl << "PRICE: " << setprecision(8) << fixed << bot.price << endl;
if(price > pastPrice){
difference = ((price - pastPrice)/price)*100;
cout << endl << "DIFF: " << difference << endl;
if(difference >= sellPercent){
cout << "We should sell." << endl;
}
}
}
void printPrice(json::value const &value){ //temp
if(!value.is_null()){
json::value test = value;
string v = test["price"].as_string();
double price = stof(v.c_str());
bot.setPrice(price);
// cout << test["price"] << endl << v << endl;
}
}
void botData::getPrice(const string pair){
http_client client(U(RESTfulHost)); //The base connection for the RESTful API
string full_request = "/api/v3/ticker/price?symbol=" + pair; //The rest of the URL we are going to ping to the price
client.request(methods::GET, full_request).then([](http_response response)-> //Do a RESTful GET request on the full URL for the price
pplx::task<json::value>{ //Result will be in JSON
if(response.status_code() == status_codes::OK){ //If everything went okay, return the JSON (lambda function)
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask){
try{
json::value const & v = previousTask.get(); //Take the JSON gathered by the GET request and store it into CPP JSON object
printPrice(v); //pass that object to the print price
}
catch (http_exception const & e){ //This is just here to output an error if one occurs.
wcout << e.what() << endl;
}
}).wait(); //Wait for all to finish
}
inline auto binary_to_hex_digit(unsigned a) -> char{
return a + (a < 10 ? '0' : 'a' - 10);
}
auto binary_to_hex(unsigned char const* binary, unsigned binary_len) -> string {
string r(binary_len * 2, '\0');
for(unsigned i = 0; i < binary_len; ++i) {
r[i * 2] = binary_to_hex_digit(binary[i] >> 4);
r[i * 2 + 1] = binary_to_hex_digit(binary[i] & 15);
}
return r;
}
void botData::HMACsha256(string const& message, string const& key) {
unsigned char result[EVP_MAX_MD_SIZE];
unsigned result_len = 0;
HMAC(EVP_sha256(), key.data(), key.size(), reinterpret_cast<unsigned char const*>(message.data()), message.size(), result, &result_len);
signature = binary_to_hex(result, result_len);
}
void botData::getTime(){
struct timeval tp;
gettimeofday(&tp, NULL);
long long mslong = (long long) tp.tv_sec * 1000L + tp.tv_usec / 1000;
string time = to_string(mslong);
}