-
Notifications
You must be signed in to change notification settings - Fork 1
/
prod.cpp
225 lines (197 loc) · 6.09 KB
/
prod.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
/* HOW TO RUN
1) Configure things in the Configuration class
2) Compile: g++ -o bot.exe bot.cpp
3) Run in loop: while true; do ./bot.exe; sleep 1; done
*/
/* C includes for networking things */
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
/* C++ includes */
#include <string>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
/* The Configuration class is used to tell the bot how to connect
to the appropriate exchange. The `test_exchange_index` variable
only changes the Configuration when `test_mode` is set to `true`.
*/
class Configuration {
private:
/*
0 = prod-like
1 = slower
2 = empty
*/
static int const test_exchange_index = 0;
public:
std::string team_name;
std::string exchange_hostname;
int exchange_port;
/* replace REPLACEME with your team name! */
Configuration(bool test_mode) : team_name("liberalartseducation"){
exchange_port = 20000; /* Default text based port */
if(true == test_mode) {
exchange_hostname = "test-exch-" + team_name;
exchange_port += test_exchange_index;
} else {
exchange_hostname = "production";
}
}
};
/* Connection establishes a read/write connection to the exchange,
and facilitates communication with it */
class Connection {
private:
FILE * in;
FILE * out;
int socket_fd;
public:
Connection(Configuration configuration){
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
throw std::runtime_error("Could not create socket");
}
std::string hostname = configuration.exchange_hostname;
hostent *record = gethostbyname(hostname.c_str());
if(!record) {
throw std::invalid_argument("Could not resolve host '" + hostname + "'");
}
in_addr *address = reinterpret_cast<in_addr *>(record->h_addr);
std::string ip_address = inet_ntoa(*address);
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr(ip_address.c_str());
server.sin_family = AF_INET;
server.sin_port = htons(configuration.exchange_port);
int res = connect(sock, ((struct sockaddr *) &server), sizeof(server));
if (res < 0) {
throw std::runtime_error("could not connect");
}
FILE *exchange_in = fdopen(sock, "r");
if (exchange_in == NULL){
throw std::runtime_error("could not open socket for writing");
}
FILE *exchange_out = fdopen(sock, "w");
if (exchange_out == NULL){
throw std::runtime_error("could not open socket for reading");
}
setlinebuf(exchange_in);
setlinebuf(exchange_out);
this->in = exchange_in;
this->out = exchange_out;
this->socket_fd = res;
}
/** Send a string to the server */
void send_to_exchange(std::string input) {
std::string line(input);
/* All messages must always be uppercase */
std::transform(line.begin(), line.end(), line.begin(), ::toupper);
int res = fprintf(this->out, "%s\n", line.c_str());
if (res < 0) {
throw std::runtime_error("error sending to exchange");
}
}
/** Read a line from the server, dropping the newline at the end */
std::string read_from_exchange()
{
/* We assume that no message from the exchange is longer
than 10,000 chars */
const size_t len = 10000;
char buf[len];
if(!fgets(buf, len, this->in)){
throw std::runtime_error("reading line from socket");
}
int read_length = strlen(buf);
std::string result(buf);
/* Chop off the newline */
result.resize(result.length() - 1);
return result;
}
};
/** Join a vector of strings together, with a separator in-between
each string. This is useful for space-separating things */
std::string join(std::string sep, std::vector<std::string> strs) {
std::ostringstream stream;
const int size = strs.size();
for(int i = 0; i < size; ++i) {
stream << strs[i];
if(i != (strs.size() - 1)) {
stream << sep;
}
}
return stream.str();
}
int main(int argc, char *argv[])
{
ios_base::sync_with_stdio(false);
cin.tie(0);
string securities[7];
//securities = {"BOND", "VALBZ", "VALE", "GS", "MS", "WFC", "XLF"};
// Be very careful with this boolean! It switches between test and prod
bool test_mode = false;
Configuration config(test_mode);
Connection conn(config);
std::vector<std::string> data;
data.push_back(std::string("HELLO"));
data.push_back(config.team_name);
/*
A common mistake people make is to conn.send_to_exchange() > 1
time for every conn.read_from_exchange() response.
Since many write messages generate marketdata, this will cause an
exponential explosion in pending messages. Please, don't do that!
*/
conn.send_to_exchange(join(" ", data));
string line = conn.read_from_exchange();
cout << "The exchange replied: " << line << endl;
vector<string> buy;
buy.push_back(string("ADD"));
buy.push_back(string("1000"));
buy.push_back(string("BOND"));
buy.push_back(string("BUY"));
buy.push_back(string("999"));
buy.push_back(string("99"));
conn.send_to_exchange(join(" ", buy));
vector<string> sell;
sell.push_back(string("ADD"));
sell.push_back(string("1001"));
sell.push_back(string("BOND"));
sell.push_back(string("SELL"));
sell.push_back(string("1001"));
sell.push_back(string("99"));
conn.send_to_exchange(join(" ", sell));
while(true) {
string line = conn.read_from_exchange();
//cout << "The exchange replied: " << line << endl;
}
//start our trading here
while(true) {
//read from the exchange
string curline;
cin >> curline;
cout << "hello " << endl;
vector<string> res;
istringstream iss(curline);
for(string curline; iss >> curline;) {
res.push_back(curline);
cout << res[0] << endl;
}
if(curline.find("BOOK") == 0) {
}
else if(curline.find("TRADE") == 0) {
}
else if(curline.find("OPEN") == 0) {
}
else{
}
}
return 0;
}