diff --git a/CM2005 Object Oriented Programming/Topic 5/5.3.5/20200317.csv b/CM2005 Object Oriented Programming/Topic 5/5.3.5/20200317.csv index 3752ce3..44be9b5 100644 --- a/CM2005 Object Oriented Programming/Topic 5/5.3.5/20200317.csv +++ b/CM2005 Object Oriented Programming/Topic 5/5.3.5/20200317.csv @@ -1,4 +1,4 @@ -2020/03/17 17:01:24.884492,ETH/BTC,bid,0.021873a78,a.44564869 +2020/03/17 17:01:24.884492,ETH/BTC,bid,0.02187a78,2.44564869 2020/03/17 17:01:24.884492,ETH/BTC,bid,0.02187308,7.44564869 2020/03/17 17:01:24.884492,ETH/BTC,bid,0.02187307,3.467434 2020/03/17 17:01:24.884492,ETH/BTC,bid,0.02187305,6.85567013 @@ -202,11 +202,7 @@ 2020/03/17 17:01:24.884492,BTC/USDT,bid,5323.83418,0.001 2020/03/17 17:01:24.884492,BTC/USDT,bid,5323.25277012,1.97008162 2020/03/17 17:01:24.884492,BTC/USDT,bid,5321.,3.35 - - 2020/03/17 17:01:24.884492,BTC/USDT,bid,5319.450228,0.00020075 - - 2020/03/17 17:01:24.884492,BTC/USDT,bid,5315.6477491,0.00027634 2020/03/17 17:01:24.884492,BTC/USDT,bid,5315.22320561,0.0064523 2020/03/17 17:01:24.884492,BTC/USDT,bid,5314.15443661,0.000268 @@ -265,11 +261,7 @@ 2020/03/17 17:01:24.884492,BTC/USDT,ask,5396.42871506,14.1318 2020/03/17 17:01:24.884492,BTC/USDT,ask,5400.,0.14371419 2020/03/17 17:01:24.884492,BTC/USDT,ask,5400.33151632,0.0384 - - 2020/03/17 17:01:24.884492,BTC/USDT,ask,5405.41766912,0.69895055 - - 2020/03/17 17:01:24.884492,BTC/USDT,ask,5405.41766915,0.00423615 2020/03/17 17:01:24.884492,BTC/USDT,ask,5406.30673519,0.01366 2020/03/17 17:01:24.884492,BTC/USDT,ask,5406.52042159,2.92760636 diff --git a/CM2005 Object Oriented Programming/Topic 5/5.3.5/MerkelMain.cpp b/CM2005 Object Oriented Programming/Topic 5/5.3.5/MerkelMain.cpp index c024c54..79e1894 100644 --- a/CM2005 Object Oriented Programming/Topic 5/5.3.5/MerkelMain.cpp +++ b/CM2005 Object Oriented Programming/Topic 5/5.3.5/MerkelMain.cpp @@ -159,6 +159,10 @@ void MerkelMain::gotoNextTimeframe() for (OrderBookEntry& sale : sales) { std::cout << "Sale price: " << sale.price << " amount " << sale.amount << std::endl; + if(sale.username == "simuser") + { + wallet.processSale(sale); + } } } currentTime = orderBook.getNextTime(currentTime); diff --git a/CM2005 Object Oriented Programming/Topic 5/5.3.5/OrderBook.cpp b/CM2005 Object Oriented Programming/Topic 5/5.3.5/OrderBook.cpp index 9102b77..40db8cc 100644 --- a/CM2005 Object Oriented Programming/Topic 5/5.3.5/OrderBook.cpp +++ b/CM2005 Object Oriented Programming/Topic 5/5.3.5/OrderBook.cpp @@ -133,7 +133,7 @@ std::vector OrderBook::matchAsksToBids(std::string product, std: // if bid.price >= ask.price # we have a match if (bid.price >= ask.price) { - std::cout << "bid price is right " << std::endl; + //std::cout << "bid price is right " << std::endl; OrderBookEntry sale{ask.price, 0, timestamp, product, OrderBookType::asksale}; OrderBookType type = OrderBookType::asksale; if(bid.username == "simuser") @@ -183,7 +183,7 @@ std::vector OrderBook::matchAsksToBids(std::string product, std: // if bid.amount < ask.amount # bid is completely gone, slice the ask - if (bid.amount < ask.amount) + if (bid.amount < ask.amount && bid.amount != 0) { // sale.amount = bid.amount sale.amount = bid.amount; diff --git a/CM2005 Object Oriented Programming/Topic 5/5.3.5/Wallet.cpp b/CM2005 Object Oriented Programming/Topic 5/5.3.5/Wallet.cpp index b4addab..0b12e28 100644 --- a/CM2005 Object Oriented Programming/Topic 5/5.3.5/Wallet.cpp +++ b/CM2005 Object Oriented Programming/Topic 5/5.3.5/Wallet.cpp @@ -89,4 +89,30 @@ bool Wallet::canFulfillOrder(OrderBookEntry order) return containsCurrency(currency, amount); } return false; +} + +void Wallet::processSale(OrderBookEntry& sale) +{ + std::vector currs = CSVReader::tokenise(sale.product, '/'); + // ask + if(sale.orderType == OrderBookType::asksale) + { + double outAmount = sale.amount; + std::string outCurrency = currs[0]; + double incAmount = sale.amount * sale.price; + std::string incCurrency = currs[1]; + + currencies[incCurrency] += incAmount; + currencies[outCurrency] -= outAmount; + } + if(sale.orderType == OrderBookType::bidsale) + { + double incAmount = sale.amount; + std::string incCurrency = currs[0]; + double outAmount = sale.amount * sale.price; + std::string outCurrency = currs[1]; + + currencies[incCurrency] += incAmount; + currencies[outCurrency] -= outAmount; + } } \ No newline at end of file diff --git a/CM2005 Object Oriented Programming/Topic 5/5.3.5/Wallet.h b/CM2005 Object Oriented Programming/Topic 5/5.3.5/Wallet.h index 57776f8..cbdf18c 100644 --- a/CM2005 Object Oriented Programming/Topic 5/5.3.5/Wallet.h +++ b/CM2005 Object Oriented Programming/Topic 5/5.3.5/Wallet.h @@ -20,6 +20,9 @@ class Wallet /** check if the wallet can cope with the ask or bid */ bool canFulfillOrder(OrderBookEntry order); + /** update the contents of the wallet * assumes the order was amade by the owner of the wallet */ + void processSale(OrderBookEntry& sale); + /** generate a string representation of the wallet */ std::string toString(); diff --git a/CM2005 Object Oriented Programming/Topic 5/5.3.5/a.exe b/CM2005 Object Oriented Programming/Topic 5/5.3.5/a.exe index 384aa2d..f30c0d8 100644 Binary files a/CM2005 Object Oriented Programming/Topic 5/5.3.5/a.exe and b/CM2005 Object Oriented Programming/Topic 5/5.3.5/a.exe differ