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() MerkelMain::MerkelMain()
{ {
currentTime = orderBook.getEarliestTime();
}
std::string MerkelMain::getCurrentTime()
{
return currentTime;
}
std::vector<std::string> MerkelMain::getKnownProducts()
{
return orderBook.getKnownProducts();
} }
void MerkelMain::init() void MerkelMain::init()
@ -24,7 +34,6 @@ void MerkelMain::init()
} }
} }
void MerkelMain::printMenu() void MerkelMain::printMenu()
{ {
// 1 print help // 1 print help
@ -55,14 +64,10 @@ void MerkelMain::printMarketStats()
for (std::string const &p : orderBook.getKnownProducts()) for (std::string const &p : orderBook.getKnownProducts())
{ {
std::cout << "Product: " << p << std::endl; std::cout << "Product: " << p << std::endl;
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, p, currentTime);
p, currentTime);
std::cout << "Asks seen: " << entries.size() << std::endl; std::cout << "Asks seen: " << entries.size() << std::endl;
std::cout << "Max ask: " << OrderBook::getHighPrice(entries) << std::endl; std::cout << "Max ask: " << OrderBook::getHighPrice(entries) << std::endl;
std::cout << "Min ask: " << OrderBook::getLowPrice(entries) << std::endl; std::cout << "Min ask: " << OrderBook::getLowPrice(entries) << std::endl;
} }
// std::cout << "OrderBook contains : " << orders.size() << " entries" << std::endl; // std::cout << "OrderBook contains : " << orders.size() << " entries" << std::endl;
// unsigned int bids = 0; // unsigned int bids = 0;
@ -79,7 +84,6 @@ void MerkelMain::printMarketStats()
// } // }
// } // }
// std::cout << "OrderBook asks: " << asks << " bids:" << bids << std::endl; // std::cout << "OrderBook asks: " << asks << " bids:" << bids << std::endl;
} }
void MerkelMain::enterAsk() void MerkelMain::enterAsk()
@ -93,25 +97,28 @@ void MerkelMain::enterAsk()
{ {
std::cout << "MerkelMain::enterAsk Bad input! " << input << std::endl; std::cout << "MerkelMain::enterAsk Bad input! " << input << std::endl;
} }
else { else
try { {
try
{
OrderBookEntry obe = CSVReader::stringsToOBE( OrderBookEntry obe = CSVReader::stringsToOBE(
tokens[1], tokens[1],
tokens[2], tokens[2],
currentTime, currentTime,
tokens[0], tokens[0],
OrderBookType::ask OrderBookType::ask);
);
obe.username = "simuser"; obe.username = "simuser";
if (wallet.canFulfillOrder(obe)) if (wallet.canFulfillOrder(obe))
{ {
std::cout << "Wallet looks good. " << std::endl; std::cout << "Wallet looks good. " << std::endl;
orderBook.insertOrder(obe); orderBook.insertOrder(obe);
} }
else { else
{
std::cout << "Wallet has insufficient funds . " << std::endl; 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; std::cout << " MerkelMain::enterAsk Bad input " << std::endl;
} }
@ -129,15 +136,16 @@ void MerkelMain::enterBid()
{ {
std::cout << "MerkelMain::enterBid Bad input! " << input << std::endl; std::cout << "MerkelMain::enterBid Bad input! " << input << std::endl;
} }
else { else
try { {
try
{
OrderBookEntry obe = CSVReader::stringsToOBE( OrderBookEntry obe = CSVReader::stringsToOBE(
tokens[1], tokens[1],
tokens[2], tokens[2],
currentTime, currentTime,
tokens[0], tokens[0],
OrderBookType::bid OrderBookType::bid);
);
obe.username = "simuser"; obe.username = "simuser";
if (wallet.canFulfillOrder(obe)) if (wallet.canFulfillOrder(obe))
@ -145,10 +153,12 @@ void MerkelMain::enterBid()
std::cout << "Wallet looks good. " << std::endl; std::cout << "Wallet looks good. " << std::endl;
orderBook.insertOrder(obe); orderBook.insertOrder(obe);
} }
else { else
{
std::cout << "Wallet has insufficient funds . " << std::endl; 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; std::cout << " MerkelMain::enterBid Bad input " << std::endl;
} }
@ -177,7 +187,6 @@ void MerkelMain::gotoNextTimeframe()
wallet.processSale(sale); wallet.processSale(sale);
} }
} }
} }
currentTime = orderBook.getNextTime(currentTime); currentTime = orderBook.getNextTime(currentTime);
@ -189,9 +198,11 @@ int MerkelMain::getUserOption()
std::string line; std::string line;
std::cout << "Type in 1-6" << std::endl; std::cout << "Type in 1-6" << std::endl;
std::getline(std::cin, line); std::getline(std::cin, line);
try{ try
{
userOption = std::stoi(line); userOption = std::stoi(line);
}catch(const std::exception& e) }
catch (const std::exception &e)
{ {
// //
} }
@ -230,3 +241,25 @@ void MerkelMain::processUserOption(int userOption)
gotoNextTimeframe(); 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;
}

View File

@ -5,22 +5,35 @@
#include "OrderBook.h" #include "OrderBook.h"
#include "Wallet.h" #include "Wallet.h"
class MerkelMain class MerkelMain
{ {
public: public:
MerkelMain(); MerkelMain();
/** Call this to start the sim */ /** Call this to start the sim */
void init();
/** Returns the current time */
std::string getCurrentTime();
/** Returns the list of known products */
std::vector<std::string> getKnownProducts();
/** returns a vector of all asks orders for the current time */
std::vector<OrderBookEntry> getCurrentAsks();
/** returns a vector of all bids orders for the current time */
std::vector<OrderBookEntry> getCurrentBids();
void enterAsk();
private: private:
void printMenu(); void printMenu();
void printHelp(); void printHelp();
void printMarketStats(); void printMarketStats();
void enterAsk();
void enterBid(); void enterBid();
void printWallet(); void printWallet();
void gotoNextTimeframe(); void gotoNextTimeframe();
int getUserOption(); int getUserOption();
void init(); // initialization function for user interaction
void processUserOption(int userOption); void processUserOption(int userOption);
std::string currentTime; std::string currentTime;
@ -28,5 +41,4 @@ class MerkelMain
OrderBook orderBook{"20200317.csv"}; OrderBook orderBook{"20200317.csv"};
//OrderBook orderBook{"20200601.csv"}; //OrderBook orderBook{"20200601.csv"};
Wallet wallet; Wallet wallet;
}; };