Add incorporated OrderBook into MerkelMain
This commit is contained in:
3544
CM2005 Object Oriented Programming/Topic 4/4.1.5/20200317.csv
Normal file
3544
CM2005 Object Oriented Programming/Topic 4/4.1.5/20200317.csv
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,79 @@
|
|||||||
|
#include "CSVReader.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
CSVReader::CSVReader()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<OrderBookEntry> CSVReader::readCSV(std::string csvFilename)
|
||||||
|
{
|
||||||
|
std::vector<OrderBookEntry> entries;
|
||||||
|
|
||||||
|
std::ifstream csvFile{csvFilename};
|
||||||
|
std::string line;
|
||||||
|
if (csvFile.is_open())
|
||||||
|
{
|
||||||
|
while(std::getline(csvFile, line))
|
||||||
|
{
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
start = csvLine.find_first_not_of(separator, 0);
|
||||||
|
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);
|
||||||
|
tokens.push_back(token);
|
||||||
|
start = end + 1;
|
||||||
|
}while(end > 0);
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderBookEntry CSVReader::stringsToOBE(std::vector<std::string> tokens)
|
||||||
|
{
|
||||||
|
double price, amount;
|
||||||
|
|
||||||
|
if (tokens.size() != 5) // bad
|
||||||
|
{
|
||||||
|
//std::cout << "Bad line " << std::endl;
|
||||||
|
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 << "Bad float! " << tokens[3]<< std::endl;
|
||||||
|
std::cout << "Bad float! " << tokens[4]<< std::endl;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderBookEntry obe{price,
|
||||||
|
amount,
|
||||||
|
tokens[0],
|
||||||
|
tokens[1],
|
||||||
|
OrderBookEntry::stringToOrderBookType(tokens[2])};
|
||||||
|
|
||||||
|
return obe;
|
||||||
|
}
|
||||||
|
|
||||||
18
CM2005 Object Oriented Programming/Topic 4/4.1.5/CSVReader.h
Normal file
18
CM2005 Object Oriented Programming/Topic 4/4.1.5/CSVReader.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class CSVReader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CSVReader();
|
||||||
|
|
||||||
|
static std::vector<OrderBookEntry> readCSV(std::string csvFile);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::vector<std::string> tokenise(std::string csvLine, char separator);
|
||||||
|
static OrderBookEntry stringsToOBE(std::vector<std::string> strings);
|
||||||
|
|
||||||
|
};
|
||||||
127
CM2005 Object Oriented Programming/Topic 4/4.1.5/MerkelMain.cpp
Normal file
127
CM2005 Object Oriented Programming/Topic 4/4.1.5/MerkelMain.cpp
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
#include "MerkelMain.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
#include "CSVReader.h"
|
||||||
|
|
||||||
|
MerkelMain::MerkelMain()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::init()
|
||||||
|
{
|
||||||
|
int input;
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
printMenu();
|
||||||
|
input = getUserOption();
|
||||||
|
processUserOption(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::printMenu()
|
||||||
|
{
|
||||||
|
// 1 print help
|
||||||
|
std::cout << "1: Print help " << std::endl;
|
||||||
|
// 2 print exchange stats
|
||||||
|
std::cout << "2: Print exchange stats" << std::endl;
|
||||||
|
// 3 make an offer
|
||||||
|
std::cout << "3: Make an offer " << std::endl;
|
||||||
|
// 4 make a bid
|
||||||
|
std::cout << "4: Make a bid " << std::endl;
|
||||||
|
// 5 print wallet
|
||||||
|
std::cout << "5: Print wallet " << std::endl;
|
||||||
|
// 6 continue
|
||||||
|
std::cout << "6: Continue " << std::endl;
|
||||||
|
|
||||||
|
std::cout << "============== " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::printHelp()
|
||||||
|
{
|
||||||
|
std::cout << "Help - your aim is to make money. Analyse the market and make bids and offers. " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::printMarketStats()
|
||||||
|
{
|
||||||
|
// 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::enterOffer()
|
||||||
|
{
|
||||||
|
std::cout << "Mark and offer - enter the amount " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::enterBid()
|
||||||
|
{
|
||||||
|
std::cout << "Make a bid - enter the amount" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::printWallet()
|
||||||
|
{
|
||||||
|
std::cout << "Your wallet is empty. " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::gotoNextTimeframe()
|
||||||
|
{
|
||||||
|
std::cout << "Going to next time frame. " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int MerkelMain::getUserOption()
|
||||||
|
{
|
||||||
|
int userOption;
|
||||||
|
|
||||||
|
std::cout << "Type in 1-6" << std::endl;
|
||||||
|
std::cin >> userOption;
|
||||||
|
std::cout << "You chose: " << userOption << std::endl;
|
||||||
|
return userOption;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::processUserOption(int userOption)
|
||||||
|
{
|
||||||
|
if (userOption == 0) // bad input
|
||||||
|
{
|
||||||
|
std::cout << "Invalid choice. Choose 1-6" << std::endl;
|
||||||
|
}
|
||||||
|
if (userOption == 1)
|
||||||
|
{
|
||||||
|
printHelp();
|
||||||
|
}
|
||||||
|
if (userOption == 2)
|
||||||
|
{
|
||||||
|
printMarketStats();
|
||||||
|
}
|
||||||
|
if (userOption == 3)
|
||||||
|
{
|
||||||
|
enterOffer();
|
||||||
|
}
|
||||||
|
if (userOption == 4)
|
||||||
|
{
|
||||||
|
enterBid();
|
||||||
|
}
|
||||||
|
if (userOption == 5)
|
||||||
|
{
|
||||||
|
printWallet();
|
||||||
|
}
|
||||||
|
if (userOption == 6)
|
||||||
|
{
|
||||||
|
gotoNextTimeframe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
#include "OrderBook.h"
|
||||||
|
|
||||||
|
class MerkelMain
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MerkelMain();
|
||||||
|
/** Call this to start the sim */
|
||||||
|
void init();
|
||||||
|
private:
|
||||||
|
void loadOrderBook();
|
||||||
|
void printMenu();
|
||||||
|
void printHelp();
|
||||||
|
void printMarketStats();
|
||||||
|
void enterOffer();
|
||||||
|
void enterBid();
|
||||||
|
void printWallet();
|
||||||
|
void gotoNextTimeframe();
|
||||||
|
int getUserOption();
|
||||||
|
void processUserOption(int userOption);
|
||||||
|
OrderBook orderBook{"20200317.csv"};
|
||||||
|
};
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
#include "OrderBook.h"
|
||||||
|
#include "CSVReader.h"
|
||||||
|
|
||||||
|
/* Construct, reading a csv data file */
|
||||||
|
OrderBook::OrderBook(std::string filename)
|
||||||
|
{
|
||||||
|
orders = CSVReader::readCSV(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* return vector of all known products in the dataset */
|
||||||
|
std::vector<std::string> OrderBook::getKnownProducts()
|
||||||
|
{
|
||||||
|
std::vector<std::string> products;
|
||||||
|
return products;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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> order_selection;
|
||||||
|
return order_selection;
|
||||||
|
}
|
||||||
19
CM2005 Object Oriented Programming/Topic 4/4.1.5/OrderBook.h
Normal file
19
CM2005 Object Oriented Programming/Topic 4/4.1.5/OrderBook.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
#include "CSVReader.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class OrderBook
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/* Construct, reading a csv data file */
|
||||||
|
OrderBook(std::string filename);
|
||||||
|
/* return vector of all known 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);
|
||||||
|
private:
|
||||||
|
std::vector<OrderBookEntry> orders;
|
||||||
|
};
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
#include "OrderBookEntry.h"
|
||||||
|
|
||||||
|
OrderBookEntry::OrderBookEntry( double _price,
|
||||||
|
double _amount,
|
||||||
|
std::string _timestamp,
|
||||||
|
std::string _product,
|
||||||
|
OrderBookType _orderType)
|
||||||
|
: price(_price),
|
||||||
|
amount(_amount),
|
||||||
|
timestamp(_timestamp),
|
||||||
|
product(_product),
|
||||||
|
orderType(_orderType)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderBookType OrderBookEntry::stringToOrderBookType(std::string s)
|
||||||
|
{
|
||||||
|
if (s == "ask")
|
||||||
|
{
|
||||||
|
return OrderBookType::ask;
|
||||||
|
}
|
||||||
|
if (s == "bid")
|
||||||
|
{
|
||||||
|
return OrderBookType::bid;
|
||||||
|
}
|
||||||
|
return OrderBookType::unknown;
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
enum class OrderBookType{bid, ask, unknown};
|
||||||
|
|
||||||
|
class OrderBookEntry
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
OrderBookEntry( double _price,
|
||||||
|
double _amount,
|
||||||
|
std::string _timestamp,
|
||||||
|
std::string _product,
|
||||||
|
OrderBookType _orderType);
|
||||||
|
|
||||||
|
static OrderBookType stringToOrderBookType(std::string s);
|
||||||
|
|
||||||
|
double price;
|
||||||
|
double amount;
|
||||||
|
std::string timestamp;
|
||||||
|
std::string product;
|
||||||
|
OrderBookType orderType;
|
||||||
|
};
|
||||||
|
|
||||||
BIN
CM2005 Object Oriented Programming/Topic 4/4.1.5/a.exe
Normal file
BIN
CM2005 Object Oriented Programming/Topic 4/4.1.5/a.exe
Normal file
Binary file not shown.
56
CM2005 Object Oriented Programming/Topic 4/4.1.5/main.cpp
Normal file
56
CM2005 Object Oriented Programming/Topic 4/4.1.5/main.cpp
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
#include "MerkelMain.h"
|
||||||
|
#include "CSVReader.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
MerkelMain app{};
|
||||||
|
app.init();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// std::vector<OrderBookEntry> orders;
|
||||||
|
|
||||||
|
// orders.push_back( OrderBookEntry{1000,
|
||||||
|
// 0.02,
|
||||||
|
// "2020/03/17 17:01:24.884492",
|
||||||
|
// "BTC/USDT",
|
||||||
|
// OrderBookType::bid} );
|
||||||
|
|
||||||
|
// orders.push_back( OrderBookEntry{2000,
|
||||||
|
// 0.02,
|
||||||
|
// "2020/03/17 17:01:24.884492",
|
||||||
|
// "BTC/USDT",
|
||||||
|
// OrderBookType::bid} );
|
||||||
|
|
||||||
|
|
||||||
|
// // std::cout << "The price is " << orders[1].price << std::endl;
|
||||||
|
|
||||||
|
// for (OrderBookEntry& order : orders)
|
||||||
|
// {
|
||||||
|
// std::cout << "The price is " << order.price << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (unsigned int i = 0; i < orders.size() ; ++i)
|
||||||
|
// {
|
||||||
|
// std::cout << "The price is " << orders[i].price << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (unsigned int i = 0; i < orders.size() ; ++i)
|
||||||
|
// {
|
||||||
|
// std::cout << "The price is " << orders.at(i).price << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user