Merge branch 'master' of bitbucket.org:bluetradesys/spark
This commit is contained in:
commit
79f8604eb6
22 files changed
+495
-109
No files matched your search
+1
-1
Submodule externals updated: 1884a761e4...466db3141f.
@@ -139,8 +139,8 @@ message NativeOrderState {
|
||||
}
|
||||
|
||||
message SpreaderOrderState {
|
||||
double target_price = 1;
|
||||
uint32 target_quantity = 2 ;
|
||||
double price = 1;
|
||||
uint32 quantity = 2 ;
|
||||
string account_name = 3;
|
||||
string operator_id = 4;
|
||||
}
|
||||
|
||||
@@ -29,4 +29,42 @@ inline Price encode_exchange_price(api::Security const&sec, Price in) {
|
||||
return in;
|
||||
}
|
||||
|
||||
inline double raw_round_to_quote_price (double raw_price, double tick_size, api::BuySell side)
|
||||
{
|
||||
double factor = 1000000;
|
||||
auto i_price = llround (factor * raw_price);
|
||||
auto i_tick_size = llround (tick_size * factor);
|
||||
|
||||
long new_i_price = 0;
|
||||
long mod = i_price % i_tick_size;
|
||||
|
||||
if (mod == 0)
|
||||
// price is already a valid tick
|
||||
new_i_price = i_price;
|
||||
else if (side == api::BUY) {
|
||||
// buy, round down
|
||||
if (i_price > 0)
|
||||
new_i_price = i_price - mod;
|
||||
else
|
||||
new_i_price = i_price - mod - i_tick_size;
|
||||
}
|
||||
else if (side == api::SELL) {
|
||||
// sell, round up
|
||||
if (i_price > 0)
|
||||
new_i_price = i_price - mod + i_tick_size;
|
||||
else
|
||||
new_i_price = i_price - mod;
|
||||
}
|
||||
|
||||
return new_i_price / factor;
|
||||
}
|
||||
|
||||
inline Price round_to_quote_price(api::Security const&sec, Price in, api::BuySell side) {
|
||||
auto p = in.value();
|
||||
if (p < -sec.contract_spec().price_break() || p > sec.contract_spec().price_break())
|
||||
return Price(raw_round_to_quote_price(p,sec.contract_spec().high_tick(),side));
|
||||
else
|
||||
return Price(raw_round_to_quote_price(p,sec.contract_spec().low_tick(),side));
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -40,11 +40,11 @@ struct InitSysLog {
|
||||
|
||||
auto formatter() {
|
||||
return [] (LogMsg const&m) {
|
||||
return fmt::format("{}: {} | ~{}:{}~ {}:{} tid:{} pid:{}",
|
||||
return fmt::format("{}| {:<10} {:<8} | {} --- {}:{} tid:{} pid:{}",
|
||||
datetime::time_string(datetime::now()),
|
||||
m.msg,
|
||||
m.subsystem_name,
|
||||
m.level_name,
|
||||
m.msg,
|
||||
boost::filesystem::path(m.file).filename().string() ,
|
||||
m.line,
|
||||
m.tid,
|
||||
|
||||
@@ -63,7 +63,7 @@ private:
|
||||
std::atomic<bool> _done;
|
||||
};
|
||||
|
||||
#define LOG(subsystem,level,...) bts::log::_thread_logger.logger->enqueue(log::subsystem,level,__FILE__,__LINE__,__VA_ARGS__)
|
||||
#define LOG(subsystem,level,...) bts::log::_thread_logger.logger->enqueue(bts::log::subsystem,level,__FILE__,__LINE__,__VA_ARGS__)
|
||||
|
||||
#define LOG_ONCE_MAYBE(subsystem,level,...) bts::log::_thread_logger.logger->enqueue_once_maybe(log::subsystem,level,__FILE__,__LINE__,__VA_ARGS__)
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "server/sparkd/orderman/service/OrderServiceServer.h"
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/api/services/OrderService.client.pb.h"
|
||||
#include "server/sparkd/cmds/Run.h"
|
||||
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
@@ -98,7 +99,11 @@ int TestOrderman::execute(std::vector<std::string> const &)
|
||||
CurrentOrderType current_order_type (CurrentOrderType::UNDEFINED);
|
||||
|
||||
api::ServiceContext ctx;
|
||||
OrderServiceServer server(ctx);
|
||||
auto server_thread = std::thread([]() {
|
||||
cmd::Run run;
|
||||
run.execute({});
|
||||
});
|
||||
|
||||
api::orderman::OrderService client(ctx.locate<api::orderman::OrderService> ());
|
||||
|
||||
client.subscribe_orders().subscribe([](const api::orderman::OrdersUpdate &update) {
|
||||
@@ -111,7 +116,7 @@ int TestOrderman::execute(std::vector<std::string> const &)
|
||||
while (true) {
|
||||
std::cout <<
|
||||
"create native order: n sec_key b/s price quantity\n"
|
||||
"create spreader order: s sec_key b/s price quantity\n"
|
||||
"create spreader order: s b/s price quantity\n"
|
||||
"update order: u price quantity\n"
|
||||
"cancel order: x\n"
|
||||
"change current order: o order_key n|s\n";
|
||||
@@ -197,3 +202,4 @@ std::string TestOrderman::help ()
|
||||
}}
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "server/sparkd/cmds/Run.h"
|
||||
#include "server/sparkd/cmds/Help.h"
|
||||
#include "server/sparkd/cmds/TestOrderman.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "server/sparkd/cmds/TestCeloxica.h"
|
||||
|
||||
#define BEGIN_CMD
|
||||
@@ -15,8 +16,11 @@
|
||||
|
||||
int main( int argc, char*argv [] )
|
||||
{
|
||||
bts::os::set_thread_name("main");
|
||||
bts::utils::init_spark_lib();
|
||||
using namespace bts;
|
||||
|
||||
os::set_thread_name("main");
|
||||
utils::init_spark_lib();
|
||||
LOG(Gen,Warn,"Sparkd launched --------------------------------------------------------------------");
|
||||
|
||||
BEGIN_CMD;
|
||||
REGISTER (Test);
|
||||
@@ -26,7 +30,7 @@ int main( int argc, char*argv [] )
|
||||
REGISTER (TestCeloxica);
|
||||
END_CMD;
|
||||
|
||||
bts::utils::finit_spark_lib();
|
||||
utils::finit_spark_lib();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,9 +40,10 @@ SpreaderOrderMan::SpreaderOrderMan (securities::SecurityDB &sec_db,
|
||||
: me(new PImpl(sec_db,native_order_factory))
|
||||
{
|
||||
for (auto const &os : existing_orders)
|
||||
if (os.has_spreader())
|
||||
me->spreader_orders[OrderKey(os.order_key()) ] =
|
||||
std::make_unique<SpreaderOrder>(sec_db,me->native_order_factory,os);
|
||||
if (os.has_spreader()) {
|
||||
auto spreader = std::make_unique<SpreaderOrder>(sec_db,me->native_order_factory,os);
|
||||
me->spreader_orders[OrderKey(os.order_key()) ] = std::move(spreader);
|
||||
}
|
||||
}
|
||||
|
||||
SpreaderOrderMan::~SpreaderOrderMan() {}
|
||||
@@ -50,8 +51,15 @@ SpreaderOrderMan::~SpreaderOrderMan() {}
|
||||
void SpreaderOrderMan::on(event_grid::event::Init const&)
|
||||
{
|
||||
LOG(OrderMan,Diag, "SpreaderOrderMan init");
|
||||
for (auto const &kvp : me->spreader_orders)
|
||||
kvp.second->init();
|
||||
for (auto const &kvp : me->spreader_orders) {
|
||||
auto &spreader = kvp.second;
|
||||
spreader->init_signals(spreader_order_state_change_signal,
|
||||
create_native_order_signal,
|
||||
update_native_order_signal,
|
||||
cancel_native_order_signal,
|
||||
md_subscription_signal);
|
||||
spreader->init();
|
||||
}
|
||||
}
|
||||
|
||||
void SpreaderOrderMan::on(event::CreateSpreaderOrder const&cso)
|
||||
@@ -65,6 +73,11 @@ void SpreaderOrderMan::on(event::CreateSpreaderOrder const&cso)
|
||||
order = std::make_unique<SpreaderOrder> (me->sec_db,
|
||||
me->native_order_factory,
|
||||
cso.order_key,cso.create);
|
||||
order->init_signals(spreader_order_state_change_signal,
|
||||
create_native_order_signal,
|
||||
update_native_order_signal,
|
||||
cancel_native_order_signal,
|
||||
md_subscription_signal);
|
||||
order->init();
|
||||
if (me->gateway_ready)
|
||||
order->on_connect();
|
||||
|
||||
@@ -31,8 +31,8 @@ api::orderman::OrderState to_pb(SpreaderOrderHeader const&header,SpreaderOrderSt
|
||||
if (current_state.reject_msg)
|
||||
result.set_reject_msg(*current_state.reject_msg);
|
||||
|
||||
spreader.set_target_price(current_state.target_price.value());
|
||||
spreader.set_target_quantity(current_state.target_quantity.value());
|
||||
spreader.set_price(current_state.price.value());
|
||||
spreader.set_quantity(current_state.quantity.value());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,30 @@ class MDSource;
|
||||
|
||||
class Book {
|
||||
public:
|
||||
Book () :
|
||||
_security_key(),
|
||||
_book()
|
||||
{}
|
||||
|
||||
Book (SecurityKey key,
|
||||
BookSide const&bid,
|
||||
BookSide const&ask) :
|
||||
_security_key(key),
|
||||
_book {bid,ask}
|
||||
{}
|
||||
|
||||
Book (Book const&rhs)
|
||||
: _security_key(rhs._security_key),
|
||||
_book(rhs._book)
|
||||
{}
|
||||
|
||||
Book &operator=(Book const &rhs)
|
||||
{
|
||||
_security_key = rhs._security_key;
|
||||
_book = rhs._book;
|
||||
return *this;
|
||||
}
|
||||
|
||||
SecurityKey security_key() const {return _security_key;} ;
|
||||
|
||||
BookSide const &bid () const {return _book[0];}
|
||||
|
||||
@@ -296,6 +296,7 @@ OrderServiceServer::OrderServiceServer (bts::api::ServiceContext& ctx)
|
||||
event_grid::forward<event::ExecutionReport> (me->fast.node,me->slow.node);
|
||||
event_grid::forward<event::GatewayStatusChange> (me->fast.node,me->slow.node);
|
||||
event_grid::forward<event::NativeOrderStateChange> (me->fast.node,me->slow.node);
|
||||
event_grid::forward<event::SpreaderOrderStateChange> (me->fast.node,me->slow.node);
|
||||
me->publish_orders = make_orders_publisher();
|
||||
me->publish_fills = make_fills_publisher();
|
||||
me->publish_gateway_status = make_gateway_publisher();
|
||||
@@ -533,14 +534,6 @@ void OrderServiceServer::update_order(const api::orderman::UpdateOrderRequest& u
|
||||
api::orderman::UpdateOrderResponse resp;
|
||||
resp.set_order_key(update.order_key());
|
||||
|
||||
if (!update.has_native()) {
|
||||
resp.set_error_code(api::orderman::OE_UNSUPPORTED_ORDER_TYPE);
|
||||
on_reply(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
auto const&native = update.native();
|
||||
|
||||
if (me->state == State::DISCONNECTED) {
|
||||
resp.set_error_code(api::orderman::OE_GATEWAY_NOT_READY);
|
||||
on_reply(resp);
|
||||
@@ -560,6 +553,7 @@ void OrderServiceServer::update_order(const api::orderman::UpdateOrderRequest& u
|
||||
}
|
||||
|
||||
if (update.has_native()) {
|
||||
auto const&native = update.native();
|
||||
auto it = me->native_order_objs.find(OrderKey(update.order_key()));
|
||||
if (it == me->native_order_objs.end()) {
|
||||
resp.set_error_code(api::orderman::OE_UNKNOWN_ORDER);
|
||||
|
||||
@@ -1,20 +1,36 @@
|
||||
#include "SpreaderOrder.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "server/sparkd/orderman/spreader_orders/SpreaderOrderState.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
struct SpreaderOrder::PImpl
|
||||
{
|
||||
PImpl()
|
||||
PImpl(api::orderman::OrderStatus status,
|
||||
Price price, Quantity quantity,
|
||||
std::string const &reject_msg)
|
||||
:status(status),
|
||||
price(price),
|
||||
quantity(quantity),
|
||||
reject_msg(reject_msg)
|
||||
{}
|
||||
|
||||
api::orderman::OrderStatus status;
|
||||
Price price;
|
||||
Quantity quantity;
|
||||
std::string reject_msg;
|
||||
};
|
||||
|
||||
SpreaderOrder::SpreaderOrder(securities::SecurityDB &db,
|
||||
NativeOrderFactory &f,
|
||||
api::orderman::OrderState const&os)
|
||||
: SpreaderOrderBase(db,f,os) , me (new PImpl())
|
||||
: SpreaderOrderBase(db,f,os) ,
|
||||
me (new PImpl(os.order_status(),
|
||||
Price(os.spreader().price()),
|
||||
Quantity(os.spreader().quantity()),
|
||||
os.reject_msg()))
|
||||
{
|
||||
LOG(OrderMan,Diag,"recovered SpreaderOrder from state. state: {}",
|
||||
utils::pb::to_string(os));
|
||||
@@ -24,7 +40,11 @@ SpreaderOrder::SpreaderOrder(securities::SecurityDB &db,
|
||||
NativeOrderFactory &f,
|
||||
OrderKey k,
|
||||
api::orderman::CreateOrderRequest const&cor)
|
||||
: SpreaderOrderBase(db,f,k,cor) , me (new PImpl())
|
||||
: SpreaderOrderBase(db,f,k,cor) ,
|
||||
me (new PImpl(api::orderman::OS_UNACKNOWLEDGED,
|
||||
Price(cor.spreader().price()),
|
||||
Quantity(cor.spreader().quantity()),
|
||||
std::string("")))
|
||||
{
|
||||
LOG(OrderMan,Diag,"creating new SpreaderOrder. request: {}",
|
||||
utils::pb::to_string(cor));
|
||||
@@ -32,9 +52,17 @@ SpreaderOrder::SpreaderOrder(securities::SecurityDB &db,
|
||||
|
||||
SpreaderOrder::~SpreaderOrder () {}
|
||||
|
||||
void SpreaderOrder::report_state()
|
||||
{
|
||||
report_state_change(
|
||||
SpreaderOrderState {
|
||||
me->status, me->price,me->quantity,me->reject_msg.empty()? nullptr:&me->reject_msg});
|
||||
}
|
||||
|
||||
void SpreaderOrder::init()
|
||||
{
|
||||
LOG(OrderMan,Diag,"Init");
|
||||
report_state();
|
||||
}
|
||||
|
||||
void SpreaderOrder::on_connect()
|
||||
|
||||
@@ -44,6 +44,7 @@ public:
|
||||
void on_external_error(std::string const&);
|
||||
|
||||
private:
|
||||
void report_state();
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
@@ -9,8 +9,8 @@ class SpreaderOrderState
|
||||
{
|
||||
public:
|
||||
api::orderman::OrderStatus order_status;
|
||||
Price target_price;
|
||||
Quantity target_quantity;
|
||||
Price price;
|
||||
Quantity quantity;
|
||||
std::string *reject_msg;
|
||||
};
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ boost::filesystem::path db_location (std::string const&store_root,std::string co
|
||||
{
|
||||
std::string base_path = store_root;
|
||||
boost::filesystem::path result = utils::normalize_data_path (base_path);
|
||||
result /= "PersistenceService";
|
||||
result /= "databases";
|
||||
result /= db + ".sqlite";
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -17,25 +17,22 @@ class LegPropertyData
|
||||
{
|
||||
public:
|
||||
const size_t leg_index;
|
||||
const bts::api::Security spread_security;
|
||||
const bts::api::ContractLeg leg_contract;
|
||||
const bts::spark::LegSpreaderParams leg_params;
|
||||
const api::BuySell trading_side;
|
||||
LegPropertyData(size_t leg_index_arg,
|
||||
const bts::api::Security& spread_security_arg,
|
||||
const bts::api::ContractLeg& leg_contract_arg,
|
||||
const bts::spark::LegSpreaderParams& leg_params_arg,
|
||||
api::BuySell trading_side_arg)
|
||||
:
|
||||
const bts::api::ContractLeg& leg_contract_arg,
|
||||
const bts::spark::LegSpreaderParams& leg_params_arg,
|
||||
api::BuySell trading_side_arg)
|
||||
:
|
||||
// copying the values via copy cons
|
||||
leg_index(leg_index_arg),
|
||||
spread_security(spread_security_arg),
|
||||
leg_contract(leg_contract_arg),
|
||||
leg_params(leg_params_arg),
|
||||
trading_side (trading_side_arg)
|
||||
trading_side(trading_side_arg)
|
||||
{
|
||||
}
|
||||
uint32_t security_key() const { return spread_security.id(); }
|
||||
}
|
||||
uint32_t security_key() const { return leg_contract.security().id(); }
|
||||
double price_multiplier() const { return leg_contract.price_multiplier(); }
|
||||
double size_multiplier() const { return leg_contract.quantity(); }
|
||||
double tick_size() const { return leg_contract.security().contract_spec().low_tick(); }
|
||||
|
||||
@@ -60,11 +60,11 @@ namespace spreader
|
||||
bts_assert(st_it != execution_strategy.leg_params_map().end());
|
||||
const bts::api::ContractLeg& leg = *it;
|
||||
api::BuySell leg_order_side = SpreaderUtils::get_leg_side(create.order.side(), leg);
|
||||
LegPropertyData leg_property_data(index++, sec, leg, st_it->second, leg_order_side);
|
||||
LegPropertyData leg_property_data(index++, leg, st_it->second, leg_order_side);
|
||||
SpreaderBookkeepingData::LegData leg_data(leg_property_data);
|
||||
leg_data_vector.push_back(leg_data);
|
||||
}
|
||||
_bookkeeping_data = std::make_unique<SpreaderBookkeepingData>(leg_data_vector);
|
||||
_bookkeeping_data = std::make_unique<SpreaderBookkeepingData>(create.order.security(), leg_data_vector);
|
||||
// now settings up the quoters
|
||||
for (size_t index = 0; index < _bookkeeping_data->count(); index++)
|
||||
{
|
||||
@@ -413,7 +413,7 @@ namespace spreader
|
||||
const OrderTemplate& SimpleSpreader::_get_order_template(size_t leg_index)
|
||||
{
|
||||
SpreaderBookkeepingData::LegData& leg_data = _bookkeeping_data->get_leg_data(leg_index);
|
||||
return make_order_template(leg_data.leg_shared_data.spread_security,
|
||||
return make_order_template(_bookkeeping_data->security,
|
||||
orderman::AccountName(), //TODO _create_order_request.native_order().account_name
|
||||
orderman::OperatorID(), //_create_order_request.native_order().operator_id(),
|
||||
leg_data.leg_shared_data.trading_side,
|
||||
|
||||
@@ -22,7 +22,8 @@ SingleLegQuoter::SingleLegQuoter(ISpreaderManager& order_bridge,
|
||||
_state(QuoterState::START),
|
||||
_price_quantity_state(QuoteLegPriceQuantityState(spread_order_price, Quantity(static_cast<double>(spread_order_quantity.value() * _this_leg_data.size_multiplier()))))
|
||||
{
|
||||
_aux_comp_data.reserve(order_bridge.n_legs());
|
||||
for (size_t q_idx = 0; q_idx < order_bridge.n_legs(); q_idx++)
|
||||
_aux_comp_data.push_back(PQPair());
|
||||
}
|
||||
|
||||
void SingleLegQuoter::init()
|
||||
@@ -372,14 +373,17 @@ void SingleLegQuoter::_compute_price_quantity(Price& price, Quantity& quantity)
|
||||
{
|
||||
if (idx != _this_leg_index)
|
||||
{
|
||||
const orderman::Book leg_book = _order_bridge.get_book(idx);
|
||||
Price leg_price; Quantity leg_quantity, quoter_leg_quantity;
|
||||
Quantity leg_target_quantity = _price_quantity_state.remaining_to_fill();
|
||||
SpreaderUtils::traverse_depth_for_quantity(leg_book, api::BuySell::BUY, Quantity(static_cast<uint32_t>(leg_target_quantity.value() * 1.0 /*TODO*/)), leg_price, leg_quantity);
|
||||
quoter_leg_quantity = Quantity(static_cast<uint32_t>(1.0 /*TODOit->lean_ratio*/ * leg_quantity.value()));
|
||||
const orderman::Book& other_leg_book = _order_bridge.get_book(idx);
|
||||
const LegPropertyData& other_leg_data = _order_bridge.get_leg_data(idx);
|
||||
Price other_leg_calc_price; Quantity other_leg_calc_quantity, this_leg_calc_quantity;
|
||||
Quantity this_leg_target_quantity = _price_quantity_state.remaining_to_fill();
|
||||
double other_leg_total_lean_ratio = other_leg_data.leg_params.quote_params().lean_quantity_ratio() * other_leg_data.size_multiplier() / _this_leg_data.size_multiplier();
|
||||
double other_leg_target_quantity = this_leg_target_quantity.value() * other_leg_total_lean_ratio;
|
||||
SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side), Quantity(other_leg_target_quantity), other_leg_calc_price, other_leg_calc_quantity);
|
||||
this_leg_calc_quantity = Quantity(static_cast<uint32_t>(1.0 /*TODOit->lean_ratio*/ * other_leg_calc_quantity.value()));
|
||||
PQPair& leg_price_quantity = _aux_comp_data.at(idx);
|
||||
leg_price_quantity.set(leg_price, leg_quantity, quoter_leg_quantity);
|
||||
if (_aux_comp_data.at(min_quoter_leg_quantity_index).quoter_leg_quantity.value() <= quoter_leg_quantity.value())
|
||||
leg_price_quantity.set(other_leg_calc_price, other_leg_calc_quantity, this_leg_calc_quantity);
|
||||
if (_aux_comp_data.at(min_quoter_leg_quantity_index).quoter_leg_quantity.value() <= this_leg_calc_quantity.value())
|
||||
min_quoter_leg_quantity_index = idx;
|
||||
}
|
||||
}
|
||||
@@ -389,14 +393,14 @@ void SingleLegQuoter::_compute_price_quantity(Price& price, Quantity& quantity)
|
||||
|
||||
Price SingleLegQuoter::_compute_leg_price(const std::vector<PQPair>& leg_values)
|
||||
{
|
||||
size_t idx = 0;
|
||||
double sum = 0;
|
||||
for (auto it = leg_values.begin(); it != leg_values.end(); it++)
|
||||
for (size_t idx = 0; idx < _order_bridge.n_legs(); idx++)
|
||||
{
|
||||
if (idx != _this_leg_index)
|
||||
{
|
||||
const PQPair& leg_pq_pair = leg_values[idx];
|
||||
const LegPropertyData& leg_data = _order_bridge.get_leg_data (idx);
|
||||
sum += it->leg_price.value() * leg_data.price_multiplier();
|
||||
sum += leg_pq_pair.leg_price.value() * leg_data.price_multiplier();
|
||||
}
|
||||
}
|
||||
double rv = _price_quantity_state.target_spread_price().value() - sum; /// TODO, needs a divisor
|
||||
|
||||
@@ -28,6 +28,7 @@ class SingleLegQuoter
|
||||
// cancel of this leg order (automatic or manual)
|
||||
CancelOrder,
|
||||
};
|
||||
public:
|
||||
// the states of the quoter state machine
|
||||
enum class QuoterState
|
||||
{
|
||||
@@ -55,7 +56,7 @@ class SingleLegQuoter
|
||||
// there are no working orders in this state.
|
||||
END
|
||||
};
|
||||
|
||||
private:
|
||||
// the external events and data are packaged into
|
||||
// this class and sent over to the state machine.
|
||||
// depending on the value of the external_event,
|
||||
@@ -104,7 +105,8 @@ class SingleLegQuoter
|
||||
Quantity leg_quantity;
|
||||
Quantity quoter_leg_quantity;
|
||||
PQPair()
|
||||
: leg_price(0.0),
|
||||
:
|
||||
leg_price(0.0),
|
||||
leg_quantity(0),
|
||||
quoter_leg_quantity(0)
|
||||
{}
|
||||
@@ -158,6 +160,7 @@ public:
|
||||
// called when the hedge order is cancelled, either
|
||||
// manually or automatically
|
||||
void cancel_order();
|
||||
QuoterState get_state() { return _state; }
|
||||
private:
|
||||
/********************External Event Handling in START***********************/
|
||||
void _handle_events_in_START(const EventData& event_data);
|
||||
@@ -189,8 +192,8 @@ private:
|
||||
// to the size of an other leg.
|
||||
double _get_equivelant_quantity(size_t leg_index, Quantity quantity);
|
||||
// using the market of the other legs, computes the
|
||||
// quoting price and quantity, and if the quantity > 0
|
||||
// quoting price and quantity
|
||||
void _compute_price_quantity(Price& price, Quantity& quantity);
|
||||
Price _compute_leg_price(const std::vector<PQPair>& leg_values);
|
||||
Price _compute_leg_price(const std::vector<PQPair>& leg_values);
|
||||
};
|
||||
}}
|
||||
@@ -92,9 +92,12 @@ private:
|
||||
std::vector<LegData> _leg_data;
|
||||
boost::container::flat_map<uint32_t, size_t> _sec_key_to_index;
|
||||
public:
|
||||
SpreaderBookkeepingData(const std::vector<LegData>& leg_data)
|
||||
const api::Security security;
|
||||
public:
|
||||
SpreaderBookkeepingData(const api::Security& security_arg, const std::vector<LegData>& leg_data)
|
||||
:
|
||||
_leg_data (leg_data)
|
||||
_leg_data (leg_data),
|
||||
security(security_arg)
|
||||
{
|
||||
for (auto it = leg_data.begin(); it != leg_data.end(); ++it)
|
||||
_sec_key_to_index.insert(std::pair<uint32_t, size_t>(it->leg_shared_data.security_key(), it->leg_shared_data.leg_index));
|
||||
|
||||
@@ -1,37 +1,307 @@
|
||||
#include "catch/catch.hpp"
|
||||
|
||||
#include "../risk_limits/RiskLimitManager.h"
|
||||
#include "../risk_limits/SharedRiskLimiter.h"
|
||||
#include "../risk_limits/RiskLimiter.h"
|
||||
#include "../risk_limits/RiskLimitState.h"
|
||||
#include "../risk_limits/IRiskLimitObserver.h"
|
||||
|
||||
#include "server/sparkd/orderman/utils/orderman_utils.h"
|
||||
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/Account.pb.h"
|
||||
#include "lib/config/protos/User.pb.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
#include "ISpreaderManager.h"
|
||||
#include "SpreaderBase.h"
|
||||
#include "LegPropertyData.h"
|
||||
#include "SpreaderBookkeepingData.h"
|
||||
#include "SpreaderUtils.h"
|
||||
#include "../src/lib/utils/bts_chrono.h"
|
||||
#include "SpreaderOrderUpdateMessage.h"
|
||||
|
||||
#include <future>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
using namespace bts::orderman;
|
||||
using namespace bts::spark;
|
||||
|
||||
namespace bts{
|
||||
namespace spreader{
|
||||
|
||||
TEST_CASE("Simple Test", "[spreader]")
|
||||
{
|
||||
SECTION("nothing_really")
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
class SpreaderManagerSim : public ISpreaderManager
|
||||
{
|
||||
std::vector<int> v;
|
||||
for (int i = 0; i < 100; i++)
|
||||
v.push_back(i);
|
||||
REQUIRE(v.size() == 100);
|
||||
private:
|
||||
Book b;
|
||||
api::FutureContract _leg_1_f;
|
||||
api::Security _leg_1_s;
|
||||
api::ContractLeg _leg_1_contract;
|
||||
api::FutureContract _leg_2_f;
|
||||
api::Security _leg_2_s;
|
||||
api::ContractLeg _leg_2_contract;
|
||||
api::ContractSpecification _spec;
|
||||
api::SyntheticContract _synth_contract;
|
||||
api::Security _spread_security;
|
||||
QuoteParams _leg_1_q_params;
|
||||
QuoteMuteZone _leg_1_qmz;
|
||||
PreHedgeParams _leg_1_ph_params;
|
||||
PostHedgeParams _leg_1_pph_params;
|
||||
LegSpreaderParams _leg_1_spr_params;
|
||||
LegPropertyData* _leg_1_property_data;
|
||||
QuoteParams _leg_2_q_params;
|
||||
QuoteMuteZone _leg_2_qmz;
|
||||
PreHedgeParams _leg_2_ph_params;
|
||||
PostHedgeParams _leg_2_pph_params;
|
||||
LegSpreaderParams _leg_2_spr_params;
|
||||
LegPropertyData* _leg_2_property_data;
|
||||
public:
|
||||
SpreaderBookkeepingData* bookkeeping_data;
|
||||
SpreaderManagerSim()
|
||||
{
|
||||
//security
|
||||
// common to leg1 and leg1
|
||||
_spec.set_low_tick(0.25);
|
||||
_spec.set_high_tick(0.25);
|
||||
// leg1
|
||||
_leg_1_f.set_expiration_day(15);
|
||||
_leg_1_f.set_expiration_month(12);
|
||||
_leg_1_f.set_expiration_year(2017);
|
||||
_leg_1_s.set_id(1000);
|
||||
_leg_1_s.set_symbol("ES");
|
||||
_leg_1_s.set_exchange(api::Venue::GLBX);
|
||||
_leg_1_s.set_allocated_contract_spec(&_spec);
|
||||
_leg_1_s.set_allocated_future(&_leg_1_f);
|
||||
|
||||
_leg_1_contract.set_allocated_security(&_leg_1_s);
|
||||
_leg_1_contract.set_quantity(1.0);
|
||||
_leg_1_contract.set_side(api::BuySell::BUY);
|
||||
_leg_1_contract.set_price_multiplier(1.0);
|
||||
|
||||
// leg2
|
||||
_leg_2_f.set_expiration_day(16);
|
||||
_leg_2_f.set_expiration_month(3);
|
||||
_leg_2_f.set_expiration_year(2018);
|
||||
|
||||
_leg_2_s.set_id(1001);
|
||||
_leg_2_s.set_symbol("ES");
|
||||
_leg_2_s.set_exchange(api::Venue::GLBX);
|
||||
_leg_2_s.set_allocated_contract_spec(&_spec);
|
||||
_leg_2_s.set_allocated_future(&_leg_2_f);
|
||||
|
||||
_leg_2_contract.set_allocated_security(&_leg_2_s);
|
||||
_leg_2_contract.set_quantity(1.0);
|
||||
_leg_2_contract.set_side(api::BuySell::SELL);
|
||||
_leg_2_contract.set_price_multiplier(1.0);
|
||||
|
||||
_synth_contract.set_name("spread_test_1");
|
||||
_synth_contract.set_price_calc(bts::api::SyntheticContract_PriceCalc::SyntheticContract_PriceCalc_diff);
|
||||
|
||||
_synth_contract.mutable_legs()->AddAllocated(&_leg_1_contract);
|
||||
_synth_contract.mutable_legs()->AddAllocated(&_leg_2_contract);
|
||||
|
||||
|
||||
_spread_security.set_allocated_synthetic(&_synth_contract);
|
||||
|
||||
// LegSpreaderParams
|
||||
_leg_1_qmz.set_outside_ticks(1);
|
||||
_leg_1_qmz.set_inside_ticks(1);
|
||||
_leg_1_q_params.set_allocated_quote_mute_zone(&_leg_1_qmz);
|
||||
_leg_1_q_params.set_lean_quantity_ratio(1.0);
|
||||
_leg_1_spr_params.set_allocated_quote_params(&_leg_1_q_params);
|
||||
_leg_1_property_data = new LegPropertyData(0, _leg_1_contract, _leg_1_spr_params, api::BuySell::BUY);
|
||||
|
||||
_leg_2_qmz.set_outside_ticks(1);
|
||||
_leg_2_qmz.set_inside_ticks(1);
|
||||
_leg_2_q_params.set_allocated_quote_mute_zone(&_leg_2_qmz);
|
||||
_leg_2_q_params.set_lean_quantity_ratio(1.0);
|
||||
_leg_2_spr_params.set_allocated_quote_params(&_leg_2_q_params);
|
||||
_leg_2_property_data = new LegPropertyData(1, _leg_2_contract, _leg_1_spr_params, api::BuySell::BUY);
|
||||
|
||||
SpreaderBookkeepingData::LegData leg_1_data(*_leg_1_property_data);
|
||||
SpreaderBookkeepingData::LegData leg_2_data(*_leg_2_property_data);
|
||||
|
||||
std::vector<SpreaderBookkeepingData::LegData> leg_data_v;
|
||||
leg_data_v.push_back(LegPropertyData(0, _leg_1_contract, _leg_1_spr_params, api::BuySell::BUY));
|
||||
leg_data_v.push_back(LegPropertyData(1, _leg_2_contract, _leg_1_spr_params, api::BuySell::SELL));
|
||||
|
||||
bookkeeping_data = new SpreaderBookkeepingData(_spread_security, leg_data_v);
|
||||
}
|
||||
bts::orderman::OrderKey send_limit_order(const OrderTemplate&, bts::Price, bts::Quantity) override
|
||||
{
|
||||
OrderKey key(1);
|
||||
return key;
|
||||
}
|
||||
// orders are modified through this. order_key uniquely distinguishes orders
|
||||
void update_order(bts::orderman::OrderKey, bts::Price, bts::Quantity) override
|
||||
{
|
||||
|
||||
}
|
||||
// call this to cancel orders
|
||||
void cancel_order(bts::orderman::OrderKey) override
|
||||
{
|
||||
}
|
||||
// for a leg index, returns the Book in O(1). the caller can hold at
|
||||
// reference to the returned type forever (the data in the returned
|
||||
// type can change beneath it)
|
||||
const orderman::Book& get_book(size_t index) const override
|
||||
{
|
||||
SpreaderBookkeepingData::LegData& leg_data = bookkeeping_data->get_leg_data(index);
|
||||
return leg_data.book;
|
||||
}
|
||||
// the caller can keep a reference to the returned type forever. the
|
||||
// data in the returned type is all const, so nothing will change
|
||||
// beneath it
|
||||
const LegPropertyData& get_leg_data(size_t index) const override
|
||||
{
|
||||
SpreaderBookkeepingData::LegData& leg_data = bookkeeping_data->get_leg_data(index);
|
||||
return leg_data.leg_shared_data;
|
||||
}
|
||||
// number of legs on the spread
|
||||
size_t n_legs() const override
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
|
||||
void _fill_book(Book& book, double inside_market, double tick_size, uint16_t* sizes, bts::api::BuySell side)
|
||||
{
|
||||
BookSide& bookside = book.side(side);
|
||||
for (size_t i = 0; i < book.n_levels(); i++)
|
||||
{
|
||||
bookside[i] = md2::PriceSize::from_double<2>(inside_market, sizes[i]);
|
||||
if (side == bts::api::BuySell::BUY)
|
||||
inside_market -= tick_size;
|
||||
else
|
||||
inside_market += tick_size;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Simple Test", "[spreader]")
|
||||
{
|
||||
SECTION("nothing_really")
|
||||
{
|
||||
std::vector<int> v;
|
||||
for (int i = 0; i < 100; i++)
|
||||
v.push_back(i);
|
||||
REQUIRE(v.size() == 100);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Depth Traversal Test", "[spreader]")
|
||||
{
|
||||
SECTION("Depth Traversal Test")
|
||||
{
|
||||
// fire md update
|
||||
Book book;
|
||||
// run 1
|
||||
uint16_t book_sizes_1[] = { 10, 30, 100, 5, 5 };
|
||||
_fill_book(book, 2500, 0.25, book_sizes_1, api::BuySell::BUY);
|
||||
Quantity out_quantity; Price out_price;
|
||||
SpreaderUtils::traverse_depth_for_quantity(book, api::BuySell::BUY, Quantity(100), out_price, out_quantity);
|
||||
REQUIRE(out_quantity.value()==100);
|
||||
REQUIRE(out_price.value() == 2499.5);
|
||||
// run 2
|
||||
uint16_t book_sizes_2[] = { 11, 13, 17, 19, 23 };
|
||||
_fill_book(book, 2500, 0.25, book_sizes_2, api::BuySell::BUY);
|
||||
SpreaderUtils::traverse_depth_for_quantity(book, api::BuySell::BUY, Quantity(100), out_price, out_quantity);
|
||||
REQUIRE(out_quantity.value() == 83);
|
||||
REQUIRE(out_price.value() == 2499.0);
|
||||
// run 3
|
||||
uint16_t book_sizes_3[] = { 100, 13, 17, 19, 23 };
|
||||
_fill_book(book, 2500, 0.25, book_sizes_3, api::BuySell::BUY);
|
||||
SpreaderUtils::traverse_depth_for_quantity(book, api::BuySell::BUY, Quantity(100), out_price, out_quantity);
|
||||
REQUIRE(out_quantity.value() == 100);
|
||||
REQUIRE(out_price.value() == 2500.0);
|
||||
// run 4
|
||||
uint16_t book_sizes_4[] = { 1, 0, 17, 19, 23 };
|
||||
_fill_book(book, 2500, 0.25, book_sizes_4, api::BuySell::BUY);
|
||||
SpreaderUtils::traverse_depth_for_quantity(book, api::BuySell::BUY, Quantity(100), out_price, out_quantity);
|
||||
REQUIRE(out_quantity.value() == 1);
|
||||
REQUIRE(out_price.value() == 2500.0);
|
||||
// run 5
|
||||
uint16_t book_sizes_5[] = { 0, 1, 17, 19, 23 };
|
||||
_fill_book(book, 2500, 0.25, book_sizes_5, api::BuySell::BUY);
|
||||
SpreaderUtils::traverse_depth_for_quantity(book, api::BuySell::BUY, Quantity(100), out_price, out_quantity);
|
||||
REQUIRE(out_quantity.value() == 0);
|
||||
REQUIRE(out_price.value() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Depth Traversal Perf Test", "[spreader]")
|
||||
{
|
||||
SECTION("Depth Traversal Performance Test")
|
||||
{
|
||||
for (int t = 0; t < 10; t++)
|
||||
{
|
||||
// fire md update
|
||||
Book book;
|
||||
// run 1
|
||||
uint16_t book_sizes_1[] = { 11, 13, 17, 19, 23 };
|
||||
_fill_book(book, 2500, 0.25, book_sizes_1, api::BuySell::BUY);
|
||||
size_t n_runs = 1000000;
|
||||
Quantity out_quantity; Price out_price;
|
||||
auto t0 = Timer::now();
|
||||
for (size_t i = 0; i < n_runs; i++)
|
||||
{
|
||||
SpreaderUtils::traverse_depth_for_quantity(book, api::BuySell::BUY, Quantity(100), out_price, out_quantity);
|
||||
}
|
||||
auto t1 = Timer::now();
|
||||
int64_t diff = us_diff(t1, t0);
|
||||
REQUIRE(diff < 1000000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TEST_CASE("Simple Single Quoter Tester", "[spreader]")
|
||||
//{
|
||||
// SECTION("Start state")
|
||||
// {
|
||||
// // create sim spreader
|
||||
// bts::spreader::SpreaderManagerSim sim;
|
||||
// // create quoter
|
||||
// OrderTemplate ord_templ;
|
||||
// SingleLegQuoter quoter(sim, ord_templ, 0, Price(0.0), Quantity(100));
|
||||
// REQUIRE(quoter.get_state() == SingleLegQuoter::QuoterState::START);
|
||||
|
||||
// quoter.init();
|
||||
// REQUIRE(quoter.get_state() == SingleLegQuoter::QuoterState::WAITING_FOR_MARKET);
|
||||
// // fire md update
|
||||
// Book book;
|
||||
// uint16_t book_sizes[] = { 10, 30, 100, 5, 5 };
|
||||
// _fill_book(book, 2500, 0.25, book_sizes, api::BuySell::BUY);
|
||||
// sim.bookkeeping_data->get_leg_data(1).set_book(book);
|
||||
// quoter.update_md(1);
|
||||
// }
|
||||
//}
|
||||
|
||||
TEST_CASE("Simple Single Quoter Reprice Perf", "[spreader]")
|
||||
{
|
||||
SECTION("Reprice Perf")
|
||||
{
|
||||
// create sim spreader
|
||||
bts::spreader::SpreaderManagerSim sim;
|
||||
// create quoter
|
||||
OrderTemplate ord_templ;
|
||||
SingleLegQuoter quoter(sim, ord_templ, 0, Price(0.0), Quantity(100));
|
||||
REQUIRE(quoter.get_state() == SingleLegQuoter::QuoterState::START);
|
||||
|
||||
quoter.init();
|
||||
REQUIRE(quoter.get_state() == SingleLegQuoter::QuoterState::WAITING_FOR_MARKET);
|
||||
// fire md update
|
||||
Book book;
|
||||
uint16_t book_sizes[] = { 10, 30, 100, 5, 5 };
|
||||
_fill_book(book, 2500, 0.25, book_sizes, api::BuySell::BUY);
|
||||
sim.bookkeeping_data->get_leg_data(1).set_book(book);
|
||||
quoter.update_md(1);
|
||||
quoter.update_md(1);
|
||||
quoter.update_order_status(SpreaderOrderUpdateMessage(OrderKey(1), api::orderman::OrderStatus::OS_NEW, Quantity(0), Price(0), Quantity(0), Price(0), NULL));
|
||||
|
||||
size_t n_runs = 10, n_updates = 1000000;
|
||||
for (size_t t = 0; t < n_runs; t++)
|
||||
{
|
||||
auto t0 = Timer::now();
|
||||
for (size_t u = 0; u < n_updates; u++)
|
||||
quoter.update_md(1);
|
||||
auto t1 = Timer::now();
|
||||
int64_t diff = us_diff(t1, t0);
|
||||
REQUIRE(diff < 10000000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
|
||||
}
|
||||
@@ -49,22 +49,7 @@ public:
|
||||
bts_assert_d(out_index < key_to_index_map.size());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
static void fill_leg_shared_data(const bts::api::Security& sec, const bts::spark::ExecutionStrategy& execution_strategy, api::BuySell order_side,
|
||||
std::vector<LegPropertyData>& out_v)
|
||||
{
|
||||
bts_assert(sec.has_synthetic());
|
||||
size_t index = 0;
|
||||
for (auto it = sec.synthetic().legs().begin(); it != sec.synthetic().legs().end(); ++it)
|
||||
{
|
||||
uint32_t sec_key = it->security().id();
|
||||
auto st_it = execution_strategy.leg_params_map().find(sec_key);
|
||||
bts_assert(st_it != execution_strategy.leg_params_map().end());
|
||||
const bts::api::ContractLeg& leg = *it;
|
||||
api::BuySell leg_order_side = get_leg_side(order_side, leg);
|
||||
out_v.push_back(LegPropertyData(index, sec, leg, st_it->second, leg_order_side));
|
||||
}
|
||||
}
|
||||
}
|
||||
const static bts::spark::LegSpreaderParams& find_spread_params(const bts::spark::ExecutionStrategy& exec_strategy,
|
||||
const bts::api::Security& leg_security)
|
||||
{
|
||||
@@ -109,26 +94,42 @@ public:
|
||||
void static traverse_depth_for_quantity(const orderman::Book& leg_book, const api::BuySell book_side, Quantity target_quantity,
|
||||
Price& out_price, Quantity& out_quantity)
|
||||
{
|
||||
double out_price_d = 0.0;
|
||||
uint32_t out_quantity_u = 0;
|
||||
uint32_t cum_quantity_u = 0;
|
||||
const size_t n_levels = leg_book.n_levels();
|
||||
// we could have kept a running price, updated at each level,
|
||||
// but to avoid the price() call on PriceSize, we keep a running
|
||||
// index. (could have piggy backed on the for index, but i think
|
||||
// this is clearer)
|
||||
size_t last_nonzero_level = 0;
|
||||
const orderman::BookSide& book_depth = leg_book.side(book_side);
|
||||
for (size_t level = 0; level < n_levels; level++)
|
||||
{
|
||||
md2::PriceSize level_price_qty = book_depth[level];
|
||||
md2::Size level_qty = level_price_qty.size();
|
||||
out_price_d = level_price_qty.price();
|
||||
out_quantity_u += level_qty;
|
||||
if (out_quantity_u >= target_quantity.value())
|
||||
{
|
||||
out_quantity_u = target_quantity.value();
|
||||
break;
|
||||
}
|
||||
if (level_qty == 0)
|
||||
break;
|
||||
else
|
||||
{
|
||||
last_nonzero_level = level;
|
||||
cum_quantity_u += level_qty;
|
||||
if (cum_quantity_u >= target_quantity.value())
|
||||
{
|
||||
// early break, we have already accumulted the target
|
||||
cum_quantity_u = target_quantity.value();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cum_quantity_u == 0)
|
||||
{
|
||||
out_quantity = Quantity(0);
|
||||
out_price = Price(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
out_quantity = Quantity(cum_quantity_u);
|
||||
out_price = Price(book_depth[last_nonzero_level].price());
|
||||
}
|
||||
out_price = Price(out_price_d);
|
||||
out_quantity = Quantity(out_quantity_u);
|
||||
}
|
||||
// given the new price, applies the quoting parameters (mute etc)
|
||||
// to see if quote needs to be repriced
|
||||
|
||||
Reference in new issue
Block a user