Files
UoL/CM2005 Object Oriented Programming/Topic 4/4.5.8/MerkelMain.cpp
2021-06-12 10:36:26 -05:00

204 lines
5.6 KiB
C++

#include "MerkelMain.h"
#include <iostream>
#include <vector>
#include "OrderBookEntry.h"
#include "CSVReader.h"
#include <limits>
MerkelMain::MerkelMain()
{
}
void MerkelMain::init()
{
int input;
currentTime = orderBook.getEarliestTime();
while (true)
{
printMenu();
input = getUserOption();
processUserOption(input);
}
}
void MerkelMain::printMenu()
{
// 1 print help
std::cout << "1: Print help " << std::endl;
// 2 print exchange stats
std::cout << "2: Print exchange stats" << std::endl;
// 3 make an offer
std::cout << "3: Make an ask " << std::endl;
// 4 make a bid
std::cout << "4: Make a bid " << std::endl;
// 5 print wallet
std::cout << "5: Print wallet " << std::endl;
// 6 continue
std::cout << "6: Continue " << std::endl;
std::cout << "============== " << std::endl;
std::cout << "Current time is " << currentTime << std::endl;
}
void MerkelMain::printHelp()
{
std::cout << "Help - your aim is to make money. Analyse the market and make bids and offers. " << std::endl;
}
void MerkelMain::printMarketStats()
{
// for product Asks
// for (std::string const p : orderBook.getKnownProducts())
// {
// std::cout << "Product: " << p << std::endl;
// std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, p, currentTime);
// std::cout << "Asks for product " << p << " are " << entries.size() << std::endl;
// std::cout << "Max ask for " << p << " is " << orderBook.getHighPrice(entries) << std::endl;
// std::cout << "Min ask for " << p << " is " << orderBook.getLowPrice(entries) << std::endl;
// }
// for product Bids
for (std::string const p : orderBook.getKnownProducts())
{
std::cout << "Product: " << p << std::endl;
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::bid, p, currentTime);
std::cout << "Bids for product " << p << " are " << entries.size() << std::endl;
std::cout << "Max bid for " << p << " is " << orderBook.getHighPrice(entries) << std::endl;
std::cout << "Min bid for " << p << " is " << orderBook.getLowPrice(entries) << std::endl;
}
// Overall stats
// 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()
{
std::cout << "Make an ask (product, price, amount. e.g. 'ETH/BTC, 125, 0.5'): " << std::endl;
std::string input;
std::getline(std::cin, input);
std::vector<std::string> tokens = CSVReader::tokenise(input, ',');
if (tokens.size() != 3)
{
std::cout << "MerkelMain::enterAsk - Bad input!" << input << std::endl;
}
else
{
try
{
OrderBookEntry obe = CSVReader::stringsToOBE(tokens[1], tokens[2], currentTime, tokens[0], OrderBookType::ask);
orderBook.insertOrder(obe);
}
catch (const std::exception &e)
{
std::cout << "MerkelMain::enterAsk - Bad input!" << input << std::endl;
}
}
//std::cout << "You typed: " << input << std::endl;
}
void MerkelMain::enterBid()
{
std::cout << "Make a bid (product, price, amount. e.g. 'ETH/BTC, 150, 0.5'): " << std::endl;
std::string input;
std::getline(std::cin, input);
std::vector<std::string> tokens = CSVReader::tokenise(input, ',');
if (tokens.size() != 3)
{
std::cout << "MerkelMain::enterBid - Bad input!" << input << std::endl;
}
else
{
try
{
OrderBookEntry obe = CSVReader::stringsToOBE(tokens[1], tokens[2], currentTime, tokens[0], OrderBookType::bid);
orderBook.insertOrder(obe);
}
catch (const std::exception &e)
{
std::cout << "MerkelMain::enterAsk - Bad input!" << input << std::endl;
}
}
}
void MerkelMain::printWallet()
{
std::cout << "Your wallet is empty. " << std::endl;
}
void MerkelMain::gotoNextTimeframe()
{
std::cout << "Going to next time frame. " << std::endl;
std::vector<OrderBookEntry> sales = orderBook.matchAsksToBids("ETH/BTC", currentTime);
std::cout << "Sales: " << sales.size() << std::endl;
for(OrderBookEntry& sale : sales)
{
std::cout << "Sale price: " << sale.price << " Sale amount: " << sale.amount << std::endl;
}
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)
{
std::cout << "Invalid input!" << std::endl;
}
std::cout << "You chose: " << userOption << std::endl;
return userOption;
}
void MerkelMain::processUserOption(int userOption)
{
if (userOption == 0) // bad input
{
std::cout << "Invalid choice. Choose 1-6" << std::endl;
}
if (userOption == 1)
{
printHelp();
}
if (userOption == 2)
{
printMarketStats();
}
if (userOption == 3)
{
enterAsk();
}
if (userOption == 4)
{
enterBid();
}
if (userOption == 5)
{
printWallet();
}
if (userOption == 6)
{
gotoNextTimeframe();
}
}