CBOE Emulator  1.0
system_account.hpp
1 // An account on the server.
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_SYSTEM_ACCOUNT_HPP
19 #define ORDER_ENTRY_SYSTEM_ACCOUNT_HPP
20 
21 #include "limit_order_book/limit_order_book.hpp"
22 #include "messages.hpp"
23 #include <string>
24 #include <ostream>
25 #include <iomanip>
26 
28 namespace OrderEntry {
29 
34 template<typename Handler>
35 struct SystemAccount : public LOB::Account {
37  std::string username = "";
39  std::string password = "";
41  Handler* handler = nullptr;
43  bool is_connected = false;
44 
46  SystemAccount() : LOB::Account() { }
47 
53  SystemAccount(const std::string& username_, const std::string& password_) :
54  LOB::Account(),
55  username(username_),
56  password(password_) { }
57 
62  explicit SystemAccount(Handler* handler_) :
63  LOB::Account(),
64  handler(handler_) { }
65 
70  inline static const std::string table_header() {
71  return "\
72 | UN | Password | Auth | Shares | Capital | Orders \n\
73 |:-----|:-------------|:-----|:----------|:----------|:----------|\
74 ";
75  }
76 
83  inline friend std::ostream& operator<<(
84  std::ostream& stream,
85  const SystemAccount& account
86  ) {
87  // write the basic scalar values
88  stream
89  << "| " << std::setw(5) << std::left << account.username
90  << "| " << std::setw(13) << std::left << account.password
91  << "| " << std::setw(5) << std::left << account.is_connected
92  << "| " << std::setw(10) << std::left << account.shares
93  << "| " << std::setw(10) << std::left << account.capital
94  << "| " << std::setw(10) << std::left << account.orders.size()
95  << "|";
96  return stream;
97  }
98 
104  inline void limit_fill(LOB::Order* limit, LOB::Order* market) override {
106  if (handler == nullptr) return;
107  handler->trade(
108  limit->uid,
109  limit->price,
110  limit->quantity,
111  0, // filled, no quantity left
112  bool_to_side(static_cast<bool>(limit->side))
113  );
114  }
115 
121  inline void limit_partial(LOB::Order* limit, LOB::Order* market) override {
123  if (handler == nullptr) return;
124  handler->trade(
125  limit->uid,
126  limit->price,
127  market->quantity,
128  limit->quantity,
129  bool_to_side(static_cast<bool>(limit->side))
130  );
131  }
132 
138  inline void market_fill(LOB::Order* limit, LOB::Order* market) override {
140  if (handler == nullptr) return;
141  handler->trade(
142  0, // market->uid,
143  limit->price,
144  market->quantity,
145  0, // filled, no quantity left
146  bool_to_side(static_cast<bool>(market->side))
147  );
148  }
149 
155  inline void market_partial(LOB::Order* limit, LOB::Order* market) override {
157  if (handler == nullptr) return;
158  handler->trade(
159  0, // market->uid,
160  limit->price,
161  limit->quantity,
162  market->quantity,
163  bool_to_side(static_cast<bool>(market->side))
164  );
165  }
166 };
167 
168 } // namespace OrderEntry
169 
170 #endif // ORDER_ENTRY_SYSTEM_ACCOUNT_HPP
OrderEntry::bool_to_side
constexpr Side bool_to_side(bool side)
Convert a boolean to an order side.
Definition: messages.hpp:124
OrderEntry
Logic for sending/receiving application messages in a financial market.
Definition: authorizer.hpp:26
OrderEntry::SystemAccount::is_connected
bool is_connected
whether the account is currently logged in
Definition: system_account.hpp:43
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::side
const Side side
a boolean determining whether the order id a buy (true) or sell (false)
Definition: structures.hpp:69
OrderEntry::SystemAccount::market_partial
void market_partial(LOB::Order *limit, LOB::Order *market) override
Partially fill a market order.
Definition: system_account.hpp:155
OrderEntry::LOB::Account::market_fill
virtual void market_fill(Order *limit, Order *market)
Fill a market order.
Definition: structures.hpp:229
OrderEntry::SystemAccount::limit_partial
void limit_partial(LOB::Order *limit, LOB::Order *market) override
Partially fill a limit order.
Definition: system_account.hpp:121
OrderEntry::SystemAccount::username
std::string username
the user name for the account
Definition: system_account.hpp:37
OrderEntry::LOB::Account::Account
Account(Shares shares_=0, Capital capital_=0)
Create an account with given values.
Definition: structures.hpp:168
OrderEntry::SystemAccount::SystemAccount
SystemAccount(Handler *handler_)
Initialize a new system account with given handler.
Definition: system_account.hpp:62
OrderEntry::LOB::Account::limit
void limit(Order *order)
Place a limit order.
Definition: structures.hpp:176
OrderEntry::SystemAccount::operator<<
friend std::ostream & operator<<(std::ostream &stream, const SystemAccount &account)
Write the data from the account to a stream.
Definition: system_account.hpp:83
OrderEntry::SystemAccount::SystemAccount
SystemAccount()
Initialize a new system account.
Definition: system_account.hpp:46
OrderEntry::SystemAccount::handler
Handler * handler
the connection for this account
Definition: system_account.hpp:41
OrderEntry::SystemAccount::SystemAccount
SystemAccount(const std::string &username_, const std::string &password_)
Initialize a new system account with given handler.
Definition: system_account.hpp:53
OrderEntry::SystemAccount::market_fill
void market_fill(LOB::Order *limit, LOB::Order *market) override
Fill a market order.
Definition: system_account.hpp:138
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::SystemAccount::limit_fill
void limit_fill(LOB::Order *limit, LOB::Order *market) override
Fill a limit order.
Definition: system_account.hpp:104
OrderEntry::SystemAccount::password
std::string password
the password for the account
Definition: system_account.hpp:39
OrderEntry::LOB::Account::limit_partial
virtual void limit_partial(Order *limit, Order *market)
Partially fill a limit order.
Definition: structures.hpp:220
OrderEntry::LOB::Account::shares
Shares shares
the number of shares owned by the account
Definition: structures.hpp:157
OrderEntry::SystemAccount
A subclass of the LOB::Account that manages client state on the market server.
Definition: system_account.hpp:35
OrderEntry::LOB::Order::quantity
Quantity quantity
the quantity of the order, i.e., the number of shares
Definition: structures.hpp:71
OrderEntry::SystemAccount::table_header
static const std::string table_header()
Return the header for the account table.
Definition: system_account.hpp:70
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