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

@ -2,10 +2,8 @@
#include <iostream>
#include <fstream>
CSVReader::CSVReader()
{
}
std::vector<OrderBookEntry> CSVReader::readCSV(std::string csvFilename)
@ -16,38 +14,44 @@ std::vector<OrderBookEntry> CSVReader::readCSV(std::string csvFilename)
std::string line;
if (csvFile.is_open())
{
while(std::getline(csvFile, line))
while (std::getline(csvFile, line))
{
try {
try
{
OrderBookEntry obe = stringsToOBE(tokenise(line, ','));
entries.push_back(obe);
}catch(const std::exception& e)
{
std::cout << "CSVReader::readCSV bad data" << std::endl;
}
}// end of while
}
catch (const std::exception &e)
{
std::cout << "CSVReader::readCSV bad data" << std::endl;
}
} // end of while
}
std::cout << "CSVReader::readCSV read " << entries.size() << " entries" << std::endl;
return entries;
std::cout << "CSVReader::readCSV read " << entries.size() << " entries" << std::endl;
return entries;
}
std::vector<std::string> CSVReader::tokenise(std::string csvLine, char separator)
{
std::vector<std::string> tokens;
signed int start, end;
std::string token;
std::vector<std::string> tokens;
signed int start, end;
std::string token;
start = csvLine.find_first_not_of(separator, 0);
do{
do
{
end = csvLine.find_first_of(separator, start);
if (start == csvLine.length() || start == end) break;
if (end >= 0) token = csvLine.substr(start, end - start);
else token = csvLine.substr(start, csvLine.length() - start);
if (start == csvLine.length() || start == end)
break;
if (end >= 0)
token = csvLine.substr(start, end - start);
else
token = csvLine.substr(start, csvLine.length() - start);
tokens.push_back(token);
start = end + 1;
}while(end > 0);
start = end + 1;
} while (end > 0);
return tokens;
return tokens;
}
OrderBookEntry CSVReader::stringsToOBE(std::vector<std::string> tokens)
@ -60,46 +64,50 @@ OrderBookEntry CSVReader::stringsToOBE(std::vector<std::string> tokens)
throw std::exception{};
}
// we have 5 tokens
try {
price = std::stod(tokens[3]);
amount = std::stod(tokens[4]);
}catch(const std::exception& e){
std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[3]<< std::endl;
std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[4]<< std::endl;
throw;
try
{
price = std::stod(tokens[3]);
amount = std::stod(tokens[4]);
}
catch (const std::exception &e)
{
std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[3] << std::endl;
std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[4] << std::endl;
throw;
}
OrderBookEntry obe{price,
amount,
tokens[0],
tokens[1],
OrderBookEntry::stringToOrderBookType(tokens[2])};
OrderBookEntry obe{price,
amount,
tokens[0],
tokens[1],
OrderBookEntry::stringToOrderBookType(tokens[2])};
return obe;
}
OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
std::string amountString,
std::string timestamp,
std::string product,
OrderBookType orderType)
{
double price, amount;
try
{
price = std::stod(priceString);
amount = std::stod(amountString);
}
catch (const std::exception &e)
{
std::cout << "CSVReader::stringsToOBE Bad float! " << priceString << std::endl;
std::cout << "CSVReader::stringsToOBE Bad float! " << amountString << std::endl;
throw;
}
OrderBookEntry obe{price,
amount,
timestamp,
product,
orderType};
return obe;
}
OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
std::string amountString,
std::string timestamp,
std::string product,
OrderBookType orderType)
{
double price, amount;
try {
price = std::stod(priceString);
amount = std::stod(amountString);
}catch(const std::exception& e){
std::cout << "CSVReader::stringsToOBE Bad float! " << priceString<< std::endl;
std::cout << "CSVReader::stringsToOBE Bad float! " << amountString<< std::endl;
throw;
}
OrderBookEntry obe{price,
amount,
timestamp,
product,
orderType};
return obe;
}