diff --git a/exchange/CMakeLists.txt b/exchange/CMakeLists.txt index a55d5f3a..11ecc333 100644 --- a/exchange/CMakeLists.txt +++ b/exchange/CMakeLists.txt @@ -67,7 +67,6 @@ add_library( src/exchange/bots/bot_container.cpp src/exchange/bots/bot_types/retail.cpp - src/exchange/bots/variance.cpp src/exchange/bots/bot_types/market_maker.cpp src/exchange/matching/engine.cpp diff --git a/exchange/src/exchange/bots/bot_container.cpp b/exchange/src/exchange/bots/bot_container.cpp index bf516a90..93f7a89c 100644 --- a/exchange/src/exchange/bots/bot_container.cpp +++ b/exchange/src/exchange/bots/bot_container.cpp @@ -3,6 +3,7 @@ #include "common/types/decimal.hpp" #include "exchange/bots/bot_types/market_maker.hpp" #include "exchange/bots/bot_types/retail.hpp" +#include "exchange/bots/shared_bot_state.hpp" #include "exchange/traders/trader_container.hpp" #include @@ -16,8 +17,6 @@ BotContainer::generate_orders( common::decimal_price midprice, common::decimal_price theo ) { - variance_calculator_.record_price(midprice); - common::decimal_price cumulative_interest_limit{}; common::decimal_quantity cumulative_quantity_held{}; @@ -27,10 +26,12 @@ BotContainer::generate_orders( bot->get_portfolio().get_holdings(bot->get_ticker()); } - return generate_orders( - {midprice, theo, variance_calculator_.calculate_volatility(), - cumulative_interest_limit, cumulative_quantity_held} - ); + generate_orders(shared_bot_state{ + .MIDPRICE = midprice, + .THEO = theo, + .CUMULATIVE_INTEREST_LIMIT = cumulative_interest_limit, + .CUMULATIVE_QUANTITY_HELD = cumulative_quantity_held + }); } template diff --git a/exchange/src/exchange/bots/bot_container.hpp b/exchange/src/exchange/bots/bot_container.hpp index 28d8255d..55664d25 100644 --- a/exchange/src/exchange/bots/bot_container.hpp +++ b/exchange/src/exchange/bots/bot_container.hpp @@ -4,7 +4,6 @@ #include "exchange/traders/trader_container.hpp" #include "exchange/traders/trader_types/bot_trader.hpp" #include "shared_bot_state.hpp" -#include "variance.hpp" namespace nutc::exchange { @@ -13,7 +12,6 @@ namespace nutc::exchange { */ class BotContainer { using BotVector = std::vector>; - VarianceCalculator variance_calculator_; BotVector bots_{}; diff --git a/exchange/src/exchange/bots/shared_bot_state.hpp b/exchange/src/exchange/bots/shared_bot_state.hpp index f9274b02..d0dc0aa7 100644 --- a/exchange/src/exchange/bots/shared_bot_state.hpp +++ b/exchange/src/exchange/bots/shared_bot_state.hpp @@ -6,7 +6,6 @@ namespace nutc::exchange { struct shared_bot_state { const common::decimal_price MIDPRICE; const common::decimal_price THEO; - const common::decimal_quantity REALIZED_VOLATILITY; const common::decimal_price CUMULATIVE_INTEREST_LIMIT; const common::decimal_quantity CUMULATIVE_QUANTITY_HELD; }; diff --git a/exchange/src/exchange/bots/variance.cpp b/exchange/src/exchange/bots/variance.cpp deleted file mode 100644 index ec33f1fb..00000000 --- a/exchange/src/exchange/bots/variance.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "variance.hpp" - -namespace nutc::exchange { - -void -VarianceCalculator::record_price(common::decimal_price price) -{ - if (price == common::decimal_price{0.0}) - return; - - prices.push_back(price); - - if (prices.size() > PRICES_LOOKBACK_TICKS) - prices.pop_front(); -} - -std::vector -VarianceCalculator::calculate_returns() const -{ - std::vector returns; - returns.reserve(prices.size() - 1); - - for (auto price = prices.begin() + 1; price != prices.end(); price++) { - double tickReturn = - (double{*price} - double{*(price - 1)}) / double{*(price - 1)}; - returns.push_back(tickReturn); - } - return returns; -} - -double -VarianceCalculator::calculate_mean(const std::vector& data) const -{ - double sum{0}; - for (double d : data) { - sum += d; - } - return sum / static_cast(data.size()); -} - -double -VarianceCalculator::calculate_variance(const std::vector& returns, double mean) - const -{ - double variance = 0.0; - for (double r : returns) { - variance += (r - mean) * (r - mean); - } - return variance / static_cast(returns.size() - 1); -} - -double -VarianceCalculator::calculate_volatility() const -{ - if (prices.size() < 10) - return 0; - - std::vector returns = calculate_returns(); - double mean = calculate_mean(returns); - double variance = calculate_variance(returns, mean); - return std::sqrt(variance); -} -} // namespace nutc::exchange diff --git a/exchange/src/exchange/bots/variance.hpp b/exchange/src/exchange/bots/variance.hpp deleted file mode 100644 index 5edab48b..00000000 --- a/exchange/src/exchange/bots/variance.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "common/types/decimal.hpp" - -#include - -namespace nutc::exchange { -class VarianceCalculator { - static constexpr size_t PRICES_LOOKBACK_TICKS{1000}; - - std::deque prices; - - std::vector calculate_returns() const; - double calculate_mean(const std::vector& data) const; - double calculate_variance(const std::vector& returns, double mean) const; - -public: - void record_price(common::decimal_price price); - double calculate_volatility() const; -}; - -} // namespace nutc::exchange diff --git a/exchange/src/exchange/orders/ticker_container.cpp b/exchange/src/exchange/orders/ticker_container.cpp index a82bdb6e..6ff08805 100644 --- a/exchange/src/exchange/orders/ticker_container.cpp +++ b/exchange/src/exchange/orders/ticker_container.cpp @@ -101,6 +101,7 @@ std::vector TickerContainer::create_tickers() { std::vector result; + result.reserve(common::TICKERS.size()); for (std::size_t ticker = 0; ticker < common::TICKERS.size(); ticker++) { result.emplace_back(static_cast(ticker)); } diff --git a/exchange/src/exchange/orders/ticker_data.hpp b/exchange/src/exchange/orders/ticker_data.hpp index 1d7f9081..4973efea 100644 --- a/exchange/src/exchange/orders/ticker_data.hpp +++ b/exchange/src/exchange/orders/ticker_data.hpp @@ -2,12 +2,8 @@ #include "common/types/decimal.hpp" #include "common/types/ticker.hpp" #include "exchange/bots/bot_container.hpp" -#include "exchange/config/dynamic/ticker_config.hpp" #include "exchange/orders/orderbook/composite_orderbook.hpp" #include "exchange/theo/brownian.hpp" -#include "exchange/traders/trader_container.hpp" - -#include namespace nutc::exchange {