Add created and set up the CSVReader.h file

This commit is contained in:
Lev
2021-05-24 09:54:46 -05:00
parent f812c0e244
commit d7eb70f18f
10 changed files with 3882 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,15 @@
#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 stringsToBE(std::vector<std::string> strings);
};

View File

@ -0,0 +1,129 @@
#include "MerkelMain.h"
#include <iostream>
#include <vector>
#include "OrderBookEntry.h"
MerkelMain::MerkelMain()
{
}
void MerkelMain::init()
{
loadOrderBook();
int input;
while(true)
{
printMenu();
input = getUserOption();
processUserOption(input);
}
}
void MerkelMain::loadOrderBook()
{
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} );
}
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;
}
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();
}
}

View File

@ -0,0 +1,27 @@
#pragma once
#include <vector>
#include "OrderBookEntry.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);
std::vector<OrderBookEntry> orders;
};

View File

@ -0,0 +1,15 @@
#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)
{
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <string>
enum class OrderBookType{bid, ask};
class OrderBookEntry
{
public:
OrderBookEntry( double _price,
double _amount,
std::string _timestamp,
std::string _product,
OrderBookType _orderType);
double price;
double amount;
std::string timestamp;
std::string product;
OrderBookType orderType;
};

Binary file not shown.

View File

@ -0,0 +1,53 @@
#include <iostream>
#include <string>
#include <vector>
#include "OrderBookEntry.h"
#include "MerkelMain.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;
// }

View File

@ -0,0 +1,72 @@
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
std::vector<std::string> 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;
}
int main()
{
std::string filename = "20200317.csv";
std::string line;
std::ifstream csvFile{filename};
std::vector<std::string> tokens;
if(csvFile.is_open())
{
std::cout << "File open" << std::endl;
while(std::getline(csvFile, line))
{
std::cout << "Read line: " << line << std::endl;
tokens = Tokenise(line, ',');
if(tokens.size() != 5) //bad
{
std::cout << "Bad line" << std::endl;
continue;
}
double price{};
double amount{};
// We checked and we have 5 tokens
try
{
price = std::stod(tokens[3]);
amount = std::stod(tokens[4]);
std::cout << price << std::endl;
std::cout << amount << std::endl;
}
catch(std::exception& e)
{
//error reading file
std::cout << "ERROR" << std::endl;
}
break; // can be removed to read all lines in the csv
}
csvFile.close();
}
else
{
std::cout << "Could not open file" << std::endl;
}
return 0;
}