Update fixed getMean not returning correct value

This commit is contained in:
Lev
2021-07-03 04:42:08 -05:00
parent fc2985f982
commit 64c0ffee5c
2 changed files with 33 additions and 18 deletions

View File

@ -26,6 +26,7 @@ void TradingBot::printMenu()
// 1 print help // 1 print help
std::cout << "1: Retrieve current orders" << std::endl; std::cout << "1: Retrieve current orders" << std::endl;
std::cout << "2: Predict ask and bid rates" << std::endl; std::cout << "2: Predict ask and bid rates" << std::endl;
std::cout << "3: Print predictions" << std::endl;;
std::cout << "8: Exit bot" << std::endl; std::cout << "8: Exit bot" << std::endl;
std::cout << spacer << std::endl; std::cout << spacer << std::endl;
std::cout << "Enter an option: "; std::cout << "Enter an option: ";
@ -111,6 +112,7 @@ void TradingBot::predictRates()
for (const std::string &product : products) for (const std::string &product : products)
{ {
askMap[product] = getMean(bookAsks, product); askMap[product] = getMean(bookAsks, product);
std::cout << askMap[product] << std::endl;
} }
askHistory.push_back(askMap); askHistory.push_back(askMap);
@ -143,17 +145,23 @@ void TradingBot::predictRates()
double TradingBot::getMean(std::vector<OrderBookEntry> orders, std::string product) double TradingBot::getMean(std::vector<OrderBookEntry> orders, std::string product)
{ {
double sum = 0; double sum = 0;
int count = 0;
for (const OrderBookEntry &order : orders) for (const OrderBookEntry &order : orders)
{ {
if (order.product == product) if (order.product == product)
{
sum += order.price; sum += order.price;
++count;
} }
return sum / orders.size(); }
if (count == 0) return 0.0;
return sum / count;
} }
// Print predictions if they have been made // Print predictions if they have been made
void TradingBot::printPredictions() void TradingBot::printPredictions()
{ {
std::cout.precision(8);
std::cout << "Asks" << std::endl; std::cout << "Asks" << std::endl;
if (predictedAsks.size() == 0) if (predictedAsks.size() == 0)
std::cout << "No ask predictions have been made yet" << std::endl; std::cout << "No ask predictions have been made yet" << std::endl;
@ -173,20 +181,6 @@ void TradingBot::printPredictions()
void TradingBot::testFunc() void TradingBot::testFunc()
{ {
std::cout << "Asks" << std::endl;
for (auto const &ask : predictedAsks)
{
std::cout << ask.first << " - " << ask.second << std::endl;
}
std::cout << "Bids" << std::endl;
for (auto const &bid : predictedBids)
{
std::cout << bid.first << " - " << bid.second << std::endl;
}
//std::cout << "Ask: " << askHistory[0] << std::endl;
return;
std::cout << "Asks" << std::endl; std::cout << "Asks" << std::endl;
for (int i = 0; i < bookAsks.size(); ++i) for (int i = 0; i < bookAsks.size(); ++i)
{ {
@ -210,6 +204,21 @@ void TradingBot::testFunc()
} }
return; return;
std::cout << "Asks" << std::endl;
for (auto const &ask : predictedAsks)
{
std::cout << ask.first << " - " << ask.second << std::endl;
}
std::cout << "Bids" << std::endl;
for (auto const &bid : predictedBids)
{
std::cout << bid.first << " - " << bid.second << std::endl;
}
//std::cout << "Ask: " << askHistory[0] << std::endl;
return;
std::vector<OrderBookEntry> currentAsks = merkel->getCurrentAsks(); std::vector<OrderBookEntry> currentAsks = merkel->getCurrentAsks();
std::cout.precision(8); std::cout.precision(8);
for (int i = 0; i < currentAsks.size(); ++i) for (int i = 0; i < currentAsks.size(); ++i)

View File

@ -21,12 +21,18 @@ private:
void printMenu(); void printMenu();
int getUserOption(); int getUserOption();
void processUserOption(int userOption); void processUserOption(int userOption);
void printPredictions();
// Exchange functions // Exchange functions
void retrieveOrders(); // Retrieves current orders from Merkel and stores them in bookBids and BookAsks void retrieveOrders(); // Retrieves current orders from Merkel and stores them in bookBids and BookAsks
void predictRates();
// Helper functions
static double getMean(std::vector<OrderBookEntry> orders, std::string product);
// ===== Variables ===== // ===== Variables =====
MerkelMain *merkel; MerkelMain *merkel;
std::vector<OrderBookEntry> bookBids; std::vector<OrderBookEntry> bookBids;
std::vector<OrderBookEntry> bookAsks; std::vector<OrderBookEntry> bookAsks;
double bidHistory = {}; std::vector<std::map<std::string, double>> bidHistory;
std::vector<std::map<std::string, double>> askHistory;
std::map<std::string, double> predictedBids;
std::map<std::string, double> predictedAsks;
}; };