-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoMain.cpp
More file actions
276 lines (238 loc) · 6.23 KB
/
CryptoMain.cpp
File metadata and controls
276 lines (238 loc) · 6.23 KB
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
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include "CryptoMain.h"
#include "OrderBookEntry.h"
#include "CSVReader.h"
#include "Timestamp.h"
using namespace std;
void CryptoMain::init()
{
running = true;
currentTime = orderBook.getEarliestTime();
int input;
while (running)
{
printMenu();
input = getInput();
processInput(input);
}
}
void CryptoMain::printMenu()
{
// 1 print help
cout << "1: Help" << endl;
// 2 print exchange stats
cout << "2: Print exchange stats" << endl;
// 3 make an ask
cout << "3: Make an ask" << endl;
// 4 make a bid
cout << "4: Make a bid" << endl;
// 5 print wallet
cout << "5: Print wallet" << endl;
// 6 continue
cout << "6: Continue" << endl;
// 7 exit
cout << "7: Exit" << endl;
cout << "================" << endl;
cout << "Current time is: " << currentTime << endl;
cout << "Type in 1-7" << endl;
}
int CryptoMain::getInput()
{
string line;
getline(cin, line);
int input;
try
{
input = stoi(line);
}
catch (const exception &e)
{
cout << "CryptoMain::getInput error: Could not convert input to int." << endl;
input = 0;
}
cout << "You chose: " << input << endl;
return input;
}
void CryptoMain::printHelp()
{
cout << "Make money, analyse the market, make bids and offers." << endl;
cout << "Choose options from the menu and follow the on screen instructions." << endl;
}
/**
* Print exchange stats
* This function will print the current stats for the exchange for the current timestamp
* and the stats for the last 24 hours.
* @returns void
*/
void CryptoMain::printExchangeStats()
{
for (auto const &product : orderBook.getKnownProducts())
{
cout << "================" << endl;
cout << "Product: " << product << endl;
cout << "================" << endl;
// Current stats
vector<OrderBookEntry> orders = orderBook.getOrders(
OrderBookType::ask,
product,
currentTime);
cout << "Current timestamp stats: " << endl;
cout << "Asks for product: " << orders.size() << endl;
double maxPrice = OrderBook::getHighPrice(orders);
double minPrice = OrderBook::getLowPrice(orders);
cout << "Max ask: " << maxPrice << endl;
cout << "Min ask: " << minPrice << endl;
cout << "Spread: " << OrderBook::getPriceSpread(minPrice, maxPrice) << endl;
// 24 Hour stats
Timestamp ts(currentTime);
string startTime = ts.getMinTimestamp();
string endTime = ts.getMaxTimestamp();
cout << "24 hour stats: " << endl;
cout << "Between " << startTime << " and " << endTime << endl;
vector<OrderBookEntry> orders24 = orderBook.getOrdersForTimePeriod(
OrderBookType::ask,
product,
startTime,
endTime);
cout << "Asks for product: " << orders24.size() << endl;
double maxPrice24 = OrderBook::getHighPrice(orders24);
double minPrice24 = OrderBook::getLowPrice(orders24);
cout << "Max ask: " << maxPrice24 << endl;
cout << "Min ask: " << minPrice24 << endl;
cout << "Spread: " << OrderBook::getPriceSpread(minPrice24, maxPrice24) << endl;
}
cout << "================" << endl;
}
void CryptoMain::enterAsk()
{
cout << "Make an ask in the format: product,price,amount. E.g. ETH/BTC,200,0.5" << endl;
string input;
getline(cin, input);
cout << "You entered: " << input << endl;
vector<string> tokens = CSVReader::tokenise(input, ',');
if (tokens.size() != 3)
{
cout << "CryptoMain::enterAsk error: invalid input, please enter in the format: product,price,amount" << endl;
}
else
{
try
{
OrderBookEntry order = CSVReader::stringsToOBE(
tokens[1],
tokens[2],
currentTime,
tokens[0],
OrderBookType::ask);
order.setUsername("user");
if (wallet.canFulfillOrder(order))
{
cout << "Fulfilling order..." << endl;
orderBook.insertOrder(order);
}
else
{
cout << "CryptoMain::enterAsk error: Not enough currency in wallet to fulfill order." << endl;
}
}
catch (const exception &e)
{
cout << "CryptoMain::enterAsk error: Could not convert price or amount to double." << endl;
}
}
}
void CryptoMain::enterBid()
{
cout << "Make a bid in the format: product,price,amount. E.g. ETH/BTC,200,0.5" << endl;
string input;
getline(cin, input);
cout << "You entered: " << input << endl;
vector<string> tokens = CSVReader::tokenise(input, ',');
if (tokens.size() != 3)
{
cout << "CryptoMain::enterBid error: invalid input, please enter in the format: product,price,amount" << endl;
}
else
{
try
{
OrderBookEntry order = CSVReader::stringsToOBE(
tokens[1],
tokens[2],
currentTime,
tokens[0],
OrderBookType::bid);
order.setUsername("user");
if (wallet.canFulfillOrder(order))
{
cout << "Fulfilling order..." << endl;
orderBook.insertOrder(order);
}
else
{
cout << "CryptoMain::enterBid error: Not enough currency in wallet to fulfill order." << endl;
}
}
catch (const exception &e)
{
cout << "CryptoMain::enterBid error: Could not convert price or amount to double." << endl;
}
}
}
void CryptoMain::printWallet()
{
cout << "Print wallet." << endl;
cout << wallet.toString() << endl;
}
void CryptoMain::handleContinue()
{
cout << "Going to next time frame..." << endl;
for (auto const &product : orderBook.getKnownProducts())
{
vector<OrderBookEntry> sales = orderBook.matchAsksToBids(product, currentTime);
cout << "Sales for " << product << ": " << sales.size() << endl;
for (auto &sale : sales)
{
cout << "Sale price: " << sale.getPrice() << " amount: " << sale.getAmount() << endl;
if (sale.getUsername() == "user")
{
wallet.processSale(sale);
}
}
}
currentTime = orderBook.getNextTime(currentTime);
}
void CryptoMain::handleExit()
{
cout << "Exiting..." << endl;
running = false;
}
void CryptoMain::handleInvalidInput()
{
cout << "Invalid choice. Choose 1-7." << endl;
}
void CryptoMain::processInput(int &input)
{
// Map from ints to function pointers
map<int, void (CryptoMain::*)()> menu;
menu[1] = &CryptoMain::printHelp;
menu[2] = &CryptoMain::printExchangeStats;
menu[3] = &CryptoMain::enterAsk;
menu[4] = &CryptoMain::enterBid;
menu[5] = &CryptoMain::printWallet;
menu[6] = &CryptoMain::handleContinue;
menu[7] = &CryptoMain::handleExit;
auto it = menu.find(input);
if (it != menu.end())
{
// Call the member function
(this->*(it->second))();
}
else
{
handleInvalidInput();
}
}