-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerkelMain.cpp
182 lines (155 loc) · 6 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
#include "MerkelMain.h"
#include <iostream>
#include <vector>
#include "OrderBookEntry.h"
#include "CSVReader.h"
#include <limits>
MerkelMain::MerkelMain(){}
void MerkelMain::init(){
int input;
currentTime = orderbook.getEarliestTime();
wallet.insertCurrency("BTC", 10);
while(true){
printMenu();
input = getUserOption();
processUserOption(input);
}
}
void MerkelMain::printMenu(){
std::cout << "1. Print Help" << std::endl;
std::cout << "2. Print Exchange Stats" << std::endl;
std::cout << "3. Place a ask" << std::endl;
std::cout << "4. Place a bid" << std::endl;
std::cout << "5. Print Wallet" << std::endl;
std::cout << "6. Continue" << std::endl;
std::cout << "====================" << std::endl;
std::cout << "Current Time is: " << currentTime << std::endl;
}
int MerkelMain::getUserOption(){
int userOption;
std::string line = "";
std::cout << "Type in 1-6" << std::endl;
std::getline(std::cin, line);
try{
userOption = std::stoi(line);
std::cout << "You chose: " << userOption << std::endl;
}catch(std::exception& e){
std::cout << "Input number from 1-6" << std::endl;
}
std::cout << std::endl;
return userOption;
}
void MerkelMain::printHelp(){
std::cout << "Help: Choose opitons from the menu " << std::endl;
std::cout << "and follow the on screen instructions." << std::endl;
}
void MerkelMain::printMarketStats(){
std::cout << "Below is the list of products in orders." << std::endl;
for (std::string const& prod : orderbook.getKnownProducts()){
std::cout << "Product: " << prod << std::endl;
std::vector<OrderBookEntry> entries = orderbook.getOrders(OrderBookType::ask, prod, currentTime);
std::cout << "Asks seen: " << entries.size() << std::endl;
// std::cout << "Max Ask Price: " << orderbook.getHighPrice(entries) << std::endl;
// std::cout << "Min Ask Price: " << orderbook.getLowPrice(entries) << std::endl;
std::cout << "Mean Price: " << orderbook.meanPrice(entries) << std::endl;
}
}
void MerkelMain::enterAsk(){
std::cout << "Make a ask, Enter the amount: product, price, amount (eg ETH/BTC, 200, 0.55)" << std::endl;
std::string user_input;
// Clear the cin buffer.
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
std::getline(std::cin, user_input);
std::vector<std::string> tokens = CSVReader::tokenise(user_input, ',');
if(tokens.size() != 3){
std::cout << "Bad Input: " << user_input << std::endl;
}else{
try{
OrderBookEntry obe = CSVReader::strings2OrderBookEntry(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 << "Insufficient funds. " << std::endl;
}
}catch (const std::exception& e){
std::cout << "MerkelMain::enterAsk, Bad Input: " << std::endl;
}
}
std::cout << "You entered: " << user_input << std::endl;
}
void MerkelMain::enterBid(){
std::cout << "Make a Bid, Enter the amount: product, price, amount (eg ETH/BTC, 200, 0.55)" << std::endl;
std::string user_input;
std::getline(std::cin, user_input);
std::vector<std::string> tokens = CSVReader::tokenise(user_input, ',');
if(tokens.size() != 3){
std::cout << "Bad Input: " << user_input << std::endl;
}else{
try{
OrderBookEntry obe = CSVReader::strings2OrderBookEntry(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 << "Insufficient funds. " << std::endl;
}
}catch (const std::exception& e){
std::cout << "MerkelMain::enterBid, Bad Input: " << std::endl;
}
}
std::cout << "You entered: " << user_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 prod : orderbook.getKnownProducts()){
std::cout << "Matching: " << prod << std::endl;
std::vector<OrderBookEntry> sales = orderbook.matchAskstoBids(prod, 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"){
//username
wallet.processSale(sale);
}
}
}
}
void MerkelMain::processUserOption(int userOption){
switch(userOption){
case 0:
std::cout << "Invalid Choice. Choose 1-6 " << std::endl;
break;
case 1:
printHelp();
break;
case 2:
printMarketStats();
break;
case 3:
enterAsk();
break;
case 4:
enterBid();
break;
case 5:
printWallet();
break;
case 6:
gotoNextTimeframe();
break;
}
}