-
Notifications
You must be signed in to change notification settings - Fork 0
/
MerkelMain.cpp
173 lines (150 loc) · 4.25 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
#include "MerkelMain.h"
#include <iostream>
#include <vector>
#include "OrderBookEntry.h"
#include "CSVReader.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: Place an ask" << std::endl;
// 4 make a bid
std::cout << "4: Place a bid" << std::endl;
// 5 print wallet
std::cout << "5: Print wallet" << std::endl;
// 6 continue
std::cout << "6: Continue" << std::endl;
std::cout << "========================" << std::endl;
std::cout << "Current time is " << currentTime << 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::cerr << e.what() << '\n';
}
std::cout << "You select " << userOption << std::endl;
return userOption;
}
void MerkelMain::printHelp()
{
std::cout << "Help - choose options from the menu" << std::endl;
std::cout << "and follow the on screen instructions." << 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;
std::cout << "Spread ask : " << OrderBook::getSpreadPrice(entries) << std::endl << std::endl;
}
}
void MerkelMain::enterAsk()
{
std::cout << "Place an Ask: Enter details to make an ask on the exchange : product,price,amount, eg: ETH/BTC,5400,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 << "Bad input! " << input << std::endl;
}
else
{
try
{
OrderBookEntry obe = CSVReader::stringsToOBE(
tokens[1],
tokens[2],
currentTime,
tokens[0],
OrderBookType::ask
);
orderBook.insertOrder(obe);
}
catch(const std::exception& e)
{
std::cout << "MerkelMain::enterAsk Bad input!" << std::endl;
}
}
std::cout << "You typed : " << input << std::endl << std::endl;
}
void MerkelMain::enterBid()
{
std::cout << "Place a Bid: Enter details to make a bid on the exchange." << std::endl;
}
void MerkelMain::printWallet()
{
std::cout << "Print Wallet: Display information about your wallet." << std::endl;
std::cout << wallet.toString() << std::endl;
}
void MerkelMain::goToNextWallet()
{
std::cout << "Continue: Proceed to the next step." << std::endl;
std::vector<OrderBookEntry> sales = orderBook.matchAsksToBids("ETH/BTC", currentTime);
for (OrderBookEntry& sale : sales)
{
std::cout << "Sale price: " << sale.price << " amount: " << sale.amount << std::endl;
}
currentTime = orderBook.getNextTime(currentTime);
}
void MerkelMain::processUserOption(int userOption){
if (userOption == 1)
{
printHelp();
}
if (userOption == 2)
{
printMarketStats();
}
if (userOption == 3)
{
enterBid();
}
if (userOption == 4)
{
enterAsk();
}
if (userOption == 5)
{
printWallet();
}
if (userOption == 6)
{
goToNextWallet();
}
if (userOption < 1 || userOption > 6)
{
std::cout << "Invalid Option - Please choose a number between 1-6" << std::endl;
}
}