Update formatting
This commit is contained in:
@ -4,69 +4,64 @@
|
|||||||
#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)
|
||||||
{
|
{
|
||||||
orders = CSVReader::readCSV(filename);
|
orders = CSVReader::readCSV(filename);
|
||||||
|
|
||||||
|
std::map<std::string, bool> prodMap;
|
||||||
|
|
||||||
|
for (OrderBookEntry &e : orders)
|
||||||
|
{
|
||||||
|
prodMap[e.product] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// now flatten the map to a vector of strings
|
||||||
|
for (auto const &e : prodMap)
|
||||||
|
{
|
||||||
|
knownProducts.push_back(e.first);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** return vector of all know products in the dataset*/
|
/** return vector of all know products in the dataset*/
|
||||||
std::vector<std::string> OrderBook::getKnownProducts()
|
std::vector<std::string> OrderBook::getKnownProducts()
|
||||||
{
|
{
|
||||||
std::vector<std::string> products;
|
return knownProducts;
|
||||||
|
|
||||||
std::map<std::string,bool> prodMap;
|
|
||||||
|
|
||||||
for (OrderBookEntry& e : orders)
|
|
||||||
{
|
|
||||||
prodMap[e.product] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// now flatten the map to a vector of strings
|
|
||||||
for (auto const& e : prodMap)
|
|
||||||
{
|
|
||||||
products.push_back(e.first);
|
|
||||||
}
|
|
||||||
|
|
||||||
return products;
|
|
||||||
}
|
}
|
||||||
/** return vector of Orders according to the sent filters*/
|
/** return vector of Orders according to the sent filters*/
|
||||||
std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type,
|
std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type, std::string product, std::string timestamp)
|
||||||
std::string product,
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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,9 +74,9 @@ 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)
|
||||||
{
|
{
|
||||||
next_timestamp = e.timestamp;
|
next_timestamp = e.timestamp;
|
||||||
break;
|
break;
|
||||||
@ -94,7 +89,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,17 +97,13 @@ 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, timestamp);
|
||||||
product,
|
// bids = orderbook.bids
|
||||||
timestamp);
|
std::vector<OrderBookEntry> bids = getOrders(OrderBookType::bid, product, timestamp);
|
||||||
// bids = orderbook.bids
|
|
||||||
std::vector<OrderBookEntry> bids = getOrders(OrderBookType::bid,
|
|
||||||
product,
|
|
||||||
timestamp);
|
|
||||||
|
|
||||||
// sales = []
|
// sales = []
|
||||||
std::vector<OrderBookEntry> sales;
|
std::vector<OrderBookEntry> sales;
|
||||||
|
|
||||||
// I put in a little check to ensure we have bids and asks
|
// I put in a little check to ensure we have bids and asks
|
||||||
// to process.
|
// to process.
|
||||||
@ -127,24 +118,24 @@ 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)
|
||||||
{
|
{
|
||||||
// sale = new order()
|
// sale = new order()
|
||||||
// sale.price = ask.price
|
// sale.price = ask.price
|
||||||
OrderBookEntry sale{ask.price, 0, timestamp,
|
OrderBookEntry sale{ask.price, 0, timestamp,
|
||||||
product,
|
product,
|
||||||
OrderBookType::asksale};
|
OrderBookType::asksale};
|
||||||
|
|
||||||
if (bid.username == "simuser")
|
if (bid.username == "simuser")
|
||||||
{
|
{
|
||||||
@ -154,63 +145,62 @@ std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std:
|
|||||||
if (ask.username == "simuser")
|
if (ask.username == "simuser")
|
||||||
{
|
{
|
||||||
sale.username = "simuser";
|
sale.username = "simuser";
|
||||||
sale.orderType = OrderBookType::asksale;
|
sale.orderType = OrderBookType::asksale;
|
||||||
}
|
}
|
||||||
|
|
||||||
// # now work out how much was sold and
|
// # now work out how much was sold and
|
||||||
// # create new bids and asks covering
|
// # create new bids and asks covering
|
||||||
// # anything that was not sold
|
// # anything that was not sold
|
||||||
// if bid.amount == ask.amount: # bid completely clears ask
|
// if bid.amount == ask.amount: # bid completely clears ask
|
||||||
if (bid.amount == ask.amount)
|
if (bid.amount == ask.amount)
|
||||||
{
|
{
|
||||||
// sale.amount = ask.amount
|
// sale.amount = ask.amount
|
||||||
sale.amount = ask.amount;
|
sale.amount = ask.amount;
|
||||||
// sales.append(sale)
|
// sales.append(sale)
|
||||||
sales.push_back(sale);
|
sales.push_back(sale);
|
||||||
// bid.amount = 0 # make sure the bid is not processed again
|
// bid.amount = 0 # make sure the bid is not processed again
|
||||||
bid.amount = 0;
|
bid.amount = 0;
|
||||||
// # can do no more with this ask
|
// # can do no more with this ask
|
||||||
// # go onto the next ask
|
// # go onto the next ask
|
||||||
// break
|
// break
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// if bid.amount > ask.amount: # ask is completely gone slice the bid
|
// if bid.amount > ask.amount: # ask is completely gone slice the bid
|
||||||
if (bid.amount > ask.amount)
|
if (bid.amount > ask.amount)
|
||||||
{
|
{
|
||||||
// sale.amount = ask.amount
|
// sale.amount = ask.amount
|
||||||
sale.amount = ask.amount;
|
sale.amount = ask.amount;
|
||||||
// sales.append(sale)
|
// sales.append(sale)
|
||||||
sales.push_back(sale);
|
sales.push_back(sale);
|
||||||
// # we adjust the bid in place
|
// # we adjust the bid in place
|
||||||
// # so it can be used to process the next ask
|
// # so it can be used to process the next ask
|
||||||
// bid.amount = bid.amount - ask.amount
|
// bid.amount = bid.amount - ask.amount
|
||||||
bid.amount = bid.amount - ask.amount;
|
bid.amount = bid.amount - ask.amount;
|
||||||
// # ask is completely gone, so go to next ask
|
// # ask is completely gone, so go to next ask
|
||||||
// break
|
// break
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
// sale.amount = bid.amount
|
// sale.amount = bid.amount
|
||||||
sale.amount = bid.amount;
|
sale.amount = bid.amount;
|
||||||
// sales.append(sale)
|
// sales.append(sale)
|
||||||
sales.push_back(sale);
|
sales.push_back(sale);
|
||||||
// # update the ask
|
// # update the ask
|
||||||
// # and allow further bids to process the remaining amount
|
// # and allow further bids to process the remaining amount
|
||||||
// ask.amount = ask.amount - bid.amount
|
// ask.amount = ask.amount - bid.amount
|
||||||
ask.amount = ask.amount - bid.amount;
|
ask.amount = ask.amount - bid.amount;
|
||||||
// bid.amount = 0 # make sure the bid is not processed again
|
// bid.amount = 0 # make sure the bid is not processed again
|
||||||
bid.amount = 0;
|
bid.amount = 0;
|
||||||
// # some ask remains so go to the next bid
|
// # some ask remains so go to the next bid
|
||||||
// continue
|
// continue
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sales;
|
return sales;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,33 +6,30 @@
|
|||||||
|
|
||||||
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*/
|
||||||
std::vector<std::string> getKnownProducts();
|
std::vector<std::string> getKnownProducts();
|
||||||
/** return vector of Orders according to the sent filters*/
|
/** return vector of Orders according to the sent filters*/
|
||||||
std::vector<OrderBookEntry> getOrders(OrderBookType type,
|
std::vector<OrderBookEntry> getOrders(OrderBookType type, std::string product, std::string timestamp);
|
||||||
std::string product,
|
|
||||||
std::string timestamp);
|
|
||||||
|
|
||||||
/** returns the earliest time in the orderbook*/
|
/** returns the earliest time in the orderbook*/
|
||||||
std::string getEarliestTime();
|
std::string getEarliestTime();
|
||||||
/** returns the next time after the
|
/** returns the next time after the
|
||||||
* sent time in the orderbook
|
* sent time in the orderbook
|
||||||
* If there is no next timestamp, wraps around to the start
|
* If there is no next timestamp, wraps around to the start
|
||||||
* */
|
* */
|
||||||
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 getLowPrice(std::vector<OrderBookEntry>& orders);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<OrderBookEntry> orders;
|
|
||||||
|
|
||||||
|
static double getHighPrice(std::vector<OrderBookEntry> &orders);
|
||||||
|
static double getLowPrice(std::vector<OrderBookEntry> &orders);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<OrderBookEntry> orders; // private variable which holds all loaded orders from the file
|
||||||
|
std::vector<std::string> knownProducts;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user