-
Notifications
You must be signed in to change notification settings - Fork 0
/
MerkelMain.cpp
418 lines (366 loc) · 12.1 KB
/
MerkelMain.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "MerkelMain.h"
#include <iostream>
#include <vector>
#include "OrderBookEntry.h"
#include "CSVReader.h"
#include "Candlestick.h"
#include "TechnicalAnalysis.h"
MerkelMain::MerkelMain()
{
}
void MerkelMain::init()
{
int input;
currentTime = orderBook.getEarliestTime();
wallet.insertCurrency("BTC", 10);
while (true)
{
printMenu();
input = getUserOption();
processUserOption(input);
}
}
void MerkelMain::printMenu()
{
// 1 print help
std::cout << "1: Print help " << std::endl;
// 2 print exchange stats
std::cout << "2: Print exchange stats" << std::endl;
// 3 make an offer
std::cout << "3: Make an ask " << std::endl;
// 4 make a bid
std::cout << "4: Make a bid " << std::endl;
// 5 print wallet
std::cout << "5: Print wallet " << std::endl;
//6 plotVolumeData
std::cout << "6: Plot Volume Data " << std::endl;
//7 plotPriceData
std::cout << "7: Plot Price Data " << std::endl;
// 8 continue
std::cout << "8: Continue " << std::endl;
std::cout << "============== " << std::endl;
std::cout << "Current time is: " << currentTime << std::endl;
}
void MerkelMain::printHelp()
{
std::cout << "Help - your aim is to make money. Analyse the market and make bids and offers. " << std::endl;
}
void MerkelMain::printMarketStats()
{
for (std::string const& p : orderBook.getKnownProducts())
{
std::cout << "Product: " << p << std::endl;
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask,
p, currentTime);
std::cout << "Asks seen: " << entries.size() << std::endl;
std::cout << "Max ask: " << OrderBook::getHighPrice(entries) << std::endl;
std::cout << "Min ask: " << OrderBook::getLowPrice(entries) << std::endl;
}
}
void MerkelMain::enterAsk()
{
std::cout << "Make an ask - enter the amount: product,price, amount, eg ETH/BTC,200,0.5" << std::endl;
std::string input;
std::getline(std::cin, input);
std::vector<std::string> tokens = CSVReader::tokenise(input, ',');
if (tokens.size() != 3)
{
std::cout << "MerkelMain::enterAsk Bad input! " << input << std::endl;
}
else {
try {
OrderBookEntry obe = CSVReader::stringsToOBE(
tokens[1],
tokens[2],
currentTime,
tokens[0],
OrderBookType::ask
);
obe.username = "simuser";
if (wallet.canFulfillOrder(obe)) {
std::cout << "Wallet looks good. " << std::endl;
orderBook.insertOrder(obe);
}
else {
std::cout << "Wallet has insufficient funds" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << " MerkelMain::enterAsk Bad input " << std::endl;
}
}
std::cout << "You typed: " << input << std::endl;
}
void MerkelMain::enterBid()
{
std::cout << "Make a bid - enter the amount: product,price, amount, eg ETH/BTC,200,0.5" << std::endl;
std::string input;
std::getline(std::cin, input);
std::vector<std::string> tokens = CSVReader::tokenise(input, ',');
if (tokens.size() != 3)
{
std::cout << "MerkelMain::enterBid Bad input! " << input << std::endl;
}
else {
try {
OrderBookEntry obe = CSVReader::stringsToOBE(
tokens[1],
tokens[2],
currentTime,
tokens[0],
OrderBookType::bid
);
obe.username = "simuser";
if (wallet.canFulfillOrder(obe)) {
std::cout << "Wallet looks good. " << std::endl;
orderBook.insertOrder(obe);
}
else {
std::cout << "Wallet has insufficient funds" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << " MerkelMain::enterBid Bad input " << std::endl;
}
}
std::cout << "You typed: " << input << std::endl;
}
void MerkelMain::printWallet()
{
std::cout << wallet.toString() << std::endl;
}
/*
void MerkelMain::gotoNextTimeframe()
{
std::cout << "Going to next time frame. " << std::endl;
for (std::string& p : orderBook.getKnownProducts())
{
std::cout << "matching " << p << std::endl;
std::vector<OrderBookEntry> sales = orderBook.matchAsksToBids(p, currentTime);
std::cout << "Sales: " << sales.size() << std::endl;
for (OrderBookEntry& sale : sales)
{
std::cout << "Sale price: " << sale.price << " amount " << sale.amount << std::endl;
if (sale.username == "simuser") {
//update wallet
wallet.processSale(sale);
}
}
}
currentTime = orderBook.getNextTime(currentTime);
}
*/
//my code
void MerkelMain::gotoNextTimeframe(const std::string& currencyPair)
{
std::cout << "Going to the next time frame." << std::endl;
for (std::string& p : orderBook.getKnownProducts())
{
std::cout << "Matching " << p << std::endl;
std::vector<OrderBookEntry> sales = orderBook.matchAsksToBids(p, currentTime);
std::cout << "Sales: " << sales.size() << std::endl;
for (OrderBookEntry& sale : sales)
{
std::cout << "Sale price: " << sale.price << ", amount: " << sale.amount << std::endl;
if (sale.username == "simuser")
{
// Update wallet
wallet.processSale(sale);
}
}
}
currentTime = orderBook.getNextTime(currentTime);
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, currencyPair, currentTime);
std::vector<Candlestick> candlesticks = CSVReader::computeCandlestickData(entries);
std::cout << "Candlestick data for 'asks on " << currencyPair << "':" << std::endl;
for (const Candlestick& candlestick : candlesticks)
{
std::cout << "Open: " << candlestick.open << ", High: " << candlestick.high
<< ", Low: " << candlestick.low << ", Close: " << candlestick.close << std::endl;
}
}
int MerkelMain::getUserOption()
{
int userOption = 0;
std::string line;
std::cout << "Type in 1-6" << std::endl;
std::getline(std::cin, line);
try {
userOption = std::stoi(line);
}
catch (const std::exception& e)
{
//
}
std::cout << "You chose: " << userOption << std::endl;
return userOption;
}
//my code
void MerkelMain::plotVolumeData(const std::vector<OrderBookEntry>& entries, const std::string& currencyPair)
{
std::vector<double> volumeData = TechnicalAnalysis::computeVolumeData(entries);
std::cout << "Volume Data Plot for " << currencyPair << ":" << std::endl;
// Calculate the maximum volume for scaling the graph
double maxVolume = *std::max_element(volumeData.begin(), volumeData.end());
for (double volume : volumeData)
{
// Calculate the number of asterisks to represent the volume
int numAsterisks = static_cast<int>((volume / maxVolume) * 50);
// Print the volume bar as the graph with volume value
std::cout << std::string(numAsterisks, '|') << " " << volume << std::endl;
}
}
//my code
void MerkelMain::plotPriceData(const std::vector<OrderBookEntry>& entries, const std::string& currencyPair)
{
std::vector<double> priceData = TechnicalAnalysis::computePriceData(entries);
std::cout << "Price Data Plot for " << currencyPair << ":" << std::endl;
// Calculate the minimum and maximum prices for scaling the graph
double minPrice = *std::min_element(priceData.begin(), priceData.end());
double maxPrice = *std::max_element(priceData.begin(), priceData.end());
for (double price : priceData)
{
// Calculate the position of the price on a scale of 0 to 50
int position = static_cast<int>(((price - minPrice) / (maxPrice - minPrice)) * 50);
// Print the price line as the graph with price value
std::cout << std::string(position, '-') << " " << price << std::endl;
}
}
/*
void MerkelMain::plotVolumeData(const std::vector<OrderBookEntry>& entries)
{
std::vector<double> volumeData = TechnicalAnalysis::computeVolumeData(entries);
std::cout << "Volume Data Plot:" << std::endl;
// Calculate the maximum volume for scaling the graph
double maxVolume = *std::max_element(volumeData.begin(), volumeData.end());
for (double volume : volumeData)
{
// Calculate the number of asterisks to represent the volume
int numAsterisks = static_cast<int>((volume / maxVolume) * 50);
// Print the volume bar as the graph with volume value
std::cout << std::string(numAsterisks, '|') << " " << volume << std::endl;
}
}
void MerkelMain::plotPriceData(const std::vector<OrderBookEntry>& entries)
{
std::vector<double> priceData = TechnicalAnalysis::computePriceData(entries);
std::cout << "Price Data Plot:" << std::endl;
// Calculate the minimum and maximum prices for scaling the graph
double minPrice = *std::min_element(priceData.begin(), priceData.end());
double maxPrice = *std::max_element(priceData.begin(), priceData.end());
for (double price : priceData)
{
// Calculate the position of the price on a scale of 0 to 50
int position = static_cast<int>(((price - minPrice) / (maxPrice - minPrice)) * 50);
// Print the price line as the graph with price value
std::cout << std::string(position, '-') << " " << price << std::endl;
}
}
*/
/*
void MerkelMain::processUserOption(int userOption)
{
if (userOption == 0) // bad input
{
std::cout << "Invalid choice. Choose 1-8" << std::endl;
}
if (userOption == 1)
{
printHelp();
}
if (userOption == 2)
{
printMarketStats();
}
if (userOption == 3)
{
enterAsk();
}
if (userOption == 4)
{
enterBid();
}
if (userOption == 5)
{
printWallet();
}
if (userOption == 6)
{
for (const std::string& currencyPair : orderBook.getKnownProducts())
{
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, currencyPair, currentTime);
plotVolumeData(entries);
}
}
if (userOption == 7)
{
for (const std::string& currencyPair : orderBook.getKnownProducts())
{
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, currencyPair, currentTime);
plotPriceData(entries);
}
}
if (userOption == 8)
{
for (const std::string& currencyPair : orderBook.getKnownProducts())
{
gotoNextTimeframe(currencyPair);
}
}
}*/
//my code
/*print candlestick data for all currency pairs*/
void MerkelMain::processUserOption(int userOption)
{
if (userOption == 0) // bad input
{
std::cout << "Invalid choice. Choose 1-8" << std::endl;
}
if (userOption == 1)
{
printHelp();
}
if (userOption == 2)
{
printMarketStats();
}
if (userOption == 3)
{
enterAsk();
}
if (userOption == 4)
{
enterBid();
}
if (userOption == 5)
{
printWallet();
}
if (userOption == 6)
{
std::vector<std::string> currencyPairs = orderBook.getKnownProducts();
for (const std::string& currencyPair : currencyPairs)
{
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, currencyPair, currentTime);
plotVolumeData(entries, currencyPair);
}
}
if (userOption == 7)
{
std::vector<std::string> currencyPairs = orderBook.getKnownProducts();
for (const std::string& currencyPair : currencyPairs)
{
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, currencyPair, currentTime);
plotPriceData(entries, currencyPair);
}
}
if (userOption == 8)
{
for (const std::string& currencyPair : orderBook.getKnownProducts())
{
gotoNextTimeframe(currencyPair);
}
}
}