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)
@ -18,10 +16,12 @@ std::vector<OrderBookEntry> CSVReader::readCSV(std::string csvFilename)
{
while (std::getline(csvFile, line))
{
try {
try
{
OrderBookEntry obe = stringsToOBE(tokenise(line, ','));
entries.push_back(obe);
}catch(const std::exception& e)
}
catch (const std::exception &e)
{
std::cout << "CSVReader::readCSV bad data" << std::endl;
}
@ -38,11 +38,15 @@ std::vector<std::string> CSVReader::tokenise(std::string csvLine, char separator
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);
@ -60,10 +64,13 @@ OrderBookEntry CSVReader::stringsToOBE(std::vector<std::string> tokens)
throw std::exception{};
}
// we have 5 tokens
try {
try
{
price = std::stod(tokens[3]);
amount = std::stod(tokens[4]);
}catch(const std::exception& e){
}
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;
@ -78,7 +85,6 @@ OrderBookEntry CSVReader::stringsToOBE(std::vector<std::string> tokens)
return obe;
}
OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
std::string amountString,
std::string timestamp,
@ -86,10 +92,13 @@ OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
OrderBookType orderType)
{
double price, amount;
try {
try
{
price = std::stod(priceString);
amount = std::stod(amountString);
}catch(const std::exception& e){
}
catch (const std::exception &e)
{
std::cout << "CSVReader::stringsToOBE Bad float! " << priceString << std::endl;
std::cout << "CSVReader::stringsToOBE Bad float! " << amountString << std::endl;
throw;
@ -102,4 +111,3 @@ OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
return obe;
}

View File

@ -4,7 +4,6 @@
#include <vector>
#include <string>
class CSVReader
{
public:
@ -21,5 +20,4 @@ class CSVReader
private:
static OrderBookEntry stringsToOBE(std::vector<std::string> strings);
};

View File

@ -6,7 +6,6 @@
MerkelMain::MerkelMain()
{
}
void MerkelMain::init()
@ -24,7 +23,6 @@ void MerkelMain::init()
}
}
void MerkelMain::printMenu()
{
// 1 print help
@ -60,26 +58,7 @@ void MerkelMain::printMarketStats()
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;
// 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()
@ -93,25 +72,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 +111,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 +128,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;
}
@ -177,7 +162,6 @@ void MerkelMain::gotoNextTimeframe()
wallet.processSale(sale);
}
}
}
currentTime = orderBook.getNextTime(currentTime);
@ -189,9 +173,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)
{
//
}

View File

@ -5,13 +5,13 @@
#include "OrderBook.h"
#include "Wallet.h"
class MerkelMain
{
public:
MerkelMain();
/** Call this to start the sim */
void init();
private:
void printMenu();
void printHelp();
@ -28,5 +28,4 @@ class MerkelMain
// OrderBook orderBook{"20200317.csv"};
OrderBook orderBook{"20200601.csv"};
Wallet wallet;
};

View File

@ -4,7 +4,6 @@
#include <algorithm>
#include <iostream>
/** construct, reading a csv data file */
OrderBook::OrderBook(std::string filename)
{
@ -49,24 +48,24 @@ std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type,
return orders_sub;
}
double OrderBook::getHighPrice(std::vector<OrderBookEntry> &orders)
{
double max = orders[0].price;
for (OrderBookEntry &e : orders)
{
if (e.price > max)max = e.price;
if (e.price > max)
max = e.price;
}
return max;
}
double OrderBook::getLowPrice(std::vector<OrderBookEntry> &orders)
{
double min = orders[0].price;
for (OrderBookEntry &e : orders)
{
if (e.price < min)min = e.price;
if (e.price < min)
min = e.price;
}
return min;
}
@ -190,7 +189,6 @@ std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std:
break;
}
// if bid.amount < ask.amount # bid is completely gone, slice the ask
if (bid.amount < ask.amount &&
bid.amount > 0)

View File

@ -33,6 +33,4 @@ class OrderBook
private:
std::vector<OrderBookEntry> orders;
};

View File

@ -1,20 +1,20 @@
#include "OrderBookEntry.h"
OrderBookEntry::OrderBookEntry( double _price,
OrderBookEntry::OrderBookEntry(
double _price,
double _amount,
std::string _timestamp,
std::string _product,
OrderBookType _orderType,
std::string _username)
: price(_price),
:
price(_price),
amount(_amount),
timestamp(_timestamp),
product(_product),
orderType(_orderType),
username(_username)
{
}
OrderBookType OrderBookEntry::stringToOrderBookType(std::string s)

View File

@ -2,13 +2,18 @@
#include <string>
enum class OrderBookType{bid, ask, unknown, asksale, bidsale};
enum class OrderBookType
{
bid,
ask,
unknown,
asksale,
bidsale
};
class OrderBookEntry
{
public:
OrderBookEntry(double _price,
double _amount,
std::string _timestamp,

View File

@ -0,0 +1,20 @@
#include "TradingBot.h"
#include <iostream>
Bot::Bot(std::string filename)
{
orders = CSVReader::readCSV(filename);
Print(20);
}
void Bot::Print(int intLimit)
{
int i = 0;
for (OrderBookEntry &entry : orders)
{
std::cout << entry.product << " " << entry.price << " " << entry.amount << std::endl;
i++;
if (i == intLimit)
break;
}
}

View File

@ -0,0 +1,15 @@
#pragma once
#include "OrderBook.h"
#include <vector>
//#include "CSVReader.h"
class Bot
{
public:
Bot(std::string filename);
void Print(int intLimit);
private:
std::vector<OrderBookEntry> orders;
};

View File

@ -4,8 +4,6 @@
Wallet::Wallet()
{
}
void Wallet::insertCurrency(std::string type, double amount)
@ -19,7 +17,8 @@ void Wallet::insertCurrency(std::string type, double amount)
{
balance = 0;
}
else { // is there
else
{ // is there
balance = currencies[type];
}
balance += amount;
@ -37,7 +36,8 @@ bool Wallet::removeCurrency(std::string type, double amount)
//std::cout << "No currency for " << type << std::endl;
return false;
}
else { // is there - do we have enough
else
{ // is there - do we have enough
if (containsCurrency(type, amount)) // we have enough
{
//std::cout << "Removing " << type << ": " << amount << std::endl;
@ -55,7 +55,6 @@ bool Wallet::containsCurrency(std::string type, double amount)
return false;
else
return currencies[type] >= amount;
}
std::string Wallet::toString()
@ -91,11 +90,9 @@ bool Wallet::canFulfillOrder(OrderBookEntry order)
return containsCurrency(currency, amount);
}
return false;
}
void Wallet::processSale(OrderBookEntry &sale)
{
std::vector<std::string> currs = CSVReader::tokenise(sale.product, '/');
@ -109,7 +106,6 @@ void Wallet::processSale(OrderBookEntry& sale)
currencies[incomingCurrency] += incomingAmount;
currencies[outgoingCurrency] -= outgoingAmount;
}
// bid
if (sale.orderType == OrderBookType::bidsale)
@ -128,4 +124,3 @@ std::ostream& operator<<(std::ostream& os, Wallet& wallet)
os << wallet.toString();
return os;
}

View File

@ -23,18 +23,10 @@ class Wallet
*/
void processSale(OrderBookEntry &sale);
/** generate a string representation of the wallet */
std::string toString();
friend std::ostream &operator<<(std::ostream &os, Wallet &wallet);
private:
std::map<std::string, double> currencies;
};

View File

@ -1,10 +1,11 @@
#include "Wallet.h"
#include <iostream>
#include "MerkelMain.h"
#include "TradingBot.h"
int main()
{
MerkelMain app{};
app.init();
Bot myBot("20200317.csv");
//app.init();
}