CBOE Emulator  1.0
structures.hpp
1 // Types and structures for the LimitOrderBook: order, limit, account
2 // Copyright 2020 Christian Kauten
3 //
4 // Author: Christian Kauten (kautenja@auburn.edu)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef ORDER_ENTRY_LIMIT_ORDER_BOOK_STRUCTURES_HPP
19 #define ORDER_ENTRY_LIMIT_ORDER_BOOK_STRUCTURES_HPP
20 
21 #include "doubly_linked_list.hpp"
22 #include "binary_search_tree.hpp"
23 #include <cstdint>
24 #include <set>
25 
27 namespace OrderEntry {
28 
30 namespace LOB {
31 
32 // ---------------------------------------------------------------------------
33 // MARK: Types
34 // ---------------------------------------------------------------------------
35 
37 enum class Side : bool { Sell = false, Buy = true };
38 
44 inline Side operator!(Side side) {
45  return static_cast<Side>(!static_cast<bool>(side));
46 }
47 
49 typedef uint64_t UID;
51 typedef uint32_t Quantity;
53 typedef uint64_t Price;
54 
55 // ---------------------------------------------------------------------------
56 // MARK: Order
57 // ---------------------------------------------------------------------------
58 
59 // forward declare the `Account` structure so `Order` can reference it
60 struct Account;
61 // forward declare the `Limit` structure so `Order` can reference it
62 struct Limit;
63 
65 struct Order : DLL::Node {
67  const UID uid = 0;
69  const Side side = Side::Sell;
73  const Price price = 0;
75  Limit* limit = nullptr;
77  Account* account = nullptr;
78 
80  Order() : DLL::Node() { }
81 
92  UID uid_,
93  Side side_,
94  Quantity quantity_,
95  Price price_,
96  Limit* limit_ = nullptr,
97  Account* account_ = nullptr
98  ) :
99  DLL::Node(),
100  uid(uid_),
101  side(side_),
102  quantity(quantity_),
103  price(price_),
104  limit(limit_),
105  account(account_) { }
106 };
107 
108 // ---------------------------------------------------------------------------
109 // MARK: Limit
110 // ---------------------------------------------------------------------------
111 
113 typedef uint32_t Count;
115 typedef uint64_t Volume;
116 
118 struct Limit : BST::Node<Price> {
122  const uint32_t _padding = 0;
126  Order* order_head = nullptr;
128  Order* order_tail = nullptr;
129 
131  Limit() : BST::Node<Price>() { }
132 
137  explicit Limit(Order* order) :
138  BST::Node<Price>(order->price),
139  count(1),
140  volume(order->quantity),
141  order_head(order),
142  order_tail(order) { }
143 };
144 
145 // ---------------------------------------------------------------------------
146 // MARK: Account
147 // ---------------------------------------------------------------------------
148 
150 typedef int64_t Shares;
152 typedef int64_t Capital;
153 
155 struct Account {
161  std::set<Order*> orders= {};
162 
168  Account(Shares shares_ = 0, Capital capital_ = 0) :
169  shares(shares_),
170  capital(capital_) { }
171 
176  inline void limit(Order* order) { orders.insert(order); }
177 
182  inline void cancel(Order* order) { orders.erase(order); }
183 
190  inline void fill(Side side, Quantity quantity, Price price) {
191  switch (side) {
192  case Side::Sell: {
193  shares -= quantity;
194  capital += quantity * price;
195  break;
196  }
197  case Side::Buy: {
198  shares += quantity;
199  capital -= quantity * price;
200  break;
201  }
202  }
203  }
204 
210  inline virtual void limit_fill(Order* limit, Order* market) {
211  cancel(limit);
212  fill(limit->side, limit->quantity, limit->price);
213  }
214 
220  inline virtual void limit_partial(Order* limit, Order* market) {
221  fill(limit->side, market->quantity, limit->price);
222  }
223 
229  inline virtual void market_fill(Order* limit, Order* market) {
230  fill(market->side, market->quantity, limit->price);
231  }
232 
238  inline virtual void market_partial(Order* limit, Order* market) {
239  fill(market->side, limit->quantity, limit->price);
240  }
241 };
242 
243 } // namespace LOB
244 
245 } // namespace OrderEntry
246 
247 #endif // ORDER_ENTRY_LIMIT_ORDER_BOOK_STRUCTURES_HPP
OrderEntry::LOB::operator!
Side operator!(Side side)
Return the opposite side using the invert operator.
Definition: structures.hpp:44
OrderEntry
Logic for sending/receiving application messages in a financial market.
Definition: authorizer.hpp:26
OrderEntry::LOB::Capital
int64_t Capital
the amount of capital in a user account
Definition: structures.hpp:152
OrderEntry::LOB::Account
A trading account for a LimitOrderBook client.
Definition: structures.hpp:155
OrderEntry::LOB::Order
A single order in the LimitOrderBook.
Definition: structures.hpp:65
OrderEntry::LOB::Order::uid
const UID uid
the day-unique ID for this particular order
Definition: structures.hpp:67
OrderEntry::LOB::Limit::volume
Volume volume
the total amount of volume at this limit price
Definition: structures.hpp:124
OrderEntry::LOB::Order::side
const Side side
a boolean determining whether the order id a buy (true) or sell (false)
Definition: structures.hpp:69
OrderEntry::LOB::Quantity
uint32_t Quantity
a type for order quantities
Definition: structures.hpp:51
OrderEntry::LOB::Limit::order_tail
Order * order_tail
the last order in the queue (last to execute)
Definition: structures.hpp:128
OrderEntry::LOB::Account::market_fill
virtual void market_fill(Order *limit, Order *market)
Fill a market order.
Definition: structures.hpp:229
OrderEntry::LOB::Count
uint32_t Count
a type for limit price order counts
Definition: structures.hpp:113
OrderEntry::LOB::Limit::Limit
Limit(Order *order)
Initialize a new limit.
Definition: structures.hpp:137
OrderEntry::LOB::Order::price
const Price price
the limit price for the order (market price if market order)
Definition: structures.hpp:73
OrderEntry::LOB::Limit::order_head
Order * order_head
the first order in the queue (next to execute)
Definition: structures.hpp:126
OrderEntry::LOB::Account::Account
Account(Shares shares_=0, Capital capital_=0)
Create an account with given values.
Definition: structures.hpp:168
OrderEntry::LOB::Limit::count
Count count
the number of orders at this limit price
Definition: structures.hpp:120
OrderEntry::LOB::Account::limit
void limit(Order *order)
Place a limit order.
Definition: structures.hpp:176
OrderEntry::LOB::Price
uint64_t Price
a type for order prices
Definition: structures.hpp:53
OrderEntry::LOB::Shares
int64_t Shares
the number of shares in a user account
Definition: structures.hpp:150
OrderEntry::LOB::Limit::Limit
Limit()
Initialize a new limit.
Definition: structures.hpp:131
OrderEntry::LOB::Account::orders
std::set< Order * > orders
the set of active orders for the account
Definition: structures.hpp:161
OrderEntry::LOB::Account::limit_fill
virtual void limit_fill(Order *limit, Order *market)
Fill a limit order.
Definition: structures.hpp:210
OrderEntry::LOB::Order::Order
Order(UID uid_, Side side_, Quantity quantity_, Price price_, Limit *limit_=nullptr, Account *account_=nullptr)
Initialize a new order data.
Definition: structures.hpp:91
OrderEntry::LOB::Volume
uint64_t Volume
a type for limit total volume
Definition: structures.hpp:115
OrderEntry::LOB::Order::account
Account * account
the account this order belongs to
Definition: structures.hpp:77
OrderEntry::LOB::Side
Side
the possible sides for the LimitTree
Definition: structures.hpp:37
OrderEntry::LOB::Account::limit_partial
virtual void limit_partial(Order *limit, Order *market)
Partially fill a limit order.
Definition: structures.hpp:220
OrderEntry::LOB::Limit::_padding
const uint32_t _padding
padding for byte alignment
Definition: structures.hpp:122
OrderEntry::LOB::Account::cancel
void cancel(Order *order)
Cancel a limit order.
Definition: structures.hpp:182
OrderEntry::LOB::Account::shares
Shares shares
the number of shares owned by the account
Definition: structures.hpp:157
OrderEntry::LOB::Limit
A price limit containing a FIFO queue of Order objects.
Definition: structures.hpp:118
OrderEntry::LOB::Order::quantity
Quantity quantity
the quantity of the order, i.e., the number of shares
Definition: structures.hpp:71
OrderEntry::LOB::Order::Order
Order()
Initialize a new order data.
Definition: structures.hpp:80
OrderEntry::LOB::UID
uint64_t UID
a type for order IDs
Definition: structures.hpp:49
OrderEntry::LOB::Account::capital
Capital capital
the total capital the account has (funds)
Definition: structures.hpp:159
OrderEntry::LOB::Account::market_partial
virtual void market_partial(Order *limit, Order *market)
Partially fill a market order.
Definition: structures.hpp:238
OrderEntry::LOB::Account::fill
void fill(Side side, Quantity quantity, Price price)
Fill a market order on given side with quantity and price.
Definition: structures.hpp:190
OrderEntry::LOB::Order::limit
Limit * limit
the limit this order falls under
Definition: structures.hpp:75