Merge branch 'spartan_tas' of bitbucket.org:bluetradesys/spark into spartan_tas

This commit is contained in:
Pedro Pinto committed 2020-09-28 15:03:12 -04:00
commit e2f4fc83a2
5 files changed
+192 -41

No files matched your search

@@ -2,9 +2,43 @@
#include <time.h>
#include <stdio.h>
#include <boost/container/flat_set.hpp>
#include "../../../../../server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/algos/SimSpreader.h"
#include "../../../../../server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h"
#include "../../../../../server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/AlgoEvent.h"
#include "../../../../../server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h"
#include "../../../../../server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.h"
using namespace bts::spartan::mdp3;
namespace bts {
namespace orderman {
class TestSimServices : public bts::spartan::mdp3::ISimServices{
private:
std::vector<InstrumentDefinition const*> _instruments;
public:
OrderChange last_change;
size_t n_updates = 0;
public:
std::vector<InstrumentDefinition const*> const &instruments() const override{
bts_assert(false);
return _instruments;
}
MDP3OrderKey generate_order_key() override{
return MDP3OrderKey(0);
}
virtual MDP3TimeStamp now() const override{
return MDP3TimeStamp(0);
}
void wait(uint64_t) override{
}
void send(const OrderChange& c, uint64_t) override{
last_change = c;
n_updates++;
}
~TestSimServices(){}
};
// QuoteLegPriceQuantityState is used by the quoter
// to keep the state of the order state, ie
// target price, qty
@@ -4106,6 +4140,94 @@ TEST_CASE ("net_change_transformation", "[spreader]")
}
}
}
bts::spartan::mdp3::MBPUpdate get_book_update(
const std::vector<std::tuple<double,uint32_t>>& bids,
const std::vector<std::tuple<double,uint32_t>>& asks,
uint32_t key){
bts::spartan::mdp3::MBPUpdate rv;
size_t i=0;
for(auto[p,q] : bids)
rv.mbp_buy[i++] = BookLevel{
md::IntPrice_from_double<7>(p),
bts::spartan::mdp3::MDP3Quantity(q),
0
};
i=0;
for(auto[p,q] : asks)
rv.mbp_sell[i++] = BookLevel{
md::IntPrice_from_double<7>(p),
bts::spartan::mdp3::MDP3Quantity(q),
0
};
rv.security_id = MDP3SecurityID(key);
return rv;
}
TEST_CASE ("sim_spreader", "[spreader]"){
bts::orderman::OrderKey order_key(1);
// security
ContractLeg leg_1, leg_2;
fill_future_contract_leg (leg_1, 1, "CL",
bts::api::Venue::VENUE_GLBX,
2020, 11, 15,
1.0, 1.0,
api::Side::SIDE_BUY,
1.0, 0.01);
fill_future_contract_leg (leg_2, 2, "CL",
bts::api::Venue::VENUE_GLBX,
2020, 12, 15,
-1.0, -1.0,
api::Side::SIDE_SELL,
1.0, 0.01);
std::vector<ContractLeg> legs;
legs.push_back(leg_1);
legs.push_back(leg_2);
Security sec;
fill_synthetic_security (sec, 2000, "simspreader_2legs", legs);
// spreader params
LegSpreaderParams lsp_1, lsp_2, lsp_3, lsp_4;
fill_leg_spreader_params(lsp_1, 1.0, false, 1.0);
fill_leg_spreader_params(lsp_2, -1.0, true, 1.0);
// post hedge time out here
std::vector<LegSpreaderParams> params;
params.push_back(lsp_1);
params.push_back(lsp_2);
// putting it together in the order def
bts::api::orderman::OrderDefinition order_def;
fill_spreader_order_definition(order_def, Side::SIDE_BUY, sec, params);
bts::spartan::mdp3::SimSpreader sim_spreader(order_def,sec,Price(-10),Quantity(25));
//ISimServices* sim_services = new TestSimServices();
TestSimServices sim_services;
sim_spreader.init(*(ISimServices*)&sim_services);
SECTION("requoting"){
std::vector<std::tuple<double,uint32_t>> bids =
{
{100,5},
{101,5},
{102,5}
},
asks =
{
{102,5},
{104,5},
{105,5}
};
auto md_udpate_leg_1 = get_book_update(bids,asks,leg_1.security().id());
// send leg_1 book update => leg_2 should quote
sim_spreader.on_notification(
{
bts::spartan::mdp3::MDP3TimeStamp{},
bts::spartan::mdp3::MDP3TimeStamp{},
md_udpate_leg_1
}
);
sim_spreader.on_time(bts::spartan::mdp3::MDP3TimeStamp());
REQUIRE(sim_services.n_updates==1);
}
}
class RV
{
public:
@@ -15,6 +15,7 @@ struct OrderTuple{
OrderChange ochange;
uint32_t total_fills = 0;
double avg_filled_price = 0;
bool order_send_acked = false;
OrderTuple (OrderKey key,
OrderChange ochange)
:
@@ -55,7 +56,7 @@ OrderTuple* find_ord(MDP3OrderKey key,
struct SimOrderGateway::PImpl {
ISimServices* sim_services = nullptr;
SpreaderOrder* spreader = nullptr;
std::vector<OrderTuple> orders;
std::vector<OrderTuple> orders;
};
SimOrderGateway::SimOrderGateway()
@@ -75,34 +76,23 @@ void SimOrderGateway:: place_order(SecurityKey skey,
Price p,
Quantity q){
bts_assert(me->sim_services!=nullptr);
auto& t =
me->orders.emplace_back(
okey,
OrderChange{
MDP3Order(),
MDP3Order{
me->sim_services->generate_order_key(),
MDP3SecurityID{static_cast<int>(skey.value())},
side,
MDP3OrderPriority{},
md::IntPrice_from_double<7>(p.value()),
MDP3Quantity{static_cast<int>(q.value())},
MDP3Quantity(0)
},
OrderChange::added
});
const auto& ochange = t.ochange;
me->sim_services->send(ochange);
// since there is no working order ack, fake one
route_update_to_spreader(
OrderUpdate{
t.ochange.order.key,
OrderUpdate::Status::PARTIALLY_FILLED,
t.ochange.order.security,
t.ochange.order.side,
MDP3Quantity{},
md::IntPrice()
});
const auto& t =
me->orders.emplace_back(
okey,
OrderChange{
MDP3Order(),
MDP3Order{
me->sim_services->generate_order_key(),
MDP3SecurityID{static_cast<int>(skey.value())},
side,
MDP3OrderPriority{},
md::IntPrice_from_double<7>(p.value()),
MDP3Quantity{static_cast<int>(q.value())},
MDP3Quantity(0)
},
OrderChange::added
});
me->sim_services->send(t.ochange);
}
void SimOrderGateway::modify_order(OrderKey okey,
@@ -111,6 +101,7 @@ void SimOrderGateway::modify_order(OrderKey okey,
bts_assert(me->sim_services!=nullptr);
OrderTuple* t = find_ord(okey,me->orders);
bts_assert(t != nullptr);
bts_assert(t->order_send_acked);
auto& ochange = t->ochange;
ochange.previous_order = ochange.order;
ochange.order.price = md::IntPrice_from_double<7>(p.value());
@@ -120,19 +111,38 @@ void SimOrderGateway::modify_order(OrderKey okey,
}
void SimOrderGateway::cancel_order(OrderKey okey){
bts_assert(me->sim_services!=nullptr);
bts_assert(me->sim_services!=nullptr);
OrderTuple* t = find_ord(okey,me->orders);
bts_assert(t != nullptr);
bts_assert(t->order_send_acked);
auto& ochange = t->ochange;
ochange.change = OrderChange::removed;
me->sim_services->send(ochange);
}
OrderKey SimOrderGateway::generate_key(){
auto rv = static_cast<uint32_t>(bts::Timer::now().time_since_epoch().count());
auto rv =
static_cast<uint32_t>(bts::Timer::now().time_since_epoch().count());
return OrderKey(rv);
}
void SimOrderGateway::poll(){
for(auto& t : me->orders){
if(!t.order_send_acked){
t.order_send_acked=true;
route_update_to_spreader(
OrderUpdate{
t.ochange.order.key,
OrderUpdate::Status::PARTIALLY_FILLED,
t.ochange.order.security,
t.ochange.order.side,
MDP3Quantity{},
md::IntPrice()
});
}
}
}
void SimOrderGateway::route_update_to_spreader(const OrderUpdate& u){
OrderTuple* t = find_ord(u.key,me->orders);
bts_assert(t!=nullptr);
@@ -153,5 +163,4 @@ void SimOrderGateway::route_update_to_spreader(const OrderUpdate& u){
Price(t->avg_filled_price)
);
}
}
@@ -32,6 +32,7 @@ public:
void cancel_order(bts::orderman::OrderKey) override;
bts::orderman::OrderKey generate_key() override;
// Sends ISimServices's updates to spreader
void poll();
void route_update_to_spreader(const OrderUpdate&);
};
@@ -19,6 +19,7 @@ void translate_book(const MBPUpdate& u_in,
if(++i<5)
b_out.side(api::Side::SIDE_BUY)[i] =
md::PriceSize (bl.price,bl.quantity.value());
i=-1;
for (auto const& bl : u_in.mbp_sell)
if(++i<5)
b_out.side(api::Side::SIDE_SELL)[i] =
@@ -41,19 +42,25 @@ bts::orderman::Book* find_book(MDP3SecurityID id,
struct SimSpreader::PImpl {
const OrderDefinition order_def;
const SpreaderPreActivationOrderServices pao_services;
const SpreaderOrderServices so_services;
SpreaderOrderServices so_services;
ISimServices* sim_services = nullptr;
SimOrderGateway sim_order_gateway;
SpreaderOrder spreader;
Price price;
Quantity quantity;
std::vector<bts::orderman::Book> books;
PImpl(const OrderDefinition& order_def,
const bts::api::Security& sec)
const bts::api::Security& sec,
Price price,
Quantity quantity)
:
order_def(order_def),
pao_services(sec),
so_services(nullptr),
sim_order_gateway(),
spreader(&sim_order_gateway),
price(price),
quantity(quantity),
books(){
for (const auto& l : sec.synthetic().legs())
books.emplace_back(SecurityKey(l.security().id()));
@@ -61,18 +68,26 @@ struct SimSpreader::PImpl {
};
SimSpreader::SimSpreader(const OrderDefinition& order_def,
const bts::api::Security& sec)
const bts::api::Security& sec,
Price price,
Quantity qty)
:
me(new PImpl(order_def,sec)){
me(new PImpl(order_def,sec,price,qty)){
}
void SimSpreader::init(ISimServices& sim_services){
me->sim_services = &sim_services;
me->sim_order_gateway.init(sim_services,me->spreader);
// TODO (no test_ blah)
me->spreader.test_only_on_init_new (me->order_def,
me->pao_services);
me->spreader.test_only_on_activate(me->price,
me->quantity,
me->so_services);
}
void SimSpreader::on_time(MDP3TimeStamp){
me->sim_order_gateway.poll();
}
void SimSpreader::on_notification(AlgoNotification const& ev) {
if(std::holds_alternative<MBPUpdate>(ev.event)){
// book update
@@ -15,12 +15,16 @@ class ISimServices;
class SimSpreader : public IAlgoModel {
private:
struct PImpl;
std::unique_ptr<PImpl> me;
//std::unique_ptr<PImpl> me;
PImpl* me;
public:
SimSpreader(const bts::api::orderman::OrderDefinition&,
const bts::api::Security&);
const bts::api::Security&,
Price, Quantity);
void init(ISimServices&) override;
void on_notification(AlgoNotification const&) override;
void on_time(MDP3TimeStamp) override;
void on_notification(AlgoNotification const&) override;
~SimSpreader(){}
};
}
}