forked from SLMolenaar/orderbook-simulator-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExchangeRules.h
More file actions
77 lines (65 loc) · 2.5 KB
/
Copy pathExchangeRules.h
File metadata and controls
77 lines (65 loc) · 2.5 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include "Types.h"
// Exchange trading rules
struct ExchangeRules {
Price tickSize = 1; // Minimum price increment (in cents)
Quantity lotSize = 1; // Minimum quantity increment (e.g., 1 share)
Quantity minQuantity = 1; // Minimum order size
Quantity maxQuantity = 1000000; // Maximum order size
Price minNotional = 0; // Minimum order value (price * quantity)
// Validate if a price conforms to tick size rules
bool IsValidPrice(Price price) const {
if (price <= 0) return false;
return price % tickSize == 0;
}
// Validate if a quantity conforms to lot size rules
bool IsValidQuantity(Quantity quantity) const {
if (quantity < minQuantity || quantity > maxQuantity) return false;
return quantity % lotSize == 0;
}
// Validate if order value meets minimum notional
bool IsValidNotional(Price price, Quantity quantity) const {
// Calculate notional value (price * quantity)
int64_t notional = static_cast<int64_t>(price) * static_cast<int64_t>(quantity);
return notional >= minNotional;
}
// Validate an entire order
bool IsValidOrder(Price price, Quantity quantity) const {
return IsValidPrice(price) &&
IsValidQuantity(quantity) &&
IsValidNotional(price, quantity);
}
// Round price to nearest valid tick
Price RoundToTick(Price price) const {
if (tickSize <= 1) return price;
return (price / tickSize) * tickSize;
}
// Round quantity to nearest valid lot
Quantity RoundToLot(Quantity quantity) const {
if (lotSize <= 1) return quantity;
return (quantity / lotSize) * lotSize;
}
};
// Enum for order rejection reasons
enum class RejectReason {
None,
InvalidPrice, // Price doesn't conform to tick size
InvalidQuantity, // Quantity doesn't conform to lot size
BelowMinQuantity, // Quantity below minimum
AboveMaxQuantity, // Quantity above maximum
BelowMinNotional, // Order value too small
DuplicateOrderId, // Order ID already exists
InvalidOrderType, // Unsupported order type
EmptyBook // Market order on empty book
};
// Structure to hold order validation result
struct OrderValidation {
bool isValid = true;
RejectReason reason = RejectReason::None;
static OrderValidation Accept() {
return OrderValidation{true, RejectReason::None};
}
static OrderValidation Reject(RejectReason reason) {
return OrderValidation{false, reason};
}
};