Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
coinbase_common.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common/order.hpp>
4#include <nlohmann/json.hpp>
5#include <utils/timestamp.hpp>
6
7using json = nlohmann::json;
8
10
17
45
49inline std::string dump_json(const json &value) {
50 return value.dump(-1, ' ', false, json::error_handler_t::replace);
51}
52
53inline std::string_view to_coinbase_string(TimeInForce tif) {
54 switch(tif) {
56 return "GOOD_UNTIL_DATE_TIME";
58 return "GOOD_UNTIL_CANCELLED";
60 return "IMMEDIATE_OR_CANCEL";
62 return "FILL_OR_KILL";
63 default:
64 return "UNKNOWN_TIME_IN_FORCE";
65 }
66}
67
68inline json to_json(const Order& order) {
69 json order_config;
70 if (order.type == OrderType::MARKET) {
72 order_config = {
73 {
74 "market_market_ioc", {
75 {"base_size", to_qty_string(order.quantity)}
76 }
77 }
78 };
79 }
80 else if (order.time_in_force == TimeInForce::FILL_OR_KILL) {
81 order_config = {
82 {
83 "market_market_fok", {
84 {"base_size", to_qty_string(order.quantity)}
85 }
86 }
87 };
88 }
89 }
90 else if (order.type == OrderType::LIMIT) {
92 order_config = {
93 {
94 "limit_limit_gtc", {
95 {"base_size", to_qty_string(order.quantity)},
96 {"limit_price", to_price_string(order.price)},
97 {"post_only", order.post_only},
98 }
99 }
100 };
101 }
102 // TODO:
103 // else if (order.time_in_force == TimeInForce::FILL_OR_KILL) {
104 // order_config = {
105 // {
106 // "market_market_fok", {
107 // {"base_size", to_qty_string(order.quantity)},
108 // {"limit_price", to_price_string(order.price)},
109 // {"end_time", order.end_time},
110 // {"post_only", order.post_only},
111 // }
112 // }
113 // };
114 // }
116 order_config = {
117 {
118 "sor_limit_ioc", {
119 {"base_size", to_qty_string(order.quantity)},
120 {"limit_price", to_price_string(order.price)}
121 }
122 }
123 };
124 }
125 else if (order.time_in_force == TimeInForce::FILL_OR_KILL) {
126 order_config = {
127 {
128 "market_market_fok", {
129 {"base_size", to_qty_string(order.quantity)},
130 {"limit_price", to_price_string(order.price)},
131 }
132 }
133 };
134 }
135 }
136
137 return {
138 {"order_id", order.order_id.c_str()},
139 {"product_id", order.symbol.c_str()},
140 {"user_id", order.user_id.c_str()},
141 {"order_configuration", order_config},
142 {"side", to_string(order.side)},
143 {"client_order_id", order.client_order_id.c_str()},
144 {"status", to_string(order.status)},
145 {"time_in_force", to_coinbase_string(order.time_in_force)},
146 {"created_time", utils::format_timestamp_iso8601()},
147 {"completion_percentage", std::to_string(order.cum_qty_double() / order.qty_double() * 100)},
148 {"filled_size", to_qty_string(order.cum_quantity)},
149 {"average_filled_price", to_price_string(order.avg_fill_price)},
150 {"number_of_fills", std::to_string(order.num_fills)},
151 {"fee", std::to_string(order.fee)},
152 {"filled_value", std::to_string(order.filled_value)},
153 {"pending_cancel", order.pending_cancel},
154 {"size_in_quote", false},
155 {"total_fee", std::to_string(order.fee)},
156 {"size_inclusive_of_fees", false},
157 {"total_value_after_fees", std::to_string(order.filled_value - order.fee)},
158 {"trigger_status", "UNKNOWN_TRIGGER_STATUS"},
159 {"order_type", to_string(order.type)},
160 {"reject_reason", ""},
161 {"settled", false},
162 {"product_type", to_string(order.product_type)},
163 {"reject_message", order.reject_message},
164 {"cancel_message", order.cancel_message},
165 {"order_placement_source", "UNKNOWN_PLACEMENT_SOURCE"},
166 {"outstanding_hold_amount", ""},
167 {"is_liquidation", false},
168 {"last_fill_time", (order.last_fill_time.has_value() ? utils::format_timestamp_iso8601(order.last_fill_time.value()) : "")},
169 {"edit_history", order.edit_history},
170 {"leverage", ""},
171 {"margin_type", "CROSS"},
172 {"attached_order_configuration", {{}}},
173 {"current_pending_replace", {{}}},
174 {"commission_detail_total", {{}}},
175 {"workable_size", to_qty_string(order.leaves_quantity)},
176 {"workable_size_completion_pct", std::to_string(order.leaves_qty_double() / order.qty_double() * 100)},
177 };
178}
179
180} // end namespace slick::sim::order_gateway
const char * c_str() const noexcept
nlohmann::json json
Definition order.hpp:16
RoutingEffect
What an execution report means for the order-id -> symbol routing table.
@ Learn
The order exists: remember which symbol its id belongs to.
@ Forget
The order is gone: drop its routing.
std::string dump_json(const json &value)
nlohmann's dump() throws on invalid UTF-8.
std::string_view to_coinbase_string(TimeInForce tif)
constexpr RoutingEffect routing_effect(OrderStatus status)
Terminal reports retire routing.
std::string format_timestamp_iso8601(const std::chrono::system_clock::time_point &tp, int num_sebsecond_digits=3)
Definition timestamp.hpp:9
constexpr const char * to_string(FeedState state) noexcept
std::string to_price_string(price_t price)
Definition types.hpp:107
std::string to_qty_string(qty_t qty)
Definition types.hpp:108
utils::fixed_string< 32 > symbol
Definition order.hpp:289
utils::fixed_string< 37 > client_order_id
Definition order.hpp:291
utils::fixed_string< 37 > order_id
Definition order.hpp:292
utils::fixed_string< 37 > user_id
Definition order.hpp:290
price_t avg_fill_price
Definition order.hpp:317
OrderType type
Definition order.hpp:306
std::string reject_message
Definition order.hpp:333
double filled_value
Definition order.hpp:324
std::optional< uint64_t > last_fill_time
Definition order.hpp:329
TimeInForce time_in_force
Definition order.hpp:308
OrderStatus status
Definition order.hpp:307
std::string cancel_message
Definition order.hpp:334
uint32_t num_fills
Definition order.hpp:322
double qty_double() const noexcept
Definition order.hpp:366
qty_t leaves_quantity
Definition order.hpp:316
double leaves_qty_double() const noexcept
Definition order.hpp:374
double cum_qty_double() const noexcept
Definition order.hpp:370
ProductType product_type
Definition order.hpp:309