Add Bot.h and Bot.cpp

This commit is contained in:
Lev
2021-06-13 17:11:24 -05:00
parent 312341dca6
commit fdfd01b0f3
14 changed files with 362 additions and 347 deletions

View File

@ -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<std::string,double> pair : currencies)
for (std::pair<std::string, double> 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<std::string> 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;
}