-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMerkel.cpp
223 lines (207 loc) · 6.91 KB
/
Merkel.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
#include "Merkel.h"
#include <iostream>
#include "CSVReader.h"
Merkel::Merkel()
{
}
void Merkel::init()
{
int input;
currentTime = orderBook.getEarliestTime();
wallet.insertCurrency("BTC", 10);
while(true)
{
printMenu();
input = getUserOption();
std::cout << std::endl;
processUserOption(input);
}
}
void Merkel::printMenu()
{
// 1. Print help -> instruction for the app
std::cout << "1: Print help" << std::endl;
// 2. Print exchange stats -> current internal statistics like offers, bids etc.
std::cout << "2: Print exchange stats" << std::endl;
// 3. Make an ask -> what I've got to sell
std::cout << "3: Make an ask" << std::endl;
// 4. Make a bid -> what I would like to buy
std::cout << "4: Make a bid" << std::endl;
// 5. Print wallet -> current balance of my assets
std::cout << "5: Print wallet" << std::endl;
// 6. Continue -> got to next time step
std::cout << "6: Continue" << std::endl;
std::cout << "========================" << std::endl;
std::cout << "Current time is: " << currentTime << std::endl;
std::cout << "========================" << std::endl;
}
void Merkel::printHelp()
{
std::cout << "Help - your aim is to make big money. Analyse the market and make bids and offers!" << std::endl;
std::cout << std::endl;
}
void Merkel::printMarketStats()
{
for (const std::string& p : orderBook.getKnownProducts())
{
std::cout << "''''''''''''''''''''''" <<std::endl;
std::cout << "Product: " << p << std::endl;
std::vector<OrderBookEntry> asks = orderBook.getOrders(OrderBookType::ask, p, currentTime);
std::cout << "Asks seen: " << asks.size() << std::endl;
std::cout << "Max ask: " << OrderBook::getHighPrice(asks) << std::endl;
std::cout << "Min ask: " << OrderBook::getLowPrice(asks) << std::endl;
std::vector<OrderBookEntry> bids = orderBook.getOrders(OrderBookType::bid, p, currentTime);
std::cout << "Bids seen: " << bids.size() << std::endl;
std::cout << "Max bid: " << OrderBook::getHighPrice(bids) << std::endl;
std::cout << "Min bid: " << OrderBook::getLowPrice(bids) << std::endl;
std::cout << "''''''''''''''''''''''" <<std::endl;
}
std::cout << std::endl;
}
void Merkel::enterAsk()
{
std::cout << "Make an ask - enter the data: product, price, amount (e.g ETH/BTC,100,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 << "Merkel::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.canFullfillOrder(obe))
{
std::cout << "Wallet looks good, order inserted!" << std::endl;
orderBook.insertOrder(obe);
}
else
{
std::cout << "Wallet cannot fullfill order, insufficiant funds!" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "Merkel::enterAsk Bad input! " << input << std::endl;
}
}
std::cout << std::endl;
}
void Merkel::enterBid()
{
std::cout << "Make a bid - enter the data: product, price, amount (e.g ETH/BTC,100,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 << "Merkel::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.canFullfillOrder(obe))
{
orderBook.insertOrder(obe);
std::cout << "Wallet looks good, order inserted!" << std::endl;
}
else
{
std::cout << "Wallet cannot fullfill order, insufficiant funds!" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "Merkel::enterBid Bad input! " << input << std::endl;
}
}
std::cout << std::endl;
}
void Merkel::printWallet()
{
std::cout << "*******************" << std::endl;
std::cout << "Here your wallet: " << std::endl;
std::cout << wallet.toString();
std::cout << "*******************" << std::endl;
std::cout << std::endl;
}
void Merkel::gotToNextTimeFrame()
{
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 the wallet
wallet.processSale(sale);
}
}
}
currentTime = orderBook.getNextTime(currentTime);
std::cout << std::endl;
}
void Merkel::processUserOption(int userOption)
{
switch (userOption)
{
case 1:
printHelp();
break;
case 2:
printMarketStats();
break;
case 3:
enterAsk();
break;
case 4:
enterBid();
break;
case 5:
printWallet();
break;
case 6:
gotToNextTimeFrame();
break;
default:
std::cout << "Invalid choice. Choose 1-6!" << std::endl;
std::cout << std::endl;
break;
}
}
int Merkel::getUserOption()
{
// getting user input
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 << "Merkel::getUserOption Bad input! " << line << std::endl;
}
std::cout << "You chose: " << userOption << std::endl;
return userOption;
}