ATS
An algorithmic trading system completely written in C++
MarketData.h
Go to the documentation of this file.
1 
8 #ifndef ATS_MARKETDATA_H
9 #define ATS_MARKETDATA_H
10 #include <thread>
11 #include <mutex>
12 #include <unordered_map>
13 #include <unordered_set>
14 #include "ExchangeManager.h"
15 #include "OrderManager.h"
16 
17 namespace ats {
21  struct Klines {
22  std::vector<time_t> times;
23  std::vector<double> opens;
24  std::vector<double> highs;
25  std::vector<double> lows;
26  std::vector<double> closes;
27  std::vector<double> volumes;
28 
29  Klines() = default;
30 
31  void push_back(time_t time, double open, double high, double low, double close, double volume) {
32  times.push_back(time);
33  opens.push_back(open);
34  highs.push_back(high);
35  lows.push_back(low);
36  closes.push_back(close);
37  volumes.push_back(volume);
38  }
39 
40  void pop_back() {
41  if (times.empty()) return;
42  times.pop_back();
43  opens.pop_back();
44  highs.pop_back();
45  lows.pop_back();
46  closes.pop_back();
47  volumes.pop_back();
48  }
49  };
50 
54  class MarketData {
55  private:
56  std::thread mMarketDataThread;
57  std::mutex mDataMutex;
58  bool mRunning;
59  std::unordered_set<std::string> mSymbols;
60  std::unordered_map<std::string, std::vector<double>> mPrices;
61  ExchangeManager& mExchangeManager;
62  time_t mUpdateInterval;
63  std::unordered_map<std::string,OrderBook> mOrderBooks;
64  std::map<std::string,double> mBalances;
65  std::map<std::pair<std::string,std::string>,Klines> mKlines;
67  public:
73  MarketData(ExchangeManager& ems, time_t updateInterval=1);
74 
78  ~MarketData();
79 
86  explicit MarketData(const std::vector<std::string>& symbols, ExchangeManager& ems, time_t updateInterval=1);
87 
91  void start();
92 
96  void run();
97 
101  void stop();
102 
107  bool isRunning();
108 
114  void subscribe(const std::string& symbol, std::string interval="3m");
115 
120  void unsubscribe(const std::string& symbol);
121 
127  double getPrice(const std::string& symbol);
128 
134  std::vector<double> getPrices(const std::string& symbol);
135 
141  std::vector<Trade> getTradeHistory(const std::string& symbol);
142 
149  double getQtyForPrice(const std::string& symbol, double price);
150 
155  std::map<std::string,double> getBalances();
156 
164  std::string getOrderStatus(long id, const std::string& symbol);
165 
171  OrderBook getOrderBook(const std::string &symbol);
172 
179  Klines getKlines(const std::string &symbol, const std::string& interval);
180 
181  private:
186  void updatePrice(const std::string& symbol);
187 
191  void updatePrices();
192 
196  void updateKlines();
197 
202  void updateOrderBook(const std::string& symbol);
203 
207  void updateOrderBooks();
208 
212  void updateBalances();
213 
217  double jsonToDouble(Json::Value res);
218  };
219 
220 
221 } // ats
222 
223 #endif //ATS_MARKETDATA_H
Contains an abstract class ExchangeManager The ExchangeManager class is an abstract class that define...
Contains the declaration of the OrderManager class and related enums and structs.
The ExchangeManager class is an abstract class that defines the interface for managing orders on an e...
Definition: ExchangeManager.h:27
Handles the streaming of market data for the trading system.
Definition: MarketData.h:54
double getPrice(const std::string &symbol)
Retrieves the current price for a symbol.
Definition: MarketData.cpp:75
Klines getKlines(const std::string &symbol, const std::string &interval)
Retrieves Kline data.
Definition: MarketData.cpp:175
std::string getOrderStatus(long id, const std::string &symbol)
Retrieves order status.
Definition: MarketData.cpp:158
void start()
Starts the market data stream.
Definition: MarketData.cpp:24
MarketData(ExchangeManager &ems, time_t updateInterval=1)
Constructs a new MarketData object.
Definition: MarketData.cpp:10
bool isRunning()
Checks whether the market data stream is running.
Definition: MarketData.cpp:145
std::vector< double > getPrices(const std::string &symbol)
Returns prices recorded for a symbol.
Definition: MarketData.cpp:87
std::map< std::string, double > getBalances()
Retrives user's balances.
Definition: MarketData.cpp:153
double getQtyForPrice(const std::string &symbol, double price)
Retrieves the quantity for a given price and symbol.
Definition: MarketData.cpp:149
~MarketData()
Destroys the MarketData object.
Definition: MarketData.cpp:20
void subscribe(const std::string &symbol, std::string interval="3m")
Subscribes to a symbol for market data.
Definition: MarketData.cpp:52
void stop()
Stops the market data stream.
Definition: MarketData.cpp:46
void run()
Runs the market data stream.
Definition: MarketData.cpp:31
OrderBook getOrderBook(const std::string &symbol)
Retrieves the OrderBook.
Definition: MarketData.cpp:167
std::vector< Trade > getTradeHistory(const std::string &symbol)
Returns trade history for a symbol.
Definition: MarketData.cpp:92
void unsubscribe(const std::string &symbol)
Unsubscribes from a symbol for market data.
Definition: MarketData.cpp:61
Namespace for the algorithmic trading system.
Definition: BinanceExchangeManager.cpp:8
The Klines stores kline data for a specific symbol.
Definition: MarketData.h:21
The OrderBook struct represents the orderbook.
Definition: OrderManager.h:131