Skip to content

Commit

Permalink
Add exchange_state
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenewald committed Jan 9, 2025
1 parent ecae00f commit 61d1769
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 87 deletions.
18 changes: 18 additions & 0 deletions exchange/src/exchange/exchange_state.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "exchange/config/dynamic/ticker_config.hpp"
#include "exchange/orders/ticker_container.hpp"
#include "exchange/traders/trader_container.hpp"

namespace nutc::exchange {
struct exchange_state {
TraderContainer traders;
TickerContainer tickers;

explicit exchange_state(const std::vector<ticker_config>& configs) :
tickers{configs, traders}
{}

exchange_state() : tickers{{}, traders} {}
};
} // namespace nutc::exchange
22 changes: 12 additions & 10 deletions exchange/src/exchange/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include "exchange/algos/algo_manager.hpp"
#include "exchange/config/dynamic/argparse.hpp"
#include "exchange/config/dynamic/config.hpp"
#include "exchange/exchange_state.hpp"
#include "exchange/matching_cycle/base/base_cycle.hpp"
#include "exchange/matching_cycle/cycle_interface.hpp"
#include "exchange/matching_cycle/dev/dev_cycle.hpp"
#include "exchange/matching_cycle/sandbox/sandbox_cycle.hpp"
#include "exchange/traders/trader_container.hpp"

#include <csignal>

Expand All @@ -18,27 +18,25 @@ namespace {
using namespace nutc::exchange; // NOLINT

std::unique_ptr<MatchingCycleInterface>
create_cycle(TraderContainer& traders, const auto& mode)
create_cycle(exchange_state& state, const auto& mode)
{
using nutc::common::Mode;
const auto& ticker_config = Config::get().get_tickers();
double order_fee = Config::get().constants().ORDER_FEE;
auto tickers = TickerContainer(ticker_config, traders);
auto max_order_volume = Config::get().constants().MAX_CUMULATIVE_OPEN_ORDER_VOLUME;

switch (mode) {
case Mode::normal:
return std::make_unique<BaseMatchingCycle>(
tickers, traders, order_fee, max_order_volume
state, order_fee, max_order_volume
);
case Mode::sandbox:
return std::make_unique<SandboxMatchingCycle>(
tickers, traders, order_fee, max_order_volume
state, order_fee, max_order_volume
);
case Mode::bots_only:
case Mode::dev:
return std::make_unique<DevMatchingCycle>(
tickers, traders, order_fee, max_order_volume
state, order_fee, max_order_volume
);
}

Expand All @@ -64,10 +62,14 @@ main(int argc, const char** argv)
std::signal(SIGPIPE, SIG_IGN);

auto mode = process_arguments(argc, argv);
TraderContainer traders{};
AlgoInitializer::get_algo_initializer(mode)->initialize_algo_management(traders);
const auto& ticker_config = Config::get().get_tickers();
exchange_state state{ticker_config};

AlgoInitializer::get_algo_initializer(mode)->initialize_algo_management(
state.traders
);

main_event_loop(create_cycle(traders, mode));
main_event_loop(create_cycle(state, mode));

return 0;
}
10 changes: 5 additions & 5 deletions exchange/src/exchange/matching_cycle/base/base_cycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace nutc::exchange {
void
BaseMatchingCycle::before_cycle_(uint64_t)
{
for (auto [symbol, ticker_data] : tickers_) {
for (auto [symbol, ticker_data] : state_.tickers) {
ticker_data.generate_bot_orders();
}
}
Expand Down Expand Up @@ -47,7 +47,7 @@ BaseMatchingCycle::collect_orders(uint64_t) -> std::vector<OrderVariant>
);
};

std::for_each(traders_.begin(), traders_.end(), collect_orders);
std::for_each(state_.traders.begin(), state_.traders.end(), collect_orders);

return orders;
}
Expand All @@ -58,7 +58,7 @@ BaseMatchingCycle::match_orders_(std::vector<OrderVariant> orders)
std::vector<tagged_match> matches;

auto match_incoming_order = [&]<typename OrderT>(OrderT& order) {
auto& ticker_data = tickers_[order.ticker];
auto& ticker_data = state_.tickers[order.ticker];
auto& orderbook = ticker_data.get_orderbook();
if constexpr (std::is_same_v<OrderT, common::cancel_order>) {
orderbook.remove_order(order.order_id);
Expand Down Expand Up @@ -88,7 +88,7 @@ BaseMatchingCycle::get_orderbook_updates_()
{
std::vector<common::position> ob_updates;

for (auto [symbol, info] : tickers_) {
for (auto [symbol, info] : state_.tickers) {
auto tmp = info.get_orderbook().get_and_reset_updates();
std::ranges::copy(tmp, std::back_inserter(ob_updates));
}
Expand Down Expand Up @@ -146,7 +146,7 @@ BaseMatchingCycle::send_market_updates_(const std::vector<tagged_match>& matches
}

std::for_each(
traders_.begin(), traders_.end(),
state_.traders.begin(), state_.traders.end(),
[&message = *update](GenericTrader& trader) { trader.send_message(message); }
);
}
Expand Down
14 changes: 7 additions & 7 deletions exchange/src/exchange/matching_cycle/base/base_cycle.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "exchange/exchange_state.hpp"
#include "exchange/matching_cycle/cycle_interface.hpp"
#include "exchange/orders/ticker_container.hpp"
#include "exchange/traders/trader_container.hpp"
Expand All @@ -9,32 +10,31 @@ namespace nutc::exchange {
* @brief Barebones matching cycle. Likely to be overridden for more logging
*/
class BaseMatchingCycle : public MatchingCycleInterface {
TickerContainer tickers_;
TraderContainer& traders_;
exchange_state& state_;
common::decimal_price order_fee_;
common::decimal_quantity max_cumulative_order_volume_;

public:
// Require transfer of ownership
BaseMatchingCycle(
TickerContainer tickers, TraderContainer& traders,
common::decimal_price order_fee, common::decimal_quantity max_order_volume
exchange_state& state, common::decimal_price order_fee,
common::decimal_quantity max_order_volume
) :
tickers_(std::move(tickers)), traders_(traders), order_fee_(order_fee),
state_(state), order_fee_(order_fee),
max_cumulative_order_volume_{max_order_volume}
{}

protected:
auto&
get_tickers()
{
return tickers_;
return state_.tickers;
}

TraderContainer&
get_traders()
{
return traders_;
return state_.traders;
}

void before_cycle_(uint64_t) override;
Expand Down
9 changes: 4 additions & 5 deletions exchange/src/exchange/matching_cycle/dev/dev_cycle.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "exchange/exchange_state.hpp"
#include "exchange/matching_cycle/base/base_cycle.hpp"
#include "exchange/metrics/on_tick_metrics.hpp"
#include "exchange/traders/trader_container.hpp"
Expand All @@ -14,11 +15,9 @@ class DevMatchingCycle : public BaseMatchingCycle {

public:
DevMatchingCycle(
TickerContainer tickers, TraderContainer& traders,
common::decimal_price order_fee, common::decimal_quantity max_order_volume
) :
BaseMatchingCycle(std::move(tickers), traders, order_fee, max_order_volume),
pusher(traders)
exchange_state& state, common::decimal_price order_fee,
common::decimal_quantity max_order_volume
) : BaseMatchingCycle(state, order_fee, max_order_volume), pusher(state.traders)
{}

protected:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "exchange/exchange_state.hpp"
#include "exchange/matching_cycle/dev/dev_cycle.hpp"
#include "exchange/sandbox_server/crow.hpp"
#include "exchange/traders/trader_container.hpp"
Expand All @@ -13,9 +14,9 @@ class SandboxMatchingCycle : public DevMatchingCycle {

public:
SandboxMatchingCycle(
TickerContainer tickers, TraderContainer& traders,
common::decimal_price order_fee, common::decimal_quantity max_order_volume
) : DevMatchingCycle(std::move(tickers), traders, order_fee, max_order_volume)
exchange_state& state, common::decimal_price order_fee,
common::decimal_quantity max_order_volume
) : DevMatchingCycle(state, order_fee, max_order_volume)
{}

private:
Expand Down
2 changes: 1 addition & 1 deletion exchange/src/exchange/orders/ticker_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ std::vector<TickerData>
TickerContainer::create_tickers()
{
std::vector<TickerData> result;
result.reserve(common::TICKERS.size());
result.reserve(common::TICKERS.size());
for (std::size_t ticker = 0; ticker < common::TICKERS.size(); ticker++) {
result.emplace_back(static_cast<common::Ticker>(ticker));
}
Expand Down
Loading

0 comments on commit 61d1769

Please sign in to comment.