FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
order_book.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include <map>
11#include <list>
12#include <unordered_map>
13#include <vector>
14#include <functional>
15#include <optional>
16#include "app/model/order.hpp"
17
18namespace fix40 {
19
27struct Trade {
28 std::string tradeID;
29 std::string symbol;
30 double price;
31 int64_t qty;
32 std::chrono::system_clock::time_point timestamp;
33
34 // 买方信息
35 std::string buyOrderID;
36 std::string buyClOrdID;
37 int64_t buyOrderQty;
38 double buyPrice;
40 int64_t buyCumQty;
41 int64_t buyLeavesQty;
42 double buyAvgPx;
44
45 // 卖方信息
46 std::string sellOrderID;
47 std::string sellClOrdID;
48 int64_t sellOrderQty;
49 double sellPrice;
51 int64_t sellCumQty;
52 int64_t sellLeavesQty;
53 double sellAvgPx;
55
63};
64
71struct PriceLevel {
72 double price;
73 std::list<Order> orders;
74 int64_t totalQty;
75
76 explicit PriceLevel(double p = 0.0) : price(p), totalQty(0) {}
77
78 bool empty() const { return orders.empty(); }
79};
80
111public:
116 explicit OrderBook(const std::string& symbol);
117
121 const std::string& getSymbol() const { return symbol_; }
122
123 // =========================================================================
124 // 订单操作
125 // =========================================================================
126
138 std::vector<Trade> addOrder(Order& order);
139
145 std::optional<Order> cancelOrder(const std::string& clOrdID);
146
152 const Order* findOrder(const std::string& clOrdID) const;
153
154 // =========================================================================
155 // 行情查询
156 // =========================================================================
157
162 std::optional<double> getBestBid() const;
163
168 std::optional<double> getBestAsk() const;
169
175 std::vector<PriceLevel> getBidLevels(size_t levels = 5) const;
176
182 std::vector<PriceLevel> getAskLevels(size_t levels = 5) const;
183
187 size_t getBidOrderCount() const { return bidOrderCount_; }
188
192 size_t getAskOrderCount() const { return askOrderCount_; }
193
197 bool empty() const { return bids_.empty() && asks_.empty(); }
198
199private:
203 std::string generateOrderID();
204
208 std::string generateTradeID();
209
215 std::vector<Trade> matchBuyOrder(Order& order);
216
222 std::vector<Trade> matchSellOrder(Order& order);
223
227 void addToBids(const Order& order);
228
232 void addToAsks(const Order& order);
233
240 std::optional<Order> removeOrder(const std::string& clOrdID, OrderSide side);
241
247 int64_t calculateMatchableQty(const Order& order) const;
248
249 std::string symbol_;
250
252 std::map<double, PriceLevel, std::greater<double>> bids_;
253
255 std::map<double, PriceLevel, std::less<double>> asks_;
256
258 struct OrderLocation {
259 OrderSide side;
260 double price;
261 };
262 std::unordered_map<std::string, OrderLocation> orderIndex_;
263
264 size_t bidOrderCount_ = 0;
265 size_t askOrderCount_ = 0;
266 uint64_t nextOrderID_ = 1;
267 uint64_t nextTradeID_ = 1;
268};
269
270} // namespace fix40
订单簿
Definition order_book.hpp:110
bool empty() const
检查订单簿是否为空
Definition order_book.hpp:197
std::vector< PriceLevel > getBidLevels(size_t levels=5) const
获取买盘深度
Definition order_book.cpp:516
const std::string & getSymbol() const
获取合约代码
Definition order_book.hpp:121
size_t getAskOrderCount() const
获取卖盘总订单数
Definition order_book.hpp:192
std::optional< Order > cancelOrder(const std::string &clOrdID)
撤销订单
Definition order_book.cpp:397
std::vector< Trade > addOrder(Order &order)
添加订单并尝试撮合
Definition order_book.cpp:30
size_t getBidOrderCount() const
获取买盘总订单数
Definition order_book.hpp:187
std::optional< double > getBestAsk() const
获取最优卖价
Definition order_book.cpp:509
std::optional< double > getBestBid() const
获取最优买价
Definition order_book.cpp:502
std::vector< PriceLevel > getAskLevels(size_t levels=5) const
获取卖盘深度
Definition order_book.cpp:530
const Order * findOrder(const std::string &clOrdID) const
查找订单
Definition order_book.cpp:470
Definition matching_engine.hpp:23
OrderStatus
订单状态
Definition order.hpp:58
@ NEW
新订单(已接受)
OrderSide
买卖方向
Definition order.hpp:26
OrderType
订单类型
Definition order.hpp:35
@ LIMIT
限价单
内部订单数据结构
Order order
Definition simulation_app.cpp:46
内部订单表示
Definition order.hpp:90
价格档位
Definition order_book.hpp:71
int64_t totalQty
该价位总数量
Definition order_book.hpp:74
std::list< Order > orders
订单队列(时间优先)
Definition order_book.hpp:73
bool empty() const
Definition order_book.hpp:78
double price
价格
Definition order_book.hpp:72
PriceLevel(double p=0.0)
Definition order_book.hpp:76
成交记录
Definition order_book.hpp:27
double buyAvgPx
买方成交后平均价
Definition order_book.hpp:42
Trade()
Definition order_book.hpp:56
std::string buyOrderID
买方订单ID
Definition order_book.hpp:35
std::string sellOrderID
卖方订单ID
Definition order_book.hpp:46
int64_t buyLeavesQty
买方成交后剩余数量
Definition order_book.hpp:41
int64_t sellCumQty
卖方成交后累计数量
Definition order_book.hpp:51
OrderType sellOrdType
卖方订单类型
Definition order_book.hpp:50
std::chrono::system_clock::time_point timestamp
成交时间
Definition order_book.hpp:32
OrderStatus buyStatus
买方成交后状态
Definition order_book.hpp:43
OrderType buyOrdType
买方订单类型
Definition order_book.hpp:39
std::string tradeID
成交ID
Definition order_book.hpp:28
OrderStatus sellStatus
卖方成交后状态
Definition order_book.hpp:54
double price
成交价格
Definition order_book.hpp:30
double sellAvgPx
卖方成交后平均价
Definition order_book.hpp:53
int64_t buyCumQty
买方成交后累计数量
Definition order_book.hpp:40
int64_t qty
成交数量
Definition order_book.hpp:31
double sellPrice
卖方订单价格
Definition order_book.hpp:49
int64_t sellLeavesQty
卖方成交后剩余数量
Definition order_book.hpp:52
double buyPrice
买方订单价格
Definition order_book.hpp:38
std::string symbol
合约代码
Definition order_book.hpp:29
std::string buyClOrdID
买方客户订单ID
Definition order_book.hpp:36
std::string sellClOrdID
卖方客户订单ID
Definition order_book.hpp:47
int64_t buyOrderQty
买方原始订单数量
Definition order_book.hpp:37
int64_t sellOrderQty
卖方原始订单数量
Definition order_book.hpp:48