Orderbook Simulation
OrderbookSim is a C++ application simulating a financial market order book. It efficiently manages and matches buy and sell orders while calculating the Volume-Weighted Average Price (VWAP).
OrderDetailHistory.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <vector>
12 #include <iostream>
13 #include <fstream>
14 #include <unordered_map>
15 #include "json.hpp"
16 #include "NeuralNetwork.hpp"
17 #include "OrderDetail.hpp"
18 #include "MatchedOrderDetails.hpp"
19 
28 private:
29  std::vector<OrderDetail> buyHistory;
30  std::vector<OrderDetail> sellHistory;
31  std::vector<MatchedOrderDetail> _purchaseHistory;
32  std::unordered_map<OrderId, OrderDetail> _liveOrders;
33 
34  NeuralNetwork neutralNetwork;
35  Price lastPrediction = 0;
36  Price PRICESCALE = 500;
37 
42  std::pair<std::vector<OrderDetail>, std::vector<OrderDetail>> _getLiveOrders() const;
43 
47  void addOrderToBuyHistory(const OrderType type, const OrderId id, const Side side, const Price price, const Quantity qty);
48 
52  void addOrderToSellHistory(const OrderType type, const OrderId id, const Side side, const Price price, const Quantity qty);
53 
57  void saveHistoryToJson(const std::string& filename, const std::vector<OrderDetail> history);
58 
62  void saveHistoryToJson(const std::string& filename, const std::vector<MatchedOrderDetail> history);
63 
67  void _loadJsonLiveOrders(const std::string& SellFilename, const std::string& buyFilename);
68 
72  void _printLiveOrders(const std::string& SellFilename, const std::string& buyFilename);
73 
77  void _printAHistory(const std::vector<OrderDetail>& history);
78 
83  double getCurrentTimeAsFractionOfDay();
84 
88  void loadHistoryToNeuralNetwork(std::vector<Price> inputPrice, std::vector<Price> outputPrice);
89 
90 public:
95  const std::vector<unsigned> topology = { 2, 3, 1 };
96  neutralNetwork.loadTogology(topology);
97  }
98 
103  int buyHistorySize() const;
104 
109  int sellHistorySize() const;
110 
115  int purchaseHistorySize() const;
116 
120  void updateNeutralNetwork(Price price, Quantity quantity);
121 
125  void addOrderToHistory(const OrderType type, const OrderId id, const Side side, const Price price, const Quantity qty);
126 
130  void addOrderToPurchaseHistory(const Price price, const Quantity qty);
131 
135  void saveHistoryToJson(const std::string& buyfilename, const std::string& sellfilename, const std::string& purchasefilename);
136 
140  void saveHistoryToJson(const std::string& buyfilename = "BuyHistory.json", const std::string& sellfilename = "SellHistory.json", const std::string& purchasefilename = "PurchaseHistory.json", const std::string& liveSellOrdersFilename = "LiveSellHistory.json", const std::string& buySellOrdersFilename = "LiveBuyHistory.json");
141 
145  void removeMatchedOrder(const OrderId bidId, const OrderId askID);
146 
150  void _printSellHistory();
151 
155  void _printBuyHistory();
156 
160  void _printPurchaseHistory();
161 
166 
171  Price getVWAP();
172 
177  Price getVWAP(int n);
178 
184 
189  std::unordered_map<OrderId, OrderDetail> getLiveOrders();
190 
194  void deleteALiveOrder(OrderId id);
195 };
Defines the NeuralNetwork class for managing and running a neural network.
Defines the OrderDetail class for storing detailed information about individual orders.
OrderType
Specifies the different types of orders that can be used in a trading system.
Definition: OrderType.hpp:18
Side
Represents the side of an order in a trading system (either buy or sell).
Definition: Side.hpp:16
std::int32_t Price
Represents the price of an order or trade.
Definition: Usings.hpp:21
std::uint32_t Quantity
Represents the quantity or volume of assets in an order or trade.
Definition: Usings.hpp:29
std::uint64_t OrderId
Represents a unique identifier for an order.
Definition: Usings.hpp:37
Manages a neural network's structure, data, and training process.
Definition: NeuralNetwork.hpp:22
void loadTogology(const std::vector< unsigned > &topology)
Loads the topology of the neural network.
Definition: NeuralNetwork.cpp:19
Manages the history of orders and tracks live orders in the orderbook.
Definition: OrderDetailHistory.hpp:27
void addOrderToPurchaseHistory(const Price price, const Quantity qty)
Adds a new purchase order to the purchase history.
Definition: OrderDetailHistory.cpp:91
void removeMatchedOrder(const OrderId bidId, const OrderId askID)
Removes matched orders by IDs from the live orders map.
Definition: OrderDetailHistory.cpp:48
OrderDetailHistory()
Default constructor that initializes the neural network with a predefined topology.
Definition: OrderDetailHistory.hpp:94
std::unordered_map< OrderId, OrderDetail > getLiveOrders()
Retrieves a map of all currently live orders.
Definition: OrderDetailHistory.cpp:194
void _printLiveOrders()
Prints the live orders to the console.
void saveHistoryToJson(const std::string &buyfilename, const std::string &sellfilename, const std::string &purchasefilename)
Saves the buy, sell, and purchase histories to JSON files.
void _printBuyHistory()
Prints the buy history to the console.
Definition: OrderDetailHistory.cpp:107
void addOrderToHistory(const OrderType type, const OrderId id, const Side side, const Price price, const Quantity qty)
Adds an order to the appropriate history (buy or sell) based on its side.
Definition: OrderDetailHistory.cpp:77
int sellHistorySize() const
Returns the size of the sell history.
Price getPrediction()
Retrieves the last prediction from the neural network.
Definition: OrderDetailHistory.cpp:185
void deleteALiveOrder(OrderId id)
Deletes a live order by ID.
Definition: OrderDetailHistory.cpp:203
int buyHistorySize() const
Returns the size of the buy history.
void saveHistoryToJson(const std::string &buyfilename="BuyHistory.json", const std::string &sellfilename="SellHistory.json", const std::string &purchasefilename="PurchaseHistory.json", const std::string &liveSellOrdersFilename="LiveSellHistory.json", const std::string &buySellOrdersFilename="LiveBuyHistory.json")
Saves multiple histories including live orders to JSON files.
void _printSellHistory()
Prints the sell history to the console.
Definition: OrderDetailHistory.cpp:99
Price getVWAP()
Calculates the Volume-Weighted Average Price (VWAP) of all purchases.
Definition: OrderDetailHistory.cpp:130
int purchaseHistorySize() const
Returns the size of the purchase history.
void updateNeutralNetwork(Price price, Quantity quantity)
Updates the neural network with the latest order price and quantity data.
Definition: OrderDetailHistory.cpp:213
void _printPurchaseHistory()
Prints the purchase history to the console.
Definition: OrderDetailHistory.cpp:115