quoting crash fix and comments

This commit is contained in:
Mohammad Arham committed 2024-04-01 19:01:27 -04:00
1 parent 966ccb5f2c
commit ca40e278ff
1 file changed
+59 -41
@@ -19,6 +19,7 @@
#include <bits/stdint-uintn.h>
#include <boost/circular_buffer.hpp>
#include <boost/tokenizer.hpp>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
@@ -38,6 +39,7 @@ template <class T>
bool from_string(const std::string &str, T *out) {
return google::protobuf::TextFormat::ParseFromString(str, out);
}
//All information required from user
struct QuotingAlgoParams {
SecurityKey security_key_id;
@@ -81,7 +83,8 @@ struct QuotingAlgoParams {
};
// enum class QuotingAlgoParams::State { WORKING, DONE };
// namespace orderman
// All state information needed for this algo
struct QuotingAlgo::PImpl {
@@ -97,7 +100,7 @@ struct QuotingAlgo::PImpl {
double tick_size;
IAlgoServices *algo = nullptr;
analytics::ITheoCalculator const *theo_calc = nullptr;
uint32_t driver_sec_key = 0;
SecurityKey driver_sec_key;
bool subbed_to_driver = false;
std::optional<Book> driver_book = std::nullopt;
std::optional<Book> sec_book = std::nullopt;
@@ -110,6 +113,9 @@ struct QuotingAlgo::PImpl {
QuotingAlgo::QuotingAlgo() : me(new PImpl()) {}
QuotingAlgo::~QuotingAlgo() {}
IAlgoServices &QuotingAlgo::_algo() const { return *me->algo; }
//create a child template so we can
// quickly launch our child when the time comes.
ChildTemplate *_get_child_template(
IBeforeLaunchAlgoServices &serv,
const std::optional<CustomOrderDefinition> &cod,
@@ -122,6 +128,7 @@ ChildTemplate *_get_child_template(
rv.mutable_custom_def()->CopyFrom(cod.value());
return &(serv.make_child_template(rv));
}
//if child order not defined defaults to limit order
rv.mutable_custom_def()->mutable_limit()->MergeFrom(
api::orderman::LimitOrderDefinition());
return &(serv.make_child_template(rv));
@@ -164,6 +171,9 @@ bool _convert_params(
}
// This is called outside of the latency sensitive path
// once. Expensive initialization should be done here, rather than
// in do_launch()
OrderOpResult QuotingAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) {
try {
auto &def = serv.definition();
@@ -174,6 +184,10 @@ OrderOpResult QuotingAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) {
utils::pb::to_string(serv.definition()));
// Validate our parameters, returning an error if we find a
// problem. If an error is returned here the order will be
// abandoned.
if (def.has_custom_def() && def.custom_def().has_dynamic() &&
_convert_params(serv, def.custom_def(), me->algo_params)) {
@@ -198,25 +212,19 @@ OrderOpResult QuotingAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) {
}
void QuotingAlgo::run_algo() {}
// initalise all structs and pointers
// Called in the latency sensitive path, once, when the algo is first launched.
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);
// Cache our algo services
me->algo = &algo_services;
if(me->algo_params->quote_around_theo){
me->algo->subscribe_theos();
}
} catch (...) {
bts_assert(false);
// Subscribe to market data
me->algo->subscribe_book(me->algo_params->security_key_id);
if(me->algo_params->quote_around_theo){
me->algo->subscribe_theos();
}
}
void QuotingAlgo::do_launch_attached( // not needed right now
IAlgoServices &algo_services,
OrderKey,
@@ -253,42 +261,46 @@ void QuotingAlgo::on_theos(const analytics::ITheoCalculator *theo_calculator){
"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->driver_sec_key = SecurityKey(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
// Called when the book for the specified security has changed
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) {
if (sec_key == me->driver_sec_key) {
me->driver_book = book;
return;
}
}
double normalizePrice(double price, double tickSize) {
// Round the price to the nearest tick
if (sec_key != me->algo_params->security_key_id)
return;
me->sec_book = book;
if(!me->algo_params->quote_around_market)
return;
const md::PriceSize &bid = me->sec_book->bid().at(0);
const md::PriceSize &ask = me->sec_book->ask().at(0);
//infers if limit order got filled and if it did does not try to update it
if (me->child_order_key != null_order_key() &&
((me->algo_params->side == api::SIDE_BUY && ask.price() <= me->child_order_price.value()) ||
(me->algo_params->side != api::SIDE_BUY && bid.price() >= me->child_order_price.value()))) {
return;
}
double mid_price=(bid.price() + ask.price())/2;
quote(mid_price);
}
// Round the price to the nearest tick
double normalizePrice(double price, double tickSize) {
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);
@@ -296,6 +308,11 @@ void QuotingAlgo::quote(double price){
}
void QuotingAlgo::_fire_orders(double price){
if(price == me->child_order_price.value()){
return;//avoid unnecessary updates
}
me->child_order_price = Price(price);
if(me->child_order_key==null_order_key())
{
me->child_order_key =
@@ -325,8 +342,9 @@ void QuotingAlgo::do_update(
void QuotingAlgo::on_child_change(OrderKey key, const ChildState &state) {
if(me->child_order_key==key){
me->algo->update_fill_state(state.total_fills, state.last_price);
if(state.status != bts::orderman::ChildStatus::WORKING)
if(state.status != bts::orderman::ChildStatus::WORKING){
me->algo->complete();
}
}
}