Update implemented getCurrentAsks and getCurrentBids

This commit is contained in:
Lev
2021-07-02 17:24:59 -05:00
parent bb6aa4a02e
commit 719d345291
2 changed files with 114 additions and 69 deletions

View File

@ -6,7 +6,17 @@
MerkelMain::MerkelMain()
{
currentTime = orderBook.getEarliestTime();
}
std::string MerkelMain::getCurrentTime()
{
return currentTime;
}
std::vector<std::string> MerkelMain::getKnownProducts()
{
return orderBook.getKnownProducts();
}
void MerkelMain::init()
@ -16,7 +26,7 @@ void MerkelMain::init()
wallet.insertCurrency("BTC", 10);
while(true)
while (true)
{
printMenu();
input = getUserOption();
@ -24,7 +34,6 @@ void MerkelMain::init()
}
}
void MerkelMain::printMenu()
{
// 1 print help
@ -33,11 +42,11 @@ void MerkelMain::printMenu()
std::cout << "2: Print exchange stats" << std::endl;
// 3 make an offer
std::cout << "3: Make an offer " << std::endl;
// 4 make a bid
// 4 make a bid
std::cout << "4: Make a bid " << std::endl;
// 5 print wallet
std::cout << "5: Print wallet " << std::endl;
// 6 continue
// 6 continue
std::cout << "6: Continue " << std::endl;
std::cout << "============== " << std::endl;
@ -52,17 +61,13 @@ void MerkelMain::printHelp()
void MerkelMain::printMarketStats()
{
for (std::string const& p : orderBook.getKnownProducts())
for (std::string const &p : orderBook.getKnownProducts())
{
std::cout << "Product: " << p << std::endl;
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask,
p, currentTime);
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 << "OrderBook contains : " << orders.size() << " entries" << std::endl;
// unsigned int bids = 0;
@ -76,10 +81,9 @@ void MerkelMain::printMarketStats()
// if (e.orderType == OrderBookType::bid)
// {
// bids ++;
// }
// }
// }
// }
// std::cout << "OrderBook asks: " << asks << " bids:" << bids << std::endl;
}
void MerkelMain::enterAsk()
@ -93,28 +97,31 @@ void MerkelMain::enterAsk()
{
std::cout << "MerkelMain::enterAsk Bad input! " << input << std::endl;
}
else {
try {
else
{
try
{
OrderBookEntry obe = CSVReader::stringsToOBE(
tokens[1],
tokens[2],
currentTime,
tokens[0],
OrderBookType::ask
);
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 {
else
{
std::cout << "Wallet has insufficient funds . " << std::endl;
}
}catch (const std::exception& e)
}
catch (const std::exception &e)
{
std::cout << " MerkelMain::enterAsk Bad input " << std::endl;
}
}
}
}
@ -129,15 +136,16 @@ void MerkelMain::enterBid()
{
std::cout << "MerkelMain::enterBid Bad input! " << input << std::endl;
}
else {
try {
else
{
try
{
OrderBookEntry obe = CSVReader::stringsToOBE(
tokens[1],
tokens[2],
currentTime,
tokens[0],
OrderBookType::bid
);
tokens[2],
currentTime,
tokens[0],
OrderBookType::bid);
obe.username = "simuser";
if (wallet.canFulfillOrder(obe))
@ -145,13 +153,15 @@ void MerkelMain::enterBid()
std::cout << "Wallet looks good. " << std::endl;
orderBook.insertOrder(obe);
}
else {
else
{
std::cout << "Wallet has insufficient funds . " << std::endl;
}
}catch (const std::exception& e)
}
catch (const std::exception &e)
{
std::cout << " MerkelMain::enterBid Bad input " << std::endl;
}
}
}
}
@ -159,41 +169,42 @@ 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::vector<OrderBookEntry> sales = orderBook.matchAsksToBids(p, currentTime);
std::cout << "Sales: " << sales.size() << std::endl;
for (OrderBookEntry& sale : sales)
for (OrderBookEntry &sale : sales)
{
std::cout << "Sale price: " << sale.price << " amount " << sale.amount << std::endl;
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);
}
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)
try
{
//
userOption = std::stoi(line);
}
catch (const std::exception &e)
{
//
}
std::cout << "You chose: " << userOption << std::endl;
return userOption;
@ -205,28 +216,50 @@ void MerkelMain::processUserOption(int userOption)
{
std::cout << "Invalid choice. Choose 1-6" << std::endl;
}
if (userOption == 1)
if (userOption == 1)
{
printHelp();
}
if (userOption == 2)
if (userOption == 2)
{
printMarketStats();
}
if (userOption == 3)
if (userOption == 3)
{
enterAsk();
}
if (userOption == 4)
if (userOption == 4)
{
enterBid();
}
if (userOption == 5)
if (userOption == 5)
{
printWallet();
}
if (userOption == 6)
if (userOption == 6)
{
gotoNextTimeframe();
}
}
}
std::vector<OrderBookEntry> MerkelMain::getCurrentAsks()
{
std::vector<OrderBookEntry> currentAsks;
for (const std::string &prod : orderBook.getKnownProducts())
{
std::vector<OrderBookEntry> thisProductAsks = orderBook.getOrders(OrderBookType::ask, prod, currentTime);
currentAsks.insert(currentAsks.end(), thisProductAsks.begin(), thisProductAsks.end());
}
return currentAsks;
}
std::vector<OrderBookEntry> MerkelMain::getCurrentBids()
{
std::vector<OrderBookEntry> currentBids;
for (const std::string &prod : orderBook.getKnownProducts())
{
std::vector<OrderBookEntry> thisProductBids = orderBook.getOrders(OrderBookType::bid, prod, currentTime);
currentBids.insert(currentBids.end(), thisProductBids.begin(), thisProductBids.end());
}
return currentBids;
}