Add Bot.h and Bot.cpp
This commit is contained in:
@ -2,10 +2,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
|
||||||
CSVReader::CSVReader()
|
CSVReader::CSVReader()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<OrderBookEntry> CSVReader::readCSV(std::string csvFilename)
|
std::vector<OrderBookEntry> CSVReader::readCSV(std::string csvFilename)
|
||||||
@ -16,16 +14,18 @@ std::vector<OrderBookEntry> CSVReader::readCSV(std::string csvFilename)
|
|||||||
std::string line;
|
std::string line;
|
||||||
if (csvFile.is_open())
|
if (csvFile.is_open())
|
||||||
{
|
{
|
||||||
while(std::getline(csvFile, line))
|
while (std::getline(csvFile, line))
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
try {
|
|
||||||
OrderBookEntry obe = stringsToOBE(tokenise(line, ','));
|
OrderBookEntry obe = stringsToOBE(tokenise(line, ','));
|
||||||
entries.push_back(obe);
|
entries.push_back(obe);
|
||||||
}catch(const std::exception& e)
|
}
|
||||||
|
catch (const std::exception &e)
|
||||||
{
|
{
|
||||||
std::cout << "CSVReader::readCSV bad data" << std::endl;
|
std::cout << "CSVReader::readCSV bad data" << std::endl;
|
||||||
}
|
}
|
||||||
}// end of while
|
} // end of while
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "CSVReader::readCSV read " << entries.size() << " entries" << std::endl;
|
std::cout << "CSVReader::readCSV read " << entries.size() << " entries" << std::endl;
|
||||||
@ -38,14 +38,18 @@ std::vector<std::string> CSVReader::tokenise(std::string csvLine, char separator
|
|||||||
signed int start, end;
|
signed int start, end;
|
||||||
std::string token;
|
std::string token;
|
||||||
start = csvLine.find_first_not_of(separator, 0);
|
start = csvLine.find_first_not_of(separator, 0);
|
||||||
do{
|
do
|
||||||
|
{
|
||||||
end = csvLine.find_first_of(separator, start);
|
end = csvLine.find_first_of(separator, start);
|
||||||
if (start == csvLine.length() || start == end) break;
|
if (start == csvLine.length() || start == end)
|
||||||
if (end >= 0) token = csvLine.substr(start, end - start);
|
break;
|
||||||
else token = csvLine.substr(start, csvLine.length() - start);
|
if (end >= 0)
|
||||||
|
token = csvLine.substr(start, end - start);
|
||||||
|
else
|
||||||
|
token = csvLine.substr(start, csvLine.length() - start);
|
||||||
tokens.push_back(token);
|
tokens.push_back(token);
|
||||||
start = end + 1;
|
start = end + 1;
|
||||||
}while(end > 0);
|
} while (end > 0);
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
@ -60,12 +64,15 @@ OrderBookEntry CSVReader::stringsToOBE(std::vector<std::string> tokens)
|
|||||||
throw std::exception{};
|
throw std::exception{};
|
||||||
}
|
}
|
||||||
// we have 5 tokens
|
// we have 5 tokens
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
price = std::stod(tokens[3]);
|
price = std::stod(tokens[3]);
|
||||||
amount = std::stod(tokens[4]);
|
amount = std::stod(tokens[4]);
|
||||||
}catch(const std::exception& e){
|
}
|
||||||
std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[3]<< std::endl;
|
catch (const std::exception &e)
|
||||||
std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[4]<< std::endl;
|
{
|
||||||
|
std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[3] << std::endl;
|
||||||
|
std::cout << "CSVReader::stringsToOBE Bad float! " << tokens[4] << std::endl;
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +85,6 @@ OrderBookEntry CSVReader::stringsToOBE(std::vector<std::string> tokens)
|
|||||||
return obe;
|
return obe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
|
OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
|
||||||
std::string amountString,
|
std::string amountString,
|
||||||
std::string timestamp,
|
std::string timestamp,
|
||||||
@ -86,12 +92,15 @@ OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
|
|||||||
OrderBookType orderType)
|
OrderBookType orderType)
|
||||||
{
|
{
|
||||||
double price, amount;
|
double price, amount;
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
price = std::stod(priceString);
|
price = std::stod(priceString);
|
||||||
amount = std::stod(amountString);
|
amount = std::stod(amountString);
|
||||||
}catch(const std::exception& e){
|
}
|
||||||
std::cout << "CSVReader::stringsToOBE Bad float! " << priceString<< std::endl;
|
catch (const std::exception &e)
|
||||||
std::cout << "CSVReader::stringsToOBE Bad float! " << amountString<< std::endl;
|
{
|
||||||
|
std::cout << "CSVReader::stringsToOBE Bad float! " << priceString << std::endl;
|
||||||
|
std::cout << "CSVReader::stringsToOBE Bad float! " << amountString << std::endl;
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
OrderBookEntry obe{price,
|
OrderBookEntry obe{price,
|
||||||
@ -102,4 +111,3 @@ OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
|
|||||||
|
|
||||||
return obe;
|
return obe;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4,10 +4,9 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
class CSVReader
|
class CSVReader
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CSVReader();
|
CSVReader();
|
||||||
|
|
||||||
static std::vector<OrderBookEntry> readCSV(std::string csvFile);
|
static std::vector<OrderBookEntry> readCSV(std::string csvFile);
|
||||||
@ -19,7 +18,6 @@ class CSVReader
|
|||||||
std::string product,
|
std::string product,
|
||||||
OrderBookType OrderBookType);
|
OrderBookType OrderBookType);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static OrderBookEntry stringsToOBE(std::vector<std::string> strings);
|
static OrderBookEntry stringsToOBE(std::vector<std::string> strings);
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
MerkelMain::MerkelMain()
|
MerkelMain::MerkelMain()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MerkelMain::init()
|
void MerkelMain::init()
|
||||||
@ -16,7 +15,7 @@ void MerkelMain::init()
|
|||||||
|
|
||||||
wallet.insertCurrency("BTC", 10);
|
wallet.insertCurrency("BTC", 10);
|
||||||
|
|
||||||
while(true)
|
while (true)
|
||||||
{
|
{
|
||||||
printMenu();
|
printMenu();
|
||||||
input = getUserOption();
|
input = getUserOption();
|
||||||
@ -24,7 +23,6 @@ void MerkelMain::init()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MerkelMain::printMenu()
|
void MerkelMain::printMenu()
|
||||||
{
|
{
|
||||||
// 1 print help
|
// 1 print help
|
||||||
@ -52,7 +50,7 @@ void MerkelMain::printHelp()
|
|||||||
|
|
||||||
void MerkelMain::printMarketStats()
|
void MerkelMain::printMarketStats()
|
||||||
{
|
{
|
||||||
for (std::string const& p : orderBook.getKnownProducts())
|
for (std::string const &p : orderBook.getKnownProducts())
|
||||||
{
|
{
|
||||||
std::cout << "Product: " << p << std::endl;
|
std::cout << "Product: " << p << std::endl;
|
||||||
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask,
|
std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask,
|
||||||
@ -60,26 +58,7 @@ void MerkelMain::printMarketStats()
|
|||||||
std::cout << "Asks seen: " << entries.size() << std::endl;
|
std::cout << "Asks seen: " << entries.size() << std::endl;
|
||||||
std::cout << "Max ask: " << OrderBook::getHighPrice(entries) << std::endl;
|
std::cout << "Max ask: " << OrderBook::getHighPrice(entries) << std::endl;
|
||||||
std::cout << "Min ask: " << OrderBook::getLowPrice(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()
|
void MerkelMain::enterAsk()
|
||||||
@ -93,25 +72,28 @@ void MerkelMain::enterAsk()
|
|||||||
{
|
{
|
||||||
std::cout << "MerkelMain::enterAsk Bad input! " << input << std::endl;
|
std::cout << "MerkelMain::enterAsk Bad input! " << input << std::endl;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
OrderBookEntry obe = CSVReader::stringsToOBE(
|
OrderBookEntry obe = CSVReader::stringsToOBE(
|
||||||
tokens[1],
|
tokens[1],
|
||||||
tokens[2],
|
tokens[2],
|
||||||
currentTime,
|
currentTime,
|
||||||
tokens[0],
|
tokens[0],
|
||||||
OrderBookType::ask
|
OrderBookType::ask);
|
||||||
);
|
|
||||||
obe.username = "simuser";
|
obe.username = "simuser";
|
||||||
if (wallet.canFulfillOrder(obe))
|
if (wallet.canFulfillOrder(obe))
|
||||||
{
|
{
|
||||||
std::cout << "Wallet looks good. " << std::endl;
|
std::cout << "Wallet looks good. " << std::endl;
|
||||||
orderBook.insertOrder(obe);
|
orderBook.insertOrder(obe);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
std::cout << "Wallet has insufficient funds . " << std::endl;
|
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;
|
std::cout << " MerkelMain::enterAsk Bad input " << std::endl;
|
||||||
}
|
}
|
||||||
@ -129,15 +111,16 @@ void MerkelMain::enterBid()
|
|||||||
{
|
{
|
||||||
std::cout << "MerkelMain::enterBid Bad input! " << input << std::endl;
|
std::cout << "MerkelMain::enterBid Bad input! " << input << std::endl;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
OrderBookEntry obe = CSVReader::stringsToOBE(
|
OrderBookEntry obe = CSVReader::stringsToOBE(
|
||||||
tokens[1],
|
tokens[1],
|
||||||
tokens[2],
|
tokens[2],
|
||||||
currentTime,
|
currentTime,
|
||||||
tokens[0],
|
tokens[0],
|
||||||
OrderBookType::bid
|
OrderBookType::bid);
|
||||||
);
|
|
||||||
obe.username = "simuser";
|
obe.username = "simuser";
|
||||||
|
|
||||||
if (wallet.canFulfillOrder(obe))
|
if (wallet.canFulfillOrder(obe))
|
||||||
@ -145,10 +128,12 @@ void MerkelMain::enterBid()
|
|||||||
std::cout << "Wallet looks good. " << std::endl;
|
std::cout << "Wallet looks good. " << std::endl;
|
||||||
orderBook.insertOrder(obe);
|
orderBook.insertOrder(obe);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
std::cout << "Wallet has insufficient funds . " << std::endl;
|
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;
|
std::cout << " MerkelMain::enterBid Bad input " << std::endl;
|
||||||
}
|
}
|
||||||
@ -168,7 +153,7 @@ void MerkelMain::gotoNextTimeframe()
|
|||||||
std::cout << "matching " << p << std::endl;
|
std::cout << "matching " << p << std::endl;
|
||||||
std::vector<OrderBookEntry> sales = orderBook.matchAsksToBids(p, currentTime);
|
std::vector<OrderBookEntry> sales = orderBook.matchAsksToBids(p, currentTime);
|
||||||
std::cout << "Sales: " << sales.size() << std::endl;
|
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;
|
std::cout << "Sale price: " << sale.price << " amount " << sale.amount << std::endl;
|
||||||
if (sale.username == "simuser")
|
if (sale.username == "simuser")
|
||||||
@ -177,7 +162,6 @@ void MerkelMain::gotoNextTimeframe()
|
|||||||
wallet.processSale(sale);
|
wallet.processSale(sale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentTime = orderBook.getNextTime(currentTime);
|
currentTime = orderBook.getNextTime(currentTime);
|
||||||
@ -189,9 +173,11 @@ int MerkelMain::getUserOption()
|
|||||||
std::string line;
|
std::string line;
|
||||||
std::cout << "Type in 1-6" << std::endl;
|
std::cout << "Type in 1-6" << std::endl;
|
||||||
std::getline(std::cin, line);
|
std::getline(std::cin, line);
|
||||||
try{
|
try
|
||||||
|
{
|
||||||
userOption = std::stoi(line);
|
userOption = std::stoi(line);
|
||||||
}catch(const std::exception& e)
|
}
|
||||||
|
catch (const std::exception &e)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,14 +5,14 @@
|
|||||||
#include "OrderBook.h"
|
#include "OrderBook.h"
|
||||||
#include "Wallet.h"
|
#include "Wallet.h"
|
||||||
|
|
||||||
|
|
||||||
class MerkelMain
|
class MerkelMain
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MerkelMain();
|
MerkelMain();
|
||||||
/** Call this to start the sim */
|
/** Call this to start the sim */
|
||||||
void init();
|
void init();
|
||||||
private:
|
|
||||||
|
private:
|
||||||
void printMenu();
|
void printMenu();
|
||||||
void printHelp();
|
void printHelp();
|
||||||
void printMarketStats();
|
void printMarketStats();
|
||||||
@ -25,8 +25,7 @@ class MerkelMain
|
|||||||
|
|
||||||
std::string currentTime;
|
std::string currentTime;
|
||||||
|
|
||||||
// OrderBook orderBook{"20200317.csv"};
|
// OrderBook orderBook{"20200317.csv"};
|
||||||
OrderBook orderBook{"20200601.csv"};
|
OrderBook orderBook{"20200601.csv"};
|
||||||
Wallet wallet;
|
Wallet wallet;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
/** construct, reading a csv data file */
|
/** construct, reading a csv data file */
|
||||||
OrderBook::OrderBook(std::string filename)
|
OrderBook::OrderBook(std::string filename)
|
||||||
{
|
{
|
||||||
@ -16,15 +15,15 @@ std::vector<std::string> OrderBook::getKnownProducts()
|
|||||||
{
|
{
|
||||||
std::vector<std::string> products;
|
std::vector<std::string> products;
|
||||||
|
|
||||||
std::map<std::string,bool> prodMap;
|
std::map<std::string, bool> prodMap;
|
||||||
|
|
||||||
for (OrderBookEntry& e : orders)
|
for (OrderBookEntry &e : orders)
|
||||||
{
|
{
|
||||||
prodMap[e.product] = true;
|
prodMap[e.product] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now flatten the map to a vector of strings
|
// now flatten the map to a vector of strings
|
||||||
for (auto const& e : prodMap)
|
for (auto const &e : prodMap)
|
||||||
{
|
{
|
||||||
products.push_back(e.first);
|
products.push_back(e.first);
|
||||||
}
|
}
|
||||||
@ -37,11 +36,11 @@ std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type,
|
|||||||
std::string timestamp)
|
std::string timestamp)
|
||||||
{
|
{
|
||||||
std::vector<OrderBookEntry> orders_sub;
|
std::vector<OrderBookEntry> orders_sub;
|
||||||
for (OrderBookEntry& e : orders)
|
for (OrderBookEntry &e : orders)
|
||||||
{
|
{
|
||||||
if (e.orderType == type &&
|
if (e.orderType == type &&
|
||||||
e.product == product &&
|
e.product == product &&
|
||||||
e.timestamp == timestamp )
|
e.timestamp == timestamp)
|
||||||
{
|
{
|
||||||
orders_sub.push_back(e);
|
orders_sub.push_back(e);
|
||||||
}
|
}
|
||||||
@ -49,24 +48,24 @@ std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type,
|
|||||||
return orders_sub;
|
return orders_sub;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double OrderBook::getHighPrice(std::vector<OrderBookEntry> &orders)
|
||||||
double OrderBook::getHighPrice(std::vector<OrderBookEntry>& orders)
|
|
||||||
{
|
{
|
||||||
double max = orders[0].price;
|
double max = orders[0].price;
|
||||||
for (OrderBookEntry& e : orders)
|
for (OrderBookEntry &e : orders)
|
||||||
{
|
{
|
||||||
if (e.price > max)max = e.price;
|
if (e.price > max)
|
||||||
|
max = e.price;
|
||||||
}
|
}
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double OrderBook::getLowPrice(std::vector<OrderBookEntry> &orders)
|
||||||
double OrderBook::getLowPrice(std::vector<OrderBookEntry>& orders)
|
|
||||||
{
|
{
|
||||||
double min = orders[0].price;
|
double min = orders[0].price;
|
||||||
for (OrderBookEntry& e : orders)
|
for (OrderBookEntry &e : orders)
|
||||||
{
|
{
|
||||||
if (e.price < min)min = e.price;
|
if (e.price < min)
|
||||||
|
min = e.price;
|
||||||
}
|
}
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
@ -79,7 +78,7 @@ std::string OrderBook::getEarliestTime()
|
|||||||
std::string OrderBook::getNextTime(std::string timestamp)
|
std::string OrderBook::getNextTime(std::string timestamp)
|
||||||
{
|
{
|
||||||
std::string next_timestamp = "";
|
std::string next_timestamp = "";
|
||||||
for (OrderBookEntry& e : orders)
|
for (OrderBookEntry &e : orders)
|
||||||
{
|
{
|
||||||
if (e.timestamp > timestamp)
|
if (e.timestamp > timestamp)
|
||||||
{
|
{
|
||||||
@ -94,7 +93,7 @@ std::string OrderBook::getNextTime(std::string timestamp)
|
|||||||
return next_timestamp;
|
return next_timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OrderBook::insertOrder(OrderBookEntry& order)
|
void OrderBook::insertOrder(OrderBookEntry &order)
|
||||||
{
|
{
|
||||||
orders.push_back(order);
|
orders.push_back(order);
|
||||||
std::sort(orders.begin(), orders.end(), OrderBookEntry::compareByTimestamp);
|
std::sort(orders.begin(), orders.end(), OrderBookEntry::compareByTimestamp);
|
||||||
@ -102,11 +101,11 @@ void OrderBook::insertOrder(OrderBookEntry& order)
|
|||||||
|
|
||||||
std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std::string timestamp)
|
std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std::string timestamp)
|
||||||
{
|
{
|
||||||
// asks = orderbook.asks
|
// asks = orderbook.asks
|
||||||
std::vector<OrderBookEntry> asks = getOrders(OrderBookType::ask,
|
std::vector<OrderBookEntry> asks = getOrders(OrderBookType::ask,
|
||||||
product,
|
product,
|
||||||
timestamp);
|
timestamp);
|
||||||
// bids = orderbook.bids
|
// bids = orderbook.bids
|
||||||
std::vector<OrderBookEntry> bids = getOrders(OrderBookType::bid,
|
std::vector<OrderBookEntry> bids = getOrders(OrderBookType::bid,
|
||||||
product,
|
product,
|
||||||
timestamp);
|
timestamp);
|
||||||
@ -127,15 +126,15 @@ std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std:
|
|||||||
// sort bids highest first
|
// sort bids highest first
|
||||||
std::sort(bids.begin(), bids.end(), OrderBookEntry::compareByPriceDesc);
|
std::sort(bids.begin(), bids.end(), OrderBookEntry::compareByPriceDesc);
|
||||||
// for ask in asks:
|
// for ask in asks:
|
||||||
std::cout << "max ask " << asks[asks.size()-1].price << std::endl;
|
std::cout << "max ask " << asks[asks.size() - 1].price << std::endl;
|
||||||
std::cout << "min ask " << asks[0].price << std::endl;
|
std::cout << "min ask " << asks[0].price << std::endl;
|
||||||
std::cout << "max bid " << bids[0].price << std::endl;
|
std::cout << "max bid " << bids[0].price << std::endl;
|
||||||
std::cout << "min bid " << bids[bids.size()-1].price << std::endl;
|
std::cout << "min bid " << bids[bids.size() - 1].price << std::endl;
|
||||||
|
|
||||||
for (OrderBookEntry& ask : asks)
|
for (OrderBookEntry &ask : asks)
|
||||||
{
|
{
|
||||||
// for bid in bids:
|
// for bid in bids:
|
||||||
for (OrderBookEntry& bid : bids)
|
for (OrderBookEntry &bid : bids)
|
||||||
{
|
{
|
||||||
// if bid.price >= ask.price # we have a match
|
// if bid.price >= ask.price # we have a match
|
||||||
if (bid.price >= ask.price)
|
if (bid.price >= ask.price)
|
||||||
@ -190,7 +189,6 @@ std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std:
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// if bid.amount < ask.amount # bid is completely gone, slice the ask
|
// if bid.amount < ask.amount # bid is completely gone, slice the ask
|
||||||
if (bid.amount < ask.amount &&
|
if (bid.amount < ask.amount &&
|
||||||
bid.amount > 0)
|
bid.amount > 0)
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
class OrderBook
|
class OrderBook
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/** construct, reading a csv data file */
|
/** construct, reading a csv data file */
|
||||||
OrderBook(std::string filename);
|
OrderBook(std::string filename);
|
||||||
/** return vector of all know products in the dataset*/
|
/** return vector of all know products in the dataset*/
|
||||||
@ -24,15 +24,13 @@ class OrderBook
|
|||||||
* */
|
* */
|
||||||
std::string getNextTime(std::string timestamp);
|
std::string getNextTime(std::string timestamp);
|
||||||
|
|
||||||
void insertOrder(OrderBookEntry& order);
|
void insertOrder(OrderBookEntry &order);
|
||||||
|
|
||||||
std::vector<OrderBookEntry> matchAsksToBids(std::string product, std::string timestamp);
|
std::vector<OrderBookEntry> matchAsksToBids(std::string product, std::string timestamp);
|
||||||
|
|
||||||
static double getHighPrice(std::vector<OrderBookEntry>& orders);
|
static double getHighPrice(std::vector<OrderBookEntry> &orders);
|
||||||
static double getLowPrice(std::vector<OrderBookEntry>& orders);
|
static double getLowPrice(std::vector<OrderBookEntry> &orders);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<OrderBookEntry> orders;
|
std::vector<OrderBookEntry> orders;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,20 +1,20 @@
|
|||||||
#include "OrderBookEntry.h"
|
#include "OrderBookEntry.h"
|
||||||
|
|
||||||
OrderBookEntry::OrderBookEntry( double _price,
|
OrderBookEntry::OrderBookEntry(
|
||||||
|
double _price,
|
||||||
double _amount,
|
double _amount,
|
||||||
std::string _timestamp,
|
std::string _timestamp,
|
||||||
std::string _product,
|
std::string _product,
|
||||||
OrderBookType _orderType,
|
OrderBookType _orderType,
|
||||||
std::string _username)
|
std::string _username)
|
||||||
: price(_price),
|
:
|
||||||
|
price(_price),
|
||||||
amount(_amount),
|
amount(_amount),
|
||||||
timestamp(_timestamp),
|
timestamp(_timestamp),
|
||||||
product(_product),
|
product(_product),
|
||||||
orderType(_orderType),
|
orderType(_orderType),
|
||||||
username(_username)
|
username(_username)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OrderBookType OrderBookEntry::stringToOrderBookType(std::string s)
|
OrderBookType OrderBookEntry::stringToOrderBookType(std::string s)
|
||||||
|
|||||||
@ -2,14 +2,19 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
enum class OrderBookType{bid, ask, unknown, asksale, bidsale};
|
enum class OrderBookType
|
||||||
|
{
|
||||||
|
bid,
|
||||||
|
ask,
|
||||||
|
unknown,
|
||||||
|
asksale,
|
||||||
|
bidsale
|
||||||
|
};
|
||||||
|
|
||||||
class OrderBookEntry
|
class OrderBookEntry
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
OrderBookEntry(double _price,
|
||||||
|
|
||||||
OrderBookEntry( double _price,
|
|
||||||
double _amount,
|
double _amount,
|
||||||
std::string _timestamp,
|
std::string _timestamp,
|
||||||
std::string _product,
|
std::string _product,
|
||||||
@ -18,15 +23,15 @@ class OrderBookEntry
|
|||||||
|
|
||||||
static OrderBookType stringToOrderBookType(std::string s);
|
static OrderBookType stringToOrderBookType(std::string s);
|
||||||
|
|
||||||
static bool compareByTimestamp(OrderBookEntry& e1, OrderBookEntry& e2)
|
static bool compareByTimestamp(OrderBookEntry &e1, OrderBookEntry &e2)
|
||||||
{
|
{
|
||||||
return e1.timestamp < e2.timestamp;
|
return e1.timestamp < e2.timestamp;
|
||||||
}
|
}
|
||||||
static bool compareByPriceAsc(OrderBookEntry& e1, OrderBookEntry& e2)
|
static bool compareByPriceAsc(OrderBookEntry &e1, OrderBookEntry &e2)
|
||||||
{
|
{
|
||||||
return e1.price < e2.price;
|
return e1.price < e2.price;
|
||||||
}
|
}
|
||||||
static bool compareByPriceDesc(OrderBookEntry& e1, OrderBookEntry& e2)
|
static bool compareByPriceDesc(OrderBookEntry &e1, OrderBookEntry &e2)
|
||||||
{
|
{
|
||||||
return e1.price > e2.price;
|
return e1.price > e2.price;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
};
|
||||||
@ -4,8 +4,6 @@
|
|||||||
|
|
||||||
Wallet::Wallet()
|
Wallet::Wallet()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wallet::insertCurrency(std::string type, double amount)
|
void Wallet::insertCurrency(std::string type, double amount)
|
||||||
@ -19,7 +17,8 @@ void Wallet::insertCurrency(std::string type, double amount)
|
|||||||
{
|
{
|
||||||
balance = 0;
|
balance = 0;
|
||||||
}
|
}
|
||||||
else { // is there
|
else
|
||||||
|
{ // is there
|
||||||
balance = currencies[type];
|
balance = currencies[type];
|
||||||
}
|
}
|
||||||
balance += amount;
|
balance += amount;
|
||||||
@ -37,8 +36,9 @@ bool Wallet::removeCurrency(std::string type, double amount)
|
|||||||
//std::cout << "No currency for " << type << std::endl;
|
//std::cout << "No currency for " << type << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else { // is there - do we have enough
|
else
|
||||||
if (containsCurrency(type, amount))// we have enough
|
{ // is there - do we have enough
|
||||||
|
if (containsCurrency(type, amount)) // we have enough
|
||||||
{
|
{
|
||||||
//std::cout << "Removing " << type << ": " << amount << std::endl;
|
//std::cout << "Removing " << type << ": " << amount << std::endl;
|
||||||
currencies[type] -= amount;
|
currencies[type] -= amount;
|
||||||
@ -55,13 +55,12 @@ bool Wallet::containsCurrency(std::string type, double amount)
|
|||||||
return false;
|
return false;
|
||||||
else
|
else
|
||||||
return currencies[type] >= amount;
|
return currencies[type] >= amount;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Wallet::toString()
|
std::string Wallet::toString()
|
||||||
{
|
{
|
||||||
std::string s;
|
std::string s;
|
||||||
for (std::pair<std::string,double> pair : currencies)
|
for (std::pair<std::string, double> pair : currencies)
|
||||||
{
|
{
|
||||||
std::string currency = pair.first;
|
std::string currency = pair.first;
|
||||||
double amount = pair.second;
|
double amount = pair.second;
|
||||||
@ -91,12 +90,10 @@ bool Wallet::canFulfillOrder(OrderBookEntry order)
|
|||||||
return containsCurrency(currency, amount);
|
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, '/');
|
std::vector<std::string> currs = CSVReader::tokenise(sale.product, '/');
|
||||||
// ask
|
// ask
|
||||||
@ -109,7 +106,6 @@ void Wallet::processSale(OrderBookEntry& sale)
|
|||||||
|
|
||||||
currencies[incomingCurrency] += incomingAmount;
|
currencies[incomingCurrency] += incomingAmount;
|
||||||
currencies[outgoingCurrency] -= outgoingAmount;
|
currencies[outgoingCurrency] -= outgoingAmount;
|
||||||
|
|
||||||
}
|
}
|
||||||
// bid
|
// bid
|
||||||
if (sale.orderType == OrderBookType::bidsale)
|
if (sale.orderType == OrderBookType::bidsale)
|
||||||
@ -123,9 +119,8 @@ void Wallet::processSale(OrderBookEntry& sale)
|
|||||||
currencies[outgoingCurrency] -= outgoingAmount;
|
currencies[outgoingCurrency] -= outgoingAmount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::ostream& operator<<(std::ostream& os, Wallet& wallet)
|
std::ostream &operator<<(std::ostream &os, Wallet &wallet)
|
||||||
{
|
{
|
||||||
os << wallet.toString();
|
os << wallet.toString();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
class Wallet
|
class Wallet
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Wallet();
|
Wallet();
|
||||||
/** insert currency to the wallet */
|
/** insert currency to the wallet */
|
||||||
void insertCurrency(std::string type, double amount);
|
void insertCurrency(std::string type, double amount);
|
||||||
@ -21,20 +21,12 @@ class Wallet
|
|||||||
/** update the contents of the wallet
|
/** update the contents of the wallet
|
||||||
* assumes the order was made by the owner of the wallet
|
* assumes the order was made by the owner of the wallet
|
||||||
*/
|
*/
|
||||||
void processSale(OrderBookEntry& sale);
|
void processSale(OrderBookEntry &sale);
|
||||||
|
|
||||||
|
|
||||||
/** generate a string representation of the wallet */
|
/** generate a string representation of the wallet */
|
||||||
std::string toString();
|
std::string toString();
|
||||||
friend std::ostream& operator<<(std::ostream& os, Wallet& wallet);
|
friend std::ostream &operator<<(std::ostream &os, Wallet &wallet);
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::map<std::string,double> currencies;
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map<std::string, double> currencies;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
BIN
CM2005 Object Oriented Programming/Midterm/Midterm Project/a.exe
Normal file
BIN
CM2005 Object Oriented Programming/Midterm/Midterm Project/a.exe
Normal file
Binary file not shown.
@ -1,10 +1,11 @@
|
|||||||
#include "Wallet.h"
|
#include "Wallet.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "MerkelMain.h"
|
#include "MerkelMain.h"
|
||||||
|
#include "TradingBot.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
MerkelMain app{};
|
MerkelMain app{};
|
||||||
app.init();
|
Bot myBot("20200317.csv");
|
||||||
|
//app.init();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user