diff --git a/CM2005 Object Oriented Programming/Midterm/Merkelrex-TradingBot/MerkelMain.cpp b/CM2005 Object Oriented Programming/Midterm/Merkelrex-TradingBot/MerkelMain.cpp index 25ac13c..ff9c909 100644 --- a/CM2005 Object Oriented Programming/Midterm/Merkelrex-TradingBot/MerkelMain.cpp +++ b/CM2005 Object Oriented Programming/Midterm/Merkelrex-TradingBot/MerkelMain.cpp @@ -6,7 +6,17 @@ MerkelMain::MerkelMain() { + currentTime = orderBook.getEarliestTime(); +} +std::string MerkelMain::getCurrentTime() +{ + return currentTime; +} + +std::vector 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 entries = orderBook.getOrders(OrderBookType::ask, - p, currentTime); + std::vector 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 sales = orderBook.matchAsksToBids(p, currentTime); + std::vector 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 MerkelMain::getCurrentAsks() +{ + std::vector currentAsks; + for (const std::string &prod : orderBook.getKnownProducts()) + { + std::vector thisProductAsks = orderBook.getOrders(OrderBookType::ask, prod, currentTime); + currentAsks.insert(currentAsks.end(), thisProductAsks.begin(), thisProductAsks.end()); + } + return currentAsks; +} + +std::vector MerkelMain::getCurrentBids() +{ + std::vector currentBids; + for (const std::string &prod : orderBook.getKnownProducts()) + { + std::vector thisProductBids = orderBook.getOrders(OrderBookType::bid, prod, currentTime); + currentBids.insert(currentBids.end(), thisProductBids.begin(), thisProductBids.end()); + } + return currentBids; } diff --git a/CM2005 Object Oriented Programming/Midterm/Merkelrex-TradingBot/MerkelMain.h b/CM2005 Object Oriented Programming/Midterm/Merkelrex-TradingBot/MerkelMain.h index 921b1b9..c97ef44 100644 --- a/CM2005 Object Oriented Programming/Midterm/Merkelrex-TradingBot/MerkelMain.h +++ b/CM2005 Object Oriented Programming/Midterm/Merkelrex-TradingBot/MerkelMain.h @@ -5,28 +5,40 @@ #include "OrderBook.h" #include "Wallet.h" - class MerkelMain { - public: - MerkelMain(); - /** 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); +public: + MerkelMain(); + /** Call this to start the sim */ - std::string currentTime; + /** Returns the current time */ + std::string getCurrentTime(); + + /** Returns the list of known products */ + std::vector getKnownProducts(); + + /** returns a vector of all asks orders for the current time */ + std::vector getCurrentAsks(); - OrderBook orderBook{"20200317.csv"}; - //OrderBook orderBook{"20200601.csv"}; - Wallet wallet; + /** returns a vector of all bids orders for the current time */ + std::vector getCurrentBids(); + 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; };