Merge branch 'spartan_tas' of bitbucket.org:bluetradesys/spark into spartan_tas
This commit is contained in:
commit
8bdce09aec
26 files changed
+509
-443
No files matched your search
@@ -2,54 +2,10 @@
|
||||
#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:
|
||||
InstrumentMap _instruments;
|
||||
std::vector<Polygon> _polygons;
|
||||
public:
|
||||
OrderChange last_change;
|
||||
size_t n_updates = 0;
|
||||
size_t key;
|
||||
public:
|
||||
TestSimServices ()
|
||||
:
|
||||
key(0){
|
||||
}
|
||||
InstrumentMap const &instruments() const override{
|
||||
bts_assert(false);
|
||||
return _instruments;
|
||||
}
|
||||
|
||||
std::vector<Polygon> const&polygons() const override {
|
||||
return _polygons;
|
||||
}
|
||||
|
||||
MDP3OrderKey generate_order_key() override{
|
||||
return MDP3OrderKey(key++);
|
||||
}
|
||||
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
|
||||
@@ -4152,301 +4108,4 @@ 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(1));
|
||||
//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 =
|
||||
{
|
||||
{103,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);
|
||||
REQUIRE(sim_services.last_change.order.side == api::Side::SIDE_SELL);
|
||||
REQUIRE(sim_services.last_change.order.quantity == bts::spartan::mdp3::MDP3Quantity(1));
|
||||
REQUIRE(
|
||||
bts::securities::is_same(Price(sim_services.last_change.order.price.price()),
|
||||
Price(113)));
|
||||
// send multiple book updates
|
||||
double p = 103;
|
||||
for(int i=0; i<100; i++){
|
||||
asks =
|
||||
{
|
||||
{p,5},
|
||||
{p+1,5},
|
||||
{p+2,5}
|
||||
};
|
||||
auto md_udpate_leg_1 = get_book_update(bids,asks,leg_1.security().id());
|
||||
sim_spreader.on_notification(
|
||||
{
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
md_udpate_leg_1
|
||||
});
|
||||
REQUIRE(sim_services.n_updates==i+1); // +1 because order_place is also counted
|
||||
REQUIRE(sim_services.last_change.order.security ==
|
||||
bts::spartan::mdp3::MDP3SecurityID(2));
|
||||
REQUIRE(sim_services.last_change.order.side == api::Side::SIDE_SELL);
|
||||
REQUIRE(sim_services.last_change.order.quantity == bts::spartan::mdp3::MDP3Quantity(1));
|
||||
REQUIRE(
|
||||
bts::securities::is_same(Price(sim_services.last_change.order.price.price()),
|
||||
Price(p+10)));
|
||||
p++;
|
||||
}
|
||||
//force a quoting leg fill
|
||||
auto q_ord = sim_services.last_change.order;
|
||||
sim_spreader.on_notification(
|
||||
{
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::OrderUpdate{
|
||||
q_ord.key,
|
||||
bts::spartan::mdp3::OrderUpdate::FILLED,
|
||||
q_ord.security,
|
||||
q_ord.side,
|
||||
bts::spartan::mdp3::MDP3Quantity(1),
|
||||
md::IntPrice_from_double<7>(202)
|
||||
}
|
||||
});
|
||||
// the above should have sent a hedge order, verify that
|
||||
auto hedge_ord = sim_services.last_change.order;
|
||||
REQUIRE(hedge_ord.security == bts::spartan::mdp3::MDP3SecurityID(1));
|
||||
REQUIRE(hedge_ord.side == api::Side::SIDE_BUY);
|
||||
REQUIRE(hedge_ord.quantity == bts::spartan::mdp3::MDP3Quantity(1));
|
||||
REQUIRE(
|
||||
bts::securities::is_same(Price(hedge_ord.price.price()),
|
||||
Price(192)));
|
||||
REQUIRE(sim_spreader.spreader().test_only_func_spreader_state() !=
|
||||
SpreaderState::END);
|
||||
sim_spreader.on_time(bts::spartan::mdp3::MDP3TimeStamp());
|
||||
// send the hedge fill and it should go to end state
|
||||
sim_spreader.on_notification(
|
||||
{
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::OrderUpdate{
|
||||
hedge_ord.key,
|
||||
bts::spartan::mdp3::OrderUpdate::FILLED,
|
||||
hedge_ord.security,
|
||||
hedge_ord.side,
|
||||
bts::spartan::mdp3::MDP3Quantity(1),
|
||||
md::IntPrice_from_double<7>(192)
|
||||
}
|
||||
});
|
||||
REQUIRE(sim_spreader.spreader().test_only_func_spreader_state() ==
|
||||
SpreaderState::END);
|
||||
}
|
||||
}
|
||||
|
||||
class RV
|
||||
{
|
||||
public:
|
||||
int i;
|
||||
RV(int i_arg)
|
||||
:
|
||||
i(i_arg){
|
||||
}
|
||||
RV()=delete;
|
||||
RV(const RV&&)=delete;
|
||||
RV (RV&& other){
|
||||
i=other.i;
|
||||
other.i=0;
|
||||
}
|
||||
RV& operator=(const RV&)=delete;
|
||||
RV& operator=(RV &&src){
|
||||
i=src.i;
|
||||
src.i=0;
|
||||
return *this;
|
||||
}
|
||||
int foo (double d1, double d2){
|
||||
return static_cast<int>(d1+d2);
|
||||
}
|
||||
};
|
||||
class TTemp{
|
||||
};
|
||||
class TInterface{
|
||||
virtual void foo() = 0;
|
||||
};
|
||||
template <class T, size_t N>
|
||||
class TestContainer : public TInterface{
|
||||
private:
|
||||
T _t;
|
||||
std::array<T,N> _at;
|
||||
public:
|
||||
void add (T &t){
|
||||
_at[0] = t;
|
||||
}
|
||||
T get() {
|
||||
return _t;
|
||||
}
|
||||
void foo () override{
|
||||
}
|
||||
};
|
||||
class RVUser{
|
||||
public:
|
||||
RV rv;
|
||||
RVUser(RV& rv_arg)
|
||||
: rv(std::move(rv_arg))
|
||||
{
|
||||
}
|
||||
};
|
||||
TEST_CASE ("scratch_pad", "[spreader]"){
|
||||
int (RV::*f_ptr)(double, double) = &RV::foo;
|
||||
char* f_addr = (char*&)f_ptr;
|
||||
TInterface* r = nullptr;
|
||||
int n = gen_rand(0.0, 10.0);
|
||||
if (n <= 5)
|
||||
r = new TestContainer<TTemp,5>();
|
||||
else
|
||||
r = new TestContainer<TTemp,10>();
|
||||
REQUIRE(r != nullptr);
|
||||
RV rv_1(100);
|
||||
RVUser rvuser_1(rv_1);
|
||||
REQUIRE(rv_1.i==0);
|
||||
REQUIRE(rvuser_1.rv.i==100);
|
||||
}
|
||||
TEST_CASE ("scratch_pad_2", "[spreader]"){
|
||||
time_t rawtime;
|
||||
time (&rawtime);
|
||||
struct tm* ptm = gmtime (&rawtime);
|
||||
LOG(OrderMan, Info, "hrs:{} mins:{} sec:{}", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
|
||||
}
|
||||
// vector insert //////////////////////////////////////
|
||||
///////////////////////////////////////////////////////
|
||||
class Elem{
|
||||
public:
|
||||
int i;
|
||||
Elem()
|
||||
:
|
||||
i(-1)
|
||||
{}
|
||||
Elem (int i_arg)
|
||||
:
|
||||
i(i_arg)
|
||||
{}
|
||||
Elem(const Elem& other){
|
||||
i = other.i;
|
||||
}
|
||||
Elem (Elem&& other){
|
||||
i=other.i;
|
||||
}
|
||||
Elem& operator=(const Elem& other){
|
||||
i=other.i;
|
||||
return *this;
|
||||
}
|
||||
Elem& operator=(Elem &&src){
|
||||
i = src.i;
|
||||
return *this;
|
||||
}
|
||||
bool operator==(const Elem& e) const {return i == e.i;}
|
||||
bool operator!=(const Elem& e) const {return i != e.i;}
|
||||
bool operator<(const Elem& e) const {return i < e.i;}
|
||||
bool operator>(const Elem& e) const {return i > e.i;}
|
||||
};
|
||||
TEST_CASE ("scratch_pad_3", "[spreader]"){
|
||||
std::vector<Elem> v;
|
||||
for(int i=1;i<10;i++)
|
||||
v.emplace_back(i);
|
||||
Elem e(0);
|
||||
// move op= is called when inserting or erasing
|
||||
v.insert(v.begin()+2,e);
|
||||
v.erase(v.begin()+2);
|
||||
std::random_shuffle(v.begin(),v.end());
|
||||
// flat set
|
||||
boost::container::flat_set<Elem> fs;
|
||||
for(int i=0;i<20;i+=2){
|
||||
Elem e(i);
|
||||
fs.insert(e);
|
||||
}
|
||||
Elem e9(9);
|
||||
// move op is called when inserting
|
||||
fs.insert(e9);
|
||||
for (const auto& e : fs)
|
||||
std::cout << e.i << std::endl;
|
||||
// vector assignment vs move assignment
|
||||
std::vector<Elem> vs;
|
||||
int n_elems = 10;
|
||||
for(int i=1;i<n_elems;i++)
|
||||
vs.emplace_back(i);
|
||||
std::vector<Elem> vt;
|
||||
// new elems are cresated in vt from the elem's in vs,
|
||||
// with their copy cons
|
||||
vt = vs;
|
||||
std::vector<Elem> vt_2;
|
||||
vt_2 = std::move(vs);
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -1 +1 @@
|
||||
set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CMECertCmd.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/FIXSim.cpp;cmds/Help.cpp;cmds/KeyCode.cpp;cmds/Md.cpp;cmds/ParseHistoricalFIX.cpp;cmds/PrintHedgeOrders.cpp;cmds/QuickTest.cpp;cmds/Run.cpp;cmds/Span.cpp;cmds/Spartan.cpp;cmds/Test.cpp;cmds/Web.cpp;cmds/spartan/algo/tas/RunTAS.cpp;cmds/spartan/algo/tas/TASOrderReactor.cpp;cmds/spartan/algo/tas/TASTriggerDef.cpp;cmds/spartan/md/mdp3/back_testing/algos/Sniper.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp;cmds/spartan/md/mdp3/back_testing/simulator/ExchangeSim.cpp;cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.cpp;cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.cpp;cmds/spartan/md/mdp3/back_testing/simulator/SimInput.cpp;cmds/spartan/md/mdp3/back_testing/simulator/SimInput.proto;cmds/spartan/md/mdp3/back_testing/simulator/agents/AlgoAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/GatewayAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/TimerAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/algos/SimOrderGateway.cpp;cmds/spartan/md/mdp3/back_testing/simulator/algos/SimSpreader.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/AgentBase.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/Simulator.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/tests/test_simulator_core.cpp;cmds/spartan/md/mdp3/back_testing/simulator/events/SimEvents.cpp;cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp;cmds/spartan/md/mdp3/feed_control/IFeedController.cpp;cmds/spartan/md/mdp3/handler/ChannelHandler.cpp;cmds/spartan/md/mdp3/parsing/MDP3MsgBox.cpp;cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp;cmds/spartan/md/mdp3/recovery/BookSnapshot.cpp;cmds/spartan/md/mdp3/recovery/DefinitionSnapshot.cpp;cmds/spartan/md/mdp3/recovery/IncrementalQueue.cpp;cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp;cmds/spartan/md/mdp3/tests/matcher_tests.cpp;cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp;cmds/spartan/md/mdp3/utils/mdp3_utils.cpp;cmds/spartan/md/mdp3/utils/polygon_utils.cpp;cmds/spartan/md/mdp3/utils/queries/DefinitionCollector.cpp;cmds/spartan/md/mdp3/utils/queries/FillCounter.cpp;cmds/spartan/md/mdp3/utils/queries/IcebergFinder.cpp;cmds/spartan/md/mdp3/utils/queries/PolygonFinder.cpp;cmds/spartan/md/mdp3/utils/queries/TransactionDeducer.cpp;cmds/spartan/md/mdp3/utils/tests/test_polygon_finder.cpp;cmds/spartan/util/MCastReceiver.cpp;cmds/spartan/util/PCapReader.cpp;cmds/spartan/util/SecurityDB.cpp;dspark/mirage/quick_queue/quick_queue.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/book/Book.cpp;exchange_sim/book/ExchangeSim.proto;exchange_sim/book/Order.cpp;exchange_sim/book/OrderMatcher.cpp;exchange_sim/book/book_transform.cpp;exchange_sim/tests/test_book_transform.cpp;http/ApiGateway.cpp;http/MultipartFormDecoder.cpp;http/WebServer.cpp;http/tests/MultiPartFormTests.cpp;main.cpp;md/ChannelVolume.cpp;md/MarketDataAuxiliaryServiceServer.cpp;md/VolumeAtPriceState.cpp;md/platforms/IMDPlatform.cpp;md/platforms/ice_onixs/ICEListener.cpp;md/platforms/ice_onixs/ICEMain.cpp;md/platforms/ice_onixs/tests/ice_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/MarginServiceServer.cpp;position/PositionAggregator.cpp;position/PositionMap.cpp;position/PositionServicePNLHelper.cpp;position/PositionServiceServer.cpp;position/import/BasePositionServiceServer.cpp;position/import/DownloadCommonStateMachine.cpp;position/import/DownloadWindow.cpp;position/import/MoneyFileServiceServer.cpp;position/import/parsing/gmi_parser.cpp;position/import/parsing/money_parser.cpp;position/import/parsing/parser_common.cpp;position/tests/PositionTests.cpp;position/tests/position_tests_internal.cpp;subprocess/SubProcessRunner.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceDB.cpp;user/AdminServiceServer.cpp;user/AuthServiceServer.cpp;user/tests/admin_service_tests.cpp)
|
||||
set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CMECertCmd.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/FIXSim.cpp;cmds/Help.cpp;cmds/KeyCode.cpp;cmds/Md.cpp;cmds/ParseHistoricalFIX.cpp;cmds/PrintHedgeOrders.cpp;cmds/QuickTest.cpp;cmds/Run.cpp;cmds/Span.cpp;cmds/Spartan.cpp;cmds/Test.cpp;cmds/Web.cpp;cmds/spartan/algo/tas/RunTAS.cpp;cmds/spartan/algo/tas/TASOrderReactor.cpp;cmds/spartan/algo/tas/TASTriggerDef.cpp;cmds/spartan/md/mdp3/back_testing/algos/Sniper.cpp;cmds/spartan/md/mdp3/back_testing/algos/spreader/SimOrderGateway.cpp;cmds/spartan/md/mdp3/back_testing/algos/spreader/SimSpreader.cpp;cmds/spartan/md/mdp3/back_testing/algos/spreader/tests/sim_spreader_tests.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/tests/matcher_tests.cpp;cmds/spartan/md/mdp3/back_testing/simulator/ExchangeSim.cpp;cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.cpp;cmds/spartan/md/mdp3/back_testing/simulator/IExchangeServices.cpp;cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.cpp;cmds/spartan/md/mdp3/back_testing/simulator/SimInput.cpp;cmds/spartan/md/mdp3/back_testing/simulator/SimInput.proto;cmds/spartan/md/mdp3/back_testing/simulator/agents/AlgoAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/GatewayAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/TimerAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/AgentBase.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/Simulator.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/tests/test_simulator_core.cpp;cmds/spartan/md/mdp3/back_testing/simulator/events/SimEvents.cpp;cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp;cmds/spartan/md/mdp3/feed_control/IFeedController.cpp;cmds/spartan/md/mdp3/handler/ChannelHandler.cpp;cmds/spartan/md/mdp3/parsing/MDP3MsgBox.cpp;cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp;cmds/spartan/md/mdp3/recovery/BookSnapshot.cpp;cmds/spartan/md/mdp3/recovery/DefinitionSnapshot.cpp;cmds/spartan/md/mdp3/recovery/IncrementalQueue.cpp;cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp;cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp;cmds/spartan/md/mdp3/utils/mdp3_utils.cpp;cmds/spartan/md/mdp3/utils/polygon_utils.cpp;cmds/spartan/md/mdp3/utils/queries/DefinitionCollector.cpp;cmds/spartan/md/mdp3/utils/queries/FillCounter.cpp;cmds/spartan/md/mdp3/utils/queries/IcebergFinder.cpp;cmds/spartan/md/mdp3/utils/queries/PolygonFinder.cpp;cmds/spartan/md/mdp3/utils/queries/TransactionDeducer.cpp;cmds/spartan/md/mdp3/utils/tests/test_polygon_finder.cpp;cmds/spartan/util/MCastReceiver.cpp;cmds/spartan/util/PCapReader.cpp;cmds/spartan/util/SecurityDB.cpp;dspark/mirage/quick_queue/quick_queue.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/book/Book.cpp;exchange_sim/book/ExchangeSim.proto;exchange_sim/book/Order.cpp;exchange_sim/book/OrderMatcher.cpp;exchange_sim/book/book_transform.cpp;exchange_sim/tests/test_book_transform.cpp;http/ApiGateway.cpp;http/MultipartFormDecoder.cpp;http/WebServer.cpp;http/tests/MultiPartFormTests.cpp;main.cpp;md/ChannelVolume.cpp;md/MarketDataAuxiliaryServiceServer.cpp;md/VolumeAtPriceState.cpp;md/platforms/IMDPlatform.cpp;md/platforms/ice_onixs/ICEListener.cpp;md/platforms/ice_onixs/ICEMain.cpp;md/platforms/ice_onixs/tests/ice_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/MarginServiceServer.cpp;position/PositionAggregator.cpp;position/PositionMap.cpp;position/PositionServicePNLHelper.cpp;position/PositionServiceServer.cpp;position/import/BasePositionServiceServer.cpp;position/import/DownloadCommonStateMachine.cpp;position/import/DownloadWindow.cpp;position/import/MoneyFileServiceServer.cpp;position/import/parsing/gmi_parser.cpp;position/import/parsing/money_parser.cpp;position/import/parsing/parser_common.cpp;position/tests/PositionTests.cpp;position/tests/position_tests_internal.cpp;subprocess/SubProcessRunner.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceDB.cpp;user/AdminServiceServer.cpp;user/AuthServiceServer.cpp;user/tests/admin_service_tests.cpp)
|
||||
@@ -18,6 +18,8 @@ struct Sniper::PImpl {
|
||||
std::unordered_map<MDP3SecurityID, MBPUpdate> books;
|
||||
std::unordered_set<MDP3OrderKey> in_flight;
|
||||
size_t current_shot;
|
||||
MDP3TimeStamp transact_time;
|
||||
MDP3TimeStamp observable_time;
|
||||
};
|
||||
|
||||
struct Sniper::Target {
|
||||
@@ -45,6 +47,8 @@ void Sniper::init(ISimServices &s) {
|
||||
void Sniper::on_time(MDP3TimeStamp) {}
|
||||
|
||||
void Sniper::on_notification(AlgoNotification const &n) {
|
||||
me->transact_time = n.transact_time;
|
||||
me->observable_time = n.observable_time;
|
||||
std::visit(*this, n.event);
|
||||
}
|
||||
|
||||
@@ -78,9 +82,15 @@ Sniper::MaybeTarget Sniper::pick() {
|
||||
valuation.insert(eval(p, api::SIDE_BUY));
|
||||
valuation.insert(eval(p, api::SIDE_SELL));
|
||||
}
|
||||
LOG(Spartan, Diag, "Evaluating --------------------------");
|
||||
for (auto const &op : valuation)
|
||||
LOG(Spartan, Diag, "Found opportunity: {}", op.value);
|
||||
if (valuation.size() && valuation.rbegin()->value>0) {
|
||||
size_t max_print = 3;
|
||||
LOG(Spartan, Info, "Top opportunities");
|
||||
for (auto it = valuation.rbegin(); it != valuation.rend(); it++) {
|
||||
LOG(Spartan, Info, " {}", it->value);
|
||||
if (max_print-- == 0 || it->value<0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
auto const &best = *valuation.rbegin();
|
||||
if (best.value > 0)
|
||||
@@ -105,8 +115,7 @@ void Sniper::fire(Target const &t) {
|
||||
order_change.change = OrderChange::added;
|
||||
if (os::is_nan(order_change.order.price.price()))
|
||||
debug_break();
|
||||
LOG(Spartan, Info, "Time: {} Shot: {} Firing order: {}",
|
||||
to_time_string(me->sim->now()), me->current_shot, order_change.order);
|
||||
LOG(Spartan, Info, "Sending order: {}", order_change.order);
|
||||
me->sim->send(order_change);
|
||||
order_change.change = OrderChange::removed;
|
||||
me->sim->send(order_change);
|
||||
@@ -120,8 +129,10 @@ void Sniper::eval_pick_fire() {
|
||||
auto best_target = pick();
|
||||
if (best_target) {
|
||||
LOG(Spartan, Info,
|
||||
"Found mispricing. Value: {}. Firing ------------------",
|
||||
best_target.value().value);
|
||||
"Time: {} Shot: {} Value: {}. Delay: {}. Firing ------------------",
|
||||
to_time_string(me->sim->now()), me->current_shot,
|
||||
best_target.value().value,
|
||||
(me->observable_time.value() - me->transact_time.value()));
|
||||
fire(best_target.value());
|
||||
transition(State::WAIT);
|
||||
}
|
||||
@@ -149,6 +160,7 @@ void Sniper::operator()(const OrderUpdate &update) {
|
||||
: "canceled"});
|
||||
bts_assert(me->state == State::WAIT);
|
||||
if (me->in_flight.empty()) {
|
||||
LOG(Spartan,Info, "PandL: {}", me->sim->pandl());
|
||||
transition(State::WATCH);
|
||||
eval_pick_fire();
|
||||
}
|
||||
@@ -164,8 +176,6 @@ const char *Sniper::state_name(State s) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Sniper::transition(State new_state) {
|
||||
me->state = new_state;
|
||||
}
|
||||
void Sniper::transition(State new_state) { me->state = new_state; }
|
||||
|
||||
} // namespace bts::spartan::mdp3
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
#include "SimOrderGateway.h"
|
||||
#include "SimSpreader.h"
|
||||
#include "../ISimServices.h"
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/orderman/orders/spreader_orders/SpreaderOrder.h"
|
||||
|
||||
@@ -163,4 +163,4 @@ void SimOrderGateway::route_update_to_spreader(const OrderUpdate& u){
|
||||
Price(t->avg_filled_price)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../../common/mdp3_common.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "../AlgoEvent.h"
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/AlgoEvent.h"
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h"
|
||||
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/IAlternateOrderGateway.h"
|
||||
|
||||
namespace bts { namespace orderman {class SpreaderOrder;}}
|
||||
@@ -36,4 +37,4 @@ public:
|
||||
void route_update_to_spreader(const OrderUpdate&);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
File renamed without changes.
+4
-4
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../../common/mdp3_common.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/back_testing/simulator/IAlgoModel.h"
|
||||
#include "SimOrderGateway.h"
|
||||
#include "../AlgoEvent.h"
|
||||
#include "../IAlgoModel.h"
|
||||
|
||||
namespace bts { namespace api { namespace orderman {class OrderDefinition;}}}
|
||||
namespace bts { namespace api { class OrderDefinition;}}
|
||||
@@ -30,4 +30,4 @@ public:
|
||||
~SimSpreader(){}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
#include <catch/catch.hpp>
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/spreader/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"
|
||||
#include "lib/orderman/orders/spreader_orders/tests/tests_common.h"
|
||||
|
||||
using namespace bts::spartan::mdp3;
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
using namespace api;
|
||||
using namespace spark;
|
||||
|
||||
class TestSimServices : public bts::spartan::mdp3::ISimServices{
|
||||
private:
|
||||
InstrumentMap _instruments;
|
||||
std::vector<Polygon> _polygons;
|
||||
public:
|
||||
OrderChange last_change;
|
||||
size_t n_updates = 0;
|
||||
size_t key;
|
||||
public:
|
||||
TestSimServices ()
|
||||
:
|
||||
key(0){
|
||||
}
|
||||
InstrumentMap const &instruments() const override{
|
||||
bts_assert(false);
|
||||
return _instruments;
|
||||
}
|
||||
|
||||
double pandl() const override {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<Polygon> const&polygons() const override {
|
||||
return _polygons;
|
||||
}
|
||||
|
||||
spartan::mdp3::Book book(MDP3SecurityID,api::Side) const override {
|
||||
return {};
|
||||
}
|
||||
MDP3OrderKey generate_order_key() override{
|
||||
return MDP3OrderKey(key++);
|
||||
}
|
||||
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(){}
|
||||
};
|
||||
|
||||
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(1));
|
||||
//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 =
|
||||
{
|
||||
{103,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);
|
||||
REQUIRE(sim_services.last_change.order.side == api::Side::SIDE_SELL);
|
||||
REQUIRE(sim_services.last_change.order.quantity == bts::spartan::mdp3::MDP3Quantity(1));
|
||||
REQUIRE(
|
||||
bts::securities::is_same(Price(sim_services.last_change.order.price.price()),
|
||||
Price(113)));
|
||||
// send multiple book updates
|
||||
double p = 103;
|
||||
for(int i=0; i<100; i++){
|
||||
asks =
|
||||
{
|
||||
{p,5},
|
||||
{p+1,5},
|
||||
{p+2,5}
|
||||
};
|
||||
auto md_udpate_leg_1 = get_book_update(bids,asks,leg_1.security().id());
|
||||
sim_spreader.on_notification(
|
||||
{
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
md_udpate_leg_1
|
||||
});
|
||||
REQUIRE(sim_services.n_updates==i+1); // +1 because order_place is also counted
|
||||
REQUIRE(sim_services.last_change.order.security ==
|
||||
bts::spartan::mdp3::MDP3SecurityID(2));
|
||||
REQUIRE(sim_services.last_change.order.side == api::Side::SIDE_SELL);
|
||||
REQUIRE(sim_services.last_change.order.quantity == bts::spartan::mdp3::MDP3Quantity(1));
|
||||
REQUIRE(
|
||||
bts::securities::is_same(Price(sim_services.last_change.order.price.price()),
|
||||
Price(p+10)));
|
||||
p++;
|
||||
}
|
||||
//force a quoting leg fill
|
||||
auto q_ord = sim_services.last_change.order;
|
||||
sim_spreader.on_notification(
|
||||
{
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::OrderUpdate{
|
||||
q_ord.key,
|
||||
bts::spartan::mdp3::OrderUpdate::FILLED,
|
||||
q_ord.security,
|
||||
q_ord.side,
|
||||
bts::spartan::mdp3::MDP3Quantity(1),
|
||||
md::IntPrice_from_double<7>(202)
|
||||
}
|
||||
});
|
||||
// the above should have sent a hedge order, verify that
|
||||
auto hedge_ord = sim_services.last_change.order;
|
||||
REQUIRE(hedge_ord.security == bts::spartan::mdp3::MDP3SecurityID(1));
|
||||
REQUIRE(hedge_ord.side == api::Side::SIDE_BUY);
|
||||
REQUIRE(hedge_ord.quantity == bts::spartan::mdp3::MDP3Quantity(1));
|
||||
REQUIRE(
|
||||
bts::securities::is_same(Price(hedge_ord.price.price()),
|
||||
Price(192)));
|
||||
REQUIRE(sim_spreader.spreader().test_only_func_spreader_state() !=
|
||||
SpreaderState::END);
|
||||
sim_spreader.on_time(bts::spartan::mdp3::MDP3TimeStamp());
|
||||
// send the hedge fill and it should go to end state
|
||||
sim_spreader.on_notification(
|
||||
{
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::MDP3TimeStamp{},
|
||||
bts::spartan::mdp3::OrderUpdate{
|
||||
hedge_ord.key,
|
||||
bts::spartan::mdp3::OrderUpdate::FILLED,
|
||||
hedge_ord.security,
|
||||
hedge_ord.side,
|
||||
bts::spartan::mdp3::MDP3Quantity(1),
|
||||
md::IntPrice_from_double<7>(192)
|
||||
}
|
||||
});
|
||||
REQUIRE(sim_spreader.spreader().test_only_func_spreader_state() ==
|
||||
SpreaderState::END);
|
||||
}
|
||||
}
|
||||
|
||||
class RV
|
||||
{
|
||||
public:
|
||||
int i;
|
||||
RV(int i_arg)
|
||||
:
|
||||
i(i_arg){
|
||||
}
|
||||
RV()=delete;
|
||||
RV(const RV&&)=delete;
|
||||
RV (RV&& other){
|
||||
i=other.i;
|
||||
other.i=0;
|
||||
}
|
||||
RV& operator=(const RV&)=delete;
|
||||
RV& operator=(RV &&src){
|
||||
i=src.i;
|
||||
src.i=0;
|
||||
return *this;
|
||||
}
|
||||
int foo (double d1, double d2){
|
||||
return static_cast<int>(d1+d2);
|
||||
}
|
||||
};
|
||||
class TTemp{
|
||||
};
|
||||
class TInterface{
|
||||
virtual void foo() = 0;
|
||||
};
|
||||
template <class T, size_t N>
|
||||
class TestContainer : public TInterface{
|
||||
private:
|
||||
T _t;
|
||||
std::array<T,N> _at;
|
||||
public:
|
||||
void add (T &t){
|
||||
_at[0] = t;
|
||||
}
|
||||
T get() {
|
||||
return _t;
|
||||
}
|
||||
void foo () override{
|
||||
}
|
||||
};
|
||||
class RVUser{
|
||||
public:
|
||||
RV rv;
|
||||
RVUser(RV& rv_arg)
|
||||
: rv(std::move(rv_arg))
|
||||
{
|
||||
}
|
||||
};
|
||||
TEST_CASE ("scratch_pad", "[spreader]"){
|
||||
int (RV::*f_ptr)(double, double) = &RV::foo;
|
||||
char* f_addr = (char*&)f_ptr;
|
||||
TInterface* r = nullptr;
|
||||
int n = gen_rand(0.0, 10.0);
|
||||
if (n <= 5)
|
||||
r = new TestContainer<TTemp,5>();
|
||||
else
|
||||
r = new TestContainer<TTemp,10>();
|
||||
REQUIRE(r != nullptr);
|
||||
RV rv_1(100);
|
||||
RVUser rvuser_1(rv_1);
|
||||
REQUIRE(rv_1.i==0);
|
||||
REQUIRE(rvuser_1.rv.i==100);
|
||||
}
|
||||
TEST_CASE ("scratch_pad_2", "[spreader]"){
|
||||
time_t rawtime;
|
||||
time (&rawtime);
|
||||
struct tm* ptm = gmtime (&rawtime);
|
||||
LOG(OrderMan, Info, "hrs:{} mins:{} sec:{}", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
|
||||
}
|
||||
// vector insert //////////////////////////////////////
|
||||
///////////////////////////////////////////////////////
|
||||
class Elem{
|
||||
public:
|
||||
int i;
|
||||
Elem()
|
||||
:
|
||||
i(-1)
|
||||
{}
|
||||
Elem (int i_arg)
|
||||
:
|
||||
i(i_arg)
|
||||
{}
|
||||
Elem(const Elem& other){
|
||||
i = other.i;
|
||||
}
|
||||
Elem (Elem&& other){
|
||||
i=other.i;
|
||||
}
|
||||
Elem& operator=(const Elem& other){
|
||||
i=other.i;
|
||||
return *this;
|
||||
}
|
||||
Elem& operator=(Elem &&src){
|
||||
i = src.i;
|
||||
return *this;
|
||||
}
|
||||
bool operator==(const Elem& e) const {return i == e.i;}
|
||||
bool operator!=(const Elem& e) const {return i != e.i;}
|
||||
bool operator<(const Elem& e) const {return i < e.i;}
|
||||
bool operator>(const Elem& e) const {return i > e.i;}
|
||||
};
|
||||
TEST_CASE ("scratch_pad_3", "[spreader]"){
|
||||
std::vector<Elem> v;
|
||||
for(int i=1;i<10;i++)
|
||||
v.emplace_back(i);
|
||||
Elem e(0);
|
||||
// move op= is called when inserting or erasing
|
||||
v.insert(v.begin()+2,e);
|
||||
v.erase(v.begin()+2);
|
||||
std::random_shuffle(v.begin(),v.end());
|
||||
// flat set
|
||||
boost::container::flat_set<Elem> fs;
|
||||
for(int i=0;i<20;i+=2){
|
||||
Elem e(i);
|
||||
fs.insert(e);
|
||||
}
|
||||
Elem e9(9);
|
||||
// move op is called when inserting
|
||||
fs.insert(e9);
|
||||
for (const auto& e : fs)
|
||||
std::cout << e.i << std::endl;
|
||||
// vector assignment vs move assignment
|
||||
std::vector<Elem> vs;
|
||||
int n_elems = 10;
|
||||
for(int i=1;i<n_elems;i++)
|
||||
vs.emplace_back(i);
|
||||
std::vector<Elem> vt;
|
||||
// new elems are cresated in vt from the elem's in vs,
|
||||
// with their copy cons
|
||||
vt = vs;
|
||||
std::vector<Elem> vt_2;
|
||||
vt_2 = std::move(vs);
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -45,5 +45,11 @@ void Matcher::match(ChangeSrc,
|
||||
}
|
||||
}
|
||||
|
||||
Book Matcher::book(MDP3SecurityID id, api::Side s) {
|
||||
auto it = _matcher_map.find(id);
|
||||
bts_assert(it != _matcher_map.end());
|
||||
return it->second.matcher.book(s);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ public:
|
||||
void match(ChangeSrc,
|
||||
std::vector<OrderChange> const&,
|
||||
std::vector<MatchEffect> *);
|
||||
|
||||
Book book(MDP3SecurityID, api::Side);
|
||||
};
|
||||
|
||||
}
|
||||
File renamed without changes.
@@ -65,15 +65,15 @@ void ExchangeSim::prepare_run(std::vector<IAlgoModel *> const &algos,
|
||||
|
||||
// Init gateways, add them to the simulator
|
||||
for (auto &i : me->inputs) {
|
||||
me->run_state->gateways.emplace_back(i.instruments, options);
|
||||
me->run_state->simulator.add_agent(me->run_state->gateways.back());
|
||||
auto &gw = me->run_state->gateways.emplace_back(i.instruments, options);
|
||||
me->run_state->simulator.add_agent(gw);
|
||||
}
|
||||
|
||||
// Init algos, add them to simulator
|
||||
for (auto algo : algos) {
|
||||
auto &algo_agent =
|
||||
me->run_state->algos.emplace_back(me->all_definitions, me->all_polygons,
|
||||
&me->max_order_id, options, *algo);
|
||||
auto &algo_agent = me->run_state->algos.emplace_back(
|
||||
me->all_definitions, me->all_polygons, *this, &me->max_order_id,
|
||||
options, *algo);
|
||||
me->run_state->simulator.add_agent(algo_agent);
|
||||
algo_agent.init();
|
||||
}
|
||||
@@ -115,4 +115,14 @@ void ExchangeSim::do_work() {
|
||||
me->run_state->simulator.run_once();
|
||||
}
|
||||
|
||||
Book ExchangeSim::book(MDP3SecurityID id, api::Side s) {
|
||||
// route it to appropriate gateway
|
||||
bts_assert(me->run_state);
|
||||
|
||||
for (size_t i = 0; i < me->inputs.size(); i++)
|
||||
if (me->inputs[i].instruments.count(id))
|
||||
return me->run_state->gateways[i].book(id, s);
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
} // namespace bts::spartan::mdp3
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "SimOptions.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "IExchangeServices.h"
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
@@ -12,7 +13,7 @@ struct SimInput;
|
||||
class IAlgoModel;
|
||||
struct AlgoRecord;
|
||||
|
||||
class ExchangeSim {
|
||||
class ExchangeSim : public IExchangeServices{
|
||||
public:
|
||||
// Cache a set of recorded inputs to use for later running multiple
|
||||
// algos.
|
||||
@@ -32,8 +33,10 @@ class ExchangeSim {
|
||||
struct PImpl;
|
||||
struct RunState;
|
||||
|
||||
void prepare_run(std::vector<IAlgoModel *> const &, const SimOptions &);
|
||||
// From IExchangeServices
|
||||
Book book(MDP3SecurityID, api::Side) override;
|
||||
|
||||
void prepare_run(std::vector<IAlgoModel *> const &, const SimOptions &);
|
||||
bool has_work() const;
|
||||
void do_work();
|
||||
std::unique_ptr<PImpl> me;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#include "IExchangeServices.h"
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include "../../common/mdp3_common.h"
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
class IExchangeServices {
|
||||
public:
|
||||
virtual Book book(MDP3SecurityID, api::Side) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "ISimServices.h"
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
ISimServices::~ISimServices() {}
|
||||
}
|
||||
@@ -10,11 +10,13 @@ class InstrumentDefinition;
|
||||
|
||||
class ISimServices {
|
||||
public:
|
||||
virtual ~ISimServices();
|
||||
virtual ~ISimServices() {}
|
||||
virtual std::vector<Polygon> const &polygons() const = 0;
|
||||
virtual Book book(MDP3SecurityID,api::Side) const = 0 ;
|
||||
virtual InstrumentMap const &instruments() const = 0;
|
||||
virtual MDP3OrderKey generate_order_key() = 0;
|
||||
virtual MDP3TimeStamp now() const = 0;
|
||||
virtual double pandl() const = 0;
|
||||
virtual void wait(uint64_t ns) = 0;
|
||||
virtual void send(OrderChange const &, uint64_t propagation_delay = 0) = 0;
|
||||
};
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
#include "AlgoAgent.h"
|
||||
#include "../IAlgoModel.h"
|
||||
#include "../IExchangeServices.h"
|
||||
#include "../SimOptions.h"
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h"
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
struct AlgoAgent::PImpl {
|
||||
InstrumentMap const &instruments;
|
||||
IExchangeServices &services;
|
||||
std::vector<Polygon> const &polygons;
|
||||
MDP3OrderKey *last_order_key;
|
||||
SimOptions options;
|
||||
@@ -16,9 +19,10 @@ struct AlgoAgent::PImpl {
|
||||
|
||||
AlgoAgent::AlgoAgent(InstrumentMap const &instruments,
|
||||
std::vector<Polygon> const &polygons,
|
||||
MDP3OrderKey *next_order_key, SimOptions const &options,
|
||||
IAlgoModel &algo)
|
||||
: me(new PImpl{instruments, polygons, next_order_key, options, algo}) {}
|
||||
IExchangeServices &services, MDP3OrderKey *next_order_key,
|
||||
SimOptions const &options, IAlgoModel &algo)
|
||||
: me(new PImpl{instruments, services, polygons, next_order_key, options,
|
||||
algo}) {}
|
||||
|
||||
AlgoAgent::AlgoAgent(AlgoAgent &&src) : me() { operator=(std::move(src)); }
|
||||
|
||||
@@ -30,13 +34,57 @@ AlgoAgent &AlgoAgent::operator=(AlgoAgent &&src) {
|
||||
void AlgoAgent::init() { me->algo.init(*this); }
|
||||
|
||||
double AlgoAgent::pandl() const {
|
||||
double result = 0;
|
||||
for (auto f : me->my_fills) {
|
||||
auto flow = (f.last_fills.value() * f.last_price.price());
|
||||
if (f.side == api::SIDE_BUY)
|
||||
result -= flow;
|
||||
|
||||
auto mkt_to_cash_flow = [](api::Side s) {
|
||||
if (s == api::SIDE_BUY)
|
||||
return -1;
|
||||
else
|
||||
result += flow;
|
||||
return 1;
|
||||
};
|
||||
|
||||
struct Pos {
|
||||
double cash = 0;
|
||||
uint32_t contracts;
|
||||
};
|
||||
std::unordered_map<MDP3SecurityID, Pos> positions;
|
||||
|
||||
// Calculate positions
|
||||
for (auto f : me->my_fills) {
|
||||
auto &this_pos = positions[f.security_id];
|
||||
|
||||
auto cash = (f.last_fills.value() * f.last_price.price());
|
||||
auto flow = mkt_to_cash_flow(f.side);
|
||||
|
||||
this_pos.cash += flow * cash;
|
||||
this_pos.contracts += -flow * cash;
|
||||
}
|
||||
|
||||
// Liquidate remainder contracts
|
||||
for (auto &[id, this_pos] : positions) {
|
||||
auto mkt_action = api::SIDE_UNDEFINED;
|
||||
|
||||
if (this_pos.contracts > 0)
|
||||
mkt_action = api::SIDE_SELL;
|
||||
else
|
||||
mkt_action = api::SIDE_BUY;
|
||||
auto &mkt = book(id, securities::other_side(mkt_action))[0];
|
||||
bts_assert(mkt.quantity.value());
|
||||
auto cash = mkt.price.price();
|
||||
auto flow = mkt_to_cash_flow(mkt_action);
|
||||
this_pos.cash += flow * cash;
|
||||
this_pos.contracts += -flow * cash;
|
||||
}
|
||||
|
||||
// Convert to human terms, sum across all contracts
|
||||
double result = 0;
|
||||
for (auto &[id, this_pos] : positions) {
|
||||
// auto it = me->instruments.find(id);
|
||||
// bts_assert(it != me->instruments.end());
|
||||
// auto display_factor = it->second->display_factor();
|
||||
auto display_factor = 1.0;
|
||||
(void) id;
|
||||
bts_assert(this_pos.contracts == 0);
|
||||
result += (display_factor * this_pos.cash);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -73,6 +121,10 @@ MDP3TimeStamp AlgoAgent::now() const { return MDP3TimeStamp{time()}; }
|
||||
|
||||
void AlgoAgent::wait(uint64_t ns) { forward_clock_by(ns); }
|
||||
|
||||
Book AlgoAgent::book(MDP3SecurityID id, api::Side s) const {
|
||||
return me->services.book(id, s);
|
||||
}
|
||||
|
||||
void AlgoAgent::send(OrderChange const &change, uint64_t propagation_delay) {
|
||||
me->my_orders.insert(change.order.key);
|
||||
emit_delayed_event<AlgoActionEvent>(
|
||||
|
||||
@@ -10,22 +10,25 @@ namespace bts::spartan::mdp3 {
|
||||
|
||||
struct SimOptions;
|
||||
class InstrumentDefinition;
|
||||
class IExchangeServices;
|
||||
|
||||
class AlgoAgent : ISimServices, public sim::AgentBase {
|
||||
public:
|
||||
AlgoAgent(InstrumentMap const &, std::vector<Polygon> const &,
|
||||
MDP3OrderKey *next_order, SimOptions const &, IAlgoModel &);
|
||||
IExchangeServices &, MDP3OrderKey *next_order, SimOptions const &,
|
||||
IAlgoModel &);
|
||||
AlgoAgent(AlgoAgent &&);
|
||||
~AlgoAgent();
|
||||
AlgoAgent &operator=(AlgoAgent &&);
|
||||
double pandl() const;
|
||||
void init() override;
|
||||
void handle(sim::Event const &) override;
|
||||
double pandl() const override;
|
||||
|
||||
private:
|
||||
std::vector<Polygon> const &polygons() const override;
|
||||
InstrumentMap const &instruments() const override;
|
||||
MDP3OrderKey generate_order_key() override;
|
||||
Book book(MDP3SecurityID, api::Side) const override;
|
||||
MDP3TimeStamp now() const override;
|
||||
void wait(uint64_t ns) override;
|
||||
void send(OrderChange const &, uint64_t propagation_delay) override;
|
||||
|
||||
@@ -55,6 +55,10 @@ void GatewayAgent::handle(const sim::Event &ev) {
|
||||
_handle(*aa);
|
||||
}
|
||||
|
||||
Book GatewayAgent::book(MDP3SecurityID id, api::Side s) {
|
||||
return me->matcher.book(id,s);
|
||||
}
|
||||
|
||||
// Handle a recorded order change, that is an order change we got from
|
||||
// a pcap recording of the market data
|
||||
void GatewayAgent::_handle(ExchangeTransactionEvent const &e) {
|
||||
|
||||
@@ -21,6 +21,7 @@ class GatewayAgent : public sim::AgentBase {
|
||||
|
||||
~GatewayAgent();
|
||||
void handle(sim::Event const &) override;
|
||||
Book book(MDP3SecurityID, api::Side);
|
||||
|
||||
private:
|
||||
void _handle(ExchangeTransactionEvent const &);
|
||||
|
||||
@@ -104,7 +104,10 @@ class InstrumentDefinition {
|
||||
// receiving it as a const&. This is because this particular
|
||||
// accessor actually mutates the message (though not the
|
||||
// payload).
|
||||
return get([](auto m) { return m.displayFactor().mantissa(); });
|
||||
return get([](auto m) {
|
||||
return md::IntPrice(m.displayFactor().mantissa(),
|
||||
m.displayFactor().exponent()).price();
|
||||
});
|
||||
}
|
||||
|
||||
bool implied_matching() const {
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
#!/bin/bash
|
||||
DIR="$(dirname "$(readlink -f "$0")")"
|
||||
SRC_ROOT=$DIR/../../../../../../../../
|
||||
BUILD_ROOT=`readlink -f $SRC_ROOT/build/ninja_release/server`
|
||||
|
||||
IN=$1
|
||||
PIPE=$(mktemp -u /tmp/spartan-compress-pipe.XXXXXX.pcap)
|
||||
mkfifo $PIPE
|
||||
xz -zc -T0 $PIPE > $IN.xz & (cd $BUILD_ROOT && ./sparkd spartan compress $IN $PIPE)
|
||||
wait
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
#!/bin/bash
|
||||
if [ "$#" -ne 1 ]; then
|
||||
echo " download_pcaps.sh <output_directory>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
AWS_ACCESS_KEY_ID=AKIAIFIGOOWGBLEMO3PQ AWS_SECRET_ACCESS_KEY=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 AWS_REGION=us-east-1 aws s3 sync s3://spark-recordings $1
|
||||
@@ -1,19 +0,0 @@
|
||||
#!/bin/bash
|
||||
DIR="$(dirname "$(readlink -f "$0")")"
|
||||
SRC_ROOT=$DIR/../../../../../../../../
|
||||
BUILD_ROOT=`readlink -f $SRC_ROOT/build/ninja_release/server`
|
||||
|
||||
IN=$1
|
||||
OUT=$2
|
||||
PIPE=$(mktemp -u /tmp/spartan-compress-pipe.XXXXXX.pcap)
|
||||
mkfifo $PIPE
|
||||
xz -dc -T0 $IN > $PIPE &
|
||||
(cd $BUILD_ROOT && ./sparkd spartan csv $PIPE $OUT)
|
||||
wait
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#!/bin/bash
|
||||
DIR="$(dirname "$(readlink -f "$0")")"
|
||||
SRC_ROOT=$DIR/../../../../../../../../
|
||||
BUILD_ROOT=`readlink -f $SRC_ROOT/build/ninja_release/server`
|
||||
|
||||
IN=$2
|
||||
PIPE=$(mktemp -u /tmp/spartan-pipe.XXXXXX.pcap)
|
||||
mkfifo $PIPE
|
||||
xz -dc -T0 $IN > $PIPE &
|
||||
(cd $BUILD_ROOT && ./sparkd spartan $1 $PIPE ${@:3} )
|
||||
wait
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user