added logs and new test

This commit is contained in:
Taha Afzal committed 2018-12-07 16:07:13 -05:00
1 parent 93abf315a3
commit b7a620d30e
5 files changed
+120 -2

No files matched your search

@@ -149,8 +149,10 @@ void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
ord_ev.last_msg, static_cast<int>(order_error));
leg_data.order_update_message().on_order_update(ord_ev);
if (order_error == ChildOrderError::NONE){
if (leg_data.order_update_message().last_update_filled_quantity().value() > 0)
if (leg_data.order_update_message().last_update_filled_quantity().value() > 0){
LOG(OrderMan,Info,"h_p child update {0}",_since_order_update(trace));
_state_worker->on_quoter_order_fill (leg_data,trace);
}
else
_state_worker->on_quoter_order_status_update (leg_data);
}
@@ -184,8 +186,10 @@ void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
key(), sec_key, order_key, status, filled_qty,
last_price, ord_ev.last_msg);
}
LOG(OrderMan,Info,"h_p fill done {0}",_since_order_update(trace));
_send_check_termination(true);
_report_state();
LOG(OrderMan, Info,"h_p end {0}",_since_order_update(trace));
LOG(Latency,Diag, "_on_child_order_update k:{} t:{}us", key().value(),
(utils::TimeStamp::now() - trace.spreader_on_child_order_update).microseconds());
}
@@ -240,6 +240,14 @@ private:
// (if settlements are not available returns nan)
// Otherwise effectiv_price==order_price.
Price _compute_effective_price (Price order_price);
// retuns the nano seconds from last
// order callback
double _since_order_update(bts::PerfTrace& trace){
double rv = (utils::TimeStamp::now()-
trace.spreader_on_child_order_update).nanoseconds();
trace.spreader_on_child_order_update = utils::TimeStamp::now();
return rv;
}
// testing only //////////////////
// this provides direct way to inject
// data into the spreader, as well as
@@ -79,6 +79,7 @@ void SpreaderWorkingStateWorker::on_quoter_order_error(SpreaderLegData& leg_data
}
void SpreaderWorkingStateWorker::on_quoter_order_fill(SpreaderLegData& leg_data,
PerfTrace &trace){
LOG(OrderMan, Info,"h_p w start {0}",_spreader->_since_order_update(trace));
// this is the most complex part of the spreader. the following needs to be done
// 1. tell other quoters about it (so they reduce their sizes)
// 2. launch a new hedger
@@ -112,17 +113,23 @@ void SpreaderWorkingStateWorker::on_quoter_order_fill(SpreaderLegData& leg_data,
UnhedgedDelta& leg_unhedged_delta = other_leg_data.unhedged_delta();
leg_unhedged_delta.add_unhedged(hedge_price.value(), hedge_qty);
uint32_t total_unhedged_qty=leg_unhedged_delta.subtract_hedge_if_available();
LOG(OrderMan, Info,"h_p comp {0} {1}", q_idx, _spreader->_since_order_update(trace));
if (total_unhedged_qty != 0){
// if it is an actively quoting leg, inform the quoter about it
// so it can decrease the proportional size
if (other_leg_data.leg_property_data().is_active_quoting())
if (other_leg_data.leg_property_data().is_active_quoting()){
other_leg_data.quoter().update_other_leg_fills(leg_data.leg_property_data().leg_index(),
Quantity(total_unhedged_qty));
LOG(OrderMan, Info,"h_p q minus {0} {1}",
q_idx, _spreader->_since_order_update(trace));
}
// a hedge is sent regardless
_spreader->_send_hedge(other_leg_data.leg_property_data().leg_index(),
hedge_price,
Quantity(total_unhedged_qty),
trace);
LOG(OrderMan, Info,"h_p send hedge {0} {1}",
q_idx, _spreader->_since_order_update(trace));
}
}
}
@@ -133,6 +140,7 @@ void SpreaderWorkingStateWorker::on_quoter_order_fill(SpreaderLegData& leg_data,
// earlier too)
// todo:this needs to be addressed, its confusing
leg_data.quoter().update_order_status (order_update_message);
LOG(OrderMan, Info,"h_p update q {0}", _spreader->_since_order_update(trace));
}
void SpreaderWorkingStateWorker::on_quoter_order_status_update(SpreaderLegData& leg_data){
bts_assert (leg_data.order_update_message().last_update_filled_quantity().value() == 0);
@@ -2796,12 +2796,26 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]")
new double[5] {20.035, 20.040, 20.045, 20.050, 20.055},
new uint16_t[5] {250,275,100,5,15},
true),
book_2_alt = get_book(SecurityKey (leg_2.security().id()),
5,
new double[5] {20.050,20.035,20.030,20.035,20.030},
new uint16_t[5] {10, 10, 20, 250, 75},
new double[5] {20.055, 20.060, 20.065, 20.070, 20.085},
new uint16_t[5] {25,275,100,5,15},
true),
book_3 = get_book(SecurityKey (leg_3.security().id()),
5,
new double[5] {19.985,19.980,19.975,19.970,19.965},
new uint16_t[5] {75,250,125,100,300},
new double[5] {19.990,19.995,20.000,20.005,20.010},
new uint16_t[5] {100,150,250,25,125},
true),
book_3_alt = get_book(SecurityKey (leg_3.security().id()),
5,
new double[5] {19.885,19.875,19.870,19.765,19.760},
new uint16_t[5] {75,25,125,100,300},
new double[5] {19.890,19.895,19.900,19.910,19.915},
new uint16_t[5] {100,50,50,125,125},
true);
spreader.test_only_on_book_update (BookUpdateEvent(book_1,
PerfTrace()));
@@ -2871,6 +2885,60 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]")
LOG(OrderMan, Info, "n leg hedges:{} unit time(u_secs):{}",n_hedges,hedge_time);
std::cout << "4 leg hedges:" << n_hedges << " unit time(usecs):" << hedge_time << std::endl;
}
SECTION ("N_requotes_1_hedge_M_times"){
// N book updates followed by M fills
// (does the hedge time stays the same as
// in a tight loop in the "hedges" test ?)
uint32_t n_runs=5,
n_book_updates=100,
n_hedges = 5;
// since its the same quoting order we are filling
// and the code downstream expects accumulated filled qty
uint32_t total_filled_qty = 0, hi = 0;
for (uint32_t i=0; i<n_runs; i++){
test_sleep(5000,false);
std::vector<double> hedge_perfs;
hedge_perfs.reserve(n_hedges);
auto t0=utils::TimeStamp::now();
for (uint32_t b=0; b<n_book_updates; b++){
spreader.test_only_on_book_update (BookUpdateEvent(book_1_alt,
PerfTrace()));
spreader.test_only_on_book_update (BookUpdateEvent(book_2_alt,
PerfTrace()));
spreader.test_only_on_book_update (BookUpdateEvent(book_3_alt,
PerfTrace()));
spreader.test_only_on_book_update (BookUpdateEvent(book_1,
PerfTrace()));
spreader.test_only_on_book_update (BookUpdateEvent(book_2,
PerfTrace()));
spreader.test_only_on_book_update (BookUpdateEvent(book_3,
PerfTrace()));
spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (total_filled_qty),
Price (20.045));
}
auto t1=utils::TimeStamp::now();
//test_sleep(5000,true);
double avg_bookupdate_time=(t1-t0).nanoseconds()/(1000.0 * 6*n_book_updates);
for(uint32_t h=0; h<n_hedges; h++){
auto t0=utils::TimeStamp::now();
spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (++total_filled_qty),
Price (20.045));
auto t1=utils::TimeStamp::now();
hedge_perfs.push_back((t1-t0).nanoseconds());
}
for (auto d : hedge_perfs)
std::cout << ++hi << " hedge: " << d << std::endl;
double min, max, mean, median;
stats(hedge_perfs,min,max,mean,median);
std::cout << "1Q 3H legs, requoting mean=" << avg_bookupdate_time << std::endl;
std::cout << "1Q 3H legs, hedging median=" << median << " mean=" << mean << std::endl;
std::cout << "------------------------------------------------------" << std::endl;
}
}
}
TEST_CASE ("simple book addition", "[spreader]"){
SECTION ("direct, book"){
@@ -602,4 +602,34 @@ void generate_random_book(size_t n_levels,
ask_prices,direct_ask_sizes,implied_ask_sizes,
bts::api::Side::SIDE_SELL,false);
}
void stats (std::vector<double>& p,
double& min,
double& max,
double& mean,
double& median){
if(p.size()>0){
std::sort(p.begin(),p.end());
min=p[0];
max=p[p.size()-1];
median=p[p.size()/2];
mean=0.0;
for(double d : p)
mean += d;
mean /= p.size();
}
}
void spin_ms (int64_t ms)
{
if (ms > 0) {
auto now = Timer::now();
while (ns_diff(Timer::now(),now) < ms * 1000 * 1000)
;
}
}
void test_sleep (int64_t ms, bool is_spin){
if (is_spin)
spin_ms(ms);
else
sleep_for(std::chrono::milliseconds(ms));
}
}}