cleanup: Tidy up IAlgoOrder, add fmt impl for ChildState

Part of a larger effort to polish the sdk for laguna
This commit is contained in:
John Colvin committed 2024-04-01 10:09:34 -04:00
1 parent 3b6cf0b187
commit cd6a052d0c
2 files changed
+103 -44

No files matched your search

@@ -1,18 +0,0 @@
#include "lib/orderman/orders/algo/IAlgoOrder.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
namespace bts {
namespace orderman {
#include "lib/orderman/orders/algo/IAlgoOrder.h"
IAlgoOrder::~IAlgoOrder() {}
OrderOpResult IAlgoOrder::do_before_launch(IBeforeLaunchAlgoServices &) {
return {};
}
void IAlgoOrder::do_launch_attached(IAlgoServices &serv, OrderKey, ChildState) {
serv.terminate_error("Algo does not implement attachment functionality");
}
} // namespace orderman
} // namespace bts
+103 -26
View File
@@ -1,17 +1,20 @@
#pragma once
#include "lib/core/FMT_IMPL.h"
#include "lib/md/protos/co2.pb.h"
#include "lib/orderman/calculations/ITheoCalculator.h"
#include "lib/orderman/events/TickerUpdateEvent.h"
#include "lib/orderman/orders/Order.h"
#include "lib/orderman/orders/OrderOpResult.h"
#include "lib/orderman/orders/algo/PositionUpdate.h"
#include <memory>
#include <string>
namespace bts {
namespace orderman {
namespace bts::orderman {
enum class ChildStatus { WORKING, COMPLETED, CANCELED };
constexpr std::string ChildStatus_Name(ChildStatus child_status);
struct ChildState {
Price price;
Quantity quantity;
@@ -29,46 +32,46 @@ class IAlgoServices;
class IAlgoOrder {
public:
virtual ~IAlgoOrder();
virtual ~IAlgoOrder() = default;
// --------------------------------------------------------------
// Events Handlers - These will be optionally invoked if subscribed to
// --------------------------------------------------------------
// The child order specified by the order key has changed
virtual void on_child_change(OrderKey, ChildState const &) {}
virtual void on_child_change(
OrderKey child_key, const ChildState& child_state);
// The book for the specified security has changed
virtual void on_book(SecurityKey, Book const &) {}
virtual void on_book(SecurityKey security_key, const Book& book);
// Trades stream for the specified security
virtual void on_ticker(SecurityKey, TradeVector const &){};
virtual void on_ticker(SecurityKey security_key, const TradeVector& trades);
// The trading status for the specified security has changed
virtual void on_status(SecurityKey, co2::TradingStatus){};
virtual void on_status(SecurityKey security_key, co2::TradingStatus status);
// The settlement price for the specified security has changed
virtual void on_settlement(SecurityKey, Price){};
virtual void on_settlement(SecurityKey security_key, Price price);
// The current position for the security, account combination
virtual void on_position(PositionUpdate const &){};
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
// sell aggressor.
virtual void on_trade_summary_signal(
SecurityKey,
api::Side /* aggressor */,
Price /* last_price */,
Quantity /* last_quantity */,
uint /* number of price levels */) {}
SecurityKey security_key,
api::Side aggressor,
Price last_price,
Quantity last_quantity,
uint num_price_levels);
// The calculator has updated itself, theos need to be recomputed
virtual void on_theos(
analytics::ITheoCalculator const * /*theo_calculator*/) {}
virtual void on_theos(const analytics::ITheoCalculator* theo_calculator);
// A periodic timer has fired
virtual void on_timer() {}
virtual void on_timer();
// --------------------------------------------------------------
// Hooks - These allow you to customize the behaviour of your algo
@@ -80,10 +83,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 &) = 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
@@ -91,12 +94,13 @@ class IAlgoOrder {
// changes for the attached order. Note that any fills reported
// 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 &, OrderKey child, ChildState);
virtual void do_launch_attached(
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 &) {}
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
@@ -104,8 +108,81 @@ class IAlgoOrder {
// for example to detach from specific children before canceling or
// to implement custom cleanup behaviour when canceled.
enum class OnCancelResponse { IGNORE, HONOR };
virtual OnCancelResponse on_cancel() { return OnCancelResponse::HONOR; }
virtual OnCancelResponse on_cancel();
};
} // namespace orderman
} // namespace bts
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
constexpr std::string ChildStatus_Name(ChildStatus child_status) {
switch (child_status) {
case ChildStatus::WORKING:
return "WORKING";
case ChildStatus::COMPLETED:
return "COMPLETED";
case ChildStatus::CANCELED:
return "CANCELED";
default:
return "UNKNOWN";
}
}
inline void IAlgoOrder::on_child_change(
OrderKey /*child_key*/, const ChildState& /*child_state*/) {}
inline void IAlgoOrder::on_book(
SecurityKey /*security_key*/, const Book& /*book*/) {}
inline void IAlgoOrder::on_ticker(
SecurityKey /*security_key*/, const TradeVector& /*trades*/) {}
inline void IAlgoOrder::on_status(
SecurityKey /*security_key*/, co2::TradingStatus /*status*/) {}
inline void IAlgoOrder::on_settlement(
SecurityKey /*security_key*/, Price /*price*/) {}
inline void IAlgoOrder::on_position(const PositionUpdate& /*position*/) {}
inline void IAlgoOrder::on_trade_summary_signal(
SecurityKey /*security_key*/,
api::Side /*aggressor*/,
Price /*last_price*/,
Quantity /*last_quantity*/,
uint /*num_price_levels*/) {}
inline void IAlgoOrder::on_theos(
analytics::ITheoCalculator const* /*theo_calculator*/) {}
inline void IAlgoOrder::on_timer() {}
inline OrderOpResult IAlgoOrder::do_before_launch(IBeforeLaunchAlgoServices&) {
return {};
}
inline void IAlgoOrder::do_launch_attached(
IAlgoServices& /*services*/,
OrderKey /*child_key*/,
ChildState /*child_state*/) {}
inline void IAlgoOrder::do_update(api::orderman::Update const& /*update*/) {}
inline IAlgoOrder::OnCancelResponse IAlgoOrder::on_cancel() {
return OnCancelResponse::HONOR;
}
} // namespace bts::orderman
FMT_IMPL(
bts::orderman::ChildState,
st,
"{} {} @ {}, filled {} @ {} (last {} @ {})",
bts::orderman::ChildStatus_Name(st.status),
st.price,
st.quantity,
st.total_fills,
st.avg_price,
st.last_fills,
st.last_price);