tests pass

This commit is contained in:
Taha Afzal committed 2019-05-23 12:37:30 -04:00
1 parent 1fd66a17ca
commit 592ad39a32
4 files changed
+360 -344

No files matched your search

+1
View File
@@ -13,4 +13,5 @@ message Spreader{
optional double fill_period_high = 30 [default=50];
}
optional CacheWarming cache_warming = 20;
optional bool n_legs_recomp_hedges_on_fill = 30 [default=false];
}
@@ -21,6 +21,9 @@ _cancelled (false),
_report_dirty(false),
_active(true),
_cancel_hedges_on_cancel(false),
_n_legs_recomp_hedges_on_fill(
config::get<config::Spreader>().n_legs_recomp_hedges_on_fill()),
_n_legs_last_recomp_hedges_on_fill_succ(false),
_cw_helper(),
_cancel_timeout (5000),// TODO:get it from config
_check_termination_period (250), // should also be a setting/config like above
@@ -331,7 +334,7 @@ bool SpreaderOrder::_all_hedger_orders_in_terminal_state(){
}
bool SpreaderOrder::_any_quoter_in_irrecoverable_error() const{
for (size_t q_idx = 0; q_idx < _bookkeeping_data().n_legs(); q_idx++){
const SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(q_idx);
const SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(q_idx);
if (leg_data.leg_property_data().is_active_quoting())
if (leg_data.quoter().irrecoverable_error())
return true;
@@ -370,21 +373,23 @@ void SpreaderOrder::_send_hedge(size_t hedge_leg_index,
void SpreaderOrder::_compute_nlegs_crossed_hedges(size_t filled_leg_index,
Price filled_price,
double filled_quantity){
SpreaderBookkeepingData& bk_data = _bookkeeping_data();
const LegPropertyData& q_leg_data =
bk_data.get_leg_data(filled_leg_index).leg_property_data();
const ISpreaderManager& spreader = *this;
bool is_succ = SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(),
bk_data.effective_price(),
bk_data.n_legs(),
q_leg_data,
filled_price,
filled_quantity,
spreader,
bk_data.nlegs_crossed_hedges());
if(!is_succ)
for(auto& p : bk_data.nlegs_crossed_hedges())
p.second = os::nan();
_n_legs_last_recomp_hedges_on_fill_succ = false;
if(_n_legs_recomp_hedges_on_fill &&
_bookkeeping_data().n_legs() > 2){
SpreaderBookkeepingData& bk_data = _bookkeeping_data();
const LegPropertyData& q_leg_data =
bk_data.get_leg_data(filled_leg_index).leg_property_data();
const ISpreaderManager& spreader = *this;
_n_legs_last_recomp_hedges_on_fill_succ =
SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(),
bk_data.effective_price(),
bk_data.n_legs(),
q_leg_data,
filled_price,
filled_quantity,
spreader,
bk_data.nlegs_crossed_hedges());
}
}
void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index,
Price fill_price,
@@ -404,11 +409,12 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index,
hedged_leg_data.leg_property_data(),
computed_hedge_price,
hedge_quantity);
else{
auto& crossed_hedge =
else{
if(_n_legs_recomp_hedges_on_fill &&
_n_legs_last_recomp_hedges_on_fill_succ){
auto& crossed_hedge =
_bookkeeping_data().nlegs_crossed_hedges()[hedge_leg_index];
if(!os::is_nan(crossed_hedge.second)){
hedge_price = crossed_hedge.first;
computed_hedge_price = crossed_hedge.first;
hedge_quantity = crossed_hedge.second;
}
else{
@@ -62,8 +62,17 @@ private:
const bool _active;
// this is the main data
bool _cancel_hedges_on_cancel;
// if this is true, then in the case of nlegs
// (>2) re-comp the hedge prices on a fill
const bool _n_legs_recomp_hedges_on_fill;
// this tells that in the n leg case the last
// call to compute cross hedges on a quoting fill
// was successful or not (TODO:need to put this,
// _n_legs_recomp_hedges_on_fill and SBKD::_nlegs_crossed_hedges
// in its own class)
bool _n_legs_last_recomp_hedges_on_fill_succ;
// helps with forced calls, and all the artificiality of these
// calls are trapped here
// calls are trapped here
CacheWarmingState _cw_helper;
// timeout before cancel waiting goes to end
const int64_t _cancel_timeout;
@@ -3032,330 +3032,330 @@ TEST_CASE ("spreader_perf_2legs", "[spreader]")
std::cout << "2 leg hedges:" << n_hedges << " unit time(usecs):" << hedge_time << std::endl;
}
}
// TEST_CASE ("spreader_perf_4legs", "[spreader]")
// {
// bts::orderman::OrderKey order_key(0);
// Price spreader_price = Price (40);
// Quantity spreader_quantity = Quantity (250);
// ContractLeg leg_1, leg_2, leg_3, qleg_4;
// fill_future_contract_leg (leg_1, 2468, "SI", bts::api::Venue::VENUE_GLBX,
// 2019, 1, 15,
// 1.0, 1.0,
// api::Side::SIDE_BUY,
// 1.0, 0.005);
// fill_future_contract_leg (leg_2, 2469, "SI", bts::api::Venue::VENUE_GLBX,
// 2019, 2, 15,
// 1.0, 1.0,
// api::Side::SIDE_BUY,
// 1.0, 0.005);
// fill_future_contract_leg (leg_3, 2470, "SI", bts::api::Venue::VENUE_GLBX,
// 2019, 3, 15,
// 1.0, 1.0,
// api::Side::SIDE_BUY,
// 1.0, 0.005);
// fill_future_contract_leg (qleg_4, 2471, "SI", bts::api::Venue::VENUE_GLBX,
// 2019, 4, 15,
// -1.0, -1.0,
// api::Side::SIDE_SELL,
// 1.0, 0.005);
// std::vector<ContractLeg> contract_legs;
// contract_legs.push_back(leg_1);
// contract_legs.push_back(leg_2);
// contract_legs.push_back(leg_3);
// contract_legs.push_back(qleg_4);
// Security synthetic_security;
// fill_synthetic_security (synthetic_security, 2000, "synthetic_security", contract_legs);
// // execution params/order def
// LegSpreaderParams lsp_1, lsp_2, lsp_3, qlsp_4;
// fill_leg_spreader_params(lsp_1, 1.0, false, 1.0);
// fill_leg_spreader_params(lsp_2, 1.0, false, 1.0);
// fill_leg_spreader_params(lsp_3, 1.0, false, 1.0);
// fill_leg_spreader_params(qlsp_4, -1.0, true, 1.0);
// std::vector<LegSpreaderParams> spreader_params;
// spreader_params.push_back(lsp_1);
// spreader_params.push_back(lsp_2);
// spreader_params.push_back(lsp_3);
// spreader_params.push_back(qlsp_4);
// bts::api::orderman::OrderDefinition order_def;
// fill_spreader_order_definition(order_def, Side::SIDE_BUY,
// synthetic_security,
// spreader_params);
// // putting it together in SpreaderBookkeepingData
// SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices
// testonly_spreader_pao_services = _create_spreader_pao_services(synthetic_security);
// SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services);
// std::vector<LegPropertyData> leg_props;
// std::vector<bts::api::orderman::OrderDefinition> quoter_ord_defs,
// hedger_ord_defs;
// SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,quoter_ord_defs,hedger_ord_defs);
// SpreaderManagerForTest spreader_manager (order_key, order_def, pao_services, leg_props,quoter_ord_defs,hedger_ord_defs);
// SpreaderOrder spreader;
// REQUIRE(spreader.test_only_func_spreader_state() == SpreaderState::START);
// // on_init_new and activate
// spreader.test_only_on_init_new (order_def, pao_services);
// SpreaderOrderServices order_services (nullptr);
// spreader.test_only_on_activate(spreader_price, spreader_quantity, order_services);
// SpreaderBookkeepingData& bookkeeping_data = spreader.test_only_func_spreader_full_data();
// verify_basic_spreader_state (spreader,
// SpreaderState::WORKING,
// spreader_price,
// spreader_quantity);
// SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3);
// verify_complete_quoter_state (qleg_data_4.quoter(),
// QuoterState::WAITING_FOR_SIZE,
// false,
// spreader_price,
// spreader_quantity,
// Quantity (0),
// Quantity(0),
// Price(0),
// Quantity(0),
// spreader_quantity,
// false);
// Book book_1 = get_book(SecurityKey (leg_1.security().id()),
// 5,
// new double[5] {20, 19.995, 19.990, 19.985, 19.980},
// new uint16_t[5] {100, 50, 75, 150, 25},
// new double[5] {20.005, 20.010, 20.015, 20.020, 20.025},
// new uint16_t[5] {25,125,175,225,100},
// true),
// book_1_alt = get_book(SecurityKey (leg_1.security().id()),
// 5,
// new double[5] {20.005, 19.990, 19.890, 19.885, 19.780},
// new uint16_t[5] {10, 25, 75, 15, 25},
// new double[5] {20.010, 20.015, 20.025, 20.030, 20.035},
// new uint16_t[5] {10, 25, 75, 15, 25},
// true),
// book_2 = get_book(SecurityKey (leg_2.security().id()),
// 5,
// new double[5] {20.030,20.025,20.020,20.015,20.010},
// new uint16_t[5] {100, 100, 200, 250, 75},
// 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,
// trace::CorID()));
// spreader.test_only_on_book_update (BookUpdateEvent(book_2,
// trace::CorID()));
// spreader.test_only_on_book_update (BookUpdateEvent(book_3,
// trace::CorID()));
// verify_complete_quoter_state (qleg_data_4.quoter(),
// QuoterState::WAITING_FOR_FIRST_ACK,
// false,
// spreader_price,
// spreader_quantity,
// Quantity (0),
// Quantity(0),
// Price(20.045),
// spreader_quantity,
// spreader_quantity,
// false);
// spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_WORKING,
// Quantity (0),
// Price (0));
// verify_complete_quoter_state (qleg_data_4.quoter(),
// QuoterState::WORKING,
// false,
// spreader_price,
// spreader_quantity,
// Quantity (0),
// Quantity(0),
// Price(20.045),
// spreader_quantity,
// spreader_quantity,
// false);
// SECTION ("Testing Hedge Reuse"){
// Quantity t_qty;
// Price t_price;
// // to keep the compiler warning happy
// LOG(OrderMan, Info, "{} {} {}", book_1_alt.n_levels(),
// book_2_alt.n_levels(),
// book_3_alt.n_levels());
// // this sends fills, makes sure the hedgers are
// // working, and on fill and recycled
// // the hedge prices used are
// // 20.015, 20.035 and 19.995
// SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3);
// // lets concentrate on the hedges sent on leg 1
// SpreaderLegData& hleg_1 = bookkeeping_data.get_leg_data (0);
// auto& hm_1 = hleg_1.hedger_manager();
// REQUIRE(hm_1.n_active_hedgers()==0);
// // send a 1 lot quoter fill /////////
// /////////////////////////////////////
// spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_WORKING,
// Quantity (1),
// Price (20.045));
// REQUIRE(hm_1.n_active_hedgers()==1);
// // legs 2 and 3
// REQUIRE(securities::test_price_equality(
// bookkeeping_data.get_leg_data (1).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(),
// 20.035,
// 0.005));
// REQUIRE(securities::test_price_equality(
// bookkeeping_data.get_leg_data (2).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(),
// 19.995,
// 0.005));
// // back to leg1's hedger, this is what will
// // be mainly tested here on
// {
// auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0);
// REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1);
// REQUIRE(securities::test_price_equality(
// hedger_1_1.order_state().out_price().value(),
// 20.015,
// 0.005));
// REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK);
// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_WORKING,
// Quantity (0),
// Price (0));
// REQUIRE(hedger_1_1.state()==HedgerState::WORKING);
// // fill h1 and it goes back to start ////
// /////////////////////////////////////////
// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_COMPLETED,
// Quantity (1),
// Price (20.015));
// REQUIRE(hedger_1_1.state()==HedgerState::START);
// }
// REQUIRE(hm_1.n_active_hedgers()==0);
// hm_1.total_fills(t_qty, t_price);
// REQUIRE(t_qty.value() == 1);
// REQUIRE(securities::test_price_equality(t_price.value(),20.015,0.005));
// ////////////////////////////
// // send another 1 lot q fill
// spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_WORKING,
// Quantity (2),
// Price (20.045));
// REQUIRE(hm_1.n_active_hedgers()==1);
// // since 2 and 3 were never filled, they should have 2 hedgers
// REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==2);
// REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==2);
// {
// auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0);
// REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1);
// REQUIRE(securities::test_price_equality(
// hedger_1_1.order_state().out_price().value(),
// 20.015,
// 0.005));
// REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK);
// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_WORKING,
// Quantity (0),
// Price (0));
// REQUIRE(hedger_1_1.state()==HedgerState::WORKING);
// // fill h1 ///////
// //////////////////
// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_COMPLETED,
// Quantity (1),
// Price (20.025));
// REQUIRE(hedger_1_1.state()==HedgerState::START);
// }
// REQUIRE(hm_1.n_active_hedgers()==0);
// hm_1.total_fills(t_qty, t_price);
// REQUIRE(t_qty.value() == 2);
// REQUIRE(securities::test_price_equality(t_price.value(),20.020,0.005));
// // send 4 more q fills /////////
// ////////////////////////////////
// for(size_t f=4; f <= 10; f+= 2)
// spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_WORKING,
// Quantity (f),
// Price (20.045));
// REQUIRE(hm_1.n_active_hedgers()==4);
// REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==6);
// REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==6);
// REQUIRE(bookkeeping_data.get_leg_data (3).hedger_manager().n_active_hedgers()==0);
// for (size_t i=0;i<4;i++){
// auto& h = hm_1.test_only_get_hedger_at(i);
// REQUIRE(h.state()==HedgerState::WAITING_FOR_FIRST_ACK);
// REQUIRE(h.order_state().out_quantity().value()==2);
// REQUIRE(securities::test_price_equality(
// h.order_state().out_price().value(),
// 20.015,
// 0.005));
// }
// // send order acks //////////
// /////////////////////////////
// for (size_t i=0;i<4;i++){
// auto& h = hm_1.test_only_get_hedger_at(i);
// spreader.test_only_on_child_order_update (h.order_state().order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_WORKING,
// Quantity (0),
// Price (0));
// REQUIRE(h.state()==HedgerState::WORKING);
// REQUIRE(h.order_state().out_quantity().value()==2);
// REQUIRE(securities::test_price_equality(
// h.order_state().out_price().value(),
// 20.015,
// 0.005));
// }
// REQUIRE(hm_1.n_active_hedgers()==4);
// // fills 1 lot on each order //
// ///////////////////////////////
// for (size_t i=0;i<4;i++){
// auto& h = hm_1.test_only_get_hedger_at(i);
// // previous hedger fills were 20.015, 20.025
// spreader.test_only_on_child_order_update (h.order_state().order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_WORKING,
// Quantity (1),
// Price (20.035));
// REQUIRE(h.state()==HedgerState::WORKING);
// REQUIRE(h.order_state().out_quantity().value()==2);
// REQUIRE(securities::test_price_equality(
// h.order_state().out_price().value(),
// 20.015,
// 0.005));
// }
// REQUIRE(hm_1.n_active_hedgers()==4);
// hm_1.total_fills(t_qty, t_price);
// REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005));
// REQUIRE(t_qty.value()==6);
TEST_CASE ("spreader_perf_4legs", "[spreader]")
{
bts::orderman::OrderKey order_key(0);
Price spreader_price = Price (40);
Quantity spreader_quantity = Quantity (250);
ContractLeg leg_1, leg_2, leg_3, qleg_4;
fill_future_contract_leg (leg_1, 2468, "SI", bts::api::Venue::VENUE_GLBX,
2019, 1, 15,
1.0, 1.0,
api::Side::SIDE_BUY,
1.0, 0.005);
fill_future_contract_leg (leg_2, 2469, "SI", bts::api::Venue::VENUE_GLBX,
2019, 2, 15,
1.0, 1.0,
api::Side::SIDE_BUY,
1.0, 0.005);
fill_future_contract_leg (leg_3, 2470, "SI", bts::api::Venue::VENUE_GLBX,
2019, 3, 15,
1.0, 1.0,
api::Side::SIDE_BUY,
1.0, 0.005);
fill_future_contract_leg (qleg_4, 2471, "SI", bts::api::Venue::VENUE_GLBX,
2019, 4, 15,
-1.0, -1.0,
api::Side::SIDE_SELL,
1.0, 0.005);
std::vector<ContractLeg> contract_legs;
contract_legs.push_back(leg_1);
contract_legs.push_back(leg_2);
contract_legs.push_back(leg_3);
contract_legs.push_back(qleg_4);
Security synthetic_security;
fill_synthetic_security (synthetic_security, 2000, "synthetic_security", contract_legs);
// execution params/order def
LegSpreaderParams lsp_1, lsp_2, lsp_3, qlsp_4;
fill_leg_spreader_params(lsp_1, 1.0, false, 1.0);
fill_leg_spreader_params(lsp_2, 1.0, false, 1.0);
fill_leg_spreader_params(lsp_3, 1.0, false, 1.0);
fill_leg_spreader_params(qlsp_4, -1.0, true, 1.0);
std::vector<LegSpreaderParams> spreader_params;
spreader_params.push_back(lsp_1);
spreader_params.push_back(lsp_2);
spreader_params.push_back(lsp_3);
spreader_params.push_back(qlsp_4);
bts::api::orderman::OrderDefinition order_def;
fill_spreader_order_definition(order_def, Side::SIDE_BUY,
synthetic_security,
spreader_params);
// putting it together in SpreaderBookkeepingData
SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices
testonly_spreader_pao_services = _create_spreader_pao_services(synthetic_security);
SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services);
std::vector<LegPropertyData> leg_props;
std::vector<bts::api::orderman::OrderDefinition> quoter_ord_defs,
hedger_ord_defs;
SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,quoter_ord_defs,hedger_ord_defs);
SpreaderManagerForTest spreader_manager (order_key, order_def, pao_services, leg_props,quoter_ord_defs,hedger_ord_defs);
SpreaderOrder spreader;
REQUIRE(spreader.test_only_func_spreader_state() == SpreaderState::START);
// on_init_new and activate
spreader.test_only_on_init_new (order_def, pao_services);
SpreaderOrderServices order_services (nullptr);
spreader.test_only_on_activate(spreader_price, spreader_quantity, order_services);
SpreaderBookkeepingData& bookkeeping_data = spreader.test_only_func_spreader_full_data();
verify_basic_spreader_state (spreader,
SpreaderState::WORKING,
spreader_price,
spreader_quantity);
SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3);
verify_complete_quoter_state (qleg_data_4.quoter(),
QuoterState::WAITING_FOR_SIZE,
false,
spreader_price,
spreader_quantity,
Quantity (0),
Quantity(0),
Price(0),
Quantity(0),
spreader_quantity,
false);
Book book_1 = get_book(SecurityKey (leg_1.security().id()),
5,
new double[5] {20, 19.995, 19.990, 19.985, 19.980},
new uint16_t[5] {100, 50, 75, 150, 25},
new double[5] {20.005, 20.010, 20.015, 20.020, 20.025},
new uint16_t[5] {25,125,175,225,100},
true),
book_1_alt = get_book(SecurityKey (leg_1.security().id()),
5,
new double[5] {20.005, 19.990, 19.890, 19.885, 19.780},
new uint16_t[5] {10, 25, 75, 15, 25},
new double[5] {20.010, 20.015, 20.025, 20.030, 20.035},
new uint16_t[5] {10, 25, 75, 15, 25},
true),
book_2 = get_book(SecurityKey (leg_2.security().id()),
5,
new double[5] {20.030,20.025,20.020,20.015,20.010},
new uint16_t[5] {100, 100, 200, 250, 75},
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,
trace::CorID()));
spreader.test_only_on_book_update (BookUpdateEvent(book_2,
trace::CorID()));
spreader.test_only_on_book_update (BookUpdateEvent(book_3,
trace::CorID()));
verify_complete_quoter_state (qleg_data_4.quoter(),
QuoterState::WAITING_FOR_FIRST_ACK,
false,
spreader_price,
spreader_quantity,
Quantity (0),
Quantity(0),
Price(20.045),
spreader_quantity,
spreader_quantity,
false);
spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (0),
Price (0));
verify_complete_quoter_state (qleg_data_4.quoter(),
QuoterState::WORKING,
false,
spreader_price,
spreader_quantity,
Quantity (0),
Quantity(0),
Price(20.045),
spreader_quantity,
spreader_quantity,
false);
SECTION ("Testing Hedge Reuse"){
Quantity t_qty;
Price t_price;
// to keep the compiler warning happy
LOG(OrderMan, Info, "{} {} {}", book_1_alt.n_levels(),
book_2_alt.n_levels(),
book_3_alt.n_levels());
// this sends fills, makes sure the hedgers are
// working, and on fill and recycled
// the hedge prices used are
// 20.015, 20.035 and 19.995
SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3);
// lets concentrate on the hedges sent on leg 1
SpreaderLegData& hleg_1 = bookkeeping_data.get_leg_data (0);
auto& hm_1 = hleg_1.hedger_manager();
REQUIRE(hm_1.n_active_hedgers()==0);
// send a 1 lot quoter fill /////////
/////////////////////////////////////
spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (1),
Price (20.045));
REQUIRE(hm_1.n_active_hedgers()==1);
// legs 2 and 3
REQUIRE(securities::test_price_equality(
bookkeeping_data.get_leg_data (1).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(),
20.035,
0.005));
REQUIRE(securities::test_price_equality(
bookkeeping_data.get_leg_data (2).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(),
19.995,
0.005));
// back to leg1's hedger, this is what will
// be mainly tested here on
{
auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0);
REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1);
REQUIRE(securities::test_price_equality(
hedger_1_1.order_state().out_price().value(),
20.015,
0.005));
REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK);
spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (0),
Price (0));
REQUIRE(hedger_1_1.state()==HedgerState::WORKING);
// fill h1 and it goes back to start ////
/////////////////////////////////////////
spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(),
api::orderman::OrderStatus::ORDER_STATUS_COMPLETED,
Quantity (1),
Price (20.015));
REQUIRE(hedger_1_1.state()==HedgerState::START);
}
REQUIRE(hm_1.n_active_hedgers()==0);
hm_1.total_fills(t_qty, t_price);
REQUIRE(t_qty.value() == 1);
REQUIRE(securities::test_price_equality(t_price.value(),20.015,0.005));
////////////////////////////
// send another 1 lot q fill
spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (2),
Price (20.045));
REQUIRE(hm_1.n_active_hedgers()==1);
// since 2 and 3 were never filled, they should have 2 hedgers
REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==2);
REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==2);
{
auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0);
REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1);
REQUIRE(securities::test_price_equality(
hedger_1_1.order_state().out_price().value(),
20.015,
0.005));
REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK);
spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (0),
Price (0));
REQUIRE(hedger_1_1.state()==HedgerState::WORKING);
// fill h1 ///////
//////////////////
spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(),
api::orderman::OrderStatus::ORDER_STATUS_COMPLETED,
Quantity (1),
Price (20.025));
REQUIRE(hedger_1_1.state()==HedgerState::START);
}
REQUIRE(hm_1.n_active_hedgers()==0);
hm_1.total_fills(t_qty, t_price);
REQUIRE(t_qty.value() == 2);
REQUIRE(securities::test_price_equality(t_price.value(),20.020,0.005));
// send 4 more q fills /////////
////////////////////////////////
for(size_t f=4; f <= 10; f+= 2)
spreader.test_only_on_child_order_update (qleg_data_4.order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (f),
Price (20.045));
REQUIRE(hm_1.n_active_hedgers()==4);
REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==6);
REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==6);
REQUIRE(bookkeeping_data.get_leg_data (3).hedger_manager().n_active_hedgers()==0);
for (size_t i=0;i<4;i++){
auto& h = hm_1.test_only_get_hedger_at(i);
REQUIRE(h.state()==HedgerState::WAITING_FOR_FIRST_ACK);
REQUIRE(h.order_state().out_quantity().value()==2);
REQUIRE(securities::test_price_equality(
h.order_state().out_price().value(),
20.015,
0.005));
}
// send order acks //////////
/////////////////////////////
for (size_t i=0;i<4;i++){
auto& h = hm_1.test_only_get_hedger_at(i);
spreader.test_only_on_child_order_update (h.order_state().order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (0),
Price (0));
REQUIRE(h.state()==HedgerState::WORKING);
REQUIRE(h.order_state().out_quantity().value()==2);
REQUIRE(securities::test_price_equality(
h.order_state().out_price().value(),
20.015,
0.005));
}
REQUIRE(hm_1.n_active_hedgers()==4);
// fills 1 lot on each order //
///////////////////////////////
for (size_t i=0;i<4;i++){
auto& h = hm_1.test_only_get_hedger_at(i);
// previous hedger fills were 20.015, 20.025
spreader.test_only_on_child_order_update (h.order_state().order_key(),
api::orderman::OrderStatus::ORDER_STATUS_WORKING,
Quantity (1),
Price (20.035));
REQUIRE(h.state()==HedgerState::WORKING);
REQUIRE(h.order_state().out_quantity().value()==2);
REQUIRE(securities::test_price_equality(
h.order_state().out_price().value(),
20.015,
0.005));
}
REQUIRE(hm_1.n_active_hedgers()==4);
hm_1.total_fills(t_qty, t_price);
REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005));
REQUIRE(t_qty.value()==6);
// // fills additional 1 lot on each, ///////
// // which should complete the orders //////
// //////////////////////////////////////////
// for (size_t i=0;i<4;i++){
// auto& h = hm_1.test_only_get_hedger_at(i);
// // previous hedger fills were 20.015, 20.025
// spreader.test_only_on_child_order_update (h.order_state().order_key(),
// api::orderman::OrderStatus::ORDER_STATUS_COMPLETED,
// Quantity (2),
// Price (20.03));
// REQUIRE(h.state()==HedgerState::START);
// REQUIRE(h.order_state().out_quantity().value()==0);
// }
// REQUIRE(hm_1.n_active_hedgers()==0);
// hm_1.total_fills(t_qty, t_price);
// REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005));
// REQUIRE(t_qty.value()==10);
// }
// }
// fills additional 1 lot on each, ///////
// which should complete the orders //////
//////////////////////////////////////////
for (size_t i=0;i<4;i++){
auto& h = hm_1.test_only_get_hedger_at(i);
// previous hedger fills were 20.015, 20.025
spreader.test_only_on_child_order_update (h.order_state().order_key(),
api::orderman::OrderStatus::ORDER_STATUS_COMPLETED,
Quantity (2),
Price (20.03));
REQUIRE(h.state()==HedgerState::START);
REQUIRE(h.order_state().out_quantity().value()==0);
}
REQUIRE(hm_1.n_active_hedgers()==0);
hm_1.total_fills(t_qty, t_price);
REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005));
REQUIRE(t_qty.value()==10);
}
}
TEST_CASE ("simple book addition", "[spreader]"){
SECTION ("direct, book"){
SecurityKey key(12345);