quotingalgo working
This commit is contained in:
1 parent
63810e3966
commit
6d0249825d
3 files changed
+399
-1
No files matched your search
@@ -0,0 +1,345 @@
|
||||
#include "lib/orderman/orders/algo/algo_orders/QuotingAlgo.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/domain_model/SecurityKey.h"
|
||||
#include "lib/domain_model/common.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/md/PriceSize.h"
|
||||
#include "lib/orderman/order_factory/IOrderFactory.h"
|
||||
#include "lib/orderman/orderman_utils/Book.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
||||
#include "lib/orderman/orders/algo/IAlgoOrder.h"
|
||||
#include "lib/orderman/orders/algo/IAlgoServices.h"
|
||||
#include "lib/orderman/orders/algo/algo_orders/QuotingAlgo.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include <bits/stdint-intn.h>
|
||||
#include <bits/stdint-uintn.h>
|
||||
#include <boost/circular_buffer.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <fcntl.h>
|
||||
#include <google/protobuf/util/time_util.h>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
using namespace google::protobuf;
|
||||
using namespace boost;
|
||||
template <class T>
|
||||
bool from_string(const std::string &str, T *out) {
|
||||
return google::protobuf::TextFormat::ParseFromString(str, out);
|
||||
}
|
||||
struct QuotingAlgoParams {
|
||||
|
||||
SecurityKey security_key_id;
|
||||
std::string security_name_str;
|
||||
size_t n_ticks;
|
||||
bool quote_around_market;
|
||||
bool quote_around_theo;
|
||||
ChildTemplate *child_template = nullptr;
|
||||
api::Side side;
|
||||
|
||||
QuotingAlgoParams(
|
||||
SecurityKey security_key_id,
|
||||
std::string security_name_str,
|
||||
size_t n_ticks,
|
||||
bool quote_around_market,
|
||||
bool quote_around_theo,
|
||||
ChildTemplate* child,
|
||||
api::Side side
|
||||
)
|
||||
: security_key_id(security_key_id), security_name_str(security_name_str),
|
||||
n_ticks(n_ticks), quote_around_market(quote_around_market),
|
||||
quote_around_theo(quote_around_theo), child_template(child),side(side) {}
|
||||
|
||||
|
||||
std::string print() {
|
||||
std::string sep_obj = ", ";
|
||||
std::string sep_key_val = ":";
|
||||
std::string s = "security_name" + sep_key_val + security_name_str;
|
||||
auto add_to_s = [&](std::string desc, std::string val) {
|
||||
if (s != "") {
|
||||
s += sep_obj;
|
||||
}
|
||||
s += desc + sep_key_val + val;
|
||||
};
|
||||
add_to_s("security_key_id", std::to_string(security_key_id.value()));
|
||||
add_to_s("quote_around_market", std::to_string(quote_around_market));
|
||||
add_to_s("quote_around_theo", std::to_string(quote_around_theo));
|
||||
add_to_s("n_ticks", std::to_string(n_ticks));
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
||||
// enum class QuotingAlgoParams::State { WORKING, DONE };
|
||||
// namespace orderman
|
||||
struct QuotingAlgo::PImpl {
|
||||
|
||||
|
||||
OrderKey self_key = null_order_key();
|
||||
OrderDefinition self_def;
|
||||
google::protobuf::uint32 self_security_key;
|
||||
IBeforeLaunchAlgoServices *servg;
|
||||
bts::orderman::OrderKey child_order_key=null_order_key();
|
||||
Price child_order_price=Price(0);
|
||||
google::protobuf::TextFormat::Printer printer;
|
||||
|
||||
|
||||
double tick_size;
|
||||
IAlgoServices *algo = nullptr;
|
||||
analytics::ITheoCalculator const *theo_calc = nullptr;
|
||||
uint32_t driver_sec_key = 0;
|
||||
bool subbed_to_driver = false;
|
||||
std::optional<Book> driver_book = std::nullopt;
|
||||
std::optional<Book> sec_book = std::nullopt;
|
||||
std::unique_ptr<QuotingAlgoParams> algo_params;
|
||||
std::optional<double> current_mid;
|
||||
|
||||
PImpl() { printer.SetSingleLineMode(true); }
|
||||
};
|
||||
|
||||
QuotingAlgo::QuotingAlgo() : me(new PImpl()) {}
|
||||
QuotingAlgo::~QuotingAlgo() {}
|
||||
IAlgoServices &QuotingAlgo::_algo() const { return *me->algo; }
|
||||
ChildTemplate *_get_child_template(
|
||||
IBeforeLaunchAlgoServices &serv,
|
||||
const CustomOrderDefinition &cod,
|
||||
SecurityKey key,
|
||||
api::Side side) {
|
||||
OrderDefinition rv = serv.definition();
|
||||
rv.set_security_key(key.value());
|
||||
rv.set_side(side);
|
||||
rv.mutable_custom_def()->CopyFrom(cod);
|
||||
return &(serv.make_child_template(rv));
|
||||
}
|
||||
|
||||
// converts paramters recieved from the user and saves them in out_p returns
|
||||
// true if all parameters have values
|
||||
bool _convert_params(
|
||||
IBeforeLaunchAlgoServices &serv,
|
||||
const CustomOrderDefinition &cod,
|
||||
std::unique_ptr<QuotingAlgoParams> &out_p) {
|
||||
const DynamicOrderDefinition &def = cod.dynamic();
|
||||
const auto ¶ms = def.order_params();
|
||||
|
||||
auto quote_around_theo = get_if<bool>(params, "quote_around_theo");
|
||||
auto quote_around_market = get_if<bool>(params, "quote_around_market");
|
||||
auto n_ticks = get_if<uint32_t>(params, "n_ticks");
|
||||
auto custom_ord = get_if<CustomOrderDefinition>(params,"child_order");
|
||||
|
||||
|
||||
if (!custom_ord.has_value() || !n_ticks.has_value() || !quote_around_theo.has_value() || !quote_around_market.has_value() || quote_around_theo.value() == quote_around_market.value()){
|
||||
LOG(OrderMan, Info,
|
||||
"QuotingAlgo::convert_params failed n_ticks:{}, quote_around_theo:{}, quote_around_market:{}, child_order:{}",
|
||||
n_ticks.has_value(), quote_around_theo.has_value(), quote_around_market.has_value(), custom_ord.has_value());
|
||||
return false;
|
||||
}
|
||||
auto key = SecurityKey(serv.security().id());
|
||||
auto side = serv.definition().side();
|
||||
ChildTemplate *child_template =
|
||||
_get_child_template(serv, custom_ord.value(), key , side);
|
||||
out_p = std::make_unique<QuotingAlgoParams>(
|
||||
key,
|
||||
serv.security().glbx().security_desc(),
|
||||
n_ticks.value(),
|
||||
quote_around_market.value(),
|
||||
quote_around_theo.value(),
|
||||
child_template,
|
||||
side
|
||||
);
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
OrderOpResult QuotingAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) {
|
||||
try {
|
||||
auto &def = serv.definition();
|
||||
me->servg = &serv;
|
||||
me->self_key = serv.order_key();
|
||||
|
||||
LOG(OrderMan, Info, "QuotingAlgo::store_and_publish_if_complete rec: {}",
|
||||
utils::pb::to_string(serv.definition()));
|
||||
|
||||
|
||||
if (def.has_custom_def() && def.custom_def().has_dynamic() &&
|
||||
_convert_params(serv, def.custom_def(), me->algo_params)) {
|
||||
|
||||
me->tick_size = serv.security().contract_spec().low_tick();
|
||||
|
||||
me->self_def = serv.definition();
|
||||
me->self_security_key = serv.security().id();
|
||||
return {};
|
||||
|
||||
|
||||
}
|
||||
// diagnostics
|
||||
LOG(OrderMan, Info,
|
||||
"QuotingAlgo::do_before_launch Has custom: {}, has dynamic: {}",
|
||||
def.has_custom_def(), def.custom_def().has_dynamic());
|
||||
return {
|
||||
OrderError::ORDER_ERROR_INVALID_PARAMETER,
|
||||
std::string("Parameters are not well formed")};
|
||||
} catch (...) {
|
||||
bts_assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void QuotingAlgo::run_algo() {}
|
||||
|
||||
// initalise all structs and pointers
|
||||
void QuotingAlgo::do_launch(IAlgoServices &algo_services) {
|
||||
try {
|
||||
me->algo = &algo_services; // subscrive book and trade, get id from
|
||||
// id will be id and security id
|
||||
me->algo->subscribe_book(me->algo_params->security_key_id);
|
||||
me->algo->subscribe_ticker(me->algo_params->security_key_id);
|
||||
|
||||
if(me->algo_params->quote_around_theo){
|
||||
me->algo->subscribe_theos();
|
||||
}
|
||||
|
||||
} catch (...) {
|
||||
bts_assert(false);
|
||||
}
|
||||
}
|
||||
void QuotingAlgo::do_launch_attached( // not needed right now
|
||||
IAlgoServices &algo_services,
|
||||
OrderKey,
|
||||
ChildState) {
|
||||
|
||||
me->algo = &algo_services;
|
||||
_cxl_and_end(true, "QuotingAlgo can not be attached to working orders");
|
||||
}
|
||||
|
||||
void QuotingAlgo::on_theos(const analytics::ITheoCalculator *theo_calculator){
|
||||
|
||||
if(theo_calculator==NULL){
|
||||
return;
|
||||
}
|
||||
|
||||
me->theo_calc = theo_calculator;
|
||||
|
||||
if (me->subbed_to_driver) {
|
||||
const md::PriceSize &driver_bid = me->driver_book->bid().at(0);
|
||||
const md::PriceSize &driver_ask = me->driver_book->ask().at(0);
|
||||
double driver = (driver_bid.price() + driver_ask.price())/2;
|
||||
bts::api::orderman::PrecomputedTheoPoint result;
|
||||
if(!theo_calculator->get_theo(me->algo_params->security_key_id.value(),driver, result)){
|
||||
LOG(OrderMan, Error, "QuotingAlgo::on_theos CANT GET THEOS");
|
||||
return;
|
||||
}
|
||||
me->current_mid = result.theo();
|
||||
quote(me->current_mid.value());
|
||||
return;
|
||||
}
|
||||
auto driver_sec = me->theo_calc->get_driver_sec(me->algo_params->security_key_id.value());
|
||||
if (driver_sec.has_value()) {
|
||||
LOG(OrderMan, Info,
|
||||
"QuotingAlgo::on_theos order_key: {} subscribing to driver sec_key: {}",
|
||||
me->algo->order_key(), driver_sec.value());
|
||||
me->subbed_to_driver = true;
|
||||
me->driver_sec_key = driver_sec.value();
|
||||
me->algo->subscribe_book(SecurityKey(me->driver_sec_key));
|
||||
}
|
||||
// theo_calculator->get_theo(uint32_t sec_id, double driver, bts::api::orderman::PrecomputedTheoPoint &result)
|
||||
|
||||
}
|
||||
// save current book for future use
|
||||
void QuotingAlgo::on_book(SecurityKey sec_key, Book const &book) {
|
||||
|
||||
|
||||
if (sec_key.value() == me->algo_params->security_key_id.value()) {
|
||||
me->sec_book = book;
|
||||
|
||||
if(me->algo_params->quote_around_market){
|
||||
const md::PriceSize &bid = me->sec_book->bid().at(0);
|
||||
const md::PriceSize &ask = me->sec_book->ask().at(0);
|
||||
double mid_price=(bid.price() + ask.price())/2;
|
||||
quote(mid_price);
|
||||
}
|
||||
|
||||
|
||||
} else if (sec_key.value() == me->driver_sec_key) {
|
||||
me->driver_book = book;
|
||||
}
|
||||
}
|
||||
|
||||
double normalizePrice(double price, double tickSize) {
|
||||
// Round the price to the nearest tick
|
||||
double roundedPrice = round(price / tickSize) * tickSize;
|
||||
return roundedPrice;
|
||||
}
|
||||
void QuotingAlgo::quote(double price){
|
||||
if(price == me->child_order_price.value()){
|
||||
return;
|
||||
}
|
||||
me->child_order_price = Price(price);
|
||||
double normalized= normalizePrice(price,me->tick_size);
|
||||
double offset = me->algo_params->n_ticks*me->tick_size;
|
||||
double price_with_offset = normalized + offset*(me->algo_params->side==api::Side::SIDE_BUY?-1:1);
|
||||
_fire_orders(price_with_offset);
|
||||
}
|
||||
|
||||
void QuotingAlgo::_fire_orders(double price){
|
||||
if(me->child_order_key==null_order_key())
|
||||
{
|
||||
me->child_order_key =
|
||||
_algo().launch_child(*me->algo_params->child_template, bts::Price(price), bts::Quantity(1));
|
||||
return;
|
||||
}
|
||||
Update update;
|
||||
update.set_price(price);
|
||||
update.set_quantity(1);
|
||||
_algo().update_child(me->child_order_key,update);
|
||||
}
|
||||
|
||||
|
||||
void QuotingAlgo::on_ticker(SecurityKey, TradeVector const &) {}
|
||||
|
||||
void QuotingAlgo::report_state() {}
|
||||
|
||||
void QuotingAlgo::do_update(
|
||||
api::orderman::Update const &upd) { // not needed right now
|
||||
LOG(OrderMan, Warn,
|
||||
"QuotingAlgo::do_update order: {} update not allowed, please "
|
||||
"cxl and re-enter {} upd key: {}",
|
||||
me->algo->order_key().value(), upd.order_key());
|
||||
_cxl_and_end(true, " update not allowed");
|
||||
}
|
||||
|
||||
void QuotingAlgo::on_child_change(OrderKey key, const ChildState &state) {
|
||||
if(me->child_order_key==key && state.status != bts::orderman::ChildStatus::WORKING){
|
||||
|
||||
_cxl_and_end(false, "completed");
|
||||
}
|
||||
}
|
||||
|
||||
void QuotingAlgo::on_status(SecurityKey key, co2::TradingStatus s) {
|
||||
LOG(OrderMan, Info, "QuotingAlgo::on_status key: {} status: {}", key.value(),
|
||||
static_cast<int>(s));
|
||||
}
|
||||
|
||||
// updates states every 500 ms, state visible if algo tree checked on front
|
||||
// end
|
||||
void QuotingAlgo::on_timer() {}
|
||||
|
||||
void QuotingAlgo::_cxl_and_end(bool is_error, const std::string &err_msg) {
|
||||
if (is_error)
|
||||
me->algo->terminate_error(err_msg);
|
||||
else
|
||||
me->algo->complete();
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/orderman/orderman_utils/Book.h"
|
||||
#include "lib/orderman/orders/algo/IAlgoOrder.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
class QuotingAlgo : public IAlgoOrder {
|
||||
public:
|
||||
QuotingAlgo();
|
||||
~QuotingAlgo();
|
||||
OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override;
|
||||
void do_launch(IAlgoServices &) override;
|
||||
void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override;
|
||||
void do_update(api::orderman::Update const &) override;
|
||||
void on_child_change(OrderKey key, const ChildState &state) override;
|
||||
void on_status(SecurityKey, co2::TradingStatus) override;
|
||||
void on_timer() override;
|
||||
void on_book(SecurityKey, Book const &) override;
|
||||
void on_theos(const analytics::ITheoCalculator *theo_calculator) override;
|
||||
void on_ticker(SecurityKey, TradeVector const &) override;
|
||||
void report_state();
|
||||
void run_algo();
|
||||
void quote(double price);
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
enum class State;
|
||||
std::unique_ptr<PImpl> me;
|
||||
|
||||
private:
|
||||
IAlgoServices &_algo() const;
|
||||
void _fire_orders(double price);
|
||||
void _cxl_and_end(bool is_error, const std::string &err_msg);
|
||||
};
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -2,13 +2,13 @@
|
||||
#include "DormanAlgo.h"
|
||||
#include "OnSignalAlgo.h"
|
||||
#include "PyramidAlgo.h"
|
||||
#include "QuotingAlgo.h"
|
||||
#include "SniperAlgo.h"
|
||||
#include "TASAlgo.h"
|
||||
#include "TASCounterAlgo.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/toggles.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
||||
#include "lib/orderman/orders/algo/algo_orders/LimitCoverAlgo.h"
|
||||
#include "lib/orderman/orders/algo/algo_orders/TaskingEyeAlgo.h"
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "lib/orderman/orders/algo/algo_orders/limit_cover/LimitCoverAdapter.h"
|
||||
#include "lib/orderman/orders/algo/algo_orders/tests/ExecutionTesterAlgo.h"
|
||||
#include "lib/plugin/SparkServices.h"
|
||||
#include <vector>
|
||||
|
||||
using namespace bts;
|
||||
using namespace api::orderman;
|
||||
@@ -232,6 +233,18 @@ extern "C" void init_limit_cover(bts::SparkServices &srv) {
|
||||
lc_desc, []() { return std::make_unique<orderman::LimitCoverAlgo>(); });
|
||||
}
|
||||
|
||||
extern "C" void init_quoting_algo_plugin(bts::SparkServices &srv) {
|
||||
DynamicOrderDescriptor quoting_algo;
|
||||
quoting_algo.set_dynamic_order_type("QuotingAlgo");
|
||||
*quoting_algo.add_param_desc() = param_uint32("n_ticks");
|
||||
*quoting_algo.add_param_desc() = param_bool("quote_around_theo");
|
||||
*quoting_algo.add_param_desc() = param_bool("quote_around_market");
|
||||
*quoting_algo.add_param_desc() = param_order("child_order");
|
||||
srv.register_dynamic_algo_order(
|
||||
quoting_algo, []() { return std::make_unique<orderman::QuotingAlgo>(); });
|
||||
}
|
||||
|
||||
|
||||
extern "C" void init_on_signal_plugin(bts::SparkServices &srv) {
|
||||
DynamicOrderDescriptor on_signal;
|
||||
on_signal.set_dynamic_order_type("OnSignal");
|
||||
@@ -360,6 +373,7 @@ extern "C" void init_spark_plugin(bts::SparkServices &srv) {
|
||||
// init_sniper_plugin(srv);
|
||||
init_task_eye_plugin(srv);
|
||||
init_vol_algo_plugin(srv);
|
||||
init_quoting_algo_plugin(srv);
|
||||
}
|
||||
|
||||
} // namespace dorman
|
||||
|
||||
Reference in new issue
Block a user