-
Notifications
You must be signed in to change notification settings - Fork 29
/
Order.h
64 lines (53 loc) · 1.91 KB
/
Order.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <list>
#include <exception>
#include <format>
#include "OrderType.h"
#include "Side.h"
#include "Usings.h"
#include "Constants.h"
class Order
{
public:
Order(OrderType orderType, OrderId orderId, Side side, Price price, Quantity quantity)
: orderType_{ orderType }
, orderId_{ orderId }
, side_{ side }
, price_{ price }
, initialQuantity_{ quantity }
, remainingQuantity_{ quantity }
{ }
Order(OrderId orderId, Side side, Quantity quantity)
: Order(OrderType::Market, orderId, side, Constants::InvalidPrice, quantity)
{ }
OrderId GetOrderId() const { return orderId_; }
Side GetSide() const { return side_; }
Price GetPrice() const { return price_; }
OrderType GetOrderType() const { return orderType_; }
Quantity GetInitialQuantity() const { return initialQuantity_; }
Quantity GetRemainingQuantity() const { return remainingQuantity_; }
Quantity GetFilledQuantity() const { return GetInitialQuantity() - GetRemainingQuantity(); }
bool IsFilled() const { return GetRemainingQuantity() == 0; }
void Fill(Quantity quantity)
{
if (quantity > GetRemainingQuantity())
throw std::logic_error(std::format("Order ({}) cannot be filled for more than its remaining quantity.", GetOrderId()));
remainingQuantity_ -= quantity;
}
void ToGoodTillCancel(Price price)
{
if (GetOrderType() != OrderType::Market)
throw std::logic_error(std::format("Order ({}) cannot have its price adjusted, only market orders can.", GetOrderId()));
price_ = price;
orderType_ = OrderType::GoodTillCancel;
}
private:
OrderType orderType_;
OrderId orderId_;
Side side_;
Price price_;
Quantity initialQuantity_;
Quantity remainingQuantity_;
};
using OrderPointer = std::shared_ptr<Order>;
using OrderPointers = std::list<OrderPointer>;