From bcbfc56e7d58bf59f684b98ff098210e720dd6b9 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 11 Aug 2025 17:27:11 +0000 Subject: [PATCH] check pt: PingPong algo --- .../spark/collections/circular_buffer.h | 106 ++++++++ .../circular_buffer_const_iterator.h | 60 +++++ .../circular_buffer_const_reverse_iterator.h | 60 +++++ .../collections/circular_buffer_iterator.h | 68 +++++ .../circular_buffer_reverse_iterator.h | 68 +++++ .../spark/collections/small_vector.h | 0 src/lib_spark/spark/core/TimeSpan.h | 5 + .../spark/core/math/rolling_variance.h | 70 +++++ .../spark/core/thread_local_singleton.h | 10 + src/lib_spark/spark/domain_model/common.cpp | 3 + src/lib_spark/spark/domain_model/common.h | 9 + .../spark/logging/structured_logging.h | 18 ++ .../orderman_utils/exec_fee_calculator.h | 46 ++++ .../orderman/orders/algo/ChildTemplate.h | 5 + .../spark/orderman/orders/algo/IAlgoOrder.cpp | 30 +++ .../spark/orderman/orders/algo/IAlgoOrder.h | 48 ++-- .../orders/algo/algo_orders/PingpongAlgo.cpp | 243 ++++++++++++++++-- .../orders/algo/algo_orders/PingpongAlgo.h | 4 +- .../algo/algo_orders/RegisterDorman.cpp | 1 + .../algo/algo_orders/pingpong_pos_data.h | 33 +++ .../orders/algo/algo_orders/pingpong_stats.h | 72 ++++++ 21 files changed, 913 insertions(+), 46 deletions(-) create mode 100644 src/lib_spark/spark/collections/circular_buffer.h create mode 100644 src/lib_spark/spark/collections/circular_buffer_const_iterator.h create mode 100644 src/lib_spark/spark/collections/circular_buffer_const_reverse_iterator.h create mode 100644 src/lib_spark/spark/collections/circular_buffer_iterator.h create mode 100644 src/lib_spark/spark/collections/circular_buffer_reverse_iterator.h create mode 100644 src/lib_spark/spark/collections/small_vector.h create mode 100644 src/lib_spark/spark/core/math/rolling_variance.h create mode 100644 src/lib_spark/spark/core/thread_local_singleton.h create mode 100644 src/lib_spark/spark/logging/structured_logging.h create mode 100644 src/lib_spark/spark/orderman/orderman_utils/exec_fee_calculator.h create mode 100644 src/lib_spark/spark/orderman/orders/algo/IAlgoOrder.cpp create mode 100644 src/lib_spark/spark/orderman/orders/algo/algo_orders/pingpong_pos_data.h create mode 100644 src/lib_spark/spark/orderman/orders/algo/algo_orders/pingpong_stats.h diff --git a/src/lib_spark/spark/collections/circular_buffer.h b/src/lib_spark/spark/collections/circular_buffer.h new file mode 100644 index 000000000..94b88e464 --- /dev/null +++ b/src/lib_spark/spark/collections/circular_buffer.h @@ -0,0 +1,106 @@ +#pragma once + +#include "spark/collections/circular_buffer_const_iterator.h" +#include "spark/collections/circular_buffer_const_reverse_iterator.h" +#include "spark/collections/circular_buffer_iterator.h" +#include "spark/collections/circular_buffer_reverse_iterator.h" + +#include + +namespace bts::collections { + +template +class CircularBuffer { + std::vector data; + std::size_t capacity; + std::size_t origin; + + public: + typedef std::vector container; + typedef container::value_type value_type; + typedef container::pointer pointer; + typedef container::const_pointer const_pointer; + typedef container::reference reference; + typedef container::const_reference const_reference; + typedef CircularBuffer_Iterator iterator; + typedef CircularBuffer_ConstIterator const_iterator; + typedef CircularBuffer_ConstReverseIterator const_reverse_iterator; + typedef CircularBuffer_ReverseIterator reverse_iterator; + typedef container::size_type size_type; + typedef container::difference_type difference_type; + typedef container::allocator_type allocator_type; + + CircularBuffer() : capacity(4096) {} + explicit CircularBuffer(std::size_t capacity) : capacity(capacity) {} + + constexpr bool empty() const { return data.empty(); } + + constexpr std::size_t size() const { return data.size(); } + + void push_back(T value) { + if (capacity == 0) { + return; + } + data.erase(data.begin()); + if (data.size() < capacity) { + data.push_back(std::move(value)); + } else { + data[origin] = std::move(value); + origin = origin < capacity ? origin + 1 : 0; + } + } + + bool push_back_pop(T value, T &popped) { + if (capacity == 0) { + return false; + } + if (data.size() < capacity) { + data.push_back(std::move(value)); + return false; + } + popped = std::move(data[origin]); + data[origin++] = std::move(value); + return true; + } + + template + T &emplace_back(Args... args) { + if (capacity == 0) { + return; + } + if (data.size() < capacity) { + data.emplace_back(std::forward(args)...); + } else { + data.emplace(data.begin() + origin, std::forward(args)...); + origin = origin < capacity ? origin + 1 : 0; + } + } + + iterator erase(const_iterator first, const_iterator last) { + data.erase(first, last); + } + + constexpr T &front() { return data.at(origin); } + + constexpr const T &front() const { return data.at(origin); } + + constexpr T &back() { + return origin == 0 ? data.back() : data.at(origin - 1); + } + + constexpr const T &back() const { + return origin == 0 ? data.back() : data.at(origin - 1); + } + + constexpr T &operator[](std::size_t index) { + const auto rem = data.size() - origin; + return data[index < rem ? origin + index : index - rem]; + } + + constexpr const T &operator[](std::size_t index) const { + const auto rem = data.size() - origin; + return data[index < rem ? origin + index : index - rem]; + } +}; + +} // namespace bts::collections diff --git a/src/lib_spark/spark/collections/circular_buffer_const_iterator.h b/src/lib_spark/spark/collections/circular_buffer_const_iterator.h new file mode 100644 index 000000000..79df304a9 --- /dev/null +++ b/src/lib_spark/spark/collections/circular_buffer_const_iterator.h @@ -0,0 +1,60 @@ +#pragma once + +#include + +namespace bts::collections { + +template +class CircularBuffer_ConstIterator { + public: + std::vector *data; + std::size_t origin; + std::size_t pos; + + const T &operator*() const { + if (origin >= data->size()) { + return nullptr; + } + const auto rem = data->size() - origin; + return (*data)[pos > rem ? pos - rem : origin + pos]; + } + + constexpr CircularBuffer_ConstIterator &operator++() { + pos++; + return *this; + } + constexpr CircularBuffer_ConstIterator operator++(int) { + return {data, origin, pos++}; + } + constexpr CircularBuffer_ConstIterator &operator+=(int value) { + pos += value; + return *this; + } + constexpr CircularBuffer_ConstIterator operator+(int value) { + return {data, origin, pos + value}; + } + constexpr CircularBuffer_ConstIterator &operator--() { + pos--; + return *this; + } + constexpr CircularBuffer_ConstIterator operator--(int) { + return {data, origin, pos--}; + } + constexpr CircularBuffer_ConstIterator &operator-=(int value) { + pos -= value; + return *this; + } + constexpr CircularBuffer_ConstIterator operator-(int value) { + return {data, origin, pos - value}; + } + constexpr bool operator==( + const CircularBuffer_ConstIterator &other) const noexcept { + return pos == other.pos && data == other.data && origin == other.origin; + } + constexpr bool operator!=( + const CircularBuffer_ConstIterator &other) const noexcept { + return pos != other.pos || data != other.data || origin != other.origin; + } +}; + +} // namespace bts::collections diff --git a/src/lib_spark/spark/collections/circular_buffer_const_reverse_iterator.h b/src/lib_spark/spark/collections/circular_buffer_const_reverse_iterator.h new file mode 100644 index 000000000..c739ca0da --- /dev/null +++ b/src/lib_spark/spark/collections/circular_buffer_const_reverse_iterator.h @@ -0,0 +1,60 @@ +#pragma once + +#include + +namespace bts::collections { + +template +class CircularBuffer_ConstReverseIterator { + public: + std::vector *data; + std::size_t origin; + std::size_t pos; + + const T &operator*() const { + if (origin >= data->size()) { + return nullptr; + } + return (*data) + [pos < origin ? origin - 1 - pos : data->size() - (pos - origin)]; + } + + constexpr CircularBuffer_ConstReverseIterator &operator++() { + pos++; + return *this; + } + constexpr CircularBuffer_ConstReverseIterator operator++(int) { + return {data, origin, pos++}; + } + constexpr CircularBuffer_ConstReverseIterator &operator+=(int value) { + pos += value; + return *this; + } + constexpr CircularBuffer_ConstReverseIterator operator+(int value) { + return {data, origin, pos + value}; + } + constexpr CircularBuffer_ConstReverseIterator &operator--() { + pos--; + return *this; + } + constexpr CircularBuffer_ConstReverseIterator operator--(int) { + return {data, origin, pos--}; + } + constexpr CircularBuffer_ConstReverseIterator &operator-=(int value) { + pos -= value; + return *this; + } + constexpr CircularBuffer_ConstReverseIterator operator-(int value) { + return {data, origin, pos - value}; + } + constexpr bool operator==( + const CircularBuffer_ConstReverseIterator &other) const noexcept { + return pos == other.pos && data == other.data && origin == other.origin; + } + constexpr bool operator!=( + const CircularBuffer_ConstReverseIterator &other) const noexcept { + return pos != other.pos || data != other.data || origin != other.origin; + } +}; + +} // namespace bts::collections diff --git a/src/lib_spark/spark/collections/circular_buffer_iterator.h b/src/lib_spark/spark/collections/circular_buffer_iterator.h new file mode 100644 index 000000000..a1cdc2f48 --- /dev/null +++ b/src/lib_spark/spark/collections/circular_buffer_iterator.h @@ -0,0 +1,68 @@ +#pragma once + +#include + +namespace bts::collections { + +template +class CircularBuffer_Iterator { + public: + std::vector *data; + std::size_t origin; + std::size_t pos; + + const T &operator*() const { + if (origin >= data->size()) { + return nullptr; + } + const auto rem = data->size() - origin; + return (*data)[pos > rem ? pos - rem : origin + pos]; + } + + T &operator*() { + if (origin >= data->size()) { + return nullptr; + } + const auto rem = data->size() - origin; + return (*data)[pos > rem ? pos - rem : origin + pos]; + } + + constexpr CircularBuffer_Iterator &operator++() { + pos++; + return *this; + } + constexpr CircularBuffer_Iterator operator++(int) { + return {data, origin, pos++}; + } + constexpr CircularBuffer_Iterator &operator+=(int value) { + pos += value; + return *this; + } + constexpr CircularBuffer_Iterator operator+(int value) { + return {data, origin, pos + value}; + } + constexpr CircularBuffer_Iterator &operator--() { + pos--; + return *this; + } + constexpr CircularBuffer_Iterator operator--(int) { + return {data, origin, pos--}; + } + constexpr CircularBuffer_Iterator &operator-=(int value) { + pos -= value; + return *this; + } + constexpr CircularBuffer_Iterator operator-(int value) { + return {data, origin, pos - value}; + } + constexpr bool operator==( + const CircularBuffer_Iterator &other) const noexcept { + return pos == other.pos && data == other.data && origin == other.origin; + } + constexpr bool operator!=( + const CircularBuffer_Iterator &other) const noexcept { + return pos != other.pos || data != other.data || origin != other.origin; + } +}; + +} // namespace bts::collections diff --git a/src/lib_spark/spark/collections/circular_buffer_reverse_iterator.h b/src/lib_spark/spark/collections/circular_buffer_reverse_iterator.h new file mode 100644 index 000000000..4f7bbbe79 --- /dev/null +++ b/src/lib_spark/spark/collections/circular_buffer_reverse_iterator.h @@ -0,0 +1,68 @@ +#pragma once + +#include + +namespace bts::collections { + +template +class CircularBuffer_ReverseIterator { + public: + std::vector *data; + std::size_t origin; + std::size_t pos; + + const T &operator*() const { + if (origin >= data->size()) { + return nullptr; + } + return (*data) + [pos < origin ? origin - 1 - pos : data->size() - (pos - origin)]; + } + + T &operator*() { + if (origin >= data->size()) { + return nullptr; + } + return (*data) + [pos < origin ? origin - 1 - pos : data->size() - (pos - origin)]; + } + + constexpr CircularBuffer_ReverseIterator &operator++() { + pos++; + return *this; + } + constexpr CircularBuffer_ReverseIterator operator++(int) { + return {data, origin, pos++}; + } + constexpr CircularBuffer_ReverseIterator &operator+=(int value) { + pos += value; + return *this; + } + constexpr CircularBuffer_ReverseIterator operator+(int value) { + return {data, origin, pos + value}; + } + constexpr CircularBuffer_ReverseIterator &operator--() { + pos--; + return *this; + } + constexpr CircularBuffer_ReverseIterator operator--(int) { + return {data, origin, pos--}; + } + constexpr CircularBuffer_ReverseIterator &operator-=(int value) { + pos -= value; + return *this; + } + constexpr CircularBuffer_ReverseIterator operator-(int value) { + return {data, origin, pos - value}; + } + constexpr bool operator==( + const CircularBuffer_ReverseIterator &other) const noexcept { + return pos == other.pos && data == other.data && origin == other.origin; + } + constexpr bool operator!=( + const CircularBuffer_ReverseIterator &other) const noexcept { + return pos != other.pos || data != other.data || origin != other.origin; + } +}; + +} // namespace bts::collections diff --git a/src/lib_spark/spark/collections/small_vector.h b/src/lib_spark/spark/collections/small_vector.h new file mode 100644 index 000000000..e69de29bb diff --git a/src/lib_spark/spark/core/TimeSpan.h b/src/lib_spark/spark/core/TimeSpan.h index 00c2fcd4d..7c1ef151a 100644 --- a/src/lib_spark/spark/core/TimeSpan.h +++ b/src/lib_spark/spark/core/TimeSpan.h @@ -70,6 +70,7 @@ constexpr TimeSpan operator-(TimeSpan lhs, TimeSpan rhs) noexcept; constexpr TimeSpan &operator-=(TimeSpan &lhs, TimeSpan rhs) noexcept; constexpr TimeSpan operator+(TimeSpan lhs, TimeSpan rhs) noexcept; constexpr TimeSpan &operator+=(TimeSpan &lhs, TimeSpan rhs) noexcept; +constexpr double operator/(TimeSpan lhs, TimeSpan rhs) noexcept; constexpr TimeSpan operator*(TimeSpan lhs, int rhs) noexcept; constexpr TimeSpan &operator*=(TimeSpan &lhs, int rhs) noexcept; @@ -217,6 +218,10 @@ constexpr TimeSpan &operator+=(TimeSpan &lhs, TimeSpan rhs) noexcept { return lhs; } +constexpr double operator/(TimeSpan lhs, TimeSpan rhs) noexcept { + return static_cast(lhs.nanos()) / static_cast(rhs.nanos()); +} + constexpr TimeSpan operator*(TimeSpan lhs, int rhs) noexcept { return TimeSpan{lhs.ticks() * rhs}; } diff --git a/src/lib_spark/spark/core/math/rolling_variance.h b/src/lib_spark/spark/core/math/rolling_variance.h new file mode 100644 index 000000000..c7742b722 --- /dev/null +++ b/src/lib_spark/spark/core/math/rolling_variance.h @@ -0,0 +1,70 @@ +#pragma once + +#include "spark/collections/circular_buffer.h" +#include "spark/core/TimeSpan.h" +#include "spark/core/TimeStamp.h" + +#include + +namespace bts::core::math { + +struct RollingVariance { + TimeSpan interval_unit{}; + std::uint32_t window_sz; + TimeStamp start_time{}; + std::int64_t curr_elapsed_units = 0; + double sum = 0; + double sum_avg_diff_sq = 0; + collections::CircularBuffer history{4}; + bool take_interval_max = true; + + RollingVariance( + TimeSpan interval_unit, std::uint32_t window_sz, bool take_interval_max) + : interval_unit(interval_unit), window_sz(window_sz), + start_time(TimeStamp::now()), history(window_sz), + take_interval_max(take_interval_max) {} + + double avg() const { + if (history.size() < window_sz) { + return std::numeric_limits::quiet_NaN(); + } + return sum / window_sz; + } + + double var() const { + if (history.size() < window_sz) { + return std::numeric_limits::quiet_NaN(); + } + return sum_avg_diff_sq / window_sz; + } + + double dev() const { return std::sqrt(var()); } + + void on_value(double value, bool is_replace) { + if (window_sz == 0) { + return; + } + if (history.empty()) { + history.push_back(value); + sum = value; + sum_avg_diff_sq = 0; + return; + } + double popped; + if (is_replace) { + popped = history[history.size() - 1]; + history[history.size() - 1] = value; + } else if (!history.push_back_pop(value, popped)) { + popped = 0; + } + sum += value - popped; + sum_avg_diff_sq += value * value - popped * popped; + } + + void push_back(double value) { on_value(value, false); } + void replace_back(double value) { on_value(value, true); } + double back() const { return history.back(); } + constexpr bool empty() const { return history.empty(); } +}; + +} // namespace bts::core::math diff --git a/src/lib_spark/spark/core/thread_local_singleton.h b/src/lib_spark/spark/core/thread_local_singleton.h new file mode 100644 index 000000000..99ce29aad --- /dev/null +++ b/src/lib_spark/spark/core/thread_local_singleton.h @@ -0,0 +1,10 @@ +#pragma once + +namespace bts::core { + +template +struct ThreadLocalSingleton { + static inline thread_local T instance; +}; + +} // namespace bts::core diff --git a/src/lib_spark/spark/domain_model/common.cpp b/src/lib_spark/spark/domain_model/common.cpp index 9dbf792b4..1c0285724 100644 --- a/src/lib_spark/spark/domain_model/common.cpp +++ b/src/lib_spark/spark/domain_model/common.cpp @@ -1,6 +1,9 @@ #include "common.h" #include "spark/utils/bts_assert.h" +#include +#include + namespace bts { double CQuantity::round_to_decimal_places(double value, int decimal_places) { diff --git a/src/lib_spark/spark/domain_model/common.h b/src/lib_spark/spark/domain_model/common.h index 2bd44dc52..1c2a42948 100644 --- a/src/lib_spark/spark/domain_model/common.h +++ b/src/lib_spark/spark/domain_model/common.h @@ -11,6 +11,8 @@ #include "spark/domain_model/SecurityKey.h" #include "spark/utils/ValueWrapper.h" +#include + namespace bts { struct CQuantity { double qty = 0; @@ -102,6 +104,13 @@ struct CQuantity { void round() { qty = round_to_decimal_places(qty, precision_digits); } }; +inline void from_json(const nlohmann::json &j, CQuantity &qty) { + qty = CQuantity::create_new( + j.is_string() ? std::stod(j.get()) : j.get()); +} + +inline void to_json(nlohmann::json &j, CQuantity &qty) { j = qty.full_value(); } + inline std::ostream &operator<<(std::ostream &out, CQuantity const &qty) { out << qty.full_value(); return out; diff --git a/src/lib_spark/spark/logging/structured_logging.h b/src/lib_spark/spark/logging/structured_logging.h new file mode 100644 index 000000000..c80b6ab23 --- /dev/null +++ b/src/lib_spark/spark/logging/structured_logging.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace bts::logging::structured { + +template +struct TimeStampedLog { + T value; + TimeStampedLog(T &&value) : value(std::move(value)) {} +}; + +template +TimeStampedLog time_stamped_log(T &&value) { + return {std::move(value)}; +} + +} // namespace bts::logging::structured diff --git a/src/lib_spark/spark/orderman/orderman_utils/exec_fee_calculator.h b/src/lib_spark/spark/orderman/orderman_utils/exec_fee_calculator.h new file mode 100644 index 000000000..5c13a40d3 --- /dev/null +++ b/src/lib_spark/spark/orderman/orderman_utils/exec_fee_calculator.h @@ -0,0 +1,46 @@ +#pragma once + +#include +#include + +namespace bts::orderman { + +enum class ExecFeeType { ABSOLUTE, PERCENT }; + +struct ExecFeeCalculator { + struct AbsoluteFee { + double value; + }; + struct PercentFee { + double value; + }; + + struct Fill { + double quantity; + double price; + double fee; + bool is_fee_percent; + }; + + ExecFeeCalculator(double total_filled_notional, double total_fee_notional) + : filled_notional(total_filled_notional), + fee_notional(total_fee_notional) {} + + ExecFeeCalculator on_fill(const Fill &fill) const { + return ExecFeeCalculator{ + fee_notional + (fill.is_fee_percent + ? fill.quantity * fill.price * fill.fee + : fill.fee), + filled_notional + fill.quantity * fill.price}; + } + + double filled_notional = 0; + double fee_notional = 0; +}; + +// inline ExecFeeCalculator::Fill operator-( +// const ExecFeeCalculator &lhs, const ExecFeeCalculator &rhs) { +// return ExecFeeCalculator::Fill { lhs.filled_notional } +// } + +} // namespace bts::orderman diff --git a/src/lib_spark/spark/orderman/orders/algo/ChildTemplate.h b/src/lib_spark/spark/orderman/orders/algo/ChildTemplate.h index 3d913e0fa..4112281c0 100644 --- a/src/lib_spark/spark/orderman/orders/algo/ChildTemplate.h +++ b/src/lib_spark/spark/orderman/orders/algo/ChildTemplate.h @@ -1,5 +1,10 @@ #pragma once +#include "spark/api/services/OrderService.pb.h" +#include "spark/orderman/OrderBuilder.h" +#include "spark/orderman/order_factory/IOrderFactory.h" +#include "spark/orderman/order_key.h" + namespace bts { namespace orderman { diff --git a/src/lib_spark/spark/orderman/orders/algo/IAlgoOrder.cpp b/src/lib_spark/spark/orderman/orders/algo/IAlgoOrder.cpp new file mode 100644 index 000000000..56d74633f --- /dev/null +++ b/src/lib_spark/spark/orderman/orders/algo/IAlgoOrder.cpp @@ -0,0 +1,30 @@ +#include "IAlgoOrder.h" +#include "spark/domain_model/common.h" + +#include + +namespace bts::orderman { + +void to_json(nlohmann::json &j, const ChildState &st) { + j["p"] = *st.price; + j["q"] = st.quantity.full_value(); + j["tf"] = st.total_fills.full_value(); + j["ap"] = *st.avg_price; + j["lf"] = st.last_fills.full_value(); + j["lp"] = *st.last_price; + j["st"] = ChildStatus_Name(st.status); +} + +void from_json(const nlohmann::json &j, ChildState &st) { + j.at("p").get_to(*st.price); + j.at("q").get_to(st.quantity); + j.at("tf").get_to(st.total_fills); + j.at("ap").get_to(st.total_fills); + j.at("lf").get_to(st.last_fills); + j.at("lp").get_to(*st.last_price); + std::string str; + j.at("st").get_to(str); + ChildStatus_Parse(str, &st.status); +} + +} // namespace bts::orderman diff --git a/src/lib_spark/spark/orderman/orders/algo/IAlgoOrder.h b/src/lib_spark/spark/orderman/orders/algo/IAlgoOrder.h index 0b014a453..f9f2244c6 100644 --- a/src/lib_spark/spark/orderman/orders/algo/IAlgoOrder.h +++ b/src/lib_spark/spark/orderman/orders/algo/IAlgoOrder.h @@ -7,6 +7,7 @@ #include "spark/orderman/orders/OrderOpResult.h" #include "spark/orderman/orders/algo/PositionUpdate.h" +#include #include namespace bts::orderman { @@ -40,13 +41,13 @@ class IAlgoOrder { // The child order specified by the order key has changed virtual void on_child_change( - OrderKey child_key, const ChildState& child_state); + OrderKey child_key, const ChildState &child_state); // The book for the specified security has changed - virtual void on_book(SecurityKey security_key, const Book& book); + virtual void on_book(SecurityKey security_key, const Book &book); // Trades stream for the specified security - virtual void on_ticker(SecurityKey security_key, const TradeVector& trades); + virtual void on_ticker(SecurityKey security_key, const TradeVector &trades); // The trading status for the specified security has changed virtual void on_status(SecurityKey security_key, co2::TradingStatus status); @@ -55,7 +56,7 @@ class IAlgoOrder { virtual void on_settlement(SecurityKey security_key, Price price); // The current position for the security, account combination - virtual void on_position(const PositionUpdate& position); + virtual void on_position(const PositionUpdate &position); // A signal sent ahead of a book update, indicating that one or // more trades between the two price levels specified for a buy or @@ -68,7 +69,7 @@ class IAlgoOrder { uint num_price_levels); // The calculator has updated itself, theos need to be recomputed - virtual void on_theos(const analytics::ITheoCalculator* theo_calculator); + virtual void on_theos(const analytics::ITheoCalculator *theo_calculator); // A periodic timer has fired virtual void on_timer(); @@ -83,10 +84,10 @@ class IAlgoOrder { // so it can be used for any expensive initialization // operations. The implementation should return an error code if it // detects an invalid parameter in the algo definition. - virtual OrderOpResult do_before_launch(IBeforeLaunchAlgoServices&); + virtual OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &); // Override this method to activate your algo - virtual void do_launch(IAlgoServices& services) = 0; + virtual void do_launch(IAlgoServices &services) = 0; // Override this method to activate your algo using an existing // working order as a child. Attached child orders are @@ -95,12 +96,12 @@ class IAlgoOrder { // from this call through IAlgoServices::update_fill_state() will // be included in the fills_at_attach_time count field. virtual void do_launch_attached( - IAlgoServices& services, OrderKey child_key, ChildState child_state); + IAlgoServices &services, OrderKey child_key, ChildState child_state); // Implement this method to respond to updates to your algo. The // implementation should validate the update and either apply it, // if valid or terminate with an error otherwise - virtual void do_update(api::orderman::Update const& update); + virtual void do_update(api::orderman::Update const &update); // Override this method to provide custom handling of a cancel // request. The default behaviour is to cancel both this order and @@ -129,14 +130,27 @@ constexpr std::string ChildStatus_Name(ChildStatus child_status) { } } +inline bool ChildStatus_Parse(const std::string &str, ChildStatus *res) { + if (str == "WORKING") { + *res = ChildStatus::WORKING; + } else if (str == "COMPLETED") { + *res = ChildStatus::COMPLETED; + } else if (str == "CANCELED") { + *res = ChildStatus::CANCELED; + } else { + return false; + } + return true; +} + inline void IAlgoOrder::on_child_change( - OrderKey /*child_key*/, const ChildState& /*child_state*/) {} + OrderKey /*child_key*/, const ChildState & /*child_state*/) {} inline void IAlgoOrder::on_book( - SecurityKey /*security_key*/, const Book& /*book*/) {} + SecurityKey /*security_key*/, const Book & /*book*/) {} inline void IAlgoOrder::on_ticker( - SecurityKey /*security_key*/, const TradeVector& /*trades*/) {} + SecurityKey /*security_key*/, const TradeVector & /*trades*/) {} inline void IAlgoOrder::on_status( SecurityKey /*security_key*/, co2::TradingStatus /*status*/) {} @@ -144,7 +158,7 @@ inline void IAlgoOrder::on_status( inline void IAlgoOrder::on_settlement( SecurityKey /*security_key*/, Price /*price*/) {} -inline void IAlgoOrder::on_position(const PositionUpdate& /*position*/) {} +inline void IAlgoOrder::on_position(const PositionUpdate & /*position*/) {} inline void IAlgoOrder::on_trade_summary_signal( SecurityKey /*security_key*/, @@ -154,20 +168,20 @@ inline void IAlgoOrder::on_trade_summary_signal( uint /*num_price_levels*/) {} inline void IAlgoOrder::on_theos( - analytics::ITheoCalculator const* /*theo_calculator*/) {} + analytics::ITheoCalculator const * /*theo_calculator*/) {} inline void IAlgoOrder::on_timer() {} -inline OrderOpResult IAlgoOrder::do_before_launch(IBeforeLaunchAlgoServices&) { +inline OrderOpResult IAlgoOrder::do_before_launch(IBeforeLaunchAlgoServices &) { return {}; } inline void IAlgoOrder::do_launch_attached( - IAlgoServices& /*services*/, + IAlgoServices & /*services*/, OrderKey /*child_key*/, ChildState /*child_state*/) {} -inline void IAlgoOrder::do_update(api::orderman::Update const& /*update*/) {} +inline void IAlgoOrder::do_update(api::orderman::Update const & /*update*/) {} inline IAlgoOrder::OnCancelResponse IAlgoOrder::on_cancel() { return OnCancelResponse::HONOR; diff --git a/src/lib_spark/spark/orderman/orders/algo/algo_orders/PingpongAlgo.cpp b/src/lib_spark/spark/orderman/orders/algo/algo_orders/PingpongAlgo.cpp index 22fb4f315..9a2e262cd 100644 --- a/src/lib_spark/spark/orderman/orders/algo/algo_orders/PingpongAlgo.cpp +++ b/src/lib_spark/spark/orderman/orders/algo/algo_orders/PingpongAlgo.cpp @@ -1,43 +1,208 @@ #include "PingpongAlgo.h" +#include "spark/api/services/OrderService.pb.h" +#include "spark/collections/circular_buffer.h" +#include "spark/core/TimeStamp.h" +#include "spark/core/math/rolling_variance.h" +#include "spark/domain/PriceComp.h" #include "spark/domain_model/Security.h" +#include "spark/domain_model/Security.pb.h" +#include "spark/domain_model/common.h" +#include "spark/md/PriceSize.h" #include "spark/orderman/order_factory/IOrderFactory.h" +#include "spark/orderman/order_key.h" #include "spark/orderman/orderman_utils/Book.h" +#include "spark/orderman/orderman_utils/PriceComp.h" #include "spark/orderman/orderman_utils/orderman_utils.h" +#include "spark/orderman/orders/algo/ChildTemplate.h" #include "spark/orderman/orders/algo/IAlgoServices.h" -#include "spark/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" +#include "spark/orderman/orders/algo/algo_orders/pingpong_pos_data.h" +#include "spark/orderman/orders/algo/algo_orders/pingpong_stats.h" #include "spark/os/os.h" -#include "spark/utils/PBUtils.h" -#include "spark/utils/datetime.h" +#include "spark/sparkproto/SparkProtos.pb.h" + +#include +#include +#include #include +#include namespace bts { namespace orderman { +#define _LOG_TRACE(level, name) \ + LOG_TRACE(md, level, std::string("pingpong_algo"), std::string(name)) +#define INFO_TRACE(name) _LOG_TRACE(bts::Info, name) +#define WARN_TRACE(name) _LOG_TRACE(bts::Warn, name) +#define ERROR_TRACE(name) _LOG_TRACE(bts::Error, name) + +#define _LOG(level, ...) LOG(OrderMan, level, __VA_ARGS__) +#define INFO(...) _LOG(Info, __VA_ARGS__) +#define SINFO(type, ...) _LOG(Info, "[pingpong_algo_" type ".json]" __VA_ARGS__) +#define WARN(...) _LOG(Warn, __VA_ARGS__) +#define ERROR(...) _LOG(Error, __VA_ARGS__) + +using bts::api::SIDE_BUY; +using bts::api::SIDE_SELL; +using bts::collections::CircularBuffer; +using bts::core::math::RollingVariance; +using std::int64_t; +using std::size_t; +using std::string; +using std::tuple; +using std::uint32_t; + using namespace api::orderman; enum class PingpongAlgo::Status { WAITING_FOR_SIGNAL, WORKING, DONE }; +const std::string &PingpongAlgo::Status_Name(Status st) { + switch (st) { + case Status::WAITING_FOR_SIGNAL: { + static const std::string rv{"WAITING_FOR_SIGNAL"}; + return rv; + } + case Status::WORKING: { + static const std::string rv{"WORKING"}; + return rv; + } + case Status::DONE: { + static const std::string rv{"DONE"}; + return rv; + } + default: { + static const std::string rv{"UNKNOWN"}; + return rv; + } + } +} + struct PingpongAlgo::PImpl { Status state = Status::WAITING_FOR_SIGNAL; IAlgoServices *algo_services = nullptr; SecurityKey sec_key = SecurityKey(0); - //PingpongAlgo::PingpongAlgoParams algo_params; + // dynamic algo params + std::uint32_t window_secs; + double show_qty; + std::optional stats; + PingpongPosData lng; + PingpongPosData sht; + Book book; + Status status; + + PingpongStats &ensure_stats() { + if (stats) { + return *stats; + } + return stats.emplace(core::TimeSpan::from_seconds(1), window_secs); + } + + OrderKey launch( + double trigger_mult, + api::Side mkt_side, + Quantity quantity, + ChildTemplate *child_template) { + const bool mkt_is_buy = mkt_side == api::SIDE_BUY; + const RollingVariance &rolling = + mkt_is_buy ? stats->rolling_bid : stats->rolling_ask; + double mkt_avg = rolling.avg(); + double mkt_dev = rolling.dev(); + double trigger = mkt_avg + mkt_dev * trigger_mult; + return algo_services->launch_child( + *child_template, Price{trigger}, quantity); + } + + void open() { + const auto q = algo_services->quantity(); + sht.open_key = launch(sht.open_trigger, SIDE_BUY, q, sht.open_template); + lng.open_key = launch(lng.open_trigger, SIDE_SELL, q, lng.open_template); + } + + void close_long(Quantity qty) { + lng.close_keys.push_back( + launch(lng.close_trigger, SIDE_BUY, qty, lng.close_template)); + } + + void close_short(Quantity qty) { + sht.close_keys.push_back( + launch(sht.close_trigger, SIDE_SELL, qty, sht.close_template)); + } + + void close(OrderKey child_key, Quantity qty) { + SINFO("close", R"("k": {}, "q": {})", qty); + if (child_key == lng.open_key) { + close_long(qty); + } else if (child_key == sht.open_key) { + SINFO("close", R"("k": {}, "q": {})", qty); + close_short(qty); + } + bts_assert_msgf(false, "Unexpected child key {}", child_key); + } + + OrderDefinition order_factory(api::Side side) const { + auto &svc = *algo_services; + auto def = svc.definition(); + def.set_side(side); + auto &cdef = *def.mutable_custom_def(); + const auto &sec = svc.security(); + if (sec.has_synthetic()) { + auto &spd = *cdef.mutable_spreader(); + spd.mutable_security()->CopyFrom(svc.security()); + auto &strategy = *spd.mutable_execution_strategy(); + strategy.set_spreader_type( + bts::spark::ExecutionStrategy_SpreaderType_REQUOTE); + auto &leg_params = *strategy.mutable_leg_params_map(); + bool is_first_leg = true; + for (const auto &leg : sec.synthetic().legs()) { + bts_assert(leg.has_security()); + auto &lp = leg_params.emplace(leg.security().id()).first->second; + lp.set_is_active_quoting(is_first_leg); + is_first_leg = false; + } + } else { + cdef.mutable_limit(); + } + return def; + } }; PingpongAlgo::PingpongAlgo() : me(new PImpl()) {} PingpongAlgo::~PingpongAlgo() {} + // initialization ///////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// -OrderOpResult PingpongAlgo::do_before_launch( - IBeforeLaunchAlgoServices & /*serv*/) { +OrderOpResult PingpongAlgo::do_before_launch(IBeforeLaunchAlgoServices &svc) { + auto &def = svc.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_dynamic()); + auto &dyn = def.custom_def().dynamic(); + auto ¶ms = dyn.order_params(); + try { + me->lng.open_trigger = params.find("Open Long Pos")->second.as_double(); + me->lng.close_trigger = params.find("Close Long Pos")->second.as_double(); + me->sht.open_trigger = params.find("Open Short Pos")->second.as_double(); + me->sht.close_trigger = + params.find("Close Short Pos")->second.as_double(); + me->window_secs = + params.find("Sliding Window Seconds")->second.as_uint32(); + } catch (const std::exception &ex) { + return {OrderError::ORDER_ERROR_INVALID_PARAMETER, string(ex.what())}; + } + auto buy_template = me->order_factory(api::SIDE_BUY); + auto sell_template = me->order_factory(api::SIDE_SELL); + me->lng.open_template = &(svc.make_child_template(buy_template)); + me->lng.close_template = &(svc.make_child_template(sell_template)); + me->sht.open_template = &(svc.make_child_template(sell_template)); + me->sht.close_template = &(svc.make_child_template(buy_template)); return {OrderError::ORDER_ERROR_OK, std::string("OK")}; } + void PingpongAlgo::do_launch(IAlgoServices &algo_services) { me->algo_services = &algo_services; _algo().subscribe_status(me->sec_key); //_algo().subscribe_timer(); } + void PingpongAlgo::do_launch_attached( IAlgoServices &algo_services, OrderKey, ChildState) { me->algo_services = &algo_services; @@ -50,9 +215,10 @@ void PingpongAlgo::do_update(const api::orderman::Update &) { "PingpongAlgo::do_update order:{} state:{} update not allowed ", _algo().order_key().value(), static_cast(me->state)); } + // callbacks ////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// -void PingpongAlgo::on_book(SecurityKey, const Book &) { +void PingpongAlgo::on_book(SecurityKey, const Book &bk) { // find the mean and std dev. for std dev, we'll most likely have to keep a sliding window of the // bids and asks, perhaps a std::vector> and we recompute it everytime. @@ -61,32 +227,55 @@ void PingpongAlgo::on_book(SecurityKey, const Book &) { // when the open long pos order gets filled, we send the close long position order at μ + cl.σ. // when the buy open pos order gets filled, we send the close long position order at μ + cl.σ. // similiarly for short. + switch (me->status) { + case Status::WAITING_FOR_SIGNAL: { + me->book = bk; + double bid = bk.bid()[0].price(); + double ask = bk.ask()[0].price(); + auto &stats = me->ensure_stats(); + stats.on_bbo(bid, ask); + if (stats.is_sufficient_sample()) { + me->open(); + _switch_state(Status::WORKING); + } + break; + } + default: + break; + } } -void PingpongAlgo::on_child_change(OrderKey, ChildState const &) {} + +void PingpongAlgo::on_child_change(OrderKey ck, ChildState const &cst) { + auto &svc = *me->algo_services; + auto k = svc.order_key(); + auto sid = svc.security().id(); + SINFO( + "child_change", + R"({{ "k": {}, "sid": {}, "ck": {}, "c": {}, "st": "{}", "lng": {}, "sht": {} }})", + k, sid, ck, cst, Status_Name(me->status), me->lng.pos, 3.4); + switch (cst.status) { + case ChildStatus::WORKING: + case ChildStatus::COMPLETED: + if (cst.last_fills > 0) { + me->close(ck, cst.last_fills); + } + break; + case ChildStatus::CANCELED: + me->algo_services->terminate_error("Unexpected child cancel"); + break; + } +} + void PingpongAlgo::on_status(SecurityKey, co2::TradingStatus) {} void PingpongAlgo::on_timer() {} IAlgoServices &PingpongAlgo::_algo() const { return *me->algo_services; } /////////////////////////////////////////////////////////////////////////////// -ChildTemplate *PingpongAlgo::_create_order_template( - IBeforeLaunchAlgoServices &, SecurityKey, api::Side) { - return nullptr; -} void PingpongAlgo::_switch_state(PingpongAlgo::Status new_state) { - switch (new_state) { - case (PingpongAlgo::Status::WAITING_FOR_SIGNAL): - bts_assert(false); - break; - case (PingpongAlgo::Status::WORKING): - bts_assert(me->state == PingpongAlgo::Status::WAITING_FOR_SIGNAL); - break; - case (PingpongAlgo::Status::DONE): - bts_assert(me->state != PingpongAlgo::Status::DONE); - break; - } - LOG(OrderMan, Info, "PingpongAlgo::_switch_state key:{} {}->{} ", - _algo().order_key(), static_cast(me->state), - static_cast(new_state)); + auto old_st = me->status; + SINFO( + "status", R"({{ "old": "{}", "new": "{}" }})", Status_Name(old_st), + Status_Name(new_state)); me->state = new_state; } @@ -99,4 +288,4 @@ void PingpongAlgo::_cxl_and_end(bool is_error, const std::string &err_msg) { } } // namespace orderman -} // namespace bts \ No newline at end of file +} // namespace bts diff --git a/src/lib_spark/spark/orderman/orders/algo/algo_orders/PingpongAlgo.h b/src/lib_spark/spark/orderman/orders/algo/algo_orders/PingpongAlgo.h index 31e09771c..f9a0f1257 100644 --- a/src/lib_spark/spark/orderman/orders/algo/algo_orders/PingpongAlgo.h +++ b/src/lib_spark/spark/orderman/orders/algo/algo_orders/PingpongAlgo.h @@ -27,7 +27,7 @@ class PingpongAlgo : public IAlgoOrder { PImpl *me; //std::unique_ptr me; - private: + static const std::string &Status_Name(Status st); IAlgoServices &_algo() const; static ChildTemplate *_create_order_template( IBeforeLaunchAlgoServices &, SecurityKey, api::Side); @@ -35,4 +35,4 @@ class PingpongAlgo : public IAlgoOrder { void _switch_state(Status new_status); void _cxl_and_end(bool is_error, const std::string &err_msg); }; -} // namespace bts::orderman \ No newline at end of file +} // namespace bts::orderman diff --git a/src/lib_spark/spark/orderman/orders/algo/algo_orders/RegisterDorman.cpp b/src/lib_spark/spark/orderman/orders/algo/algo_orders/RegisterDorman.cpp index 79e8e5778..087385a1d 100644 --- a/src/lib_spark/spark/orderman/orders/algo/algo_orders/RegisterDorman.cpp +++ b/src/lib_spark/spark/orderman/orders/algo/algo_orders/RegisterDorman.cpp @@ -380,6 +380,7 @@ extern "C" void init_pingpong_plugin(bts::SparkServices &srv) { *pingpong_algo.add_param_desc() = param_double("Open Short Pos"); // μ + cs.σ *pingpong_algo.add_param_desc() = param_double("Close Short Pos"); + *pingpong_algo.add_param_desc() = param_double("Show Quantity"); *pingpong_algo.add_param_desc() = param_uint32("Sliding Window Seconds"); srv.register_dynamic_algo_order( pingpong_algo, []() { return std::make_unique(); }); diff --git a/src/lib_spark/spark/orderman/orders/algo/algo_orders/pingpong_pos_data.h b/src/lib_spark/spark/orderman/orders/algo/algo_orders/pingpong_pos_data.h new file mode 100644 index 000000000..9812bc184 --- /dev/null +++ b/src/lib_spark/spark/orderman/orders/algo/algo_orders/pingpong_pos_data.h @@ -0,0 +1,33 @@ +#pragma once + +#include "spark/core/math/rolling_variance.h" +#include "spark/orderman/order_key.h" +#include "spark/orderman/orders/algo/ChildTemplate.h" +#include + +namespace bts::orderman { + +struct PingpongPosData { + enum Status { + IDLE = 0, + OPENING = 1, + CLOSING = 1 << 1, + }; + + ChildTemplate *open_template = nullptr; + ChildTemplate *close_template = nullptr; + double pos = 0; + double avg_open_cost; + double avg_close_cost; + OrderKey open_key{0}; + boost::container::small_vector close_keys{0}; + double open_wqty; + double close_wqty; + double open_fqty; + double close_fqty; + float open_trigger; + float close_trigger; + bool is_buy; +}; + +} // namespace bts::orderman diff --git a/src/lib_spark/spark/orderman/orders/algo/algo_orders/pingpong_stats.h b/src/lib_spark/spark/orderman/orders/algo/algo_orders/pingpong_stats.h new file mode 100644 index 000000000..95a56c537 --- /dev/null +++ b/src/lib_spark/spark/orderman/orders/algo/algo_orders/pingpong_stats.h @@ -0,0 +1,72 @@ +#pragma once + +#include "spark/collections/circular_buffer.h" +#include "spark/core/math/rolling_variance.h" + +#include +#include + +namespace bts::orderman { + +struct PingpongStats { + core::math::RollingVariance rolling_bid; + core::math::RollingVariance rolling_ask; + collections::CircularBuffer updates; + core::TimeStamp start_time; + std::uint32_t n_intervals; + core::TimeSpan interval; + std::int64_t curr_elapsed_units = 0; + + PingpongStats(core::TimeSpan interval_unit, std::uint32_t window_sz) + : rolling_bid(interval_unit, window_sz, true), + rolling_ask(interval_unit, window_sz, false), updates(window_sz), + n_intervals(window_sz) {} + + bool is_sufficient_sample() const { + const auto now = core::TimeStamp::now(); + return now - updates.front() <= + (interval * static_cast(n_intervals)); + } + + void on_bbo(double bid, double ask) { + if (n_intervals == 0) { + return; + } + const auto now = core::TimeStamp::now(); + std::int64_t elapsed_units = + (now - start_time).nanos() / interval.nanos(); + double prev_bid = rolling_bid.empty() ? std::numeric_limits::min() + : rolling_bid.back(); + double prev_ask = rolling_ask.empty() ? std::numeric_limits::max() + : rolling_ask.back(); + if (elapsed_units == curr_elapsed_units) { + // The window hasn't changed since the last time this was called (not + // enough time has elapsed). + if (bid > prev_bid) { + // Take highest bid + rolling_bid.replace_back(bid); + } + if (ask < prev_ask) { + // Take lowest ask + rolling_ask.replace_back(ask); + } + return; + } + // We're in a new window. It's possible that more than one window has + // elapsed since the last time this was called. Backfill with bbo from + // right before this update. + std::uint32_t backfill_count = elapsed_units - curr_elapsed_units; + curr_elapsed_units = elapsed_units; + if (backfill_count >= n_intervals) { + backfill_count = n_intervals - 1; + } + for (std::size_t i = 0; i < n_intervals - 1; i++) { + rolling_bid.push_back(prev_bid); + rolling_ask.push_back(prev_ask); + } + rolling_bid.push_back(bid); + rolling_ask.push_back(ask); + } +}; + +} // namespace bts::orderman