CppTrader  1.0.4.0
C++ Trader
order.inl
Go to the documentation of this file.
1 
9 namespace CppTrader {
10 namespace Matching {
11 
12 template <class TOutputStream>
13 inline TOutputStream& operator<<(TOutputStream& stream, OrderSide side)
14 {
15  switch (side)
16  {
17  case OrderSide::BUY:
18  stream << "BUY";
19  break;
20  case OrderSide::SELL:
21  stream << "SELL";
22  break;
23  default:
24  stream << "<unknown>";
25  break;
26  }
27  return stream;
28 }
29 
30 template <class TOutputStream>
31 inline TOutputStream& operator<<(TOutputStream& stream, OrderType type)
32 {
33  switch (type)
34  {
35  case OrderType::MARKET:
36  stream << "MARKET";
37  break;
38  case OrderType::LIMIT:
39  stream << "LIMIT";
40  break;
41  case OrderType::STOP:
42  stream << "STOP";
43  break;
45  stream << "STOP-LIMIT";
46  break;
48  stream << "TRAILING-STOP";
49  break;
51  stream << "TRAILING-STOP-LIMIT";
52  break;
53  default:
54  stream << "<unknown>";
55  break;
56  }
57  return stream;
58 }
59 
60 template <class TOutputStream>
61 inline TOutputStream& operator<<(TOutputStream& stream, OrderTimeInForce tif)
62 {
63  switch (tif)
64  {
66  stream << "GTC";
67  break;
69  stream << "IOC";
70  break;
72  stream << "FOK";
73  break;
75  stream << "AON";
76  break;
77  default:
78  stream << "<unknown>";
79  break;
80  }
81  return stream;
82 }
83 
84 inline Order::Order(uint64_t id, uint32_t symbol, OrderType type, OrderSide side, uint64_t price, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif, uint64_t max_visible_quantity, uint64_t slippage, int64_t trailing_distance, int64_t trailing_step) noexcept
85  : Id(id),
86  SymbolId(symbol),
87  Type(type),
88  Side(side),
89  Price(price),
90  StopPrice(stop_price),
91  Quantity(quantity),
92  ExecutedQuantity(0),
93  LeavesQuantity(quantity),
94  TimeInForce(tif),
95  MaxVisibleQuantity(max_visible_quantity),
96  Slippage(slippage),
97  TrailingDistance(trailing_distance),
98  TrailingStep(trailing_step)
99 {
100 }
101 
102 template <class TOutputStream>
103 inline TOutputStream& operator<<(TOutputStream& stream, const Order& order)
104 {
105  stream << "Order(Id=" << order.Id
106  << "; SymbolId=" << order.SymbolId
107  << "; Type=" << order.Type
108  << "; Side=" << order.Side
109  << "; Price=" << order.Price
110  << "; StopPrice=" << order.StopPrice
111  << "; Quantity=" << order.Quantity
112  << "; ExecutedQuantity=" << order.ExecutedQuantity
113  << "; LeavesQuantity=" << order.LeavesQuantity
114  << "; " << order.TimeInForce;
115  if (order.IsTrailingStop() || order.IsTrailingStopLimit())
116  {
117  stream << "; TrailingDistance=" << order.TrailingDistance;
118  stream << "; TrailingStep=" << order.TrailingStep;
119  }
120  if (order.IsHidden() || order.IsIceberg())
121  stream << "; MaxVisibleQuantity=" << order.MaxVisibleQuantity;
122  if (order.IsSlippage())
123  stream << "; Slippage=" << order.Slippage;
124  stream << ")";
125  return stream;
126 }
127 
128 inline Order Order::Market(uint64_t id, uint32_t symbol, OrderSide side, uint64_t quantity, uint64_t slippage) noexcept
129 {
130  return Order(id, symbol, OrderType::MARKET, side, 0, 0, quantity, OrderTimeInForce::IOC, std::numeric_limits<uint64_t>::max(), slippage, 0, 0);
131 }
132 
133 inline Order Order::BuyMarket(uint64_t id, uint32_t symbol, uint64_t quantity, uint64_t slippage) noexcept
134 {
135  return Order(id, symbol, OrderType::MARKET, OrderSide::BUY, 0, 0, quantity, OrderTimeInForce::IOC, std::numeric_limits<uint64_t>::max(), slippage, 0, 0);
136 }
137 
138 inline Order Order::SellMarket(uint64_t id, uint32_t symbol, uint64_t quantity, uint64_t slippage) noexcept
139 {
140  return Order(id, symbol, OrderType::MARKET, OrderSide::SELL, 0, 0, quantity, OrderTimeInForce::IOC, std::numeric_limits<uint64_t>::max(), slippage, 0, 0);
141 }
142 
143 inline Order Order::Limit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t price, uint64_t quantity, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
144 {
145  return Order(id, symbol, OrderType::LIMIT, side, price, 0, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), 0, 0);
146 }
147 
148 inline Order Order::BuyLimit(uint64_t id, uint32_t symbol, uint64_t price, uint64_t quantity, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
149 {
150  return Order(id, symbol, OrderType::LIMIT, OrderSide::BUY, price, 0, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), 0, 0);
151 }
152 
153 inline Order Order::SellLimit(uint64_t id, uint32_t symbol, uint64_t price, uint64_t quantity, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
154 {
155  return Order(id, symbol, OrderType::LIMIT, OrderSide::SELL, price, 0, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), 0, 0);
156 }
157 
158 inline Order Order::Stop(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif, uint64_t slippage) noexcept
159 {
160  return Order(id, symbol, OrderType::STOP, side, 0, stop_price, quantity, tif, std::numeric_limits<uint64_t>::max(), slippage, 0, 0);
161 }
162 
163 inline Order Order::BuyStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif, uint64_t slippage) noexcept
164 {
165  return Order(id, symbol, OrderType::STOP, OrderSide::BUY, 0, stop_price, quantity, tif, std::numeric_limits<uint64_t>::max(), slippage, 0, 0);
166 }
167 
168 inline Order Order::SellStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif, uint64_t slippage) noexcept
169 {
170  return Order(id, symbol, OrderType::STOP, OrderSide::SELL, 0, stop_price, quantity, tif, std::numeric_limits<uint64_t>::max(), slippage, 0, 0);
171 }
172 
173 inline Order Order::StopLimit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
174 {
175  return Order(id, symbol, OrderType::STOP_LIMIT, side, price, stop_price, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), 0, 0);
176 }
177 
178 inline Order Order::BuyStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
179 {
180  return Order(id, symbol, OrderType::STOP_LIMIT, OrderSide::BUY, price, stop_price, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), 0, 0);
181 }
182 
183 inline Order Order::SellStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
184 {
185  return Order(id, symbol, OrderType::STOP_LIMIT, OrderSide::SELL, price, stop_price, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), 0, 0);
186 }
187 
188 inline Order Order::TrailingStop(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step, OrderTimeInForce tif, uint64_t slippage) noexcept
189 {
190  return Order(id, symbol, OrderType::TRAILING_STOP, side, 0, stop_price, quantity, tif, std::numeric_limits<uint64_t>::max(), slippage, trailing_distance, trailing_step);
191 }
192 
193 inline Order Order::TrailingBuyStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step, OrderTimeInForce tif, uint64_t slippage) noexcept
194 {
195  return Order(id, symbol, OrderType::TRAILING_STOP, OrderSide::BUY, 0, stop_price, quantity, tif, std::numeric_limits<uint64_t>::max(), slippage, trailing_distance, trailing_step);
196 }
197 
198 inline Order Order::TrailingSellStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step, OrderTimeInForce tif, uint64_t slippage) noexcept
199 {
200  return Order(id, symbol, OrderType::TRAILING_STOP, OrderSide::SELL, 0, stop_price, quantity, tif, std::numeric_limits<uint64_t>::max(), slippage, trailing_distance, trailing_step);
201 }
202 
203 inline Order Order::TrailingStopLimit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
204 {
205  return Order(id, symbol, OrderType::TRAILING_STOP_LIMIT, side, price, stop_price, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), trailing_distance, trailing_step);
206 }
207 
208 inline Order Order::TrailingBuyStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
209 {
210  return Order(id, symbol, OrderType::TRAILING_STOP_LIMIT, OrderSide::BUY, price, stop_price, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), trailing_distance, trailing_step);
211 }
212 
213 inline Order Order::TrailingSellStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step, OrderTimeInForce tif, uint64_t max_visible_quantity) noexcept
214 {
215  return Order(id, symbol, OrderType::TRAILING_STOP_LIMIT, OrderSide::SELL, price, stop_price, quantity, tif, max_visible_quantity, std::numeric_limits<uint64_t>::max(), trailing_distance, trailing_step);
216 }
217 
218 inline OrderNode::OrderNode(const Order& order) noexcept : Order(order), Level(nullptr)
219 {
220 }
221 
222 inline OrderNode& OrderNode::operator=(const Order& order) noexcept
223 {
224  Order::operator=(order);
225  Level = nullptr;
226  return *this;
227 }
228 
229 } // namespace Matching
230 } // namespace CppTrader
OrderSide
Order side.
Definition: order.h:34
OrderTimeInForce
Order Time in Force.
Definition: order.h:110
OrderType
Order type.
Definition: order.h:82
TOutputStream & operator<<(TOutputStream &stream, ErrorCode error)
Definition: errors.inl:13
C++ Trader project definitions.
Definition: errors.h:16
Price level.
Definition: level.h:32
bool IsHidden() const noexcept
Is the 'Hidden' order?
Definition: order.h:245
bool IsIceberg() const noexcept
Is the 'Iceberg' order?
Definition: order.h:247
static Order SellLimit(uint64_t id, uint32_t symbol, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new sell limit order.
Definition: order.inl:153
static Order BuyStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new buy stop-limit order.
Definition: order.inl:178
static Order BuyMarket(uint64_t id, uint32_t symbol, uint64_t quantity, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new buy market order.
Definition: order.inl:133
Order() noexcept=default
static Order TrailingBuyStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing buy stop order.
Definition: order.inl:193
static Order Stop(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new stop order.
Definition: order.inl:158
OrderSide Side
Order side.
Definition: order.h:136
uint64_t Price
Order price.
Definition: order.h:138
uint64_t MaxVisibleQuantity
Order max visible quantity.
Definition: order.h:162
bool IsTrailingStop() const noexcept
Is the trailing stop order?
Definition: order.h:226
int64_t TrailingStep
Order trailing step.
Definition: order.h:198
bool IsSlippage() const noexcept
Is the order have slippage?
Definition: order.h:250
uint64_t ExecutedQuantity
Order executed quantity.
Definition: order.h:145
Order & operator=(const Order &) noexcept=default
static Order TrailingStop(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing stop order.
Definition: order.inl:188
static Order Market(uint64_t id, uint32_t symbol, OrderSide side, uint64_t quantity, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new market order.
Definition: order.inl:128
static Order SellStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new sell stop-limit order.
Definition: order.inl:183
static Order TrailingStopLimit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing stop-limit order.
Definition: order.inl:203
static Order StopLimit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new stop-limit order.
Definition: order.inl:173
static Order Limit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new limit order.
Definition: order.inl:143
uint64_t LeavesQuantity
Order leaves quantity.
Definition: order.h:147
uint64_t StopPrice
Order stop price.
Definition: order.h:140
OrderType Type
Order type.
Definition: order.h:134
static Order SellMarket(uint64_t id, uint32_t symbol, uint64_t quantity, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new sell market order.
Definition: order.inl:138
OrderTimeInForce TimeInForce
Time in Force.
Definition: order.h:150
static Order SellStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new sell stop order.
Definition: order.inl:168
static Order BuyLimit(uint64_t id, uint32_t symbol, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new buy limit order.
Definition: order.inl:148
int64_t TrailingDistance
Order trailing distance to market.
Definition: order.h:189
static Order TrailingBuyStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing buy stop-limit order.
Definition: order.inl:208
uint64_t Id
Order Id.
Definition: order.h:130
uint32_t SymbolId
Symbol Id.
Definition: order.h:132
bool IsTrailingStopLimit() const noexcept
Is the trailing stop-limit order?
Definition: order.h:228
static Order BuyStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new buy stop order.
Definition: order.inl:163
uint64_t Slippage
Market order slippage.
Definition: order.h:179
static Order TrailingSellStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing sell stop order.
Definition: order.inl:198
uint64_t Quantity
Order quantity.
Definition: order.h:143
static Order TrailingSellStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing sell stop-limit order.
Definition: order.inl:213
OrderNode & operator=(const Order &order) noexcept
Definition: order.inl:222
OrderNode(const Order &order) noexcept
Definition: order.inl:218