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()
@ -16,7 +26,7 @@ void MerkelMain::init()
wallet.insertCurrency("BTC", 10); wallet.insertCurrency("BTC", 10);
while(true) while (true)
{ {
printMenu(); printMenu();
input = getUserOption(); input = getUserOption();
@ -24,7 +34,6 @@ void MerkelMain::init()
} }
} }
void MerkelMain::printMenu() void MerkelMain::printMenu()
{ {
// 1 print help // 1 print help
@ -33,11 +42,11 @@ void MerkelMain::printMenu()
std::cout << "2: Print exchange stats" << std::endl; std::cout << "2: Print exchange stats" << std::endl;
// 3 make an offer // 3 make an offer
std::cout << "3: Make an offer " << std::endl; std::cout << "3: Make an offer " << std::endl;
// 4 make a bid // 4 make a bid
std::cout << "4: Make a bid " << std::endl; std::cout << "4: Make a bid " << std::endl;
// 5 print wallet // 5 print wallet
std::cout << "5: Print wallet " << std::endl; std::cout << "5: Print wallet " << std::endl;
// 6 continue // 6 continue
std::cout << "6: Continue " << std::endl; std::cout << "6: Continue " << std::endl;
std::cout << "============== " << std::endl; std::cout << "============== " << std::endl;
@ -52,17 +61,13 @@ void MerkelMain::printHelp()
void MerkelMain::printMarketStats() 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;
@ -76,10 +81,9 @@ void MerkelMain::printMarketStats()
// if (e.orderType == OrderBookType::bid) // if (e.orderType == OrderBookType::bid)
// { // {
// bids ++; // bids ++;
// } // }
// } // }
// 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,28 +97,31 @@ 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,13 +153,15 @@ 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;
} }
} }
} }
@ -159,41 +169,42 @@ void MerkelMain::printWallet()
{ {
std::cout << wallet.toString() << std::endl; std::cout << wallet.toString() << std::endl;
} }
void MerkelMain::gotoNextTimeframe() void MerkelMain::gotoNextTimeframe()
{ {
std::cout << "Going to next time frame. " << std::endl; std::cout << "Going to next time frame. " << std::endl;
for (std::string p : orderBook.getKnownProducts()) for (std::string p : orderBook.getKnownProducts())
{ {
std::cout << "matching " << p << std::endl; 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; 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") if (sale.username == "simuser")
{ {
// update the wallet // update the wallet
wallet.processSale(sale); wallet.processSale(sale);
} }
} }
} }
currentTime = orderBook.getNextTime(currentTime); currentTime = orderBook.getNextTime(currentTime);
} }
int MerkelMain::getUserOption() int MerkelMain::getUserOption()
{ {
int userOption = 0; int userOption = 0;
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);
}catch(const std::exception& e)
{ {
// userOption = std::stoi(line);
}
catch (const std::exception &e)
{
//
} }
std::cout << "You chose: " << userOption << std::endl; std::cout << "You chose: " << userOption << std::endl;
return userOption; return userOption;
@ -205,28 +216,50 @@ void MerkelMain::processUserOption(int userOption)
{ {
std::cout << "Invalid choice. Choose 1-6" << std::endl; std::cout << "Invalid choice. Choose 1-6" << std::endl;
} }
if (userOption == 1) if (userOption == 1)
{ {
printHelp(); printHelp();
} }
if (userOption == 2) if (userOption == 2)
{ {
printMarketStats(); printMarketStats();
} }
if (userOption == 3) if (userOption == 3)
{ {
enterAsk(); enterAsk();
} }
if (userOption == 4) if (userOption == 4)
{ {
enterBid(); enterBid();
} }
if (userOption == 5) if (userOption == 5)
{ {
printWallet(); printWallet();
} }
if (userOption == 6) if (userOption == 6)
{ {
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,28 +5,40 @@
#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();
private:
void printMenu();
void printHelp();
void printMarketStats();
void enterAsk();
void enterBid();
void printWallet();
void gotoNextTimeframe();
int getUserOption();
void processUserOption(int userOption);
std::string currentTime; /** 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();
OrderBook orderBook{"20200317.csv"}; /** returns a vector of all bids orders for the current time */
//OrderBook orderBook{"20200601.csv"}; std::vector<OrderBookEntry> getCurrentBids();
Wallet wallet;
void enterAsk();
private:
void printMenu();
void printHelp();
void printMarketStats();
void enterBid();
void printWallet();
void gotoNextTimeframe();
int getUserOption();
void init(); // initialization function for user interaction
void processUserOption(int userOption);
std::string currentTime;
OrderBook orderBook{"20200317.csv"};
//OrderBook orderBook{"20200601.csv"};
Wallet wallet;
}; };