diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/CSVReader.cpp b/CM2005 Object Oriented Programming/Midterm/Midterm Project/CSVReader.cpp index 8f1947d..a31c423 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/CSVReader.cpp +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/CSVReader.cpp @@ -2,10 +2,8 @@ #include #include - CSVReader::CSVReader() { - } std::vector CSVReader::readCSV(std::string csvFilename) @@ -16,38 +14,44 @@ std::vector CSVReader::readCSV(std::string csvFilename) std::string line; if (csvFile.is_open()) { - while(std::getline(csvFile, line)) + while (std::getline(csvFile, line)) { - try { + try + { OrderBookEntry obe = stringsToOBE(tokenise(line, ',')); entries.push_back(obe); - }catch(const std::exception& e) - { - std::cout << "CSVReader::readCSV bad data" << std::endl; } - }// end of while - } + catch (const std::exception &e) + { + std::cout << "CSVReader::readCSV bad data" << std::endl; + } + } // end of while + } - std::cout << "CSVReader::readCSV read " << entries.size() << " entries" << std::endl; - return entries; + std::cout << "CSVReader::readCSV read " << entries.size() << " entries" << std::endl; + return entries; } std::vector CSVReader::tokenise(std::string csvLine, char separator) { - std::vector tokens; - signed int start, end; - std::string token; + std::vector tokens; + signed int start, end; + std::string token; start = csvLine.find_first_not_of(separator, 0); - do{ + do + { end = csvLine.find_first_of(separator, start); - if (start == csvLine.length() || start == end) break; - if (end >= 0) token = csvLine.substr(start, end - start); - else token = csvLine.substr(start, csvLine.length() - start); + if (start == csvLine.length() || start == end) + break; + if (end >= 0) + token = csvLine.substr(start, end - start); + else + token = csvLine.substr(start, csvLine.length() - start); tokens.push_back(token); - start = end + 1; - }while(end > 0); + start = end + 1; + } while (end > 0); - return tokens; + return tokens; } OrderBookEntry CSVReader::stringsToOBE(std::vector tokens) @@ -60,46 +64,50 @@ OrderBookEntry CSVReader::stringsToOBE(std::vector tokens) throw std::exception{}; } // we have 5 tokens - try { - price = std::stod(tokens[3]); - amount = std::stod(tokens[4]); - }catch(const std::exception& e){ - std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[3]<< std::endl; - std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[4]<< std::endl; - throw; + try + { + price = std::stod(tokens[3]); + amount = std::stod(tokens[4]); + } + catch (const std::exception &e) + { + std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[3] << std::endl; + std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[4] << std::endl; + throw; } - OrderBookEntry obe{price, - amount, - tokens[0], - tokens[1], - OrderBookEntry::stringToOrderBookType(tokens[2])}; + OrderBookEntry obe{price, + amount, + tokens[0], + tokens[1], + OrderBookEntry::stringToOrderBookType(tokens[2])}; + + return obe; +} + +OrderBookEntry CSVReader::stringsToOBE(std::string priceString, + std::string amountString, + std::string timestamp, + std::string product, + OrderBookType orderType) +{ + double price, amount; + try + { + price = std::stod(priceString); + amount = std::stod(amountString); + } + catch (const std::exception &e) + { + std::cout << "CSVReader::stringsToOBE Bad float! " << priceString << std::endl; + std::cout << "CSVReader::stringsToOBE Bad float! " << amountString << std::endl; + throw; + } + OrderBookEntry obe{price, + amount, + timestamp, + product, + orderType}; - return obe; -} - - -OrderBookEntry CSVReader::stringsToOBE(std::string priceString, - std::string amountString, - std::string timestamp, - std::string product, - OrderBookType orderType) -{ - double price, amount; - try { - price = std::stod(priceString); - amount = std::stod(amountString); - }catch(const std::exception& e){ - std::cout << "CSVReader::stringsToOBE Bad float! " << priceString<< std::endl; - std::cout << "CSVReader::stringsToOBE Bad float! " << amountString<< std::endl; - throw; - } - OrderBookEntry obe{price, - amount, - timestamp, - product, - orderType}; - return obe; } - \ No newline at end of file diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/CSVReader.h b/CM2005 Object Oriented Programming/Midterm/Midterm Project/CSVReader.h index 789ac3f..695a5f2 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/CSVReader.h +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/CSVReader.h @@ -4,22 +4,20 @@ #include #include - class CSVReader { - public: - CSVReader(); +public: + CSVReader(); - static std::vector readCSV(std::string csvFile); - static std::vector tokenise(std::string csvLine, char separator); - - static OrderBookEntry stringsToOBE(std::string price, - std::string amount, - std::string timestamp, - std::string product, - OrderBookType OrderBookType); + static std::vector readCSV(std::string csvFile); + static std::vector tokenise(std::string csvLine, char separator); - private: - static OrderBookEntry stringsToOBE(std::vector strings); - + static OrderBookEntry stringsToOBE(std::string price, + std::string amount, + std::string timestamp, + std::string product, + OrderBookType OrderBookType); + +private: + static OrderBookEntry stringsToOBE(std::vector strings); }; \ No newline at end of file diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/MerkelMain.cpp b/CM2005 Object Oriented Programming/Midterm/Midterm Project/MerkelMain.cpp index 25ac13c..c500c9f 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/MerkelMain.cpp +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/MerkelMain.cpp @@ -6,7 +6,6 @@ MerkelMain::MerkelMain() { - } void MerkelMain::init() @@ -16,7 +15,7 @@ void MerkelMain::init() wallet.insertCurrency("BTC", 10); - while(true) + while (true) { printMenu(); input = getUserOption(); @@ -24,7 +23,6 @@ void MerkelMain::init() } } - void MerkelMain::printMenu() { // 1 print help @@ -33,11 +31,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,34 +50,15 @@ 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; - // unsigned int asks = 0; - // for (OrderBookEntry& e : orders) - // { - // if (e.orderType == OrderBookType::ask) - // { - // asks ++; - // } - // if (e.orderType == OrderBookType::bid) - // { - // bids ++; - // } - // } - // std::cout << "OrderBook asks: " << asks << " bids:" << bids << std::endl; - } void MerkelMain::enterAsk() @@ -93,28 +72,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 +111,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 +128,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 +144,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 +191,28 @@ 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(); - } + } } diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/MerkelMain.h b/CM2005 Object Oriented Programming/Midterm/Midterm Project/MerkelMain.h index daee918..22f1883 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/MerkelMain.h +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/MerkelMain.h @@ -5,28 +5,27 @@ #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 */ + void init(); - std::string currentTime; +private: + void printMenu(); + void printHelp(); + void printMarketStats(); + void enterAsk(); + void enterBid(); + void printWallet(); + void gotoNextTimeframe(); + int getUserOption(); + void processUserOption(int userOption); -// OrderBook orderBook{"20200317.csv"}; - OrderBook orderBook{"20200601.csv"}; - Wallet wallet; + std::string currentTime; + // OrderBook orderBook{"20200317.csv"}; + OrderBook orderBook{"20200601.csv"}; + Wallet wallet; }; diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBook.cpp b/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBook.cpp index 6c46766..de7d5ab 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBook.cpp +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBook.cpp @@ -4,7 +4,6 @@ #include #include - /** construct, reading a csv data file */ OrderBook::OrderBook(std::string filename) { @@ -16,15 +15,15 @@ std::vector OrderBook::getKnownProducts() { std::vector products; - std::map prodMap; + std::map prodMap; - for (OrderBookEntry& e : orders) + for (OrderBookEntry &e : orders) { prodMap[e.product] = true; } - + // now flatten the map to a vector of strings - for (auto const& e : prodMap) + for (auto const &e : prodMap) { products.push_back(e.first); } @@ -32,41 +31,41 @@ std::vector OrderBook::getKnownProducts() return products; } /** return vector of Orders according to the sent filters*/ -std::vector OrderBook::getOrders(OrderBookType type, - std::string product, - std::string timestamp) +std::vector OrderBook::getOrders(OrderBookType type, + std::string product, + std::string timestamp) { std::vector orders_sub; - for (OrderBookEntry& e : orders) + for (OrderBookEntry &e : orders) { - if (e.orderType == type && - e.product == product && - e.timestamp == timestamp ) - { - orders_sub.push_back(e); - } + if (e.orderType == type && + e.product == product && + e.timestamp == timestamp) + { + orders_sub.push_back(e); + } } return orders_sub; } - -double OrderBook::getHighPrice(std::vector& orders) +double OrderBook::getHighPrice(std::vector &orders) { double max = orders[0].price; - for (OrderBookEntry& e : orders) + for (OrderBookEntry &e : orders) { - if (e.price > max)max = e.price; + if (e.price > max) + max = e.price; } return max; } - -double OrderBook::getLowPrice(std::vector& orders) +double OrderBook::getLowPrice(std::vector &orders) { double min = orders[0].price; - for (OrderBookEntry& e : orders) + for (OrderBookEntry &e : orders) { - if (e.price < min)min = e.price; + if (e.price < min) + min = e.price; } return min; } @@ -79,9 +78,9 @@ std::string OrderBook::getEarliestTime() std::string OrderBook::getNextTime(std::string timestamp) { std::string next_timestamp = ""; - for (OrderBookEntry& e : orders) + for (OrderBookEntry &e : orders) { - if (e.timestamp > timestamp) + if (e.timestamp > timestamp) { next_timestamp = e.timestamp; break; @@ -94,7 +93,7 @@ std::string OrderBook::getNextTime(std::string timestamp) return next_timestamp; } -void OrderBook::insertOrder(OrderBookEntry& order) +void OrderBook::insertOrder(OrderBookEntry &order) { orders.push_back(order); std::sort(orders.begin(), orders.end(), OrderBookEntry::compareByTimestamp); @@ -102,17 +101,17 @@ void OrderBook::insertOrder(OrderBookEntry& order) std::vector OrderBook::matchAsksToBids(std::string product, std::string timestamp) { -// asks = orderbook.asks - std::vector asks = getOrders(OrderBookType::ask, - product, + // asks = orderbook.asks + std::vector asks = getOrders(OrderBookType::ask, + product, + timestamp); + // bids = orderbook.bids + std::vector bids = getOrders(OrderBookType::bid, + product, timestamp); -// bids = orderbook.bids - std::vector bids = getOrders(OrderBookType::bid, - product, - timestamp); // sales = [] - std::vector sales; + std::vector sales; // I put in a little check to ensure we have bids and asks // to process. @@ -127,24 +126,24 @@ std::vector OrderBook::matchAsksToBids(std::string product, std: // sort bids highest first std::sort(bids.begin(), bids.end(), OrderBookEntry::compareByPriceDesc); // for ask in asks: - std::cout << "max ask " << asks[asks.size()-1].price << std::endl; + std::cout << "max ask " << asks[asks.size() - 1].price << std::endl; std::cout << "min ask " << asks[0].price << std::endl; std::cout << "max bid " << bids[0].price << std::endl; - std::cout << "min bid " << bids[bids.size()-1].price << std::endl; - - for (OrderBookEntry& ask : asks) + std::cout << "min bid " << bids[bids.size() - 1].price << std::endl; + + for (OrderBookEntry &ask : asks) { - // for bid in bids: - for (OrderBookEntry& bid : bids) + // for bid in bids: + for (OrderBookEntry &bid : bids) { - // if bid.price >= ask.price # we have a match + // if bid.price >= ask.price # we have a match if (bid.price >= ask.price) { - // sale = new order() - // sale.price = ask.price - OrderBookEntry sale{ask.price, 0, timestamp, - product, - OrderBookType::asksale}; + // sale = new order() + // sale.price = ask.price + OrderBookEntry sale{ask.price, 0, timestamp, + product, + OrderBookType::asksale}; if (bid.username == "simuser") { @@ -154,63 +153,62 @@ std::vector OrderBook::matchAsksToBids(std::string product, std: if (ask.username == "simuser") { sale.username = "simuser"; - sale.orderType = OrderBookType::asksale; + sale.orderType = OrderBookType::asksale; } - - // # now work out how much was sold and - // # create new bids and asks covering - // # anything that was not sold - // if bid.amount == ask.amount: # bid completely clears ask + + // # now work out how much was sold and + // # create new bids and asks covering + // # anything that was not sold + // if bid.amount == ask.amount: # bid completely clears ask if (bid.amount == ask.amount) { - // sale.amount = ask.amount + // sale.amount = ask.amount sale.amount = ask.amount; - // sales.append(sale) + // sales.append(sale) sales.push_back(sale); - // bid.amount = 0 # make sure the bid is not processed again + // bid.amount = 0 # make sure the bid is not processed again bid.amount = 0; - // # can do no more with this ask - // # go onto the next ask - // break + // # can do no more with this ask + // # go onto the next ask + // break break; } - // if bid.amount > ask.amount: # ask is completely gone slice the bid + // if bid.amount > ask.amount: # ask is completely gone slice the bid if (bid.amount > ask.amount) { - // sale.amount = ask.amount + // sale.amount = ask.amount sale.amount = ask.amount; - // sales.append(sale) + // sales.append(sale) sales.push_back(sale); - // # we adjust the bid in place - // # so it can be used to process the next ask - // bid.amount = bid.amount - ask.amount - bid.amount = bid.amount - ask.amount; - // # ask is completely gone, so go to next ask - // break + // # we adjust the bid in place + // # so it can be used to process the next ask + // bid.amount = bid.amount - ask.amount + bid.amount = bid.amount - ask.amount; + // # ask is completely gone, so go to next ask + // break break; } - - // if bid.amount < ask.amount # bid is completely gone, slice the ask - if (bid.amount < ask.amount && - bid.amount > 0) + // if bid.amount < ask.amount # bid is completely gone, slice the ask + if (bid.amount < ask.amount && + bid.amount > 0) { - // sale.amount = bid.amount + // sale.amount = bid.amount sale.amount = bid.amount; - // sales.append(sale) + // sales.append(sale) sales.push_back(sale); - // # update the ask - // # and allow further bids to process the remaining amount - // ask.amount = ask.amount - bid.amount + // # update the ask + // # and allow further bids to process the remaining amount + // ask.amount = ask.amount - bid.amount ask.amount = ask.amount - bid.amount; - // bid.amount = 0 # make sure the bid is not processed again + // bid.amount = 0 # make sure the bid is not processed again bid.amount = 0; - // # some ask remains so go to the next bid - // continue + // # some ask remains so go to the next bid + // continue continue; } } } } - return sales; + return sales; } diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBook.h b/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBook.h index a331747..c8269a4 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBook.h +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBook.h @@ -6,33 +6,31 @@ class OrderBook { - public: +public: /** construct, reading a csv data file */ - OrderBook(std::string filename); + OrderBook(std::string filename); /** return vector of all know products in the dataset*/ - std::vector getKnownProducts(); + std::vector getKnownProducts(); /** return vector of Orders according to the sent filters*/ - std::vector getOrders(OrderBookType type, - std::string product, - std::string timestamp); + std::vector getOrders(OrderBookType type, + std::string product, + std::string timestamp); - /** returns the earliest time in the orderbook*/ - std::string getEarliestTime(); - /** returns the next time after the + /** returns the earliest time in the orderbook*/ + std::string getEarliestTime(); + /** returns the next time after the * sent time in the orderbook * If there is no next timestamp, wraps around to the start * */ - std::string getNextTime(std::string timestamp); + std::string getNextTime(std::string timestamp); - void insertOrder(OrderBookEntry& order); + void insertOrder(OrderBookEntry &order); - std::vector matchAsksToBids(std::string product, std::string timestamp); - - static double getHighPrice(std::vector& orders); - static double getLowPrice(std::vector& orders); - - private: - std::vector orders; + std::vector matchAsksToBids(std::string product, std::string timestamp); + static double getHighPrice(std::vector &orders); + static double getLowPrice(std::vector &orders); +private: + std::vector orders; }; diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBookEntry.cpp b/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBookEntry.cpp index 48a6957..7af218f 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBookEntry.cpp +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBookEntry.cpp @@ -1,20 +1,20 @@ #include "OrderBookEntry.h" -OrderBookEntry::OrderBookEntry( double _price, - double _amount, - std::string _timestamp, - std::string _product, - OrderBookType _orderType, - std::string _username) -: price(_price), - amount(_amount), - timestamp(_timestamp), - product(_product), - orderType(_orderType), - username(_username) +OrderBookEntry::OrderBookEntry( + double _price, + double _amount, + std::string _timestamp, + std::string _product, + OrderBookType _orderType, + std::string _username) + : + price(_price), + amount(_amount), + timestamp(_timestamp), + product(_product), + orderType(_orderType), + username(_username) { - - } OrderBookType OrderBookEntry::stringToOrderBookType(std::string s) diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBookEntry.h b/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBookEntry.h index 0ba0a56..c765b62 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBookEntry.h +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/OrderBookEntry.h @@ -2,39 +2,44 @@ #include -enum class OrderBookType{bid, ask, unknown, asksale, bidsale}; +enum class OrderBookType +{ + bid, + ask, + unknown, + asksale, + bidsale +}; class OrderBookEntry { - public: - +public: + OrderBookEntry(double _price, + double _amount, + std::string _timestamp, + std::string _product, + OrderBookType _orderType, + std::string username = "dataset"); - OrderBookEntry( double _price, - double _amount, - std::string _timestamp, - std::string _product, - OrderBookType _orderType, - std::string username = "dataset"); + static OrderBookType stringToOrderBookType(std::string s); - static OrderBookType stringToOrderBookType(std::string s); + static bool compareByTimestamp(OrderBookEntry &e1, OrderBookEntry &e2) + { + return e1.timestamp < e2.timestamp; + } + static bool compareByPriceAsc(OrderBookEntry &e1, OrderBookEntry &e2) + { + return e1.price < e2.price; + } + static bool compareByPriceDesc(OrderBookEntry &e1, OrderBookEntry &e2) + { + return e1.price > e2.price; + } - static bool compareByTimestamp(OrderBookEntry& e1, OrderBookEntry& e2) - { - return e1.timestamp < e2.timestamp; - } - static bool compareByPriceAsc(OrderBookEntry& e1, OrderBookEntry& e2) - { - return e1.price < e2.price; - } - static bool compareByPriceDesc(OrderBookEntry& e1, OrderBookEntry& e2) - { - return e1.price > e2.price; - } - - double price; - double amount; - std::string timestamp; - std::string product; - OrderBookType orderType; - std::string username; + double price; + double amount; + std::string timestamp; + std::string product; + OrderBookType orderType; + std::string username; }; diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/TradingBot.cpp b/CM2005 Object Oriented Programming/Midterm/Midterm Project/TradingBot.cpp new file mode 100644 index 0000000..5ca74e1 --- /dev/null +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/TradingBot.cpp @@ -0,0 +1,20 @@ +#include "TradingBot.h" +#include + +Bot::Bot(std::string filename) +{ + orders = CSVReader::readCSV(filename); + Print(20); +} + +void Bot::Print(int intLimit) +{ + int i = 0; + for (OrderBookEntry &entry : orders) + { + std::cout << entry.product << " " << entry.price << " " << entry.amount << std::endl; + i++; + if (i == intLimit) + break; + } +} \ No newline at end of file diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/TradingBot.h b/CM2005 Object Oriented Programming/Midterm/Midterm Project/TradingBot.h new file mode 100644 index 0000000..3afcf02 --- /dev/null +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/TradingBot.h @@ -0,0 +1,15 @@ +#pragma once + +#include "OrderBook.h" +#include +//#include "CSVReader.h" + +class Bot +{ +public: + Bot(std::string filename); + void Print(int intLimit); + +private: + std::vector orders; +}; \ No newline at end of file diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/Wallet.cpp b/CM2005 Object Oriented Programming/Midterm/Midterm Project/Wallet.cpp index 2079dcd..359db12 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/Wallet.cpp +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/Wallet.cpp @@ -4,8 +4,6 @@ Wallet::Wallet() { - - } void Wallet::insertCurrency(std::string type, double amount) @@ -19,33 +17,35 @@ void Wallet::insertCurrency(std::string type, double amount) { balance = 0; } - else { // is there + else + { // is there balance = currencies[type]; } - balance += amount; - currencies[type] = balance; + balance += amount; + currencies[type] = balance; } bool Wallet::removeCurrency(std::string type, double amount) { if (amount < 0) { - return false; + return false; } if (currencies.count(type) == 0) // not there yet { //std::cout << "No currency for " << type << std::endl; return false; } - else { // is there - do we have enough - if (containsCurrency(type, amount))// we have enough + else + { // is there - do we have enough + if (containsCurrency(type, amount)) // we have enough { //std::cout << "Removing " << type << ": " << amount << std::endl; currencies[type] -= amount; return true; - } + } else // they have it but not enough. - return false; + return false; } } @@ -53,15 +53,14 @@ bool Wallet::containsCurrency(std::string type, double amount) { if (currencies.count(type) == 0) // not there yet return false; - else + else return currencies[type] >= amount; - } std::string Wallet::toString() { std::string s; - for (std::pair pair : currencies) + for (std::pair pair : currencies) { std::string currency = pair.first; double amount = pair.second; @@ -91,12 +90,10 @@ bool Wallet::canFulfillOrder(OrderBookEntry order) return containsCurrency(currency, amount); } - - return false; + return false; } - -void Wallet::processSale(OrderBookEntry& sale) +void Wallet::processSale(OrderBookEntry &sale) { std::vector currs = CSVReader::tokenise(sale.product, '/'); // ask @@ -109,7 +106,6 @@ void Wallet::processSale(OrderBookEntry& sale) currencies[incomingCurrency] += incomingAmount; currencies[outgoingCurrency] -= outgoingAmount; - } // bid if (sale.orderType == OrderBookType::bidsale) @@ -123,9 +119,8 @@ void Wallet::processSale(OrderBookEntry& sale) currencies[outgoingCurrency] -= outgoingAmount; } } -std::ostream& operator<<(std::ostream& os, Wallet& wallet) +std::ostream &operator<<(std::ostream &os, Wallet &wallet) { os << wallet.toString(); return os; } - diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/Wallet.h b/CM2005 Object Oriented Programming/Midterm/Midterm Project/Wallet.h index 7518270..8987a36 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/Wallet.h +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/Wallet.h @@ -5,36 +5,28 @@ #include "OrderBookEntry.h" #include -class Wallet +class Wallet { - public: - Wallet(); - /** insert currency to the wallet */ - void insertCurrency(std::string type, double amount); - /** remove currency from the wallet */ - bool removeCurrency(std::string type, double amount); - - /** check if the wallet contains this much currency or more */ - bool containsCurrency(std::string type, double amount); - /** checks if the wallet can cope with this ask or bid.*/ - bool canFulfillOrder(OrderBookEntry order); - /** update the contents of the wallet +public: + Wallet(); + /** insert currency to the wallet */ + void insertCurrency(std::string type, double amount); + /** remove currency from the wallet */ + bool removeCurrency(std::string type, double amount); + + /** check if the wallet contains this much currency or more */ + bool containsCurrency(std::string type, double amount); + /** checks if the wallet can cope with this ask or bid.*/ + bool canFulfillOrder(OrderBookEntry order); + /** update the contents of the wallet * assumes the order was made by the owner of the wallet */ - void processSale(OrderBookEntry& sale); + void processSale(OrderBookEntry &sale); + /** generate a string representation of the wallet */ + std::string toString(); + friend std::ostream &operator<<(std::ostream &os, Wallet &wallet); - /** generate a string representation of the wallet */ - std::string toString(); - friend std::ostream& operator<<(std::ostream& os, Wallet& wallet); - - - private: - std::map currencies; - +private: + std::map currencies; }; - - - - - diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/a.exe b/CM2005 Object Oriented Programming/Midterm/Midterm Project/a.exe new file mode 100644 index 0000000..81f9b16 Binary files /dev/null and b/CM2005 Object Oriented Programming/Midterm/Midterm Project/a.exe differ diff --git a/CM2005 Object Oriented Programming/Midterm/Midterm Project/main.cpp b/CM2005 Object Oriented Programming/Midterm/Midterm Project/main.cpp index 8d4066e..d0e0ec9 100644 --- a/CM2005 Object Oriented Programming/Midterm/Midterm Project/main.cpp +++ b/CM2005 Object Oriented Programming/Midterm/Midterm Project/main.cpp @@ -1,10 +1,11 @@ #include "Wallet.h" #include #include "MerkelMain.h" +#include "TradingBot.h" int main() -{ +{ MerkelMain app{}; - app.init(); - + Bot myBot("20200317.csv"); + //app.init(); }