Removing old simulator, part 1.

This commit is contained in:
Pedro Pinto committed 2020-12-09 14:26:10 -05:00
1 parent 0d5b3d9018
commit 443dacaefb
38 files changed
+1222 -3484

No files matched your search

@@ -372,7 +372,7 @@ bool check_for_cumm_price_qty(const SniperPolygon& p,
Quantity cumm_qty){
if(cumm_qty.value()==0)
return true;
double price_at_cumm_qty;
double price_at_cumm_qty = 0;
for (auto& l : p.legs){
Price l_comp_price; Quantity l_comp_qty;
api::Side cross_side = get_leg_cross_market_side(trade_side,l);
+1 -1
View File
@@ -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/AlgoParamHelper.cpp;cmds/spartan/md/mdp3/back_testing/algos/ISim.cpp;cmds/spartan/md/mdp3/back_testing/algos/PolyObserver.cpp;cmds/spartan/md/mdp3/back_testing/algos/PolyObserver2.cpp;cmds/spartan/md/mdp3/back_testing/algos/Sniper.cpp;cmds/spartan/md/mdp3/back_testing/algos/Sniper2.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/FifoImpliedSecurityMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoMatcherContext.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoMergedSecurityMatcher.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/MatchingCommon.cpp;cmds/spartan/md/mdp3/back_testing/matcher/fill_depot.cpp;cmds/spartan/md/mdp3/back_testing/matcher/iceberg_helper.cpp;cmds/spartan/md/mdp3/back_testing/matcher/tests/matcher_tests.cpp;cmds/spartan/md/mdp3/back_testing/matcher/validation/iceberg_file_cache.cpp;cmds/spartan/md/mdp3/back_testing/matcher/validation/matcher_validation_observer.cpp;cmds/spartan/md/mdp3/back_testing/orchestrator/SimOrchestrator.cpp;cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.proto;cmds/spartan/md/mdp3/back_testing/orchestrator/run_sim.cpp;cmds/spartan/md/mdp3/back_testing/pcap_db/PCapDB.cpp;cmds/spartan/md/mdp3/back_testing/sim_db/SimDB.cpp;cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.cpp;cmds/spartan/md/mdp3/back_testing/simulator/Bridge/BeforeLaunchBridge.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/MDP3EventGenerator.cpp;cmds/spartan/md/mdp3/back_testing/simulator/PCapEventSource.cpp;cmds/spartan/md/mdp3/back_testing/simulator/PandL.cpp;cmds/spartan/md/mdp3/back_testing/simulator/Sim.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/Simulator2.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/InstrumentDefinition.cpp;cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp;cmds/spartan/md/mdp3/common/mdp3_common.cpp;cmds/spartan/md/mdp3/common/tests/book_merging_tests.cpp;cmds/spartan/md/mdp3/feed_control/IFeedController.cpp;cmds/spartan/md/mdp3/handler/ChannelHandler.cpp;cmds/spartan/md/mdp3/implied/implied_market.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/book_utils.cpp;cmds/spartan/md/mdp3/utils/mdp3_utils.cpp;cmds/spartan/md/mdp3/utils/order_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/PolygonOpportunityFinder.cpp;cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.cpp;cmds/spartan/md/mdp3/utils/queries/TransactionDeducer.cpp;cmds/spartan/md/mdp3/utils/security_comp.cpp;cmds/spartan/md/mdp3/utils/security_parser.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;cmds/spartan/util/action_summarizer.cpp;cmds/spartan/util/args_parser.cpp;Line truncated
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/AlgoParamHelper.cpp;cmds/spartan/md/mdp3/back_testing/algos/ISim.cpp;cmds/spartan/md/mdp3/back_testing/algos/PolyObserver2.cpp;cmds/spartan/md/mdp3/back_testing/algos/Sniper2.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/FifoImpliedSecurityMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoMatcherContext.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoMergedSecurityMatcher.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/MatchingCommon.cpp;cmds/spartan/md/mdp3/back_testing/matcher/fill_depot.cpp;cmds/spartan/md/mdp3/back_testing/matcher/iceberg_helper.cpp;cmds/spartan/md/mdp3/back_testing/matcher/tests/matcher_tests.cpp;cmds/spartan/md/mdp3/back_testing/matcher/validation/iceberg_file_cache.cpp;cmds/spartan/md/mdp3/back_testing/matcher/validation/matcher_validation_observer.cpp;cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.proto;cmds/spartan/md/mdp3/back_testing/orchestrator/run_sim.cpp;cmds/spartan/md/mdp3/back_testing/sim_db/SimDB.cpp;cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.cpp;cmds/spartan/md/mdp3/back_testing/simulator/Bridge/BeforeLaunchBridge.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/MDP3EventGenerator.cpp;cmds/spartan/md/mdp3/back_testing/simulator/PCapEventSource.cpp;cmds/spartan/md/mdp3/back_testing/simulator/PandL.cpp;cmds/spartan/md/mdp3/back_testing/simulator/Sim.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/events/SimEvents.cpp;cmds/spartan/md/mdp3/common/InstrumentDefinition.cpp;cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp;cmds/spartan/md/mdp3/common/mdp3_common.cpp;cmds/spartan/md/mdp3/common/tests/book_merging_tests.cpp;cmds/spartan/md/mdp3/feed_control/IFeedController.cpp;cmds/spartan/md/mdp3/handler/ChannelHandler.cpp;cmds/spartan/md/mdp3/implied/implied_market.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/book_utils.cpp;cmds/spartan/md/mdp3/utils/mdp3_utils.cpp;cmds/spartan/md/mdp3/utils/order_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/PolygonOpportunityFinder.cpp;cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.cpp;cmds/spartan/md/mdp3/utils/queries/TransactionDeducer.cpp;cmds/spartan/md/mdp3/utils/security_comp.cpp;cmds/spartan/md/mdp3/utils/security_parser.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;cmds/spartan/util/action_summarizer.cpp;cmds/spartan/util/args_parser.cpp;cmds/spartan/util/sql/adapters/polygon_defs.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/Line truncated
+1 -69
View File
@@ -5,7 +5,6 @@
#include "lib/utils/datetime.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/pcap_db/PCapDB.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/sim_db/SimDB.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/AlgoEvent.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/PCapEventSource.h"
@@ -20,80 +19,13 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <regex>
/* 647354939008 */
namespace bts {
namespace cmd {
using namespace spartan::mdp3;
struct MatcherValidator : IChannelObserver {
MatcherValidator(IcebergMap const &m) : ib_map(m) {}
IcebergMap ib_map;
void
on_recovery_complete(std::vector<InstrumentSnapshot const *> const &snaps) {
validate(0, snaps);
}
void on_real_time_event(MDP3Event const &ev) {
if (ev.event_id > 89000)
validate(ev.event_id, ev.affected_securities);
}
template <class V> void validate(size_t evid, V const &snaps) {
for (auto s : snaps) {
if (s->ready_to_trade()) {
MergedMatcher m;
m.add_def(s->definition());
SmallOrderChangeVec matcher_input;
for (auto &p : s->orders()) {
auto it = ib_map.find(p.first);
if (it != ib_map.end()) {
OrderChange ch{p.second, p.second, OrderChange::added};
iceberg_helper::add_iceberg_annotation(ch.order, ch.change, it->second, evid, true);
if (ch.order.key.value() == 647354939008) {
std::cout << evid << " nasty IB: " << ch << std::endl;
}
matcher_input.push_back(ch);
} else
matcher_input.push_back(
OrderChange{p.second, p.second, OrderChange::added});
}
std::vector<MergedMatcher::MatchEffect> effects;
m.match(MergedMatcher::SIMULATOR_INPUT, matcher_input, &effects);
if (s->book(api::SIDE_BUY) !=
m.book(s->security_id(), api::SIDE_BUY)) {
std::cout << "Mismatch Event id: " << evid << " BUY SIDE\n"
<< s->book(api::SIDE_BUY) << "\n"
<< "-----------------------------------------------"
"------\n"
<< m.book(s->security_id(), api::SIDE_BUY) << "\n";
bts_assert(false);
}
if (s->book(api::SIDE_SELL) !=
m.book(s->security_id(), api::SIDE_SELL)) {
std::cout << "Mismatch on Event id: " << evid << " SELL SIDE\n"
<< s->book(api::SIDE_SELL) << "\n"
<< "-----------------------------------------------"
"------\n"
<< m.book(s->security_id(), api::SIDE_SELL) << "\n";
bts_assert(false);
}
}
}
}
};
int QuickTest::execute(std::vector<std::string> const &args) {
IcebergFinder ice;
observe_channel(args[0], ice);
MatcherValidator val{ice.result};
observe_channel(args[0], val);
int QuickTest::execute(std::vector<std::string> const &) {
return 0;
}
+2 -4
View File
@@ -13,13 +13,11 @@
#include "lib/jetfix/codec/f_toa.h"
#include "lib/orderman/orderman_utils/Formatter.h"
#include "lib/utils/PBUtils.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/Sniper.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/validation/iceberg_file_cache.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/validation/matcher_validation_observer.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/run_sim.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimOrchestrator.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/ExchangeSim.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/SimInput.h"
@@ -351,9 +349,9 @@ std::map<std::string, std::function<int(std::vector<std::string> const &)>>
return 0;
}},
{"sim2",
{"sim",
[](auto const &args) {
run_sim(args[1]);
run_sim(args[1]);
return 0;
}},
@@ -1,191 +0,0 @@
#include "PolyObserver.h"
#include "../../utils/mdp3_utils.h"
#include "../simulator/ISimServices.h"
#include "lib/domain_model/Security.h"
#include "lib/logging/Logger.h"
#include "lib/os/os.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/PandL.h"
#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h"
#include <fstream>
#include <queue>
#include <sstream>
namespace bts::spartan::mdp3 {
static auto constexpr Worthless = -1.0;
struct PolyObserver::Target {
Polygon const polygon;
std::string const descriptor;
std::string const product;
double value = Worthless;
MDP3Quantity quantity = MDP3Quantity{0};
void mark_as_worthless() { value = Worthless; }
bool has_value() const { return value > securities::market_epsilon(); }
};
struct PolyObserver::Opportunity {
Target *target;
AlgoEventID event_id;
double value;
MDP3Quantity quantity;
MDP3TimeStamp transact_time;
MDP3TimeStamp observable_time;
};
struct PolyObserver::PImpl {
ISimServices *sim = nullptr;
std::unordered_map<MDP3SecurityID, MBPUpdate> books;
std::vector<std::unique_ptr<Target>> targets;
std::optional<Opportunity> current_opportunity;
AlgoEventID event_id;
MDP3TimeStamp transact_time;
MDP3TimeStamp observable_time;
};
PolyObserver::PolyObserver() : me(new PImpl()) {}
PolyObserver::~PolyObserver() {}
void PolyObserver::init(ISimServices &s, protos::SimRunOptions const &) {
me->sim = &s;
me->books.clear();
me->targets.clear();
me->event_id = {0, 0};
me->transact_time = MDP3TimeStamp{0};
me->observable_time = MDP3TimeStamp{0};
for (auto &p : s.polygons()) {
auto buy_t =
Target{p.second, polygon_descriptor(p.second, me->sim->instruments()),
polygon_product(p.second, me->sim->instruments())};
auto sell_t =
Target{flip(p.second),
polygon_descriptor(flip(p.second), me->sim->instruments()),
polygon_product(p.second, me->sim->instruments())};
me->targets.emplace_back(std::make_unique<Target>(buy_t));
me->targets.emplace_back(std::make_unique<Target>(sell_t));
}
}
void PolyObserver::on_notification(AlgoNotification const &n) {
me->event_id = n.header.event_id;
me->transact_time = n.header.transact_time;
me->observable_time = n.header.observable_time;
std::visit(*this, n.event);
}
void PolyObserver::eval() {
for (auto &t : me->targets) {
t->value = 0;
int qty = std::numeric_limits<int>::max();
for (auto &l : t->polygon.parts) {
auto mkt_side = securities::other_side(side_from_ratio(l.ratio));
auto &mkt = me->books[l.sec_id].mbp_side(mkt_side);
auto this_qty = mkt[0].quantity.value();
auto this_ratio = this_qty / std::abs(l.ratio);
if (this_ratio < qty)
qty = this_ratio;
if (qty == 0) {
t->mark_as_worthless();
break;
} else {
auto cash = me->sim->get_sec(l.sec_id).to_cash(mkt[0].price);
t->quantity.value() = qty;
t->value += -l.ratio * cash;
}
}
}
}
void PolyObserver::rank() {
std::sort(me->targets.begin(), me->targets.end(),
[](std::unique_ptr<Target> const &ptr1,
std::unique_ptr<Target> const &ptr2) {
auto val1 = ptr1->value * ptr1->quantity.value();
auto val2 = ptr2->value * ptr2->quantity.value();
if (std::abs(val1 - val2) > securities::market_epsilon())
return val1 > val2;
else
return ptr1->polygon.parts.size() <
ptr2->polygon.parts.size();
});
}
void PolyObserver::try_start_new_opportunity() {
bts_assert(!me->current_opportunity);
auto best = me->targets.begin()->get();
if (best->has_value()) {
// We have an opportunity lets record it
me->current_opportunity =
Opportunity{best, me->event_id, best->value,
best->quantity, me->transact_time, me->observable_time};
me->sim->log("opportunity_begin", best->descriptor, best->value,
best->quantity.value(), 0, 0,
us_diff(me->observable_time,me->transact_time),
1, me->event_id.first, me->event_id.first);
}
}
void PolyObserver::analyze() {
if (me->current_opportunity) {
auto &opportunity = *me->current_opportunity;
auto &target = *opportunity.target;
if (target.has_value()) {
// Add value and still has value, opportunity is still ongoing
me->sim->log(
"opportunity_continue", target.descriptor, target.value,
target.quantity.value(),
us_diff(me->transact_time,opportunity.observable_time),
us_diff(me->transact_time,opportunity.transact_time),
us_diff(opportunity.observable_time,opportunity.transact_time),
me->event_id.first - opportunity.event_id.first,
opportunity.event_id.first, me->event_id.first);
} else {
// Had value but not anymore, opportunity is concluded
me->sim->log(
"opportunity_end", target.descriptor, opportunity.value,
opportunity.quantity.value(),
us_diff(me->transact_time,opportunity.observable_time),
us_diff(me->transact_time,opportunity.transact_time),
us_diff(opportunity.observable_time,opportunity.transact_time),
me->event_id.first - opportunity.event_id.first,
opportunity.event_id.first, me->event_id.first-1);
me->current_opportunity.reset();
try_start_new_opportunity();
}
} else {
// We did not have an opportunity before. Maybe we do now?
try_start_new_opportunity();
}
}
std::pair<md::IntPrice, MDP3Quantity> available(Book const &b) {
std::pair<md::IntPrice, MDP3Quantity> result;
result.first = md::IntPrice{};
result.second = MDP3Quantity{0};
for (size_t i = 0; i < b.size(); i++) {
if (b[i].quantity.value() == 0)
break;
else {
result.first = b[i].price;
result.second.value() += b[i].quantity.value();
}
}
return result;
}
void PolyObserver::operator()(const MBPUpdate &mbp) {
me->books[mbp.security_id] = mbp;
eval();
rank();
analyze();
}
void PolyObserver::operator()(const OrderUpdate &) {}
} // namespace bts::spartan::mdp3
@@ -1,34 +0,0 @@
#pragma once
#include <memory>
#include <optional>
#include "../simulator/IAlgoModel.h"
namespace bts::spartan::mdp3 {
struct Polygon;
class PolyObserver : public IAlgoModel {
public:
PolyObserver();
~PolyObserver();
void init(ISimServices &, protos::SimRunOptions const&) override;
void on_notification(AlgoNotification const&) override;
void operator()(const MBPUpdate &);
void operator()(const OrderUpdate &);
private:
struct PImpl;
struct Target;
struct Opportunity;
struct DependentTargets;
void eval();
void rank();
void analyze();
void try_start_new_opportunity();
std::unique_ptr<PImpl> me;
};
}
@@ -1,448 +0,0 @@
#include "Sniper.h"
#include "../../utils/mdp3_utils.h"
#include "../simulator/ISimServices.h"
#include "lib/domain_model/Security.h"
#include "lib/logging/Logger.h"
#include "lib/os/os.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/PandL.h"
#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h"
#include <queue>
#include <sstream>
namespace bts::spartan::mdp3 {
enum class Sniper::State { WAIT_OPPORTUNITY, WAIT_FILLS, WAIT_CLEAR };
struct Sniper::Target {
constexpr static double Worthless = -1.0;
Polygon const polygon;
std::string const descriptor;
std::string const product;
uint32_t const n_abs_contracts;
double tick_size;
double value = Worthless;
MDP3Quantity quantity = MDP3Quantity{0};
void mark_as_worthless() { value = Worthless; }
bool has_value(double v) const { return value > v; }
int value_in_ticks() const { return value / tick_size; }
};
struct Sniper::WorkingOrder {
SmallOrderChange order;
std::vector<OrderUpdate> fills;
};
struct Sniper::Shot {
Target &target;
AlgoEventID observed_event_id;
MDP3TimeStamp observed_ts;
double value_at_observation_ts;
MDP3Quantity quantity;
bool forced_completion;
std::unordered_map<MDP3OrderKey, WorkingOrder> working;
};
struct Sniper::DependentTargets {
MBPUpdate book;
std::vector<Target *> dependencies;
};
struct Sniper::PImpl {
ISimServices *sim = nullptr;
protos::SimRunOptions options;
int min_orders_at_top;
int max_qty;
int min_depth;
bool affect_books;
size_t max_events;
double min_value;
size_t max_transaction_latency;
bool double_dip;
size_t ms_to_work_shot;
State state = State::WAIT_OPPORTUNITY;
std::unordered_map<MDP3SecurityID, DependentTargets> books;
std::vector<std::unique_ptr<Target>> targets;
std::optional<Shot> current_shot;
MDP3TimeStamp transact_time;
MDP3TimeStamp observable_time;
AlgoEventID current_event;
size_t streak_id = 0;
};
Sniper::Sniper() : me(new PImpl()) {}
Sniper::~Sniper() {}
void Sniper::init(ISimServices &s, protos::SimRunOptions const &options) {
me->sim = &s;
me->options = options;
me->min_orders_at_top = param_or_default(options, "MinOrdersAtTop", 1);
me->max_qty = param_or_default(options, "MaxQty", 1);
me->min_depth = param_or_default(options, "MinDepth", 0);
me->affect_books = param_or_default(options, "AffectBooks", true);
me->max_events =
param_or_default(options, "MaxEvents", std::numeric_limits<int>::max());
me->min_value = param_or_default(options, "MinValue", 2);
me->max_transaction_latency =
1000 * param_or_default(options, "MaxTransactionLatency", 100000);
me->double_dip = param_or_default(options, "DoubleDip", false);
me->ms_to_work_shot = param_or_default(options, "MsToWorkShot", 0);
me->state = State::WAIT_OPPORTUNITY;
me->books.clear();
me->targets.clear();
me->current_shot.reset();
me->current_event = {0, 0};
me->transact_time = MDP3TimeStamp{0};
me->observable_time = MDP3TimeStamp{0};
for (auto &p : s.polygons()) {
auto desc = polygon_descriptor(p.second, me->sim->instruments());
auto buy_t = std::make_unique<Target>(
Target{p.second, polygon_descriptor(p.second, me->sim->instruments()),
polygon_product(p.second, me->sim->instruments()),
abs_contracts(p.second, me->sim->instruments()),
tick_size(p.second, me->sim->instruments())});
auto sell_t = std::make_unique<Target>(
Target{flip(p.second),
polygon_descriptor(flip(p.second), me->sim->instruments()),
polygon_product(p.second, me->sim->instruments()),
abs_contracts(p.second, me->sim->instruments()),
tick_size(p.second, me->sim->instruments())});
me->targets.emplace_back(std::move(buy_t));
me->targets.emplace_back(std::move(sell_t));
}
for (auto &t : me->targets) {
for (auto &pp : t->polygon.parts) {
auto &dependencies = me->books[pp.sec_id].dependencies;
dependencies.push_back(t.get());
}
}
}
void Sniper::on_notification(AlgoNotification const &n) {
if (n.header.event_id.first >= me->max_events)
me->sim->terminate_simulation();
else {
me->current_event = n.header.event_id;
me->transact_time = n.header.transact_time;
me->observable_time = n.header.observable_time;
std::visit(*this, n.event);
}
}
void Sniper::on_time(MDP3TimeStamp ts) {
switch (me->state) {
case State::WAIT_OPPORTUNITY: {
break;
}
case State::WAIT_FILLS: {
bts_assert(me->current_shot);
if (me->ms_to_work_shot &&
(ts.value() - me->current_shot->observed_ts.value()) >
1000 * 1000 * me->ms_to_work_shot &&
!me->current_shot->forced_completion) {
me->current_shot->forced_completion = true;
me->sim->log("forcing_fills", me->observable_time.value(),
me->current_event.first, me->current_event.second);
auto &in_flight = me->current_shot.value();
for (auto &p : in_flight.working) {
auto &order = p.second.order;
auto &these_fills = p.second.fills;
auto total_fills = 0;
for (auto &f : these_fills) {
bts_assert(f.status != OrderUpdate::CANCELED);
total_fills += f.last_fills.value();
}
if (order.order.quantity.value() > total_fills) {
md::IntPrice worst_price = order.order.price;
if (order.order.side == api::SIDE_BUY)
worst_price += me->sim->get_sec(order.order.security)
.min_price_increment_int_price() *
100;
else
worst_price -= me->sim->get_sec(order.order.security)
.min_price_increment_int_price() *
100;
order.change = OrderChange::updated;
order.order.quantity.value() -= total_fills;
order.order.price = worst_price;
me->sim->send(order);
}
}
}
break;
}
case State::WAIT_CLEAR:
break;
}
}
void Sniper::eval(std::vector<Target *> const &targets) {
for (auto &t : targets) {
t->value = 0;
int qty = me->max_qty;
for (auto &l : t->polygon.parts) {
auto mkt_side = securities::other_side(side_from_ratio(l.ratio));
auto &mkt = me->books[l.sec_id].book.mbp_side(mkt_side);
auto this_qty = mkt[0].quantity.value();
auto this_ratio = this_qty / std::abs(l.ratio);
if (this_ratio < qty)
qty = this_ratio;
if (qty == 0 || mkt[0].n_orders < me->min_orders_at_top ||
depth(mkt).value() < me->min_depth) {
t->mark_as_worthless();
break;
} else {
auto cash = me->sim->get_sec(l.sec_id).to_cash(mkt[0].price);
t->quantity.value() = qty;
t->value += -l.ratio * cash;
}
}
}
}
Sniper::MaybeShot Sniper::pick() {
std::sort(me->targets.begin(), me->targets.end(),
[](std::unique_ptr<Target> const &ptr1,
std::unique_ptr<Target> const &ptr2) {
auto val1 = ptr1->value * ptr1->quantity.value();
auto val2 = ptr2->value * ptr2->quantity.value();
if (std::abs(val1 - val2) > securities::market_epsilon())
return val1 > val2;
else
return ptr1->polygon.parts.size() <
ptr2->polygon.parts.size();
});
if (me->targets[0]->has_value(me->min_value)) {
if (me->max_qty &&
(me->observable_time.value() - me->transact_time.value()) <
me->max_transaction_latency)
return Shot{*me->targets[0],
me->current_event,
me->sim->now(),
me->targets[0]->value,
me->targets[0]->quantity,
false,
{}};
else
return {};
} else
return {};
}
void Sniper::fire(Shot const &s) {
auto &shot = me->current_shot.emplace(s);
me->sim->log("shot_begin", me->current_event.first, me->current_event.second,
s.target.descriptor, s.value_at_observation_ts,
s.quantity.value(), me->streak_id);
if (me->ms_to_work_shot == 0) {
shot.forced_completion = true;
for (auto &p : shot.target.polygon.parts) {
auto part_side = p.ratio > 0 ? api::SIDE_BUY : api::SIDE_SELL;
auto mkt_side = securities::other_side(part_side);
auto &book = me->books[p.sec_id].book.mbp_side(mkt_side);
md::IntPrice worst_price;
size_t available_qty = 0;
for (int i = book.size() - 1; i >= 0; i--) {
if (book[i].quantity.value()) {
worst_price = book[i].price;
for (int n = 0; n <= i; n++)
available_qty += book[n].quantity.value();
break;
}
}
if (part_side == api::SIDE_BUY)
worst_price +=
me->sim->get_sec(p.sec_id).min_price_increment_int_price() *
100;
else
worst_price -=
me->sim->get_sec(p.sec_id).min_price_increment_int_price() *
100;
auto order_change = SmallOrderChange{};
order_change.affect_book = me->affect_books;
order_change.order.key = me->sim->generate_order_key();
order_change.order.security = p.sec_id;
order_change.order.price = worst_price;
order_change.order.quantity =
MDP3Quantity{s.quantity.value() * std::abs(p.ratio)};
order_change.order.side = part_side;
order_change.change = OrderChange::added;
shot.working[order_change.order.key].order = order_change;
me->sim->log(
"sending_order", me->current_event.first, me->current_event.second,
api::Side_Name(part_side), me->sim->get_sec(p.sec_id).symbol(),
book[0].price.price(), worst_price.price(), available_qty);
me->sim->send(order_change);
shot.working[order_change.order.key].order = order_change;
order_change.change = OrderChange::removed;
me->sim->send(order_change);
}
} else {
for (auto &p : shot.target.polygon.parts) {
auto part_side = p.ratio > 0 ? api::SIDE_BUY : api::SIDE_SELL;
auto mkt_side = securities::other_side(part_side);
auto &book = me->books[p.sec_id].book.mbp_side(mkt_side);
auto order_change = SmallOrderChange{};
order_change.affect_book = me->affect_books;
order_change.order.key = me->sim->generate_order_key();
order_change.order.security = p.sec_id;
order_change.order.price = book[0].price;
order_change.order.quantity =
MDP3Quantity{s.quantity.value() * std::abs(p.ratio)};
order_change.order.side = part_side;
order_change.change = OrderChange::added;
me->sim->log("sending_order", me->current_event.first,
me->current_event.second, api::Side_Name(part_side),
me->sim->get_sec(p.sec_id).symbol(),
book[0].price.price(), book[0].price.price(),
book[0].quantity.value());
me->sim->send(order_change);
shot.working[order_change.order.key].order = order_change;
}
}
}
void Sniper::operator()(const MBPUpdate &mbp) {
auto it = me->books.find(mbp.security_id);
if (it == me->books.end())
return;
// me->sim->log("book",
// me->current_event.first,
// me->current_event.second,
// me->sim->get_sec(mbp.security_id).symbol(),
// mbp.mbp_buy[0].price.price(),
// mbp.mbp_buy[0].quantity.value(),
// mbp.mbp_sell[0].price.price(),
// mbp.mbp_sell[0].quantity.value());
auto &book = it->second;
book.book = mbp;
eval(book.dependencies);
switch (me->state) {
case State::WAIT_OPPORTUNITY: {
auto best_shot = pick();
if (best_shot) {
me->streak_id++;
fire(best_shot.value());
transition(State::WAIT_FILLS);
}
break;
}
case State::WAIT_FILLS:
break;
case State::WAIT_CLEAR:
bts_assert(me->current_shot);
if (!me->current_shot->target.has_value(me->min_value)) {
me->current_shot.reset();
me->state = State::WAIT_OPPORTUNITY;
auto best_shot = pick();
if (best_shot) {
me->streak_id++;
fire(best_shot.value());
transition(State::WAIT_FILLS);
}
} else {
me->sim->log("wait_clear", me->current_event.first,
me->current_event.second);
}
break;
}
}
void Sniper::operator()(const OrderUpdate &update) {
bts_assert(me->state == State::WAIT_FILLS);
bts_assert(me->current_shot);
auto &in_flight = me->current_shot.value();
bts_assert(in_flight.working.count(update.key));
in_flight.working[update.key].fills.push_back(update);
auto pending_orders = in_flight.working.size();
for (auto const &p : in_flight.working) {
auto &these_fills = p.second.fills;
for (auto &f : these_fills)
if (f.status == OrderUpdate::CANCELED ||
f.status == OrderUpdate::FILLED) {
pending_orders--;
break;
}
}
if (pending_orders == 0) {
std::vector<OrderUpdate> all_fills;
for (auto const &p : in_flight.working) {
auto &these_fills = p.second.fills;
all_fills.insert(all_fills.end(), these_fills.begin(),
these_fills.end());
}
PandL pandl(me->sim->instruments());
me->sim->log(
"shot_outcome", in_flight.target.descriptor,
in_flight.observed_event_id.first, in_flight.observed_event_id.second,
me->current_event.first, me->current_event.second,
in_flight.value_at_observation_ts * in_flight.quantity.value(),
pandl.net_cash(all_fills), me->streak_id,
in_flight.quantity.value());
me->sim->log_pandl();
if (!me->double_dip)
transition(State::WAIT_CLEAR);
else {
auto best_shot = pick();
if (best_shot) {
me->sim->log(
"double_dip",
me->current_event.first,
me->current_event.second,
me->streak_id);
fire(best_shot.value());
}
else
transition(State::WAIT_OPPORTUNITY);
}
}
}
const char *Sniper::state_name(State s) {
switch (s) {
case State::WAIT_FILLS:
return "WAIT_FILLS";
case State::WAIT_OPPORTUNITY:
return "WAIT_OPPORTUNITY";
case State::WAIT_CLEAR:
return "WAIT_CLEAR";
}
return nullptr;
}
void Sniper::transition(State new_state) {
me->sim->log(
"transition",
state_name(new_state),
me->current_event.first,
me->current_event.second,
me->streak_id);
me->state = new_state;
}
} // namespace bts::spartan::mdp3
@@ -1,42 +0,0 @@
#pragma once
#include <memory>
#include <optional>
#include "../simulator/IAlgoModel.h"
namespace bts::spartan::mdp3 {
struct Polygon;
class Sniper : public IAlgoModel {
public:
Sniper();
~Sniper();
void init(ISimServices &, protos::SimRunOptions const&) override;
void on_notification(AlgoNotification const&) override;
void on_time(MDP3TimeStamp) override;
void operator()(const MBPUpdate &);
void operator()(const OrderUpdate &);
private:
enum class State;
struct PImpl;
struct Target;
struct Shot;
struct WorkingOrder;
struct DependentTargets;
using MaybeShot = std::optional<Shot>;
void eval(std::vector<Target*> const&);
MaybeShot pick();
void fire(Shot const&);
void transition(State);
const char *state_name(State);
std::unique_ptr<PImpl> me;
};
}
@@ -1,166 +1,166 @@
#include "SimOrderGateway.h"
#include "SimSpreader.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"
// #include "SimOrderGateway.h"
// #include "SimSpreader.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"
using namespace bts::orderman;
using namespace bts::api::orderman;
// using namespace bts::orderman;
// using namespace bts::api::orderman;
namespace bts::spartan::mdp3 {
// namespace bts::spartan::mdp3 {
struct OrderTuple{
const OrderKey key;
OrderChange ochange;
uint32_t total_fills = 0;
double avg_filled_price = 0;
bool order_send_acked = false;
OrderTuple (OrderKey key,
OrderChange ochange)
:
key(key),
ochange(ochange)
{}
};
// struct OrderTuple{
// const OrderKey key;
// OrderChange ochange;
// uint32_t total_fills = 0;
// double avg_filled_price = 0;
// bool order_send_acked = false;
// OrderTuple (OrderKey key,
// OrderChange ochange)
// :
// key(key),
// ochange(ochange)
// {}
// };
OrderStatus translate_status(const OrderUpdate& ord){
switch(ord.status){
case(OrderUpdate::Status::CANCELED):
return OrderStatus::ORDER_STATUS_CANCELED;
case(OrderUpdate::Status::FILLED):
return OrderStatus::ORDER_STATUS_COMPLETED;
case(OrderUpdate::Status::PARTIALLY_FILLED):
return OrderStatus::ORDER_STATUS_WORKING;
}
bts_assert(false);
return OrderStatus::ORDER_STATUS_WORKING;
}
// OrderStatus translate_status(const OrderUpdate& ord){
// switch(ord.status){
// case(OrderUpdate::Status::CANCELED):
// return OrderStatus::ORDER_STATUS_CANCELED;
// case(OrderUpdate::Status::FILLED):
// return OrderStatus::ORDER_STATUS_COMPLETED;
// case(OrderUpdate::Status::PARTIALLY_FILLED):
// return OrderStatus::ORDER_STATUS_WORKING;
// }
// bts_assert(false);
// return OrderStatus::ORDER_STATUS_WORKING;
// }
OrderTuple* find_ord(OrderKey key,
std::vector<OrderTuple>& orders){
for(auto& o : orders)
if(o.key == key)
return &o;
return nullptr;
}
// OrderTuple* find_ord(OrderKey key,
// std::vector<OrderTuple>& orders){
// for(auto& o : orders)
// if(o.key == key)
// return &o;
// return nullptr;
// }
OrderTuple* find_ord(MDP3OrderKey key,
std::vector<OrderTuple>& orders){
for(auto& o : orders)
if(o.ochange.order.key == key)
return &o;
return nullptr;
}
// OrderTuple* find_ord(MDP3OrderKey key,
// std::vector<OrderTuple>& orders){
// for(auto& o : orders)
// if(o.ochange.order.key == key)
// return &o;
// return nullptr;
// }
struct SimOrderGateway::PImpl {
ISimServices* sim_services = nullptr;
SpreaderOrder* spreader = nullptr;
std::vector<OrderTuple> orders;
};
// struct SimOrderGateway::PImpl {
// ISimServices* sim_services = nullptr;
// SpreaderOrder* spreader = nullptr;
// std::vector<OrderTuple> orders;
// };
SimOrderGateway::SimOrderGateway()
:
me(new PImpl()){
}
// SimOrderGateway::SimOrderGateway()
// :
// me(new PImpl()){
// }
void SimOrderGateway::init(ISimServices& sim_services,
SpreaderOrder& spreader){
me->sim_services = &sim_services;
me->spreader = &spreader;
}
// void SimOrderGateway::init(ISimServices& sim_services,
// SpreaderOrder& spreader){
// me->sim_services = &sim_services;
// me->spreader = &spreader;
// }
void SimOrderGateway:: place_order(SecurityKey skey,
OrderKey okey,
api::Side side,
Price p,
Quantity q){
bts_assert(me->sim_services!=nullptr);
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())},
{}
},
OrderChange::added
});
me->sim_services->send(t.ochange);
}
// void SimOrderGateway:: place_order(SecurityKey skey,
// OrderKey okey,
// api::Side side,
// Price p,
// Quantity q){
// bts_assert(me->sim_services!=nullptr);
// 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())},
// {}
// },
// OrderChange::added
// });
// me->sim_services->send(t.ochange);
// }
void SimOrderGateway::modify_order(OrderKey okey,
Price p,
Quantity q){
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());
ochange.order.quantity = MDP3Quantity{static_cast<int>(q.value())};
ochange.change = OrderChange::updated;
me->sim_services->send(ochange);
}
// void SimOrderGateway::modify_order(OrderKey okey,
// Price p,
// Quantity q){
// 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());
// ochange.order.quantity = MDP3Quantity{static_cast<int>(q.value())};
// ochange.change = OrderChange::updated;
// me->sim_services->send(ochange);
// }
void SimOrderGateway::cancel_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.change = OrderChange::removed;
me->sim_services->send(ochange);
}
// void SimOrderGateway::cancel_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.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());
return OrderKey(rv);
}
// OrderKey SimOrderGateway::generate_key(){
// 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::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);
uint32_t total_fills = t->total_fills +
static_cast<uint32_t>(u.last_fills.value());
double avg_filled_price = 0;
if(total_fills > 0){
avg_filled_price = (t->total_fills*t->avg_filled_price) +
(u.last_fills.value() * u.last_price.price());
avg_filled_price = avg_filled_price / total_fills;
}
t->total_fills = total_fills;
t->avg_filled_price = avg_filled_price;
me->spreader->test_only_on_child_order_update(
t->key,
translate_status(u),
Quantity(t->total_fills),
Price(t->avg_filled_price)
);
}
}
// void SimOrderGateway::route_update_to_spreader(const OrderUpdate& u){
// OrderTuple* t = find_ord(u.key,me->orders);
// bts_assert(t!=nullptr);
// uint32_t total_fills = t->total_fills +
// static_cast<uint32_t>(u.last_fills.value());
// double avg_filled_price = 0;
// if(total_fills > 0){
// avg_filled_price = (t->total_fills*t->avg_filled_price) +
// (u.last_fills.value() * u.last_price.price());
// avg_filled_price = avg_filled_price / total_fills;
// }
// t->total_fills = total_fills;
// t->avg_filled_price = avg_filled_price;
// me->spreader->test_only_on_child_order_update(
// t->key,
// translate_status(u),
// Quantity(t->total_fills),
// Price(t->avg_filled_price)
// );
// }
// }
@@ -1,40 +1,40 @@
#pragma once
// #pragma once
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/domain_model/Security.pb.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/orderman_utils/orderman_common.h"
// #include "lib/domain_model/Security.pb.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"
// #include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/IAlternateOrderGateway.h"
namespace bts { namespace orderman {class SpreaderOrder;}}
// namespace bts { namespace orderman {class SpreaderOrder;}}
namespace bts::spartan::mdp3 {
// namespace bts::spartan::mdp3 {
class ISimServices;
// class ISimServices;
class SimOrderGateway : public bts::orderman::IAlternateOrderGateway{
private:
struct PImpl;
PImpl* me;
public:
SimOrderGateway();
~SimOrderGateway(){}
void init(ISimServices&,
bts::orderman::SpreaderOrder&);
// IAlternateOrderGateway functions
// (SpreaderChildOrderState's calls need to end up here)
void place_order(SecurityKey,
bts::orderman::OrderKey,
api::Side,
Price,
Quantity) override;
void modify_order(bts::orderman::OrderKey, Price, Quantity) override;
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&);
};
// class SimOrderGateway : public bts::orderman::IAlternateOrderGateway{
// private:
// struct PImpl;
// PImpl* me;
// public:
// SimOrderGateway();
// ~SimOrderGateway(){}
// void init(ISimServices&,
// bts::orderman::SpreaderOrder&);
// // IAlternateOrderGateway functions
// // (SpreaderChildOrderState's calls need to end up here)
// void place_order(SecurityKey,
// bts::orderman::OrderKey,
// api::Side,
// Price,
// Quantity) override;
// void modify_order(bts::orderman::OrderKey, Price, Quantity) override;
// 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&);
// };
}
// }
@@ -1,114 +1,114 @@
#include "SimSpreader.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/sparkproto/SparkProtos.pb.h"
#include "lib/orderman/orders/spreader_orders/SpreaderOrder.h"
#include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderPreActivationOrderServices.h"
#include "lib/orderman/orderman_utils/Book.h"
#include "SimOrderGateway.h"
// #include "SimSpreader.h"
// #include "lib/domain_model/Security.pb.h"
// #include "lib/api/services/OrderService.pb.h"
// #include "lib/sparkproto/SparkProtos.pb.h"
// #include "lib/orderman/orders/spreader_orders/SpreaderOrder.h"
// #include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderPreActivationOrderServices.h"
// #include "lib/orderman/orderman_utils/Book.h"
// #include "SimOrderGateway.h"
using namespace bts::orderman;
using namespace bts::api::orderman;
// using namespace bts::orderman;
// using namespace bts::api::orderman;
namespace bts::spartan::mdp3 {
// namespace bts::spartan::mdp3 {
void translate_book(const MBPUpdate& u_in,
bts::orderman::Book& b_out){
size_t i=-1;
for (auto const& bl : u_in.mbp_buy)
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] =
md::PriceSize (bl.price,bl.quantity.value());
}
// void translate_book(const MBPUpdate& u_in,
// bts::orderman::Book& b_out){
// size_t i=-1;
// for (auto const& bl : u_in.mbp_buy)
// 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] =
// md::PriceSize (bl.price,bl.quantity.value());
// }
bool compare_keys (SecurityKey spark_k,
MDP3SecurityID spartan_k){
return static_cast<int>(spark_k.value()) == spartan_k.value();
}
// bool compare_keys (SecurityKey spark_k,
// MDP3SecurityID spartan_k){
// return static_cast<int>(spark_k.value()) == spartan_k.value();
// }
bts::orderman::Book* find_book(MDP3SecurityID id,
std::vector<bts::orderman::Book>& books){
for (auto& b : books)
if(compare_keys(b.security_key(),id))
return &b;
return nullptr;
}
// bts::orderman::Book* find_book(MDP3SecurityID id,
// std::vector<bts::orderman::Book>& books){
// for (auto& b : books)
// if(compare_keys(b.security_key(),id))
// return &b;
// return nullptr;
// }
struct SimSpreader::PImpl {
const OrderDefinition order_def;
const SpreaderPreActivationOrderServices pao_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,
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()));
}
};
// struct SimSpreader::PImpl {
// const OrderDefinition order_def;
// const SpreaderPreActivationOrderServices pao_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,
// 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()));
// }
// };
SimSpreader::SimSpreader(const OrderDefinition& order_def,
const bts::api::Security& sec,
Price price,
Quantity qty)
:
me(new PImpl(order_def,sec,price,qty)){
}
const SpreaderOrder& SimSpreader::spreader() const{
return me->spreader;
}
// SimSpreader::SimSpreader(const OrderDefinition& order_def,
// const bts::api::Security& sec,
// Price price,
// Quantity qty)
// :
// me(new PImpl(order_def,sec,price,qty)){
// }
// const SpreaderOrder& SimSpreader::spreader() const{
// return me->spreader;
// }
// IAlgoModel /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void SimSpreader::init(ISimServices& sim_services, protos::SimRunOptions const&){
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
const MBPUpdate& bk = std::get<MBPUpdate>(ev.event);
// copy book
bts::orderman::Book* leg_bk = find_book(bk.security_id, me->books);
if(leg_bk != nullptr){
auto& leg_bk_r = *leg_bk;
translate_book(bk, leg_bk_r);
me->spreader.test_only_on_book_update(leg_bk_r);
}
}
else{
const OrderUpdate& u = std::get<OrderUpdate>(ev.event);
me->sim_order_gateway.route_update_to_spreader(u);
}
}
// // IAlgoModel /////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
// void SimSpreader::init(ISimServices& sim_services, protos::SimRunOptions const&){
// 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
// const MBPUpdate& bk = std::get<MBPUpdate>(ev.event);
// // copy book
// bts::orderman::Book* leg_bk = find_book(bk.security_id, me->books);
// if(leg_bk != nullptr){
// auto& leg_bk_r = *leg_bk;
// translate_book(bk, leg_bk_r);
// me->spreader.test_only_on_book_update(leg_bk_r);
// }
// }
// else{
// const OrderUpdate& u = std::get<OrderUpdate>(ev.event);
// me->sim_order_gateway.route_update_to_spreader(u);
// }
// }
}
// }
@@ -1,33 +1,33 @@
#pragma once
// #pragma once
#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 "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"
namespace bts { namespace api { namespace orderman {class OrderDefinition;}}}
namespace bts { namespace api { class OrderDefinition;}}
namespace bts { namespace orderman { class SpreaderOrder;}}
// namespace bts { namespace api { namespace orderman {class OrderDefinition;}}}
// namespace bts { namespace api { class OrderDefinition;}}
// namespace bts { namespace orderman { class SpreaderOrder;}}
namespace bts::spartan::mdp3 {
// namespace bts::spartan::mdp3 {
class ISimServices;
// class ISimServices;
class SimSpreader : public IAlgoModel {
private:
struct PImpl;
//std::unique_ptr<PImpl> me;
PImpl* me;
public:
SimSpreader(const bts::api::orderman::OrderDefinition&,
const bts::api::Security&,
Price, Quantity);
const bts::orderman::SpreaderOrder& spreader() const;
// IAlgoModel
void init(ISimServices&, protos::SimRunOptions const&) override;
void on_time(MDP3TimeStamp) override;
void on_notification(AlgoNotification const&) override;
~SimSpreader(){}
};
// class SimSpreader : public IAlgoModel {
// private:
// struct PImpl;
// //std::unique_ptr<PImpl> me;
// PImpl* me;
// public:
// SimSpreader(const bts::api::orderman::OrderDefinition&,
// const bts::api::Security&,
// Price, Quantity);
// const bts::orderman::SpreaderOrder& spreader() const;
// // IAlgoModel
// void init(ISimServices&, protos::SimRunOptions const&) override;
// void on_time(MDP3TimeStamp) override;
// void on_notification(AlgoNotification const&) override;
// ~SimSpreader(){}
// };
}
// }
@@ -1,359 +1,359 @@
#include <catch/catch.hpp>
#include "lib/domain_model/Security.pb.h"
// #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"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.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"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
using namespace bts::spartan::mdp3;
// using namespace bts::spartan::mdp3;
namespace bts {
namespace orderman {
// namespace bts {
// namespace orderman {
using namespace api;
using namespace spark;
// using namespace api;
// using namespace spark;
class TestSimServices : public bts::spartan::mdp3::ISimServices{
private:
InstrumentMap _instruments;
std::unordered_map<size_t,Polygon> _polygons;
// class TestSimServices : public bts::spartan::mdp3::ISimServices{
// private:
// InstrumentMap _instruments;
// std::unordered_map<size_t,Polygon> _polygons;
protected:
void _log(std::string const &,
std::initializer_list<std::string> const &) override {}
public:
SmallOrderChange last_change;
size_t n_updates = 0;
size_t key;
public:
TestSimServices ()
:
key(0){
}
InstrumentMap const &instruments() const override{
bts_assert(false);
return _instruments;
}
void terminate_simulation() override {}
void log_pandl() override {}
std::unordered_map<size_t,Polygon> const&polygons() const override {
return _polygons;
}
// protected:
// void _log(std::string const &,
// std::initializer_list<std::string> const &) override {}
// public:
// SmallOrderChange last_change;
// size_t n_updates = 0;
// size_t key;
// public:
// TestSimServices ()
// :
// key(0){
// }
// InstrumentMap const &instruments() const override{
// bts_assert(false);
// return _instruments;
// }
// void terminate_simulation() override {}
// void log_pandl() override {}
// std::unordered_map<size_t,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++);
}
MDP3TimeStamp now() const override{
return MDP3TimeStamp(0);
}
void wait(uint64_t) override{
}
void send(const SmallOrderChange& c, uint64_t) override{
last_change = c;
n_updates++;
}
~TestSimServices(){}
};
// spartan::mdp3::Book book(MDP3SecurityID,api::Side) const override {
// return {};
// }
// MDP3OrderKey generate_order_key() override{
// return MDP3OrderKey(key++);
// }
// MDP3TimeStamp now() const override{
// return MDP3TimeStamp(0);
// }
// void wait(uint64_t) override{
// }
// void send(const SmallOrderChange& 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;
}
// 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;
spartan::protos::SimRunOptions options;
sim_spreader.init(*(ISimServices*)&sim_services,options);
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(
{
{},
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(
{
{},
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::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::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);
}
}
// 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;
// spartan::protos::SimRunOptions options;
// sim_spreader.init(*(ISimServices*)&sim_services,options);
// 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(
// {
// {},
// 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(
// {
// {},
// 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::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::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);
}
// 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,216 +0,0 @@
// #include "SimOrchestrator.h"
// #include "../simulator/SimInput.h"
// #include "lib/utils/PBUtils.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/PolyObserver.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/Sniper.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/sim_db/SimDB.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/ExchangeSim.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h"
// #include <regex>
// namespace bts::spartan::mdp3 {
// class SimOrchestrator::SimInputManager {
// public:
// SimInputManager(std::string const &path) : path(path) {}
// std::shared_ptr<SimInput> get_input() const {
// std::lock_guard<std::mutex> l(lock);
// if (auto ptr = maybe_input.lock())
// return ptr;
// else {
// LOG(Spartan, Warn, "Loading input from {}", path);
// auto new_ptr =
// std::make_shared<SimInput>(make_sim_input_from_proto(path));
// maybe_input = new_ptr;
// return new_ptr;
// }
// }
// private:
// mutable std::mutex lock;
// const std::string path;
// mutable std::weak_ptr<SimInput> maybe_input;
// };
// struct SimOrchestrator::Run {
// protos::SimRunOptions options;
// std::vector<SimInputManager const *> inputs;
// std::vector<std::shared_ptr<SimInput>> get_inputs() {
// std::vector<std::shared_ptr<SimInput>> result;
// for (auto &m : inputs) {
// result.push_back(m->get_input());
// }
// return result;
// }
// };
// using ChannelMap =
// std::unordered_map<std::string,
// std::unique_ptr<SimOrchestrator::SimInputManager>>;
// using DayMap = std::unordered_map<std::string, ChannelMap>;
// struct SimOrchestrator::PImpl {
// protos::SimSessionOptions options;
// DayMap inputs;
// std::vector<Run> runs;
// SimDB db;
// };
// SimOrchestrator::SimOrchestrator(protos::SimSessionOptions const &options)
// : me(new PImpl{}) {
// me->options = options;
// // Organize inputs per day and channel
// auto parse_day_channel =
// [](std::string const &f) -> std::tuple<std::string, std::string> {
// // example /home/ppinto/360/2020-09-07_360_24h.sim
// std::regex rx(".*(\\d\\d\\d\\d-\\d\\d-\\d\\d)_(\\d\\d\\d)_24h\\.sim");
// std::smatch matches;
// bts_assert(std::regex_match(f, matches, rx));
// return {matches[1], matches[2]};
// };
// for (auto const &file : options.file()) {
// auto [this_day, this_channel] = parse_day_channel(file);
// auto &this_day_inputs = me->inputs[this_day];
// bts_assert(this_day_inputs.count(this_channel) == 0);
// this_day_inputs[this_channel] = std::make_unique<SimInputManager>(file);
// }
// // Generate all combinations of parameters
// std::vector<size_t> dimensionality;
// dimensionality.push_back(options.base_algo_latency_ns().size());
// dimensionality.push_back(options.order_update_delay_ns().size());
// dimensionality.push_back(options.mbp_update_delay_ns().size());
// dimensionality.push_back(options.transaction_latency_ns().size());
// for (auto &p : options.parameter())
// dimensionality.push_back(p.second.item().size());
// std::vector<int> combination(dimensionality.size(), -1);
// std::vector<std::vector<int>> all_combinations;
// generate_param_combinations(dimensionality, combination, 0,
// &all_combinations);
// // Now for each day apply all parameter combinations. Each unique
// // combination of a day plus a set of specific parameters defines a
// // run.
// for (auto const &[day, day_inputs] : me->inputs) {
// for (auto &c : all_combinations) {
// auto &this_run = me->runs.emplace_back();
// auto &these_options = this_run.options;
// auto &these_inputs = this_run.inputs;
// these_options.set_algo(options.algo());
// these_options.set_market_date(day);
// these_options.set_last_eid(options.last_eid());
// // Set inputs
// for (auto const &[channel, sim_input] : day_inputs) {
// *these_options.add_channels() = channel;
// these_inputs.push_back(sim_input.get());
// }
// // Set paramaters
// if (c[0] == -1)
// these_options.set_base_algo_latency_ns(0);
// else
// these_options.set_base_algo_latency_ns(
// options.base_algo_latency_ns(c[0]));
// if (c[1] == -1)
// these_options.set_order_update_delay_ns(0);
// else
// these_options.set_order_update_delay_ns(
// options.order_update_delay_ns(c[1]));
// if (c[2] == -1)
// these_options.set_mbp_update_delay_ns(0);
// else
// these_options.set_mbp_update_delay_ns(
// options.mbp_update_delay_ns(c[2]));
// if (c[3] == -1)
// these_options.set_transaction_latency_ns(0);
// else
// these_options.set_transaction_latency_ns(
// options.transaction_latency_ns(c[3]));
// size_t n = 4;
// for (auto &p : options.parameter()) {
// auto this_parameter_name = p.first;
// auto this_parameter_value = p.second.item(c[n]);
// these_options.mutable_parameters()->insert(
// {this_parameter_name, this_parameter_value});
// n++;
// }
// }
// }
// }
// SimOrchestrator::~SimOrchestrator() {}
// std::unique_ptr<IAlgoModel> make_algo(std::string const &algo) {
// if (algo == "Sniper")
// return std::make_unique<Sniper>();
// if (algo == "PolyObserver")
// return std::make_unique<PolyObserver>();
// bts_assert(false);
// return {};
// }
// void SimOrchestrator::run() {
// struct Worker {
// std::vector<SimOrchestrator::Run *> assigned_runs;
// std::thread thread;
// void start(SimDB &db, SimDB::SessionID sid,
// std::atomic<size_t> &remaining_runs) {
// thread = std::thread([sid, &db, this, &remaining_runs]() {
// for (size_t i = 0; i < assigned_runs.size(); i++) {
// auto const &r = assigned_runs[i];
// auto algo = make_algo(r->options.algo());
// auto rid = db.begin_run(sid, r->options);
// int rr = remaining_runs;
// LOG(Spartan, Warn, "Starting run {} ({} remaining) {}", rid, rr,
// utils::pb::to_string(r->options));
// auto logger = [rid, &db](size_t sim_seq, std::string const &src,
// auto ts, auto const &ev_header,
// auto const &entry_type,
// auto const &args) {
// db.log(rid, sim_seq, src, ts, ev_header, entry_type, args);
// };
// auto s_ptrs = r->get_inputs();
// std::vector<SimInput const *> ptrs;
// for (auto const &sp : s_ptrs)
// ptrs.push_back(sp.get());
// ExchangeSim sim(rid, logger, ptrs);
// auto pandl = sim.run(*algo, r->options);
// db.end_run(rid, pandl.cash, pandl.position);
// LOG(Spartan, Warn,
// "Ended run {} ({} remaining) P&L: {} Pos P&L: {}. Options: "
// "{}",
// rid, --remaining_runs, pandl.cash, pandl.position,
// utils::pb::to_string(r->options));
// }
// });
// }
// void join() { thread.join(); }
// };
// std::vector<Worker> workers(me->options.n_cores() ? me->options.n_cores()
// : os::num_cores());
// for (size_t i = 0; i < me->runs.size(); i++)
// workers[i % workers.size()].assigned_runs.push_back(&me->runs[i]);
// auto session_id = me->db.create_session(me->options.algo());
// std::atomic<size_t> run_count = me->runs.size();
// for (auto &w : workers)
// w.start(me->db, session_id, run_count);
// for (auto &w : workers)
// w.join();
// }
// } // namespace bts::spartan::mdp3
@@ -1,21 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
namespace bts::spartan::mdp3 {
class SimOrchestrator {
public:
SimOrchestrator(protos::SimSessionOptions const&);
~SimOrchestrator();
void run();
class SimInputManager;
private:
struct Run;
struct PImpl;
std::unique_ptr<PImpl> me;
};
}
@@ -1,78 +0,0 @@
#include "PCapDB.h"
#include <SQLiteCpp/SQLiteCpp.h>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <fmt/format.h>
namespace bts::spartan::mdp3 {
struct PCapDB::PImpl {
PImpl(std::string const &path)
: db(path, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE), db_lock() {}
SQLite::Database db;
std::mutex db_lock;
};
PCapDB::PCapDB(std::string const &channel, datetime::PTime const &date,
std::string const &loc) {
auto db_path = fmt::format("{}/pcap/{}/{:04}-{:02}-{:02}_{}.sqlite", loc,
channel, (unsigned short)date.date().year(),
date.date().month().as_number(),
date.date().day().as_number(), channel);
auto parent = boost::filesystem::path(db_path).parent_path();
if (!boost::filesystem::is_directory(parent))
bts_assert(boost::filesystem::create_directories(parent));
if (!boost::filesystem::is_regular_file(db_path)) {
auto sql_path = os::sparkd_wd() + "/data_files/spartan/md.sql";
std::ifstream f(sql_path);
std::string str((std::istreambuf_iterator<char>(f)),
(std::istreambuf_iterator<char>()));
SQLite::Database db(db_path,
SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
db.exec(str);
}
me = std::make_unique<PImpl>(db_path);
SQLite::Statement sync_off(me->db, "PRAGMA synchronous = OFF");
sync_off.executeStep();
SQLite::Statement journal_mem(me->db, "PRAGMA journal_mode = MEMORY");
journal_mem.executeStep();
}
std::vector<SmallOrderChange> PCapDB::get_orders() const {
std::vector<SmallOrderChange> result;
result.reserve(1000 * 1000 * 40);
SQLite::Statement query_orders(
me->db, "select eid, t_time, sid, status,impl,change, ok, side, "
"pri, price, qty from event_items where type='order_change'");
std::lock_guard<std::mutex> l(me->db_lock);
while (query_orders.executeStep()) {
auto &change = result.emplace_back();
auto &order = change.order;
order.key.value() = (int64_t)query_orders.getColumn("ok");
order.security.value() = query_orders.getColumn("sid");
order.side = !strcmp(query_orders.getColumn("side"), "SIDE_BUY")
? api::SIDE_BUY
: api::SIDE_SELL;
order.priority.value() = (int64_t)query_orders.getColumn("pri");
order.price = md::IntPrice{query_orders.getColumn("price"), 7};
order.quantity.value() = (int64_t)query_orders.getColumn("qty");
const char *change_type = query_orders.getColumn("change");
if (strcmp(change_type, "init") == 0 || strcmp(change_type, "added") == 0)
change.change = OrderChange::added;
else if (strcmp(change_type, "updated"))
change.change = OrderChange::added;
else
change.change = OrderChange::removed;
}
return result;
}
PCapDB::~PCapDB() {}
} // namespace bts::spartan::mdp3
@@ -1,27 +0,0 @@
#pragma once
#include <memory>
#include <vector>
#include "server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h"
#include "lib/persistence/persistence_utils.h"
#include "lib/utils/datetime.h"
namespace bts::spartan::mdp3 {
class PCapDB {
public:
PCapDB(std::string const&channel,
datetime::PTime const&date,
std::string const &location = persistence::db_location());
~PCapDB();
std::vector<SmallOrderChange> get_orders() const;
private:
struct PImpl;
std::unique_ptr<PImpl> me;
};
} // namespace bts::spartan::mdp3
@@ -1,312 +1,312 @@
#include "lib/orderman/orders/algo/IAlgoOrder.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.h"
#include "AlgoServicesBridge.h"
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
#include "lib/orderman/orders/algo/ChildTemplate.h"
#include "lib/orderman/orderman_utils/Book.h"
#include "BridgeCommon.h"
#include "BeforeLaunchBridge.h"
// #include "lib/orderman/orders/algo/IAlgoOrder.h"
// #include "lib/orderman/orders/algo/IAlgoServices.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.h"
// #include "AlgoServicesBridge.h"
// #include "lib/orderman/actors/order_manager/OrderBuilder.h"
// #include "lib/orderman/orders/algo/ChildTemplate.h"
// #include "lib/orderman/orderman_utils/Book.h"
// #include "BridgeCommon.h"
// #include "BeforeLaunchBridge.h"
using namespace bts::spartan::mdp3;
using namespace bts::api;
using namespace bts::api::orderman;
// using namespace bts::spartan::mdp3;
// using namespace bts::api;
// using namespace bts::api::orderman;
namespace bts {
namespace orderman {
// namespace bts {
// namespace orderman {
struct OrderState{
// spartan outgoing order
MDP3Order order;
// spark callback object
ChildState child_state;
};
// struct OrderState{
// // spartan outgoing order
// MDP3Order order;
// // spark callback object
// ChildState child_state;
// };
struct AlgoServicesBridge::PImpl {
std::vector<PositionUpdate> pos;
BeforeLaunchBridge& bl_bridge;
IAlgoOrder& algo_order;
ISimServices& sim_services;
std::unordered_map<MDP3SecurityID,Book> book_map;
std::unordered_map<MDP3OrderKey,OrderState> order_map;
OrderDefinition order_def;
uint32_t running_ok = 0;
// struct AlgoServicesBridge::PImpl {
// std::vector<PositionUpdate> pos;
// BeforeLaunchBridge& bl_bridge;
// IAlgoOrder& algo_order;
// ISimServices& sim_services;
// std::unordered_map<MDP3SecurityID,Book> book_map;
// std::unordered_map<MDP3OrderKey,OrderState> order_map;
// OrderDefinition order_def;
// uint32_t running_ok = 0;
PImpl(BeforeLaunchBridge& bl_bridge,
IAlgoOrder& algo_order,
ISimServices& sim_services)
:
bl_bridge(bl_bridge),
algo_order(algo_order),
sim_services(sim_services),
order_def(bl_bridge.definition())
{
}
};
// PImpl(BeforeLaunchBridge& bl_bridge,
// IAlgoOrder& algo_order,
// ISimServices& sim_services)
// :
// bl_bridge(bl_bridge),
// algo_order(algo_order),
// sim_services(sim_services),
// order_def(bl_bridge.definition())
// {
// }
// };
void absorb_order_callback(const OrderUpdate& new_update,
ChildState& curr_state){
uint32_t tot_fills = new_update.last_fills.value() +
curr_state.total_fills.value();
double avg_price = 0;
if (tot_fills>0){
avg_price =
curr_state.avg_price.value()*curr_state.total_fills.value() +
new_update.last_price.price()*new_update.last_fills.value();
avg_price = avg_price / tot_fills;
}
curr_state.total_fills = Quantity(tot_fills);
curr_state.avg_price = Price(avg_price);
curr_state.last_fills = Quantity(new_update.last_fills.value());
curr_state.last_price = Price(new_update.last_price.price());
switch (new_update.status)
{
case OrderUpdate::Status::FILLED:
curr_state.status = ChildStatus::COMPLETED;
break;
case OrderUpdate::Status::PARTIALLY_FILLED:
curr_state.status = ChildStatus::WORKING;
break;
case OrderUpdate::Status::CANCELED:
curr_state.status = ChildStatus::CANCELED;
break;
}
}
// void absorb_order_callback(const OrderUpdate& new_update,
// ChildState& curr_state){
// uint32_t tot_fills = new_update.last_fills.value() +
// curr_state.total_fills.value();
// double avg_price = 0;
// if (tot_fills>0){
// avg_price =
// curr_state.avg_price.value()*curr_state.total_fills.value() +
// new_update.last_price.price()*new_update.last_fills.value();
// avg_price = avg_price / tot_fills;
// }
// curr_state.total_fills = Quantity(tot_fills);
// curr_state.avg_price = Price(avg_price);
// curr_state.last_fills = Quantity(new_update.last_fills.value());
// curr_state.last_price = Price(new_update.last_price.price());
// switch (new_update.status)
// {
// case OrderUpdate::Status::FILLED:
// curr_state.status = ChildStatus::COMPLETED;
// break;
// case OrderUpdate::Status::PARTIALLY_FILLED:
// curr_state.status = ChildStatus::WORKING;
// break;
// case OrderUpdate::Status::CANCELED:
// curr_state.status = ChildStatus::CANCELED;
// break;
// }
// }
AlgoServicesBridge::AlgoServicesBridge(BeforeLaunchBridge& bl_bridge,
IAlgoOrder& algo_order,
ISimServices& sim_services)
:
me(new PImpl(bl_bridge,algo_order, sim_services)) {
}
// AlgoServicesBridge::AlgoServicesBridge(BeforeLaunchBridge& bl_bridge,
// IAlgoOrder& algo_order,
// ISimServices& sim_services)
// :
// me(new PImpl(bl_bridge,algo_order, sim_services)) {
// }
Price AlgoServicesBridge::price() const{
return Price(0);
}
// Price AlgoServicesBridge::price() const{
// return Price(0);
// }
Quantity AlgoServicesBridge::quantity() const {
return Quantity(0);
}
// Quantity AlgoServicesBridge::quantity() const {
// return Quantity(0);
// }
Quantity AlgoServicesBridge::total_fills() const{
return Quantity(0);
}
// Quantity AlgoServicesBridge::total_fills() const{
// return Quantity(0);
// }
bool AlgoServicesBridge::is_shutting_down() const {
return false;
}
// bool AlgoServicesBridge::is_shutting_down() const {
// return false;
// }
const std::vector<PositionUpdate>&
AlgoServicesBridge::current_positions() const {
bts_assert_msg(false, "Not implemented");
return me->pos;
}
// const std::vector<PositionUpdate>&
// AlgoServicesBridge::current_positions() const {
// bts_assert_msg(false, "Not implemented");
// return me->pos;
// }
trace::CorID AlgoServicesBridge::get_correlation_id() const {
trace::CorID cid;
return cid;
}
// trace::CorID AlgoServicesBridge::get_correlation_id() const {
// trace::CorID cid;
// return cid;
// }
void AlgoServicesBridge::subscribe_book(SecurityKey k) {
auto st_sec_key = map_sec_key(k);
if(me->book_map.find(st_sec_key) == me->book_map.end())
me->book_map.insert({st_sec_key,Book()});
}
// void AlgoServicesBridge::subscribe_book(SecurityKey k) {
// auto st_sec_key = map_sec_key(k);
// if(me->book_map.find(st_sec_key) == me->book_map.end())
// me->book_map.insert({st_sec_key,Book()});
// }
void AlgoServicesBridge::unsubscribe_book(SecurityKey k) {
auto st_sec_key = map_sec_key(k);
if(me->book_map.find(st_sec_key) != me->book_map.end())
me->book_map.erase(st_sec_key);
}
// void AlgoServicesBridge::unsubscribe_book(SecurityKey k) {
// auto st_sec_key = map_sec_key(k);
// if(me->book_map.find(st_sec_key) != me->book_map.end())
// me->book_map.erase(st_sec_key);
// }
void AlgoServicesBridge::subscribe_settlement(SecurityKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::subscribe_settlement(SecurityKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::unsubscribe_settlement(SecurityKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::unsubscribe_settlement(SecurityKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::subscribe_child_change(OrderKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::subscribe_child_change(OrderKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::unsubscribe_child_change(OrderKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::unsubscribe_child_change(OrderKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::subscribe_timer() {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::subscribe_timer() {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::unsubscribe_timer() {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::unsubscribe_timer() {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::subscribe_ticker(SecurityKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::subscribe_ticker(SecurityKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::unsubscribe_ticker(SecurityKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::unsubscribe_ticker(SecurityKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::subscribe_status(SecurityKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::subscribe_status(SecurityKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::unsubscribe_status(SecurityKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::unsubscribe_status(SecurityKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::subscribe_position() {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::subscribe_position() {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::unsubscribe_position() {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::unsubscribe_position() {
// bts_assert_msg(false, "Not implemented");
// }
OrderKey AlgoServicesBridge::launch_child(ChildTemplate &,
Price p, Quantity q,
bool subscribe) {
return launch_child(me->order_def,p,q,subscribe);
}
// OrderKey AlgoServicesBridge::launch_child(ChildTemplate &,
// Price p, Quantity q,
// bool subscribe) {
// return launch_child(me->order_def,p,q,subscribe);
// }
OrderKey AlgoServicesBridge::launch_child(
const api::orderman::OrderDefinition& ord_def,
Price p, Quantity q,
bool) {
auto ord_key = me->sim_services.generate_order_key();
auto sec_key = map_sec_key(SecurityKey(ord_def.security_key()));
auto ret = me->order_map.insert(
{
ord_key,
OrderState{
MDP3Order {
ord_key,
sec_key,
ord_def.side(),
MDP3OrderPriority{},
md::IntPrice_from_double<7>(p.value()),
MDP3Quantity(q.value()),
std::nullopt
},
ChildState{
p,q,Quantity(0),Price(0),Quantity(0),Price(0),
ChildStatus::WORKING,Quantity(0)
}
}});
bts_assert(ret.second);
OrderState& ord_state = ret.first->second;
SmallOrderChange ord_change(ord_state.order,
OrderChange::ChangeType::added);
me->sim_services.send(ord_change);
return map_ord_key(ord_key);
}
// OrderKey AlgoServicesBridge::launch_child(
// const api::orderman::OrderDefinition& ord_def,
// Price p, Quantity q,
// bool) {
// auto ord_key = me->sim_services.generate_order_key();
// auto sec_key = map_sec_key(SecurityKey(ord_def.security_key()));
// auto ret = me->order_map.insert(
// {
// ord_key,
// OrderState{
// MDP3Order {
// ord_key,
// sec_key,
// ord_def.side(),
// MDP3OrderPriority{},
// md::IntPrice_from_double<7>(p.value()),
// MDP3Quantity(q.value()),
// std::nullopt
// },
// ChildState{
// p,q,Quantity(0),Price(0),Quantity(0),Price(0),
// ChildStatus::WORKING,Quantity(0)
// }
// }});
// bts_assert(ret.second);
// OrderState& ord_state = ret.first->second;
// SmallOrderChange ord_change(ord_state.order,
// OrderChange::ChangeType::added);
// me->sim_services.send(ord_change);
// return map_ord_key(ord_key);
// }
// Child order maintenance
void AlgoServicesBridge::update_child(
OrderKey k,
const api::orderman::Update& u){
auto sp_ord_key = map_ord_key(k);
auto it = me->order_map.find(sp_ord_key);
if(it != me->order_map.end()){
OrderState& ord_state = it->second;
ord_state.order.quantity = MDP3Quantity(u.quantity());
ord_state.order.price = md::IntPrice_from_double<7>(u.price());
ord_state.child_state.price = Price(u.price());
ord_state.child_state.quantity = Quantity(u.quantity());
SmallOrderChange ord_change(ord_state.order,
OrderChange::ChangeType::updated);
me->sim_services.send(ord_change);
}
else
LOG(OrderMan, Warn, "AlgoServicesBridge::update_child OrderKey:{} "
"does not exist", k);
}
// // Child order maintenance
// void AlgoServicesBridge::update_child(
// OrderKey k,
// const api::orderman::Update& u){
// auto sp_ord_key = map_ord_key(k);
// auto it = me->order_map.find(sp_ord_key);
// if(it != me->order_map.end()){
// OrderState& ord_state = it->second;
// ord_state.order.quantity = MDP3Quantity(u.quantity());
// ord_state.order.price = md::IntPrice_from_double<7>(u.price());
// ord_state.child_state.price = Price(u.price());
// ord_state.child_state.quantity = Quantity(u.quantity());
// SmallOrderChange ord_change(ord_state.order,
// OrderChange::ChangeType::updated);
// me->sim_services.send(ord_change);
// }
// else
// LOG(OrderMan, Warn, "AlgoServicesBridge::update_child OrderKey:{} "
// "does not exist", k);
// }
void AlgoServicesBridge::cancel_child(OrderKey k) {
auto sp_ord_key = map_ord_key(k);
auto it = me->order_map.find(sp_ord_key);
if(it != me->order_map.end()){
OrderState& ord_state = it->second;
SmallOrderChange ord_change(ord_state.order,
OrderChange::ChangeType::removed);
me->sim_services.send(ord_change);
}
else
LOG(OrderMan, Warn, "AlgoServicesBridge::cancel_child OrderKey:{} "
"does not exist", k);
}
// void AlgoServicesBridge::cancel_child(OrderKey k) {
// auto sp_ord_key = map_ord_key(k);
// auto it = me->order_map.find(sp_ord_key);
// if(it != me->order_map.end()){
// OrderState& ord_state = it->second;
// SmallOrderChange ord_change(ord_state.order,
// OrderChange::ChangeType::removed);
// me->sim_services.send(ord_change);
// }
// else
// LOG(OrderMan, Warn, "AlgoServicesBridge::cancel_child OrderKey:{} "
// "does not exist", k);
// }
void AlgoServicesBridge::detach_child(OrderKey) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::detach_child(OrderKey) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::detach_all_children() {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::detach_all_children() {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::cancel(const std::string&) {
for (const auto& p : me->order_map)
me->sim_services.send(
SmallOrderChange(p.second.order, OrderChange::ChangeType::removed));
}
// void AlgoServicesBridge::cancel(const std::string&) {
// for (const auto& p : me->order_map)
// me->sim_services.send(
// SmallOrderChange(p.second.order, OrderChange::ChangeType::removed));
// }
void AlgoServicesBridge::terminate_error(const std::string& msg) {
cancel(msg);
}
// void AlgoServicesBridge::terminate_error(const std::string& msg) {
// cancel(msg);
// }
void AlgoServicesBridge::complete() {
}
// void AlgoServicesBridge::complete() {
// }
void AlgoServicesBridge::set_ladder_visibility(bool) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::set_ladder_visibility(bool) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::update_custom_state(
const api::orderman::DynamicOrderState&) {
bts_assert_msg(false, "Not implemented");
}
// void AlgoServicesBridge::update_custom_state(
// const api::orderman::DynamicOrderState&) {
// bts_assert_msg(false, "Not implemented");
// }
void AlgoServicesBridge::update_fill_state(Quantity, Price) {
}
// void AlgoServicesBridge::update_fill_state(Quantity, Price) {
// }
// IAlgoModel
// // IAlgoModel
void AlgoServicesBridge::init(bts::spartan::mdp3::ISimServices&,
bts::spartan::protos::SimRunOptions const&){
}
// void AlgoServicesBridge::init(bts::spartan::mdp3::ISimServices&,
// bts::spartan::protos::SimRunOptions const&){
// }
void AlgoServicesBridge::on_notification(
const bts::spartan::mdp3::AlgoNotification& ev){
if(std::holds_alternative<MBPUpdate>(ev.event)){
// book update
const MBPUpdate& new_bk = std::get<MBPUpdate>(ev.event);
auto it = me->book_map.find(new_bk.security_id);
if(it != me->book_map.end()){
Book& bk = it->second;
translate_book(new_bk,bk);
me->algo_order.on_book(map_sec_key(new_bk.security_id), bk);
}
}
else{
const OrderUpdate& u = std::get<OrderUpdate>(ev.event);
auto it = me->order_map.find(u.key);
if(it != me->order_map.end()){
OrderState& ord_state = it->second;
absorb_order_callback(u,ord_state.child_state);
me->algo_order.on_child_change(
map_ord_key(ord_state.order.key),ord_state.child_state);
}
else
LOG(OrderMan, Error, "AlgoServicesBridge::on_notification "
"OrderKey:{} does not exist", u.key);
}
// void AlgoServicesBridge::on_notification(
// const bts::spartan::mdp3::AlgoNotification& ev){
// if(std::holds_alternative<MBPUpdate>(ev.event)){
// // book update
// const MBPUpdate& new_bk = std::get<MBPUpdate>(ev.event);
// auto it = me->book_map.find(new_bk.security_id);
// if(it != me->book_map.end()){
// Book& bk = it->second;
// translate_book(new_bk,bk);
// me->algo_order.on_book(map_sec_key(new_bk.security_id), bk);
// }
// }
// else{
// const OrderUpdate& u = std::get<OrderUpdate>(ev.event);
// auto it = me->order_map.find(u.key);
// if(it != me->order_map.end()){
// OrderState& ord_state = it->second;
// absorb_order_callback(u,ord_state.child_state);
// me->algo_order.on_child_change(
// map_ord_key(ord_state.order.key),ord_state.child_state);
// }
// else
// LOG(OrderMan, Error, "AlgoServicesBridge::on_notification "
// "OrderKey:{} does not exist", u.key);
// }
}
// }
} // namespace orderman
} // namespace bts
// } // namespace orderman
// } // namespace bts
@@ -1,103 +1,103 @@
#pragma once
// #pragma once
#include "lib/orderman/orders/algo/IAlgoOrder.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h"
// #include "lib/orderman/orders/algo/IAlgoOrder.h"
// #include "lib/orderman/orders/algo/IAlgoServices.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h"
namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}}
namespace bts {namespace spartan {namespace mdp3{class ISimServices;}}}
// namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}}
// namespace bts {namespace spartan {namespace mdp3{class ISimServices;}}}
namespace bts::spartan::protos { class SimRunOptions; }
// namespace bts::spartan::protos { class SimRunOptions; }
namespace bts {
namespace orderman {
// namespace bts {
// namespace orderman {
class ChildTemplate;
class IAlgoOrder;
class BeforeLaunchBridge;
// class ChildTemplate;
// class IAlgoOrder;
// class BeforeLaunchBridge;
class AlgoServicesBridge : public IAlgoServices, public bts::spartan::mdp3::IAlgoModel{
public:
AlgoServicesBridge(BeforeLaunchBridge& bl_bridge,
IAlgoOrder& algo_order,
bts::spartan::mdp3::ISimServices&);
~AlgoServicesBridge(){}
// class AlgoServicesBridge : public IAlgoServices, public bts::spartan::mdp3::IAlgoModel{
// public:
// AlgoServicesBridge(BeforeLaunchBridge& bl_bridge,
// IAlgoOrder& algo_order,
// bts::spartan::mdp3::ISimServices&);
// ~AlgoServicesBridge(){}
Price price() const override;
// Price price() const override;
Quantity quantity() const override;
// Quantity quantity() const override;
Quantity total_fills() const override;
// Quantity total_fills() const override;
bool is_shutting_down() const override;
// bool is_shutting_down() const override;
std::vector<PositionUpdate> const& current_positions() const override;
// std::vector<PositionUpdate> const& current_positions() const override;
trace::CorID get_correlation_id() const override;
// trace::CorID get_correlation_id() const override;
void subscribe_book(SecurityKey) override;
// void subscribe_book(SecurityKey) override;
void unsubscribe_book(SecurityKey) override;
// void unsubscribe_book(SecurityKey) override;
void subscribe_settlement(SecurityKey) override;
// void subscribe_settlement(SecurityKey) override;
void unsubscribe_settlement(SecurityKey) override;
// void unsubscribe_settlement(SecurityKey) override;
void subscribe_child_change(OrderKey) override;
// void subscribe_child_change(OrderKey) override;
void unsubscribe_child_change(OrderKey) override;
// void unsubscribe_child_change(OrderKey) override;
void subscribe_timer() override;
// void subscribe_timer() override;
void unsubscribe_timer() override;
// void unsubscribe_timer() override;
void subscribe_ticker(SecurityKey) override;
// void subscribe_ticker(SecurityKey) override;
void unsubscribe_ticker(SecurityKey) override;
// void unsubscribe_ticker(SecurityKey) override;
void subscribe_status(SecurityKey) override;
// void subscribe_status(SecurityKey) override;
void unsubscribe_status(SecurityKey) override;
// void unsubscribe_status(SecurityKey) override;
void subscribe_position() override;
// void subscribe_position() override;
void unsubscribe_position() override;
// void unsubscribe_position() override;
OrderKey launch_child(ChildTemplate &, Price, Quantity,
bool subscribe = true) override;
// OrderKey launch_child(ChildTemplate &, Price, Quantity,
// bool subscribe = true) override;
OrderKey launch_child(api::orderman::OrderDefinition const &, Price,
Quantity, bool subscribe = true) override;
// OrderKey launch_child(api::orderman::OrderDefinition const &, Price,
// Quantity, bool subscribe = true) override;
// Child order maintenance
void update_child(OrderKey child, api::orderman::Update const &) override;
void cancel_child(OrderKey child) override;
void detach_child(OrderKey child) override;
void detach_all_children() override;
// // Child order maintenance
// void update_child(OrderKey child, api::orderman::Update const &) override;
// void cancel_child(OrderKey child) override;
// void detach_child(OrderKey child) override;
// void detach_all_children() override;
void cancel(std::string const &msg = "") override;
// void cancel(std::string const &msg = "") override;
void terminate_error(std::string const &msg) override;
// void terminate_error(std::string const &msg) override;
void complete() override;
// void complete() override;
void set_ladder_visibility(bool is_visible) override;
// void set_ladder_visibility(bool is_visible) override;
void update_custom_state(api::orderman::DynamicOrderState const&) override;
// void update_custom_state(api::orderman::DynamicOrderState const&) override;
void update_fill_state(Quantity total_fills, Price last_price) override;
// void update_fill_state(Quantity total_fills, Price last_price) override;
// IAlgoModel
// // IAlgoModel
void init(bts::spartan::mdp3::ISimServices&,
bts::spartan::protos::SimRunOptions const&) override;
// void init(bts::spartan::mdp3::ISimServices&,
// bts::spartan::protos::SimRunOptions const&) override;
void on_notification(bts::spartan::mdp3::AlgoNotification const&) override;
// void on_notification(bts::spartan::mdp3::AlgoNotification const&) override;
private:
struct PImpl;
std::unique_ptr<PImpl> me;
};
// private:
// struct PImpl;
// std::unique_ptr<PImpl> me;
// };
} // namespace orderman
} // namespace bts
// } // namespace orderman
// } // namespace bts
@@ -1,120 +1,120 @@
#include "BeforeLaunchBridge.h"
#include "../ISimServices.h"
#include "BridgeCommon.h"
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
#include "lib/orderman/orders/algo/ChildTemplate.h"
// #include "BeforeLaunchBridge.h"
// #include "../ISimServices.h"
// #include "BridgeCommon.h"
// #include "lib/orderman/actors/order_manager/OrderBuilder.h"
// #include "lib/orderman/orders/algo/ChildTemplate.h"
namespace bts {
namespace orderman {
// namespace bts {
// namespace orderman {
using namespace bts::spartan::mdp3;
using namespace bts::api::orderman;
// using namespace bts::spartan::mdp3;
// using namespace bts::api::orderman;
typedef std::map<InstrumentDefinition const*, api::Security,
std::function<bool(InstrumentDefinition const*,
InstrumentDefinition const*)> > InsMap;
// typedef std::map<InstrumentDefinition const*, api::Security,
// std::function<bool(InstrumentDefinition const*,
// InstrumentDefinition const*)> > InsMap;
struct BeforeLaunchBridge::PImpl {
OrderKey ok;
MDP3SecurityID sec_id;
ISimServices& sim_services;
const api::orderman::OrderDefinition& order_def;
ChildTemplate* child_temp = nullptr;
api::Security security;
TimeStamp ts;
std::map<std::unique_ptr<ChildTemplate>,OrderDefinition> _child_temp_map;
InsMap sec_map = InsMap([](const auto& a, const auto& b) {
return a->security_id().value() < b->security_id().value();
});
// struct BeforeLaunchBridge::PImpl {
// OrderKey ok;
// MDP3SecurityID sec_id;
// ISimServices& sim_services;
// const api::orderman::OrderDefinition& order_def;
// ChildTemplate* child_temp = nullptr;
// api::Security security;
// TimeStamp ts;
// std::map<std::unique_ptr<ChildTemplate>,OrderDefinition> _child_temp_map;
// InsMap sec_map = InsMap([](const auto& a, const auto& b) {
// return a->security_id().value() < b->security_id().value();
// });
PImpl(
OrderKey ok,
MDP3SecurityID sec_id,
ISimServices& sim_services,
const api::orderman::OrderDefinition& order_def)
:
ok(ok),
sec_id(sec_id),
sim_services(sim_services),
order_def(order_def) {
}
};
// PImpl(
// OrderKey ok,
// MDP3SecurityID sec_id,
// ISimServices& sim_services,
// const api::orderman::OrderDefinition& order_def)
// :
// ok(ok),
// sec_id(sec_id),
// sim_services(sim_services),
// order_def(order_def) {
// }
// };
// TODO:
// order def translation (ex sniper)
// ChildTemplate
// timestamp
// // TODO:
// // order def translation (ex sniper)
// // ChildTemplate
// // timestamp
BeforeLaunchBridge::BeforeLaunchBridge(
ISimServices& sim_services,
const api::orderman::OrderDefinition& order_def)
:
me(new PImpl(OrderKey(0),MDP3SecurityID(0),sim_services,order_def)){
for (const auto& p : me->sim_services.instruments()){
auto mdp3_sec_ptr = p.second;
api::Security sec = convert_to_spark_security(
mdp3_sec_ptr,
me->sim_services.instruments());
me->sec_map.insert({mdp3_sec_ptr,sec});
}
bool found = false;
for (const auto& p : me->sec_map)
if(p.second.id() == order_def.security_key()){
me->security = p.second;
found = true;
}
bts_assert(found);
}
// BeforeLaunchBridge::BeforeLaunchBridge(
// ISimServices& sim_services,
// const api::orderman::OrderDefinition& order_def)
// :
// me(new PImpl(OrderKey(0),MDP3SecurityID(0),sim_services,order_def)){
// for (const auto& p : me->sim_services.instruments()){
// auto mdp3_sec_ptr = p.second;
// api::Security sec = convert_to_spark_security(
// mdp3_sec_ptr,
// me->sim_services.instruments());
// me->sec_map.insert({mdp3_sec_ptr,sec});
// }
// bool found = false;
// for (const auto& p : me->sec_map)
// if(p.second.id() == order_def.security_key()){
// me->security = p.second;
// found = true;
// }
// bts_assert(found);
// }
const api::orderman::OrderDefinition& BeforeLaunchBridge::definition() const{
return me->order_def;
}
// const api::orderman::OrderDefinition& BeforeLaunchBridge::definition() const{
// return me->order_def;
// }
const api::Security& BeforeLaunchBridge::security() const{
return me->security;
}
// const api::Security& BeforeLaunchBridge::security() const{
// return me->security;
// }
OrderKey BeforeLaunchBridge::order_key() const{
return me->ok;
}
// OrderKey BeforeLaunchBridge::order_key() const{
// return me->ok;
// }
OrderKey BeforeLaunchBridge::parent_order_key() const{
// not supported in spartan sim
return order_key();
}
// OrderKey BeforeLaunchBridge::parent_order_key() const{
// // not supported in spartan sim
// return order_key();
// }
api::Security const* BeforeLaunchBridge::lookup_security(SecurityKey k) const{
for (const auto& p : me->sec_map)
if(p.second.id() == k.value())
return &p.second;
return nullptr;
}
// api::Security const* BeforeLaunchBridge::lookup_security(SecurityKey k) const{
// for (const auto& p : me->sec_map)
// if(p.second.id() == k.value())
// return &p.second;
// return nullptr;
// }
std::vector<api::Security const*> BeforeLaunchBridge::known_securities() const{
std::vector<api::Security const*> rv;
for (const auto& p : me->sec_map)
rv.push_back(&p.second);
return rv;
}
// std::vector<api::Security const*> BeforeLaunchBridge::known_securities() const{
// std::vector<api::Security const*> rv;
// for (const auto& p : me->sec_map)
// rv.push_back(&p.second);
// return rv;
// }
bool BeforeLaunchBridge::is_attaching() const{
return false;
}
// bool BeforeLaunchBridge::is_attaching() const{
// return false;
// }
ChildTemplate& BeforeLaunchBridge::make_child_template(
const api::orderman::OrderDefinition& d,
size_t){
auto ret = me->_child_temp_map.insert(std::make_pair(std::make_unique<ChildTemplate>(),d));
bts_assert(ret.second);
const std::unique_ptr<ChildTemplate>& rv = ret.first->first;
return *rv;
}
// ChildTemplate& BeforeLaunchBridge::make_child_template(
// const api::orderman::OrderDefinition& d,
// size_t){
// auto ret = me->_child_temp_map.insert(std::make_pair(std::make_unique<ChildTemplate>(),d));
// bts_assert(ret.second);
// const std::unique_ptr<ChildTemplate>& rv = ret.first->first;
// return *rv;
// }
// Get the current time
TimeStamp BeforeLaunchBridge::get_time() const{
// TODO
return me->ts;
}
// // Get the current time
// TimeStamp BeforeLaunchBridge::get_time() const{
// // TODO
// return me->ts;
// }
}}
// }}
@@ -1,49 +1,49 @@
#pragma once
// #pragma once
#include "lib/orderman/orders/algo/IAlgoOrder.h"
#include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h"
// #include "lib/orderman/orders/algo/IAlgoOrder.h"
// #include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h"
namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}}
namespace bts {namespace spartan {namespace mdp3{class ISimServices;}}}
// namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}}
// namespace bts {namespace spartan {namespace mdp3{class ISimServices;}}}
namespace bts {
namespace orderman {
// namespace bts {
// namespace orderman {
class ChildTemplate;
// class ChildTemplate;
class BeforeLaunchBridge : public IBeforeLaunchAlgoServices{
public:
BeforeLaunchBridge(
bts::spartan::mdp3::ISimServices&,
const api::orderman::OrderDefinition&);
virtual ~BeforeLaunchBridge(){}
// class BeforeLaunchBridge : public IBeforeLaunchAlgoServices{
// public:
// BeforeLaunchBridge(
// bts::spartan::mdp3::ISimServices&,
// const api::orderman::OrderDefinition&);
// virtual ~BeforeLaunchBridge(){}
api::orderman::OrderDefinition const &definition() const override;
// api::orderman::OrderDefinition const &definition() const override;
api::Security const &security() const override;
// api::Security const &security() const override;
OrderKey order_key() const override;
// OrderKey order_key() const override;
OrderKey parent_order_key() const override;
// OrderKey parent_order_key() const override;
api::Security const *lookup_security(SecurityKey k) const override;
// api::Security const *lookup_security(SecurityKey k) const override;
std::vector<api::Security const*> known_securities() const override;
// std::vector<api::Security const*> known_securities() const override;
bool is_attaching() const override;
// bool is_attaching() const override;
ChildTemplate &
make_child_template(api::orderman::OrderDefinition const &child_def,
size_t reserve = 1) override;
// ChildTemplate &
// make_child_template(api::orderman::OrderDefinition const &child_def,
// size_t reserve = 1) override;
TimeStamp get_time() const override;
// TimeStamp get_time() const override;
private:
struct PImpl;
//std::unique_ptr<PImpl> me;
PImpl* me;
};
// private:
// struct PImpl;
// //std::unique_ptr<PImpl> me;
// PImpl* me;
// };
} // namespace orderman
} // namespace bts
// } // namespace orderman
// } // namespace bts
@@ -1,99 +1,99 @@
#pragma once
// #pragma once
#include "lib/orderman/orders/algo/IAlgoOrder.h"
//#include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h"
//#include "../ISimServices.h"
#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h"
#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentMap.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/AlgoEvent.h"
#include "lib/orderman/orderman_utils/Book.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 "lib/orderman/orders/algo/IAlgoOrder.h"
// //#include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h"
// //#include "../ISimServices.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentMap.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/AlgoEvent.h"
// #include "lib/orderman/orderman_utils/Book.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"
namespace bts{
namespace orderman{
// namespace bts{
// namespace orderman{
inline OrderKey map_ord_key(bts::spartan::mdp3::MDP3OrderKey k){
// uint64_t -> uint32_t
return OrderKey(static_cast<uint32_t>(k.value()));
}
// inline OrderKey map_ord_key(bts::spartan::mdp3::MDP3OrderKey k){
// // uint64_t -> uint32_t
// return OrderKey(static_cast<uint32_t>(k.value()));
// }
inline bts::spartan::mdp3::MDP3OrderKey map_ord_key(OrderKey k){
// uint32_t -> uint64_t
return bts::spartan::mdp3::MDP3OrderKey(static_cast<uint64_t>(k.value()));
}
// inline bts::spartan::mdp3::MDP3OrderKey map_ord_key(OrderKey k){
// // uint32_t -> uint64_t
// return bts::spartan::mdp3::MDP3OrderKey(static_cast<uint64_t>(k.value()));
// }
inline SecurityKey map_sec_key(bts::spartan::mdp3::MDP3SecurityID k){
// int -> uint32_t
return SecurityKey(static_cast<uint32_t>(k.value()));
}
// inline SecurityKey map_sec_key(bts::spartan::mdp3::MDP3SecurityID k){
// // int -> uint32_t
// return SecurityKey(static_cast<uint32_t>(k.value()));
// }
inline bts::spartan::mdp3::MDP3SecurityID map_sec_key(SecurityKey k){
// uint32_t -> int
return bts::spartan::mdp3::MDP3SecurityID(static_cast<int>(k.value()));
}
// inline bts::spartan::mdp3::MDP3SecurityID map_sec_key(SecurityKey k){
// // uint32_t -> int
// return bts::spartan::mdp3::MDP3SecurityID(static_cast<int>(k.value()));
// }
inline void translate_book(const bts::spartan::mdp3::MBPUpdate& u_in,
bts::orderman::Book& b_out){
// TODO: implieds
size_t i=-1;
for (auto const& bl : u_in.mbp_buy)
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] =
md::PriceSize (bl.price,bl.quantity.value());
}
// inline void translate_book(const bts::spartan::mdp3::MBPUpdate& u_in,
// bts::orderman::Book& b_out){
// // TODO: implieds
// size_t i=-1;
// for (auto const& bl : u_in.mbp_buy)
// 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] =
// md::PriceSize (bl.price,bl.quantity.value());
// }
inline void fill_future(const bts::spartan::mdp3::InstrumentDefinition&,
bts::api::Security& out_sec){
// TODO, set future expiration dates
out_sec.mutable_future();
}
// inline void fill_future(const bts::spartan::mdp3::InstrumentDefinition&,
// bts::api::Security& out_sec){
// // TODO, set future expiration dates
// out_sec.mutable_future();
// }
inline bts::api::Security convert_to_spark_security (
bts::spartan::mdp3::InstrumentDefinition const* def_ptr,
const bts::spartan::mdp3::InstrumentMap& sec_map){
auto fill_common = [](const auto& def, auto out_sec){
out_sec.set_id(map_sec_key(def.security_id()).value());
out_sec.set_symbol(def.security_group());
out_sec.set_exchange(bts::api::VENUE_GLBX);
out_sec.set_currency(bts::api::CURRENCY_USD);
out_sec.mutable_contract_spec()->set_low_tick(def.min_price_increment_ammount());
out_sec.mutable_contract_spec()->set_high_tick(def.min_price_increment_ammount());
};
const auto& def = *def_ptr;
bts::api::Security rv_sec;
fill_common(def,rv_sec);
switch(def.security_type()){
case bts::api::SECURITYTYPE_FUTURE:
fill_future(def,rv_sec);
break;
case bts::api::SECURITYTYPE_SPREAD:{
auto& spr = *rv_sec.mutable_spread();
for(const auto& l : def.legs()){
// getting inst def for the leg
auto it = sec_map.find(l.security_id);
bts_assert(it != sec_map.end());
const auto& leg_insdef = *it->second;
// creating spread
bts::api::ContractLeg& spr_leg = *spr.add_legs();
auto& leg_sec = *spr_leg.mutable_security();
fill_common(leg_insdef, leg_sec);
fill_future(leg_insdef,leg_sec);
spr_leg.set_quantity(l.ratio.value());
spr_leg.set_side(l.side);
spr_leg.set_price_multiplier(spr_leg.quantity());
}
break;
}
default:
break;
}
return rv_sec;
}
}}
// inline bts::api::Security convert_to_spark_security (
// bts::spartan::mdp3::InstrumentDefinition const* def_ptr,
// const bts::spartan::mdp3::InstrumentMap& sec_map){
// auto fill_common = [](const auto& def, auto out_sec){
// out_sec.set_id(map_sec_key(def.security_id()).value());
// out_sec.set_symbol(def.security_group());
// out_sec.set_exchange(bts::api::VENUE_GLBX);
// out_sec.set_currency(bts::api::CURRENCY_USD);
// out_sec.mutable_contract_spec()->set_low_tick(def.min_price_increment_ammount());
// out_sec.mutable_contract_spec()->set_high_tick(def.min_price_increment_ammount());
// };
// const auto& def = *def_ptr;
// bts::api::Security rv_sec;
// fill_common(def,rv_sec);
// switch(def.security_type()){
// case bts::api::SECURITYTYPE_FUTURE:
// fill_future(def,rv_sec);
// break;
// case bts::api::SECURITYTYPE_SPREAD:{
// auto& spr = *rv_sec.mutable_spread();
// for(const auto& l : def.legs()){
// // getting inst def for the leg
// auto it = sec_map.find(l.security_id);
// bts_assert(it != sec_map.end());
// const auto& leg_insdef = *it->second;
// // creating spread
// bts::api::ContractLeg& spr_leg = *spr.add_legs();
// auto& leg_sec = *spr_leg.mutable_security();
// fill_common(leg_insdef, leg_sec);
// fill_future(leg_insdef,leg_sec);
// spr_leg.set_quantity(l.ratio.value());
// spr_leg.set_side(l.side);
// spr_leg.set_price_multiplier(spr_leg.quantity());
// }
// break;
// }
// default:
// break;
// }
// return rv_sec;
// }
// }}
@@ -1,172 +0,0 @@
#include "AlgoAgent.h"
#include "../IAlgoModel.h"
#include "../IExchangeServices.h"
#include "lib/utils/PBUtils.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/sim_db/SimDB.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/PandL.h"
#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h"
#include "server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h"
namespace bts::spartan::mdp3 {
struct AlgoAgent::PImpl {
InstrumentMap const &instruments;
std::unordered_map<size_t, Polygon> const &polygons;
IExchangeServices &services;
MDP3OrderKey *last_order_key;
protos::SimRunOptions options;
IAlgoModel &algo;
std::unordered_set<MDP3OrderKey> my_orders = {};
std::vector<OrderUpdate> my_fills = {};
};
AlgoAgent::AlgoAgent(InstrumentMap const &instruments,
std::unordered_map<size_t, Polygon> const &polygons,
IExchangeServices &services, MDP3OrderKey *next_order_key,
protos::SimRunOptions const &options, IAlgoModel &algo)
: me(new PImpl{instruments, polygons, services, next_order_key, options,
algo}) {}
AlgoAgent::AlgoAgent(AlgoAgent &&src) : me() { operator=(std::move(src)); }
AlgoAgent &AlgoAgent::operator=(AlgoAgent &&src) {
me = std::move(src.me);
return *this;
}
void AlgoAgent::init() {
me->algo.init(*this, me->options);
}
void AlgoAgent::handle(sim::Event const &ev) {
if (dynamic_cast<TimerEvent const *>(&ev)) {
me->algo.on_time(MDP3TimeStamp{time()});
} else if (auto n = dynamic_cast<AlgoNotificationEvent const *>(&ev); n) {
std::visit(
overloaded{
[&](const MBPUpdate &) { me->algo.on_notification(n->ev); },
[&](const OrderUpdate &update) {
if (me->my_orders.count(update.key)) {
log("order_update", update.status_str(),
get_sec(update.security_id).symbol(),
update.last_price.price(), update.last_fills.value(),
api::Side_Name(update.side), update.key.value(),
update.security_id.value());
if (update.last_fills.value()) {
me->my_fills.push_back(update);
}
me->algo.on_notification(n->ev);
}
}},
n->ev.event);
}
}
double AlgoAgent::price_at_market(MDP3SecurityID id, MDP3Quantity qty,
api::Side s) const {
auto book = me->services.book(id, securities::other_side(s));
double cummulative_flow = 0;
auto remaining_qty = qty.value();
for (auto &l : book) {
if (remaining_qty == 0)
return cummulative_flow;
else {
auto from_this_level = std::min(remaining_qty, l.quantity.value());
cummulative_flow +=
(from_this_level * get_sec(id).to_cash(l.price.price()));
remaining_qty -= from_this_level;
}
}
if (remaining_qty)
return os::nan();
else
return cummulative_flow;
}
void AlgoAgent::log_pandl() {
auto mkt_pricer = [this](MDP3SecurityID id, MDP3Quantity qty, api::Side s) {
return price_at_market(id, qty, s);
};
PandL pl{me->instruments, mkt_pricer};
auto outright_pos = pl.outright_position(me->my_fills);
log("pnl", pl.net_cash(me->my_fills),
pl.position_liquidation_value(outright_pos));
for (auto &[id, pos] : outright_pos)
if (pos)
log("outright_pos", get_sec(id).symbol(), pos);
auto per_symbol_pnl = pl.symbol_net_cash(me->my_fills);
for (auto &[symbol, cash] : per_symbol_pnl)
if (std::abs(cash) > securities::market_epsilon())
log("symbol_pnl", symbol, cash);
}
AlgoAgent::~AlgoAgent() {}
std::unordered_map<size_t, Polygon> const &AlgoAgent::polygons() const {
return me->polygons;
}
InstrumentMap const &AlgoAgent::instruments() const { return me->instruments; }
MDP3OrderKey AlgoAgent::generate_order_key() {
return MDP3OrderKey{++me->last_order_key->value()};
}
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(SmallOrderChange const &change, uint64_t propagation_delay) {
auto total_delay = me->options.base_algo_latency_ns() + propagation_delay;
auto t_time = time() + total_delay;
log("order_action", change_name(change.change),
get_sec(change.order.security).symbol(), change.order.price.price(),
change.order.quantity.value(), api::Side_Name(change.order.side),
change.order.key.value(), change.order.security.value());
me->my_orders.insert(change.order.key);
emit_delayed_event<AlgoActionEvent>(
total_delay,
AlgoAction{MDP3TimeStamp{t_time}, change});
}
void AlgoAgent::terminate_simulation() {
end_simulation();
}
void AlgoAgent::_log(std::string const &log_entry_type,
std::initializer_list<std::string> const &log_args) {
std::ostringstream str;
str << to_time_string(now()) << " " << log_entry_type << " ";
for (auto const &a : log_args)
str << a << " ";
LOG(Spartan, Info, "{}", str.str());
me->services.log(now(), log_entry_type, log_args);
}
PandLSummary AlgoAgent::finit() {
log_pandl();
auto mkt_pricer = [this](MDP3SecurityID id, MDP3Quantity qty, api::Side s) {
return price_at_market(id, qty, s);
};
PandL pl{me->instruments, mkt_pricer};
auto outright_pos = pl.outright_position(me->my_fills);
return {
pl.net_cash(me->my_fills),
pl.position_liquidation_value(outright_pos)
};
}
} // namespace bts::spartan::mdp3
@@ -1,56 +0,0 @@
#pragma once
#include "../SimLogger.h"
#include "../ISimServices.h"
#include "../core/AgentBase.h"
#include "../events/SimEvents.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/PandLSummary.h"
#include <memory>
#include <vector>
namespace bts::spartan::mdp3 {
struct SimOptions;
class InstrumentDefinition;
class IExchangeServices;
class SimDB;
class AlgoAgent : ISimServices, public sim::AgentBase {
public:
AlgoAgent(InstrumentMap const &,
std::unordered_map<size_t, Polygon> const &,
IExchangeServices &,
MDP3OrderKey *next_order,
protos::SimRunOptions const &,
IAlgoModel &);
AlgoAgent(AlgoAgent &&);
~AlgoAgent();
AlgoAgent &operator=(AlgoAgent &&);
// From IAgent
void init() override;
void handle(sim::Event const &) override;
PandLSummary finit();
private:
// From ISimservices
void _log(std::string const &log_entry_type,
std::initializer_list<std::string> const &log_args) override;
void log_pandl() override;
std::unordered_map<size_t, 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(SmallOrderChange const &, uint64_t propagation_delay) override;
void terminate_simulation() override;
// End ISimServices
double price_at_market(MDP3SecurityID id, MDP3Quantity qty, api::Side) const;
struct PImpl;
std::unique_ptr<PImpl> me;
};
} // namespace bts::spartan::mdp3
@@ -1,244 +0,0 @@
// #include "GatewayAgent.h"
// #include "../../../common/InstrumentDefinition.h"
// #include "../../../common/mdp3_common.h"
// #include "../../matcher/Matcher.h"
// #include "../events/SimEvents.h"
// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
// #include <range/v3/view.hpp>
// #include <unordered_set>
// // john is investigating slowness that might related to
// // implied matching (haven't determined if its a real issue though,
// // as the original simulator seemed slow on my machine too, perhaps
// // i need to dedicate more ram to my vm).
// // If you want to run the simulator with implied matching,
// // just uncomment the line below.
// #define ENABLE_IMPLIED_MATCHING
// namespace bts::spartan::mdp3 {
// #ifdef ENABLE_IMPLIED_MATCHING
// using Matcher = MergedMatcher;
// #else
// using Matcher = DirectMatcher;
// #endif
// std::vector<MDP3SecurityID>
// extract_ids(std::unordered_map<MDP3SecurityID, InstrumentDefinition> const &m) {
// std::vector<MDP3SecurityID> result;
// for (auto const &p : m)
// result.push_back(p.first);
// return result;
// }
// struct GatewayAgent::PImpl {
// #ifdef ENABLE_IMPLIED_MATCHING
// PImpl(SimLogger const &l,
// std::unordered_map<MDP3SecurityID, InstrumentDefinition> const &defs,
// protos::SimRunOptions const &options)
// : my_instruments(defs), options(options), matcher(),
// logger(l), current_event{0, 0} {
// auto def_vals = defs | ranges::views::transform(
// [](const auto &p) { return p.second; });
// matcher.init(def_vals.begin(), def_vals.end());
// }
// #else
// PImpl(std::unordered_map<MDP3SecurityID, InstrumentDefinition> const &defs,
// protos::SimRunOptions const &options)
// : my_instruments(defs), options(options), matcher(extract_ids(defs)) {}
// #endif
// std::unordered_map<MDP3SecurityID, InstrumentDefinition> const
// &my_instruments;
// protos::SimRunOptions options;
// Matcher matcher;
// SimLogger logger;
// AlgoEventID current_event;
// std::unordered_map<MDP3OrderKey,MDP3OrderKey> cme_order_mapping;
// };
// GatewayAgent::GatewayAgent(
// SimLogger const &l,
// std::unordered_map<MDP3SecurityID, InstrumentDefinition> const &defs,
// protos::SimRunOptions const &options)
// : me(new PImpl(l, defs, options)) {
// me->options = options;
// }
// GatewayAgent::GatewayAgent(GatewayAgent &&src) : me() {
// operator=(std::move(src));
// }
// GatewayAgent &GatewayAgent::operator=(GatewayAgent &&src) {
// me = std::move(src.me);
// return *this;
// }
// GatewayAgent::~GatewayAgent() {}
// void GatewayAgent::handle(const sim::Event &ev) {
// // Recorded event
// if (auto rec_ev = dynamic_cast<ExchangeTransactionEvent const *>(&ev);
// rec_ev)
// _handle(*rec_ev);
// else if (auto aa = dynamic_cast<AlgoActionEvent const *>(&ev); aa)
// _handle(*aa);
// }
// Book GatewayAgent::book(MDP3SecurityID id, api::Side s) {
// return me->matcher.book(id, s);
// }
// template <class... T>
// void log(SimLogger &l, sim::TimeStamp ts, std::string const &log_entry_type,
// T &&... args) {
// auto lst =
// std::initializer_list<std::string>{to_str(std::forward<T>(args))...};
// l(0,"gw",MDP3TimeStamp{ts}, {},log_entry_type, lst);
// }
// // 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) {
// auto const &ev = e.ev;
// if (ev->order_changes.empty())
// return;
// auto &first_change = ev->order_changes[0];
// if (me->my_instruments.count(first_change.order.security)) {
// me->current_event = AlgoEventID{ev->event_id, 0};
// // Advance our clock to the send time in the recording (unless
// // we are already ahead of that time, possibly because we got
// // delayed processing algo operations).
// auto send_time = ev->last_packet_send_time.value();
// if (send_time > time())
// forward_clock_by(send_time - time());
// else
// forward_clock_by(me->options.transaction_latency_ns());
// if (me->options.log_all_transactions())
// for (auto const &change : ev->order_changes) {
// me->cme_order_mapping[change.order.key] = change.original_key;
// log(me->logger, time(), "gw_order", me->current_event.first,
// me->current_event.second, ev->transact_time.value(),
// change_name(change.change),
// get_sec(change.order.security).symbol(),
// change.order.price.price(), change.order.quantity.value(),
// api::Side_Name(change.order.side), change.order.key.value(),
// change.original_key.value());
// }
// // Now match the recorded orders and send out their effects as
// // algo notification events
// std::vector<Matcher::MatchEffect> effects;
// me->matcher.match(Matcher::SIMULATOR_INPUT, ev->order_changes, &effects);
// for (auto &ef : effects)
// std::visit(
// overloaded{
// [&](MBPUpdate &mbp) {
// if (me->options.log_all_transactions()) {
// log(me->logger, time(),
// "gw_book",
// me->current_event.first,
// me->current_event.second,
// ev->transact_time.value(),
// get_sec(mbp.security_id).symbol(),
// mbp.mbp_buy[0].price.price(),
// mbp.mbp_buy[0].quantity.value(),
// mbp.mbp_sell[0].price.price(),
// mbp.mbp_sell[0].quantity.value());
// }
// emit_delayed_event<AlgoNotificationEvent>(
// me->options.mbp_update_delay_ns(),
// AlgoNotification{{},mbp});
// },
// [&](OrderUpdate const &order_update) {
// if (me->options.log_all_transactions()) {
// log(me->logger, time(), "gw_fill",
// me->current_event.first, me->current_event.second,
// ev->transact_time.value(), order_update.status_str(),
// api::Side_Name(order_update.side),
// get_sec(order_update.security_id).symbol(),
// order_update.last_price.price(),
// order_update.last_fills.value(),
// order_update.key.value(),
// me->cme_order_mapping[order_update.key].value());
// }
// emit_delayed_event<AlgoNotificationEvent>(
// me->options.order_update_delay_ns(),
// AlgoNotification{{},order_update});
// }},
// ef);
// }
// }
// // Handle an algo order change, that is an order change requested by
// // one of the algos under test.
// void GatewayAgent::_handle(AlgoActionEvent const &e) {
// me->current_event.second++;
// auto const &algo_action = e.ev;
// auto const &change = algo_action.change;
// if (me->options.log_all_transactions()) {
// log(me->logger, time(), "gw_algo_order", me->current_event.first,
// me->current_event.second, algo_action.transact_time.value(),
// change_name(change.change), get_sec(change.order.security).symbol(),
// change.order.price.price(), change.order.quantity.value(),
// api::Side_Name(change.order.side), change.order.key.value(),0);
// }
// forward_clock_by(me->options.transaction_latency_ns());
// std::vector<Matcher::MatchEffect> effects;
// me->matcher.match(Matcher::ALGO, {algo_action.change}, &effects);
// for (auto &ef : effects)
// std::visit(
// overloaded{
// [&](MBPUpdate &mbp) {
// if (me->options.log_all_transactions()) {
// log(me->logger, time(), "gw_book", me->current_event.first,
// me->current_event.second,
// algo_action.transact_time.value(),
// get_sec(mbp.security_id).symbol(),
// mbp.mbp_buy[0].price.price(),
// mbp.mbp_buy[0].quantity.value(),
// mbp.mbp_sell[0].price.price(),
// mbp.mbp_sell[0].quantity.value());
// }
// emit_delayed_event<AlgoNotificationEvent>(
// me->options.mbp_update_delay_ns(),
// AlgoNotification{me->current_event,
// algo_action.transact_time,
// MDP3TimeStamp{time()}, mbp});
// },
// [&](OrderUpdate const &order_update) {
// if (me->options.log_all_transactions()) {
// log(me->logger, time(), "gw_algo_fill", me->current_event.first,
// me->current_event.second,
// algo_action.transact_time.value(),
// order_update.status_str(),
// api::Side_Name(order_update.side),
// get_sec(order_update.security_id).symbol(),
// order_update.last_price.price(),
// order_update.last_fills.value(),
// order_update.key.value(),
// me->cme_order_mapping[order_update.key].value());
// }
// emit_delayed_event<AlgoNotificationEvent>(
// me->options.order_update_delay_ns(),
// AlgoNotification{me->current_event,
// algo_action.transact_time,
// MDP3TimeStamp{time()}, order_update});
// }},
// ef);
// }
// InstrumentDefinition const &GatewayAgent::get_sec(MDP3SecurityID id) const {
// auto it = me->my_instruments.find(id);
// bts_assert(it != me->my_instruments.end());
// return it->second;
// }
// } // namespace bts::spartan::mdp3
@@ -1,36 +0,0 @@
#pragma once
#include "../core/AgentBase.h"
#include "../events/SimEvents.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/SimLogger.h"
#include <memory>
#include <vector>
namespace bts::spartan::mdp3 {
struct SimOptions;
class InstrumentDefinition;
class GatewayAgent : public sim::AgentBase {
public:
GatewayAgent(SimLogger const &logger,
std::unordered_map<MDP3SecurityID, InstrumentDefinition> const &,
protos::SimRunOptions const &);
GatewayAgent(GatewayAgent &&);
GatewayAgent &operator=(GatewayAgent &&);
~GatewayAgent();
void handle(sim::Event const &) override;
Book book(MDP3SecurityID, api::Side);
private:
void _handle(ExchangeTransactionEvent const &);
void _handle(AlgoActionEvent const &);
InstrumentDefinition const &get_sec(MDP3SecurityID) const;
struct PImpl;
std::unique_ptr<PImpl> me;
};
} // namespace bts::spartan::mdp3
@@ -1,16 +0,0 @@
#include "TimerAgent.h"
#include "../events/SimEvents.h"
namespace bts::spartan::mdp3 {
TimerAgent::TimerAgent (uint64_t period_ms) : _period_ms(period_ms) {}
void TimerAgent::handle(sim::Event const &ev) {
if (dynamic_cast<TimerEvent const*> (&ev)) {
forward_clock_by(_period_ms * 1000 * 1000);
emit_event<TimerEvent> ();
}
}
}
@@ -1,16 +0,0 @@
#pragma once
#include "../core/AgentBase.h"
namespace bts::spartan::mdp3 {
class TimerAgent : public sim::AgentBase {
public:
TimerAgent (uint64_t period_ms = 100);
void handle(sim::Event const &) override;
private:
uint64_t _period_ms;
};
}
@@ -1,64 +0,0 @@
#include "AgentBase.h"
#include "lib/utils/bts_assert.h"
#include <queue>
namespace bts::spartan::sim {
struct AgentBase::Self {
Simulator *simulator = nullptr;
std::queue<EventPtr> pending_events;
TimeStamp agent_time = 0;
};
AgentBase::AgentBase() : self(new Self {}) {}
AgentBase::~AgentBase() {}
void AgentBase::init(Simulator &sim) {
bts_assert(self->simulator == nullptr);
self->simulator = &sim;
}
void AgentBase::enqueue(EventPtr const& e) {
bts_assert(self->simulator);
self->pending_events.push(e);
}
TimeStamp AgentBase::process(TimeStamp now) {
bts_assert(self->simulator);
if (self->agent_time>now)
return self->agent_time;
else
self->agent_time = now;
while (self->pending_events.size()) {
auto ev = self->pending_events.front();
self->pending_events.pop();
handle(*ev);
}
if (self->agent_time == now)
return EndOfTime;
else
return self->agent_time;
}
void AgentBase::forward_clock_by(TimeDelta delta) {
self->agent_time += delta;
}
Simulator& AgentBase::simulator() {
bts_assert(self->simulator);
return *self->simulator;
}
void AgentBase::end_simulation() {
self->simulator->end_simulation();
}
TimeStamp AgentBase::time() const {
return self->agent_time;
}
}
@@ -1,40 +0,0 @@
#pragma once
#include "IAgent.h"
#include "Simulator.h"
#include <memory>
namespace bts::spartan::sim {
class AgentBase : public IAgent {
protected:
AgentBase();
~AgentBase();
template <class EV,typename... Args>
void emit_event(Args &&... args) {
emit_delayed_event<EV>(0,std::forward<Args>(args)...);
}
template <class EV,typename... Args>
void emit_delayed_event(TimeDelta delay,Args &&... args) {
simulator().emit_event<EV>(time()+delay,std::forward<Args>(args)...);
}
void forward_clock_by(TimeDelta);
void end_simulation();
TimeStamp time() const;
virtual void handle(Event const&) = 0;
virtual void init() {}
private:
Simulator& simulator();
void init(Simulator &) override;
void enqueue(EventPtr const&) override;
TimeStamp process(TimeStamp now) override;
struct Self;
std::unique_ptr<Self> self;
};
} // namespace bts::spartan::sim
@@ -1,18 +0,0 @@
#pragma once
#include "simulator_common.h"
#include "Event.h"
namespace bts::spartan::sim {
class Simulator;
class IAgent {
public:
virtual ~IAgent() {};
virtual void init(Simulator&) = 0;
virtual void enqueue(EventPtr const&) = 0;
virtual TimeStamp process(TimeStamp now) = 0;
};
}
@@ -1,17 +0,0 @@
#pragma once
#include "Event.h"
namespace bts::spartan::sim {
class ISimulator2;
class IAgent2 {
public:
virtual ~IAgent2() {};
virtual void init(ISimulator2&) = 0;
virtual void handle(Event const&) = 0;
virtual void finit() {};
};
}
@@ -1,32 +0,0 @@
#pragma once
#include "Event.h"
namespace bts::spartan::sim {
class Simulator2;
class ISimulator2 {
public:
virtual ~ISimulator2() {}
template <class EV,typename... Args>
void emit(Args &&... args) {
_emit(0,std::make_shared<EV> (std::forward<Args>(args)...));
}
template <class EV,typename... Args>
void emit_delayed(size_t delay_ns,Args &&... args) {
_emit(delay_ns,std::make_shared<EV> (std::forward<Args>(args)...));
}
virtual TimeStamp time() = 0;
virtual void sleep(size_t ns) = 0;
protected:
friend class Simulator2;
virtual void _emit(size_t delay_ns,EventPtr const&) = 0;
};
}
@@ -1,107 +0,0 @@
#include "Simulator.h"
#include "Event.h"
#include "IAgent.h"
#include "lib/utils/bts_assert.h"
#include <queue>
namespace bts::spartan::sim {
struct EventSort {
bool operator()(EventPtr const &l, EventPtr const &r) const {
if (l->observable_time != r->observable_time)
return l->observable_time > r->observable_time;
else
return l->event_idx > r->event_idx;
}
};
using FutureEventQueue =
std::priority_queue<EventPtr, std::vector<EventPtr>, EventSort>;
struct Simulator::PImpl {
std::vector<IAgent *> agents;
bool ended = false;
FutureEventQueue pending_events;
TimeStamp time = 0;
size_t event_count = 0;
};
Simulator::Simulator() : me(new PImpl()) {}
Simulator::~Simulator() {}
TimeStamp Simulator::simulation_time() { return me->time; }
std::optional<TimeStamp> Simulator::pending_event_time() const {
if (me->pending_events.size())
return me->pending_events.top()->observable_time;
else
return {};
}
size_t Simulator::pending_events_count() const {
return me->pending_events.size();
}
bool Simulator::run_once() {
if (me->ended)
return false;
prepare();
if (me->pending_events.top()->observable_time <= me->time) {
auto current_ev = me->pending_events.top();
me->pending_events.pop();
for (auto &a : me->agents)
a->enqueue(current_ev);
}
// Give all agents a chance to process their queued events
auto next_agent_interesting_time = EndOfTime;
for (auto &a : me->agents) {
auto next_ready = a->process(me->time);
next_agent_interesting_time =
std::min(next_ready, next_agent_interesting_time);
}
if (me->pending_events.empty())
return false;
else {
me->time = std::min(next_agent_interesting_time,
me->pending_events.top()->observable_time);
return true;
}
}
void Simulator::run() {
while (run_once())
;
}
void Simulator::end_simulation() {
me->ended = true;
}
bool Simulator::terminated() const {
return me->ended;
}
void Simulator::prepare() {
bts_assert(me->agents.size());
if (me->time == 0)
me->time = me->pending_events.top()->observable_time;
}
void Simulator::add_agent(IAgent &a) {
me->agents.emplace_back(&a);
a.init(*this);
}
void Simulator::_emit_event(TimeStamp ts, EventPtr const&ev) {
me->event_count++;
bts_assert(ts >= simulation_time());
ev->observable_time = ts;
ev->event_idx = me->event_count;
me->pending_events.push(ev);
}
} // namespace bts::spartan::sim
@@ -1,46 +0,0 @@
#pragma once
#include <memory>
#include <optional>
#include "simulator_common.h"
#include "Event.h"
namespace bts::spartan::sim {
class IAgent;
class Event;
class Simulator {
public:
Simulator();
~Simulator();
void add_agent(IAgent &);
template <class EV,typename... Args>
void emit_event(TimeStamp observable_time,
Args &&... args) {
_emit_event(observable_time,std::make_shared<EV> (std::forward<Args>(args)...));
}
TimeStamp simulation_time();
std::optional<TimeStamp> pending_event_time() const;
size_t pending_events_count() const;
void end_simulation();
bool terminated() const;
bool run_once();
void run();
private:
void reset();
void prepare();
void _emit_event(TimeStamp,EventPtr const&);
struct PImpl;
std::unique_ptr<PImpl> me;
};
}
@@ -1,165 +0,0 @@
#include "Simulator2.h"
#include "Event.h"
#include "IAgent2.h"
#include "ISimulator2.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/type_name.h"
#include "server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h"
#include <queue>
namespace bts::spartan::sim {
struct EventSort {
bool operator()(EventPtr const &l, EventPtr const &r) const {
if (l->observable_time != r->observable_time)
return l->observable_time > r->observable_time;
else
return l->event_idx > r->event_idx;
}
};
using FutureEventQueue =
std::priority_queue<EventPtr, std::vector<EventPtr>, EventSort>;
using PastEventQueue = std::vector<EventPtr>;
struct Simulator2::AgentRec : ISimulator2 {
AgentRec(IAgent2 *agent, Simulator2 &owner)
: owner(&owner), agent(agent), wakeup_ts(0), next_event_idx(0) {}
void init() { agent->init(*this); }
void finit() { agent->finit(); }
// From ISimulator2
TimeStamp time() override { return owner->now(); }
void sleep(size_t ns) override {
if (is_ready())
wakeup_ts = time() + ns;
else {
bts_assert(wakeup_ts > owner->now());
wakeup_ts += ns;
}
}
void _emit(size_t delay_ns, EventPtr const &ev) override {
owner->emit(delay_ns, ev);
}
// End ISimulator2
bool is_ready() const { return owner->now() >= wakeup_ts; }
void consume_unseen_events() {
bts_assert(is_ready());
while (is_ready()) {
auto next_ev = owner->get_past_event(next_event_idx);
if (next_ev) {
next_event_idx++;
agent->handle(*next_ev);
} else
break;
}
}
TimeStamp wakeup_time() const {
bts_assert(!is_ready());
return wakeup_ts;
}
Simulator2 *owner;
IAgent2 *agent;
TimeStamp wakeup_ts;
size_t next_event_idx;
};
struct Simulator2::PImpl {
PImpl(std::initializer_list<IAgent2 *> const &ag, Simulator2 &owner)
: agents(), future_events(), past_events(), now() {
for (auto a : ag)
agents.emplace_back(a, owner);
}
std::vector<AgentRec> agents;
FutureEventQueue future_events;
PastEventQueue past_events;
TimeStamp now;
size_t event_count = 0;
};
Simulator2::Simulator2(std::initializer_list<IAgent2 *> agents)
: me(new PImpl{agents, *this}) {}
Simulator2::~Simulator2() {}
TimeStamp Simulator2::now() const { return me->now; }
void Simulator2::advance_time(TimeStamp new_time) {
bts_assert(new_time >= me->now);
me->now = new_time;
}
void Simulator2::emit(size_t delay_ns, EventPtr const &ev) {
// auto ev_ptr = ev.get();
// LOG(Spartan, Info, "Emitting: {} at: {} Eidx: {}",
// log::CharPtrWrapper{utils::type_name(typeid(*ev_ptr))},
// now() + delay_ns, me->event_count);
me->event_count++;
ev->event_idx = me->event_count;
ev->observable_time = now() + delay_ns;
me->future_events.push(ev);
}
EventPtr Simulator2::get_past_event(size_t eidx) {
if (eidx < me->past_events.size())
return me->past_events[eidx];
else
return {};
}
void Simulator2::run() {
// Prepare agents for simulation run
for (auto &a : me->agents)
a.init();
size_t nev = 0;
do {
if (nev++ % 10000 == 0)
LOG(Spartan, Info, "Simulator time: {}",
to_time_string(mdp3::MDP3TimeStamp{me->now}));
// Move events that are now observable from the future to the
// past
while (me->future_events.size()) {
auto &next_event = me->future_events.top();
if (next_event->observable_time <= now()) {
me->past_events.push_back(next_event);
me->future_events.pop();
} else
break;
}
// Give each agent that is not sleeping a chance to process any
// past events it has not seen yet
for (auto &a : me->agents) {
if (a.is_ready())
a.consume_unseen_events();
}
// Move time to the next "interesting" point, which is either
// when a currently sleeping agent will wake up or when the
// earliest future event occurs.
auto next_interesting_time = EndOfTime;
if (me->future_events.size())
next_interesting_time = me->future_events.top()->observable_time;
for (auto &a : me->agents) {
if (!a.is_ready())
next_interesting_time =
std::min(next_interesting_time, a.wakeup_time());
}
advance_time(next_interesting_time);
} while (now() != EndOfTime);
// Simulation is complete
for (auto &a : me->agents)
a.finit();
}
} // namespace bts::spartan::sim
@@ -1,29 +0,0 @@
#pragma once
#include <memory>
#include <optional>
#include "simulator_common.h"
#include "Event.h"
namespace bts::spartan::sim {
class IAgent2;
class Simulator2 {
public:
Simulator2(std::initializer_list<IAgent2*> agents);
~Simulator2();
void run();
private:
TimeStamp now() const;
void advance_time(TimeStamp new_time);
void emit(size_t delay_ns,EventPtr const&);
EventPtr get_past_event(size_t eidx);
struct AgentRec;
struct PImpl;
std::unique_ptr<PImpl> me;
};
}
@@ -1,77 +0,0 @@
// #include "../../RecordedInputEvent.h"
// #include "../../SimInput.h"
// #include "../AgentBase.h"
// #include "../Event.h"
// #include "../simulator_common.h"
// #include <catch/catch.hpp>
// #include <stdlib.h>
// namespace bts::spartan::sim::tests {
// using namespace bts::spartan::mdp3;
// struct TestEvent1 : Event {
// TestEvent1 (RecordedInputEvent const&e) : ev(e) {}
// RecordedInputEvent ev;
// };
// struct TestEvent2 : Event {
// TestEvent2 (RecordedInputEvent const&e) : ev(e) {}
// RecordedInputEvent ev;
// };
// struct RunOptions {
// RunOptions()
// : base_algo_latency_ns(0), order_update_delay_ns(0),
// mbp_update_delay_ns(0), transaction_latency_ns(0) {}
// // The base latency added to all algo actions
// size_t base_algo_latency_ns = 0;
// // The extra time it takes for an order update to be observable
// // by an algo after a sim transaction.
// uint64_t order_update_delay_ns = 0;
// // The extra time it takes for a book update to be observable
// // by an algo after a sim transaction.
// uint64_t mbp_update_delay_ns = 0;
// // The latency we use to process algo actions
// size_t transaction_latency_ns = 50000;
// };
// struct TestAgent : AgentBase {
// RunOptions options;
// void handle(Event const&e) {
// auto recorded_ev = dynamic_cast<TestEvent1 const*>(&e);
// if (recorded_ev) {
// auto send_time = recorded_ev->ev.last_packet_send_time.value();
// if (send_time > time())
// forward_clock_by(send_time - time());
// else
// forward_clock_by(options.transaction_latency_ns);
// emit_event<TestEvent2> (recorded_ev->ev);
// }
// }
// };
// TEST_CASE("Simulator Core Tests", "[simcore]") {
// auto pcap = os::sparkd_wd() + "/data_files/small_es.pcap";
// auto sim_input = make_sim_input(pcap);
// SECTION("smoke_test") {
// REQUIRE(sim_input.events.size() > 0);
// Simulator sim;
// for (auto &ev : sim_input.events)
// sim.emit_event<TestEvent1> (ev.transact_time.value(),ev);
// sim.add_agent<TestAgent>();
// while (sim.run_once())
// ;
// REQUIRE(sim.simulation_time()>0);
// }
// }
// } // namespace bts::spartan::sim::tests