Skip to content

Commit

Permalink
Merge pull request #1 from dsec-capital/de/cancel_order_response
Browse files Browse the repository at this point in the history
De/cancel order response
  • Loading branch information
degloff authored Apr 20, 2024
2 parents 12e1ecb + f9be732 commit 5236784
Show file tree
Hide file tree
Showing 20 changed files with 897 additions and 728 deletions.
6 changes: 3 additions & 3 deletions common/exec_report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace common {

ExecReport::ExecReport(
const std::string& symbol,
const std::string& ord_id,
const std::string& cl_ord_id,
const std::string& order_id,
const std::string& exec_id,
const char exec_type,
const char ord_type,
Expand All @@ -26,8 +26,8 @@ namespace common {
double leaves_qty,
const std::string& text
) : symbol(symbol)
, ord_id(ord_id)
, cl_ord_id(cl_ord_id)
, order_id(order_id)
, exec_id(exec_id)
, exec_type(exec_type)
, ord_type(ord_type)
Expand All @@ -45,8 +45,8 @@ namespace common {

std::string ExecReport::to_string() const {
return "symbol=" + symbol + ", "
"ord_id=" + ord_id + ", "
"cl_ord_id=" + cl_ord_id + ", "
"order_id=" + order_id + ", "
"exec_id=" + exec_id + ", "
"exec_type=" + std::to_string(exec_type) + ", "
"ord_type=" + std::to_string(ord_type) + ", "
Expand Down
4 changes: 2 additions & 2 deletions common/exec_report.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace common {

ExecReport(
const std::string& symbol,
const std::string& ord_id,
const std::string& cl_ord_id,
const std::string& order_id,
const std::string& exec_id,
const char exec_type,
const char ord_type,
Expand All @@ -35,8 +35,8 @@ namespace common {
);

std::string symbol{};
std::string ord_id{};
std::string cl_ord_id{};
std::string order_id{};
std::string exec_id{};
char exec_type{};
char ord_type{};
Expand Down
68 changes: 24 additions & 44 deletions common/market.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,21 @@ namespace common {
, history_age(history_age)
, history_sample_period(history_sample_period)
, prune_bars(prune_bars)
, mutex(mutex)
, bar_builder(bar_period, [this](const std::chrono::nanoseconds& end, double o, double h, double l, double c) {
spdlog::info("[{}] new bar end={} open={:.5f} high={:.5f} low={:.5f} close={:.5f}\n", symbol, common::to_string(end), o, h, l, c);
spdlog::info("[{}] new bar end={} open={:.5f} high={:.5f} low={:.5f} close={:.5f}", symbol, common::to_string(end), o, h, l, c);
this->bars.try_emplace(end, end, o, h, l, c);
})
, history_bar_builder(current.timestamp, bar_period, [this](const std::chrono::nanoseconds& end, double o, double h, double l, double c) {
spdlog::debug("[{}] hist bar end={} open={:.5f} high={:.5f} low={:.5f} close={:.5f}\n", symbol, common::to_string(end), o, h, l, c);
spdlog::debug("[{}] hist bar end={} open={:.5f} high={:.5f} low={:.5f} close={:.5f}", symbol, common::to_string(end), o, h, l, c);
this->bars.try_emplace(end, end, o, h, l, c);
})
, current(current)
, previous(current)
, oldest(current)
, quoting(false)
, cl_ord_id(1)
, bid_order(
quoting_cl_ord_id(),
std::format("quote_ord_id_0"),
std::format("quote_cl_ord_id_0"),
symbol,
OWNER_MARKET_SIMULATOR,
"",
Expand All @@ -48,7 +47,8 @@ namespace common {
(long)current.bid_volume
)
, ask_order(
quoting_cl_ord_id(),
std::format("quote_ord_id_1"),
std::format("quote_cl_ord_id_1"),
symbol,
OWNER_MARKET_SIMULATOR,
"",
Expand Down Expand Up @@ -84,41 +84,25 @@ namespace common {
}
}

void Market::update_quotes(const TopOfBook& current, const TopOfBook& previous, std::queue<Order>& orders) {
std::lock_guard<std::mutex> ul(mutex);
if (previous.bid_price != current.bid_price) {
OrderMatcher::erase(bid_order);
bid_order = Order(
quoting_cl_ord_id(),
symbol,
OWNER_MARKET_SIMULATOR,
"",
Order::Side::buy,
Order::Type::limit,
current.bid_price,
(long)current.bid_volume
);
OrderMatcher::insert(bid_order, orders);
}
if (previous.ask_price != current.ask_price) {
OrderMatcher::erase(ask_order);
ask_order = Order(
quoting_cl_ord_id(),
symbol,
OWNER_MARKET_SIMULATOR,
"",
Order::Side::sell,
Order::Type::limit,
current.ask_price,
(long)current.ask_volume
);;
OrderMatcher::insert(ask_order, orders);
}
const Order& Market::get_bid_order() const {
return bid_order;
}

spdlog::debug("update_quotes {} by_open_quantity", symbol);
spdlog::debug(common::to_string(*this, OrderMatcher::by_open_quantity));
spdlog::debug("update_quotes {} by_last_exec_quantity", symbol);
spdlog::debug(common::to_string(*this, OrderMatcher::by_last_exec_quantity));
const Order& Market::get_ask_order() const {
return ask_order;
}

OrderInsertResult Market::quote(const Order& order_ins) {
auto result = insert(order_ins);
if (!result.error) {
if (order_ins.get_side() == Order::Side::buy) {
bid_order = order_ins;
}
else {
ask_order = order_ins;
}
}
return result;
}

std::pair<TopOfBook, TopOfBook> Market::get_top_of_book() const {
Expand Down Expand Up @@ -156,10 +140,6 @@ namespace common {
std::lock_guard<std::mutex> ul(mutex);
return to_json(from, to, bars);
}

std::string Market::quoting_cl_ord_id() {
return std::format("cl_ord_id_{}", ++cl_ord_id);
}
}


10 changes: 5 additions & 5 deletions common/market.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ namespace common {

void simulate_next();

void update_quotes(const TopOfBook& current, const TopOfBook& previous, std::queue<Order>& orders);

std::pair<TopOfBook, TopOfBook> get_top_of_book() const;

void extend_bar_history(const std::chrono::nanoseconds& until_past);
Expand All @@ -50,17 +48,19 @@ namespace common {

std::pair<nlohmann::json, int> get_bars_as_json(const std::chrono::nanoseconds& from, const std::chrono::nanoseconds& to);

private:
const Order& get_bid_order() const;

std::string quoting_cl_ord_id();
const Order& get_ask_order() const;

OrderInsertResult quote(const Order& order_ins);

private:
std::string symbol;
std::shared_ptr<PriceSampler> price_sampler;
std::chrono::nanoseconds bar_period;
std::chrono::nanoseconds history_age;
std::chrono::nanoseconds history_sample_period;
bool prune_bars;
std::mutex& mutex;

BarBuilder bar_builder;
ReverseBarBuilder history_bar_builder;
Expand Down
5 changes: 3 additions & 2 deletions common/market_config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[config]
log_level = 1
http_server_host = "0.0.0.0"
http_server_port = 8080
market_update_period_millis = 2000
Expand All @@ -16,7 +17,7 @@ history_age_hours = 96
history_sample_period_millis = 1000
[symbols.market_simulator]
model = "white-noise"
sigma = 0.05
sigma = 0.1

[[symbols]]
symbol = "AUD/USD"
Expand All @@ -31,4 +32,4 @@ history_age_hours = 96
history_sample_period_millis = 1000
[symbols.market_simulator]
model = "white-noise"
sigma = 0.05
sigma = 0.1
48 changes: 26 additions & 22 deletions common/markets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,43 @@ namespace common {

Markets::Markets(market_map_t& markets) : markets(markets) {}

typename Markets::market_map_t::iterator Markets::get_market(const std::string& symbol) {
auto it = markets.find(symbol);
if (it == markets.end()) {
throw std::runtime_error(std::format("no market defined for symbol {}", symbol));
}
return it;
}

std::tuple<const Order*, bool, int> Markets::insert(const Order& order, std::queue<Order>& orders)
OrderInsertResult Markets::insert(const Order& order)
{
auto it = get_market(order.get_symbol());
return it->second.insert(order, orders);
auto it = markets.find(order.get_symbol());
if (it != markets.end()) {
return it->second.insert(order);
}
else {
return OrderInsertResult();
}
}

void Markets::erase(const Order& order)
std::optional<Order> Markets::find(const std::string& symbol, const std::string& ord_id, Order::Side side)
{
auto it = markets.find(order.get_symbol());
if (it == markets.end()) return;
it->second.erase(order);
auto it = markets.find(symbol);
if (it != markets.end()) {
return it->second.find(ord_id, side);
}
else {
return std::optional<Order>();
}
}

Order& Markets::find(std::string symbol, Order::Side side, std::string id)
{
std::optional<Order> Markets::erase(const std::string& symbol, const std::string& ord_id, Order::Side side) {
auto it = markets.find(symbol);
if (it == markets.end()) throw std::exception();
return it->second.find(side, id);
if (it != markets.end()) {
return it->second.erase(ord_id, side);
}
else {
return std::optional<Order>();
}
}

void Markets::display(std::string symbol) const
std::string Markets::to_string(std::string symbol) const
{
auto it = markets.find(symbol);
if (it == markets.end()) return;
it->second.display();
if (it == markets.end()) return "";
return it->second.to_string();
}

void Markets::display() const
Expand Down
10 changes: 4 additions & 6 deletions common/markets.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ namespace common {

Markets(market_map_t& markets);

typename market_map_t::iterator get_market(const std::string& symbol);
OrderInsertResult insert(const Order& order);

std::tuple<const Order*, bool, int> insert(const Order& order, std::queue<Order>& orders);
std::optional<Order> find(const std::string& symbol, const std::string& ord_id, Order::Side side);

void erase(const Order& order);
std::optional<Order> erase(const std::string& symbol, const std::string& ord_id, Order::Side side);

Order& find(std::string symbol, Order::Side side, std::string id);

void display(std::string symbol) const;
std::string to_string(std::string symbol) const;

void display() const;

Expand Down
29 changes: 18 additions & 11 deletions common/order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
namespace common {

Order::Order(
const std::string& clOrdId,
const std::string& ord_id,
const std::string& cl_ord_id,
const std::string& symbol,
const std::string& owner,
const std::string& target,
Side side,
Type type,
double price,
long quantity
) : cl_ord_id(clOrdId)
, symbol(symbol)
, owner(owner)
, target(target)
, side(side)
, type(type)
, price(price)
, quantity(quantity)
) : ord_id(ord_id)
, cl_ord_id(cl_ord_id)
, symbol(symbol)
, owner(owner)
, target(target)
, side(side)
, type(type)
, price(price)
, quantity(quantity)
{
open_quantity = quantity;
executed_quantity = 0;
Expand All @@ -29,7 +31,11 @@ namespace common {
last_executed_quantity = 0;
}

const std::string& Order::get_client_id() const { return cl_ord_id; }
const std::string& Order::get_ord_id() const { return ord_id; }

const std::string& Order::get_cl_ord_id() const { return cl_ord_id; }

void Order::set_cl_ord_id(const std::string& new_cl_ord_id) { cl_ord_id = new_cl_ord_id; }

const std::string& Order::get_symbol() const { return symbol; }

Expand Down Expand Up @@ -79,7 +85,8 @@ namespace common {
std::string Order::to_string() const {
return
"symbol=" + symbol + ", " +
"clOrdId=" + cl_ord_id + ", " +
"ord_id=" + ord_id + ", " +
"cl_ord_id=" + cl_ord_id + ", " +
"owner=" + owner + ", " +
"target=" + target + ", " +
"side=" + std::to_string(side) + ", " +
Expand Down
8 changes: 6 additions & 2 deletions common/order.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace common {
enum Type { market, limit };

Order(
const std::string& clOrdId,
const std::string& ord_id,
const std::string& cl_ord_id,
const std::string& symbol,
const std::string& owner,
const std::string& target,
Expand All @@ -26,7 +27,9 @@ namespace common {
long quantity
);

const std::string& get_client_id() const;
const std::string& get_ord_id() const;
const std::string& get_cl_ord_id() const;
void set_cl_ord_id(const std::string& new_cl_ord_id);
const std::string& get_symbol() const;
const std::string& get_owner() const;
const std::string& get_target() const;
Expand All @@ -51,6 +54,7 @@ namespace common {
std::string to_string() const;

private:
std::string ord_id;
std::string cl_ord_id;
std::string symbol;
std::string owner;
Expand Down
Loading

0 comments on commit 5236784

Please sign in to comment.