tests and traverse_depth modified

This commit is contained in:
Taha Afzal committed 2017-10-05 20:10:00 -04:00
1 parent 0c383bcb56
commit fdb4eb6c5e
4 files changed
+270 -180

No files matched your search

@@ -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()
+6 -3
View File
@@ -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);
@@ -191,6 +194,6 @@ private:
// using the market of the other legs, computes the
// quoting price and quantity, and if the quantity > 0
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);
};
}}
+235 -165
View File
@@ -10,6 +10,8 @@
#include "SpreaderBase.h"
#include "LegPropertyData.h"
#include "SpreaderBookkeepingData.h"
#include "SpreaderUtils.h"
#include "../src/lib/utils/bts_chrono.h"
#include <future>
#include <stdlib.h>
@@ -18,183 +20,251 @@
using namespace bts::orderman;
using namespace bts::spark;
namespace bts{
namespace spreader{
class SpreaderManagerSim : public ISpreaderManager
{
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);
namespace bts {
namespace spreader {
class SpreaderManagerSim : public ISpreaderManager
{
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);
_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);
// 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_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);
_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.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);
_synth_contract.mutable_legs()->AddAllocated(&_leg_1_contract);
_synth_contract.mutable_legs()->AddAllocated(&_leg_2_contract);
_spread_security.set_allocated_synthetic(&_synth_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_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);
// 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_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_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);
_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_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);
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));
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
{
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);
// }
//}
}
// 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;
}
};
//class SpreaderTestHelper
//{
//public:
// static void test()
// {
// // create sim spreader
// bts::spreader::SpreaderManagerSim sim;
// // create quoter
// OrderTemplate ord_templ;
// SingleLegQuoter quoter(sim, ord_templ, 0, Price(0.0), Quantity(10));
// quoter.init();
// // fire md update
// Book book;
// sim.bookkeeping_data->get_leg_data(1).set_book(book);
// quoter.update_md(1);
// }
//};
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("Simple Single Quoter Tester", "[spreader]")
{
SECTION("nothing_really")
{
/*ISpreaderManager* sim = new SpreaderManagerSim();
OrderTemplate ot;
SingleLegQuoter leg_1_q(*sim, ot, 0, Price(0.0), Quantity(10));*/
/*std::vector<int> v;
for (int i = 0; i < 100; i++)
v.push_back(i);
REQUIRE(v.size() == 100);*/
}
}
}}
}
+27 -11
View File
@@ -109,26 +109,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