Update formatting
This commit is contained in:
@ -4,44 +4,39 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
/** construct, reading a csv data file */
|
||||
OrderBook::OrderBook(std::string filename)
|
||||
{
|
||||
orders = CSVReader::readCSV(filename);
|
||||
}
|
||||
|
||||
/** return vector of all know products in the dataset*/
|
||||
std::vector<std::string> OrderBook::getKnownProducts()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// now flatten the map to a vector of strings
|
||||
for (auto const& e : prodMap)
|
||||
for (auto const &e : prodMap)
|
||||
{
|
||||
products.push_back(e.first);
|
||||
knownProducts.push_back(e.first);
|
||||
}
|
||||
}
|
||||
|
||||
return products;
|
||||
/** return vector of all know products in the dataset*/
|
||||
std::vector<std::string> OrderBook::getKnownProducts()
|
||||
{
|
||||
return knownProducts;
|
||||
}
|
||||
/** return vector of Orders according to the sent filters*/
|
||||
std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type,
|
||||
std::string product,
|
||||
std::string timestamp)
|
||||
std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type, std::string product, std::string timestamp)
|
||||
{
|
||||
std::vector<OrderBookEntry> orders_sub;
|
||||
for (OrderBookEntry& e : orders)
|
||||
for (OrderBookEntry &e : orders)
|
||||
{
|
||||
if (e.orderType == type &&
|
||||
e.product == product &&
|
||||
e.timestamp == timestamp )
|
||||
e.timestamp == timestamp)
|
||||
{
|
||||
orders_sub.push_back(e);
|
||||
}
|
||||
@ -49,24 +44,24 @@ std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type,
|
||||
return orders_sub;
|
||||
}
|
||||
|
||||
|
||||
double OrderBook::getHighPrice(std::vector<OrderBookEntry>& orders)
|
||||
double OrderBook::getHighPrice(std::vector<OrderBookEntry> &orders)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
double OrderBook::getLowPrice(std::vector<OrderBookEntry>& orders)
|
||||
double OrderBook::getLowPrice(std::vector<OrderBookEntry> &orders)
|
||||
{
|
||||
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;
|
||||
}
|
||||
@ -79,7 +74,7 @@ std::string OrderBook::getEarliestTime()
|
||||
std::string OrderBook::getNextTime(std::string timestamp)
|
||||
{
|
||||
std::string next_timestamp = "";
|
||||
for (OrderBookEntry& e : orders)
|
||||
for (OrderBookEntry &e : orders)
|
||||
{
|
||||
if (e.timestamp > timestamp)
|
||||
{
|
||||
@ -94,7 +89,7 @@ std::string OrderBook::getNextTime(std::string timestamp)
|
||||
return next_timestamp;
|
||||
}
|
||||
|
||||
void OrderBook::insertOrder(OrderBookEntry& order)
|
||||
void OrderBook::insertOrder(OrderBookEntry &order)
|
||||
{
|
||||
orders.push_back(order);
|
||||
std::sort(orders.begin(), orders.end(), OrderBookEntry::compareByTimestamp);
|
||||
@ -102,14 +97,10 @@ void OrderBook::insertOrder(OrderBookEntry& order)
|
||||
|
||||
std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std::string timestamp)
|
||||
{
|
||||
// asks = orderbook.asks
|
||||
std::vector<OrderBookEntry> asks = getOrders(OrderBookType::ask,
|
||||
product,
|
||||
timestamp);
|
||||
// bids = orderbook.bids
|
||||
std::vector<OrderBookEntry> bids = getOrders(OrderBookType::bid,
|
||||
product,
|
||||
timestamp);
|
||||
// asks = orderbook.asks
|
||||
std::vector<OrderBookEntry> asks = getOrders(OrderBookType::ask, product, timestamp);
|
||||
// bids = orderbook.bids
|
||||
std::vector<OrderBookEntry> bids = getOrders(OrderBookType::bid, product, timestamp);
|
||||
|
||||
// sales = []
|
||||
std::vector<OrderBookEntry> sales;
|
||||
@ -127,15 +118,15 @@ std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std:
|
||||
// sort bids highest first
|
||||
std::sort(bids.begin(), bids.end(), OrderBookEntry::compareByPriceDesc);
|
||||
// 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 << "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 (OrderBookEntry& bid : bids)
|
||||
for (OrderBookEntry &bid : bids)
|
||||
{
|
||||
// if bid.price >= ask.price # we have a match
|
||||
if (bid.price >= ask.price)
|
||||
@ -190,7 +181,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)
|
||||
|
||||
@ -6,15 +6,13 @@
|
||||
|
||||
class OrderBook
|
||||
{
|
||||
public:
|
||||
public:
|
||||
/** construct, reading a csv data file */
|
||||
OrderBook(std::string filename);
|
||||
/** return vector of all know products in the dataset*/
|
||||
std::vector<std::string> getKnownProducts();
|
||||
/** return vector of Orders according to the sent filters*/
|
||||
std::vector<OrderBookEntry> getOrders(OrderBookType type,
|
||||
std::string product,
|
||||
std::string timestamp);
|
||||
std::vector<OrderBookEntry> getOrders(OrderBookType type, std::string product, std::string timestamp);
|
||||
|
||||
/** returns the earliest time in the orderbook*/
|
||||
std::string getEarliestTime();
|
||||
@ -24,15 +22,14 @@ class OrderBook
|
||||
* */
|
||||
std::string getNextTime(std::string timestamp);
|
||||
|
||||
void insertOrder(OrderBookEntry& order);
|
||||
void insertOrder(OrderBookEntry &order);
|
||||
|
||||
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