Update implemented getCurrentAsks and getCurrentBids
This commit is contained in:
@ -6,7 +6,17 @@
|
||||
|
||||
MerkelMain::MerkelMain()
|
||||
{
|
||||
currentTime = orderBook.getEarliestTime();
|
||||
}
|
||||
|
||||
std::string MerkelMain::getCurrentTime()
|
||||
{
|
||||
return currentTime;
|
||||
}
|
||||
|
||||
std::vector<std::string> 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
|
||||
@ -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<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask,
|
||||
p, currentTime);
|
||||
std::vector<OrderBookEntry> 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;
|
||||
@ -79,7 +84,6 @@ void MerkelMain::printMarketStats()
|
||||
// }
|
||||
// }
|
||||
// std::cout << "OrderBook asks: " << asks << " bids:" << bids << std::endl;
|
||||
|
||||
}
|
||||
|
||||
void MerkelMain::enterAsk()
|
||||
@ -93,25 +97,28 @@ 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
|
||||
);
|
||||
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
|
||||
);
|
||||
OrderBookType::bid);
|
||||
obe.username = "simuser";
|
||||
|
||||
if (wallet.canFulfillOrder(obe))
|
||||
@ -145,10 +153,12 @@ 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;
|
||||
}
|
||||
@ -168,7 +178,7 @@ void MerkelMain::gotoNextTimeframe()
|
||||
std::cout << "matching " << p << std::endl;
|
||||
std::vector<OrderBookEntry> 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;
|
||||
if (sale.username == "simuser")
|
||||
@ -177,7 +187,6 @@ void MerkelMain::gotoNextTimeframe()
|
||||
wallet.processSale(sale);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
currentTime = orderBook.getNextTime(currentTime);
|
||||
@ -189,9 +198,11 @@ int MerkelMain::getUserOption()
|
||||
std::string line;
|
||||
std::cout << "Type in 1-6" << std::endl;
|
||||
std::getline(std::cin, line);
|
||||
try{
|
||||
try
|
||||
{
|
||||
userOption = std::stoi(line);
|
||||
}catch(const std::exception& e)
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
//
|
||||
}
|
||||
@ -230,3 +241,25 @@ void MerkelMain::processUserOption(int userOption)
|
||||
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;
|
||||
}
|
||||
|
||||
@ -5,22 +5,35 @@
|
||||
#include "OrderBook.h"
|
||||
#include "Wallet.h"
|
||||
|
||||
|
||||
class MerkelMain
|
||||
{
|
||||
public:
|
||||
public:
|
||||
MerkelMain();
|
||||
/** Call this to start the sim */
|
||||
void init();
|
||||
private:
|
||||
|
||||
/** 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();
|
||||
|
||||
/** returns a vector of all bids orders for the current time */
|
||||
std::vector<OrderBookEntry> getCurrentBids();
|
||||
|
||||
void enterAsk();
|
||||
private:
|
||||
void printMenu();
|
||||
void printHelp();
|
||||
void printMarketStats();
|
||||
void enterAsk();
|
||||
|
||||
void enterBid();
|
||||
void printWallet();
|
||||
void gotoNextTimeframe();
|
||||
int getUserOption();
|
||||
void init(); // initialization function for user interaction
|
||||
void processUserOption(int userOption);
|
||||
|
||||
std::string currentTime;
|
||||
@ -28,5 +41,4 @@ class MerkelMain
|
||||
OrderBook orderBook{"20200317.csv"};
|
||||
//OrderBook orderBook{"20200601.csv"};
|
||||
Wallet wallet;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user