This repository has been archived by the owner on Jun 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Executor.cpp
218 lines (173 loc) · 6 KB
/
Executor.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
#include "Headers/Executor.hpp"
#include "Headers/QuotesDB.hpp"
#include "Headers/DataBase.hpp"
#include "Headers/OandaAPI.hpp"
using namespace Poco::Net;
using namespace Poco;
int main(int argc, char const *argv[]) {
Executor e;
json j = e.getTradesJson();//e.get(std::string("/v3/accounts/") + ACCOUNT_ID + std::string("/summary"));
//std::vector<Trade> trades = Trade::translateTrades(j);
std::cout << j.dump(4) << std::endl;
std::cout << e.getBalance() << std::endl;
return 0;
}
/*
* called internally by default
* can be called externally if the two convenience functions (buy() and sell()) are not preferred
*/
int Executor::buyOrSell(int units, std::string instrument) {
json j = {
{ "order", {
{ "units", std::to_string(units) },
{ "instrument", instrument },
{ "timeInForce", "FOK" },
{ "type", "MARKET" },
{ "positionFill", "DEFAULT" }
} },
};
std::vector <std::string> v;
v.push_back("Accept-Datetime-Format: UNIX");
v.push_back("Authorization: Bearer " + ACCESS_TOKEN);
v.push_back("Content-Type: application/json");
Executor::post(std::string("/v3/accounts/" + ACCOUNT_ID + "/orders"), j, v);
return 1;
}
/*
* Constructor
*/
Executor::Executor() {
}
/*
* This function is passed through curl and handles the writing of the data into the string
* No JSON parsing is done in this function
*/
size_t Executor::responseWriter(void *contents, size_t size, size_t nmemb, std::string *s) {
size_t newLength = size * nmemb;
size_t oldLength = s->size();
try
{
s->resize(oldLength + newLength);
}
catch (std::bad_alloc &e)
{
//handle memory problem
printf("shit\n");
}
std::copy((char *)contents, (char *)contents + newLength, s->begin() + oldLength);
return size * nmemb;
}
/*
* Simple curl post request
* uses above responseWriter()
*/
json Executor::post(std::string path, json j, std::vector <std::string> v) {
std::string body = j.dump();
CURL * curl = curl_easy_init();
std::string s; // Used by writing function
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, std::string("https://api-fxpractice.oanda.com" + path).c_str());
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, v[0].c_str());
headers = curl_slist_append(headers, v[1].c_str());
headers = curl_slist_append(headers, v[2].c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
//Sets up the writing function
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, responseWriter); // Our function defined above will be called to handle the response
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
// Post method
CURLcode ret = curl_easy_perform(curl);
// check for error during post
if (ret != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(ret));
std::cout << ret << '\n';
}
curl_easy_cleanup(curl);
return json::parse(s);
}
/*
* Wrapper of cURL get for oanda api
* Pass your path (the part after "oanda.com")
* returns json
*/
json Executor::get(std::string path){
CURL * curl = curl_easy_init();
std::string s; // Used by writing function
curl_easy_setopt(curl, CURLOPT_URL, std::string("https://api-fxpractice.oanda.com" + path).c_str());
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept-Datetime-Format: UNIX");
headers = curl_slist_append(headers, std::string("Authorization: Bearer " + ACCESS_TOKEN).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
//Sets up the writing function
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, responseWriter); // Our function defined above will be called to handle the response
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
// Post method
CURLcode ret = curl_easy_perform(curl);
// check for error during post
if (ret != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(ret));
std::cout << ret << '\n';
}
curl_easy_cleanup(curl);
return json::parse(s);
}
/*
* Convenience wrapper for buyOrSell()
*/
bool Executor::buy(int units, std::string pair) {
Executor::buyOrSell(units, pair);
return 0;
}
/*
* Convenience wrapper for buyOrSell()
*/
bool Executor::sell(int units, std::string pair) {
Executor::buyOrSell(-1 * units, pair);
return 0;
}
/*
* Returns total profit from current trades
* Caller must update trades array BEFORE calling
* This function does not update trades array
* Trades array can be updated using the functions in the Trade class
*/
double Executor::getProfit() {
double profit = 0;
for (size_t i = 0; i < trades.size(); i++) {
profit += trades[i].profit;
}
return profit;
}
/*
* TODO: Switch this to curl
*/
double Executor::getBalance(){
json j = get(std::string("/v3/accounts/") + ACCOUNT_ID + std::string("/summary"));
j = j.at("account").get<json>();
double y = std::stod(j.at("balance").get<std::string>());
return y;
}
/*
* TODO: Switch this to curl
*/
json Executor::getTradesJson() {
json j = get(std::string("/v3/accounts/") + ACCOUNT_ID + std::string("/openTrades"));
return j;
}
json Executor::getTradesJsonCurl(){
}
/*
* Testing function not to be used at run time
*/
void Executor::test() {
qdb::OandaAPI conn("practice");
std::string x("EUR_USD_D");
std::string endpoint("/v3/accounts/" + ACCOUNT_ID + "/instruments");
//std::cout << conn.request(endpoint) << std::endl;
json j;
j = json::parse(conn.request(endpoint));
std::cout << j.dump(4) << std::endl;
}