Cleaned up simulator input generation.

This commit is contained in:
Pedro Pinto committed 2020-09-30 08:25:33 -04:00
1 parent 617b4e2bba
commit 110af7795e
15 files changed
+88 -141

No files matched your search

@@ -15,7 +15,7 @@ namespace orderman {
class TestSimServices : public bts::spartan::mdp3::ISimServices{
private:
std::vector<InstrumentDefinition const*> _instruments;
InstrumentMap _instruments;
public:
OrderChange last_change;
size_t n_updates = 0;
@@ -25,7 +25,7 @@ public:
:
key(0){
}
std::vector<InstrumentDefinition const*> const &instruments() const override{
std::unordered_map<MDP3SecurityID,InstrumentDefinition const*> const &instruments() const override{
bts_assert(false);
return _instruments;
}
+2 -2
View File
@@ -214,9 +214,9 @@ void validate_fifo(std::string const &pcap) {
}
void gen_sim_input(std::string const &pcap) {
auto input = make_sim_input(pcap);
auto input = make_sim_input_from_pcap(pcap);
std::cout << "Secs: " << input.instruments.size()
<< " events: " << input.events.size()
<< " events: " << input.transactions.size()
<< " max_order_id: " << input.max_order_id << std::endl;
}
@@ -27,15 +27,17 @@ struct ExchangeSim::PImpl {
bts_assert(i.size());
for (auto &i : inputs) {
max_order_id.value() = std::max(i.max_order_id, max_order_id.value());
for (auto &i : i.instruments)
all_definitions.push_back(&i);
max_order_id = MDP3OrderKey{
std::max(i.max_order_id.value(), max_order_id.value())};
for (auto &[k, i] : i.instruments)
all_definitions[k] = &i;
}
bts_assert(all_definitions.size());
}
std::vector<SimInput> inputs;
MDP3OrderKey max_order_id;
std::vector<InstrumentDefinition const *> all_definitions;
std::unordered_map<MDP3SecurityID, InstrumentDefinition const *>
all_definitions;
std::optional<RunState> run_state;
};
@@ -73,7 +75,7 @@ void ExchangeSim::prepare_run(std::vector<IAlgoModel *> const &algos,
// Load up all the recorded events in the simulator
for (auto &i : me->inputs)
for (auto &rev : i.events)
for (auto &rev : i.transactions)
me->run_state->simulator.emit_event<RecordedOrderChangeEvent>(
rev.transact_time.value(), rev);
bts_assert(me->run_state->simulator.pending_events_count());
@@ -85,7 +87,7 @@ void ExchangeSim::prepare_run(std::vector<IAlgoModel *> const &algos,
}
std::vector<double> ExchangeSim::run(std::vector<IAlgoModel *> const &algos,
const SimOptions &options) {
const SimOptions &options) {
prepare_run(algos, options);
while (has_work())
@@ -1,10 +1,12 @@
#pragma once
#include "../../common/mdp3_common.h"
#include "../../handler/IChannelObserver.h"
namespace bts::spartan::mdp3 {
struct RecordedOrderChange {
// Generate a transaction stream that explains the pcap
struct ExchangeTransaction {
size_t event_id;
MDP3TimeStamp transact_time;
MDP3TimeStamp first_packet_send_time;
@@ -14,4 +16,4 @@ struct RecordedOrderChange {
std::vector<OrderChange> order_changes;
};
} // namespace bts::spartan::mdp3
}
@@ -1,6 +1,7 @@
#pragma once
#include "../../common/mdp3_common.h"
#include "../../common/InstrumentMap.h"
#include "AlgoAction.h"
namespace bts::spartan::mdp3 {
@@ -9,7 +10,7 @@ class InstrumentDefinition;
class ISimServices {
public:
virtual ~ISimServices();
virtual std::vector<InstrumentDefinition const*> const &instruments() const = 0;
virtual InstrumentMap const &instruments() const = 0;
virtual MDP3OrderKey generate_order_key() = 0;
virtual MDP3TimeStamp now() const = 0;
virtual void wait(uint64_t ns) = 0;
@@ -2,85 +2,26 @@
#include "../../common/InstrumentSnapshot.h"
#include "../../handler/IChannelObserver.h"
#include "../../utils/mdp3_utils.h"
#include "../../utils/queries/TransactionDeducer.h"
#include "../../utils/queries/DefinitionCollector.h"
namespace bts::spartan::mdp3 {
SimInput make_sim_input(std::string const &pcap) {
SimInput make_sim_input_from_pcap(std::string const &pcap) {
TransactionDeducer td;
DefinitionCollector dc;
struct Obs : IChannelObserver {
observe_channel(pcap,{&td,&dc});
SimInput result;
std::unordered_map<MDP3SecurityID, InstrumentDefinition> instruments;
std::vector<RecordedOrderChange> events;
std::unordered_set<MDP3SecurityID> ready_secs;
void on_recovery_complete(
std::vector<InstrumentSnapshot const *> const &snap) override {
for (auto const &s : snap)
instruments.insert({s->security_id(), s->definition()});
}
void on_real_time_event(MDP3Event const &ev) override {
result.events.emplace_back();
auto &this_event = result.events.back();
this_event.event_id = ev.event_id;
this_event.transact_time = ev.transact_time;
this_event.first_packet_send_time = ev.first_packet_send_time;
this_event.last_packet_send_time = ev.last_packet_send_time;
this_event.first_packet_sn = ev.first_packet_sn;
this_event.last_packet_sn = ev.last_packet_sn;
for (auto const &s : ev.affected_securities) {
if (instruments.count(s->security_id()) == 0)
instruments.insert({s->security_id(), s->definition()});
bool sec_ready = ready_secs.count(s->security_id());
if (!sec_ready && s->trading_status() ==
::mdp3::SecurityTradingStatus::ReadyToTrade) {
// Transitioned to ready, boostrap book with existing orders
ready_secs.insert(s->security_id());
for (auto const &p : s->orders()) {
OrderChange change;
change.previous_order = p.second;
change.order = p.second;
change.change = OrderChange::added;
this_event.order_changes.push_back(change);
result.max_order_id =
std::max(change.order.key.value(), result.max_order_id);
}
} else if (sec_ready &&
s->trading_status() ==
::mdp3::SecurityTradingStatus::ReadyToTrade) {
// Was ready, is ready, accumulate incremental change
auto changes = s->deduced_order_changes();
if (changes.empty())
changes = s->order_changes();
for (auto &c : changes) {
this_event.order_changes.push_back(c);
result.max_order_id =
std::max(c.order.key.value(), result.max_order_id);
}
} else if (sec_ready && s->trading_status() &&
s->trading_status() !=
::mdp3::SecurityTradingStatus::ReadyToTrade) {
ready_secs.erase(s->security_id());
}
}
}
void finit() override {
for (auto const &p : instruments)
result.instruments.push_back(p.second);
}
void on_real_time_channel_loss() override { bts_assert(false); }
} obs;
observe_channel(pcap, obs);
obs.finit();
return obs.result;
SimInput result;
result.instruments = dc.result;
result.transactions = td.result;
MDP3OrderKey::Value max_ok = 0;
for (auto & t : result.transactions)
for (auto & o : t.order_changes)
max_ok = std::max(o.order.key.value(), max_ok);
result.max_order_id = MDP3OrderKey {max_ok};
return result;
}
} // namespace bts::spartan::mdp3
@@ -1,16 +1,16 @@
#pragma once
#include "../../common/InstrumentDefinition.h"
#include "RecordedOrderChange.h"
#include "ExchangeTransaction.h"
namespace bts::spartan::mdp3 {
struct SimInput {
std::vector<InstrumentDefinition> instruments;
std::vector<RecordedOrderChange> events;
size_t max_order_id = 0;
std::unordered_map<MDP3SecurityID, InstrumentDefinition> instruments;
std::vector<ExchangeTransaction> transactions;
MDP3OrderKey max_order_id;
};
SimInput make_sim_input(std::string const&pcap);
SimInput make_sim_input_from_pcap(std::string const &pcap);
}
} // namespace bts::spartan::mdp3
@@ -5,7 +5,7 @@
namespace bts::spartan::mdp3 {
struct AlgoAgent::PImpl {
std::vector<InstrumentDefinition const *> const &definitions;
InstrumentMap const &instruments;
MDP3OrderKey *last_order_key;
SimOptions options;
IAlgoModel &algo;
@@ -13,16 +13,14 @@ struct AlgoAgent::PImpl {
std::vector<OrderUpdate> my_fills = {};
};
AlgoAgent::AlgoAgent(
std::vector<InstrumentDefinition const *> const &instruments,
MDP3OrderKey *next_order_key, SimOptions const &options, IAlgoModel &algo)
AlgoAgent::AlgoAgent(InstrumentMap const &instruments,
MDP3OrderKey *next_order_key, SimOptions const &options,
IAlgoModel &algo)
: me(new PImpl{instruments, next_order_key, options, algo}) {}
AlgoAgent::AlgoAgent(AlgoAgent &&src) : me () {
operator=(std::move(src));
}
AlgoAgent::AlgoAgent(AlgoAgent &&src) : me() { operator=(std::move(src)); }
AlgoAgent& AlgoAgent::operator=(AlgoAgent &&src) {
AlgoAgent &AlgoAgent::operator=(AlgoAgent &&src) {
me = std::move(src.me);
return *this;
}
@@ -32,11 +30,11 @@ void AlgoAgent::init() { me->algo.init(*this); }
double AlgoAgent::pandl() const {
double result = 0;
for (auto f : me->my_fills) {
auto flow = (f.last_fills.value() * f.last_price.price());
auto flow = (f.last_fills.value() * f.last_price.price());
if (f.side == api::SIDE_BUY)
result -= flow;
result -= flow;
else
result += flow;
result += flow;
}
return result;
}
@@ -61,9 +59,7 @@ void AlgoAgent::handle(sim::Event const &ev) {
AlgoAgent::~AlgoAgent() {}
std::vector<InstrumentDefinition const*> const &AlgoAgent::instruments() const {
return me->definitions;
}
InstrumentMap const &AlgoAgent::instruments() const { return me->instruments; }
MDP3OrderKey AlgoAgent::generate_order_key() {
return MDP3OrderKey{++me->last_order_key->value()};
@@ -13,7 +13,7 @@ class InstrumentDefinition;
class AlgoAgent : ISimServices, public sim::AgentBase {
public:
AlgoAgent(std::vector<InstrumentDefinition const*> const &,
AlgoAgent(InstrumentMap const &,
MDP3OrderKey *next_order,
SimOptions const&,
IAlgoModel &);
@@ -24,7 +24,7 @@ class AlgoAgent : ISimServices, public sim::AgentBase {
void init() override;
void handle(sim::Event const &) override;
private:
std::vector<InstrumentDefinition const*> const &instruments() const override;
InstrumentMap const &instruments() const override;
MDP3OrderKey generate_order_key() override;
MDP3TimeStamp now() const override ;
void wait (uint64_t ns) override;
@@ -2,36 +2,39 @@
#include "../../../common/InstrumentDefinition.h"
#include "../../../common/mdp3_common.h"
#include "../../matcher/Matcher.h"
#include "../events/SimEvents.h"
#include "../SimOptions.h"
#include "../events/SimEvents.h"
#include <unordered_set>
namespace bts::spartan::mdp3 {
struct GatewayAgent::PImpl {
std::unordered_set<MDP3SecurityID> my_securities;
PImpl(std::unordered_map<MDP3SecurityID, InstrumentDefinition> const &defs,
SimOptions const &options)
: my_instruments(defs), options(options) {}
std::unordered_map<MDP3SecurityID, InstrumentDefinition> const
&my_instruments;
SimOptions options;
Matcher matcher;
};
GatewayAgent::GatewayAgent(std::vector<InstrumentDefinition> const &defs,
SimOptions const &options)
: me(new PImpl()) {
for (auto const &d : defs)
me->my_securities.insert(d.security_id());
GatewayAgent::GatewayAgent(
std::unordered_map<MDP3SecurityID, InstrumentDefinition> const &defs,
SimOptions const &options)
: me(new PImpl(defs, options)) {
me->options = options;
}
GatewayAgent::GatewayAgent(GatewayAgent&&src) : me () {
GatewayAgent::GatewayAgent(GatewayAgent &&src) : me() {
operator=(std::move(src));
}
GatewayAgent& GatewayAgent::operator=(GatewayAgent&&src) {
GatewayAgent &GatewayAgent::operator=(GatewayAgent &&src) {
me = std::move(src.me);
return *this;
}
GatewayAgent::~GatewayAgent() {}
void GatewayAgent::handle(const sim::Event &ev) {
@@ -44,7 +47,6 @@ void GatewayAgent::handle(const sim::Event &ev) {
_handle(*aa);
}
// Handle a recorded order change, that is an order change we got from
// a pcap recording of the market data
void GatewayAgent::_handle(RecordedOrderChangeEvent const &e) {
@@ -53,7 +55,7 @@ void GatewayAgent::_handle(RecordedOrderChangeEvent const &e) {
auto &first_change = ev.order_changes[0];
if (me->my_securities.count(first_change.order.security)) {
if (me->my_instruments.count(first_change.order.security)) {
// Advance our clock either to the send time in the recording
// (unless we are already ahead of that time, possibly because
@@ -13,16 +13,18 @@ class InstrumentDefinition;
class GatewayAgent : public sim::AgentBase {
public:
GatewayAgent(std::vector<InstrumentDefinition> const &, SimOptions const &);
GatewayAgent(GatewayAgent&&);
GatewayAgent& operator=(GatewayAgent&&);
GatewayAgent(
std::unordered_map<MDP3SecurityID, InstrumentDefinition> const &,
SimOptions const &);
GatewayAgent(GatewayAgent &&);
GatewayAgent &operator=(GatewayAgent &&);
~GatewayAgent();
void handle(sim::Event const &) override;
private:
void _handle(RecordedOrderChangeEvent const&);
void _handle(AlgoActionEvent const&);
void _handle(RecordedOrderChangeEvent const &);
void _handle(AlgoActionEvent const &);
struct PImpl;
std::unique_ptr<PImpl> me;
};
@@ -3,7 +3,7 @@
#include "../../../common/mdp3_common.h"
#include "../AlgoEvent.h"
#include "../AlgoAction.h"
#include "../RecordedOrderChange.h"
#include "../ExchangeTransaction.h"
#include "../core/Event.h"
namespace bts::spartan::mdp3 {
@@ -13,7 +13,7 @@ template <class EV> struct EventWrapper : sim::Event {
EV ev;
};
using RecordedOrderChangeEvent = EventWrapper<RecordedOrderChange>;
using RecordedOrderChangeEvent = EventWrapper<ExchangeTransaction>;
using AlgoActionEvent = EventWrapper<AlgoAction>;
using AlgoNotificationEvent = EventWrapper<AlgoNotification>;
struct TimerEvent : sim::Event {};
@@ -0,0 +1,10 @@
#pragma once
#include <unordered_map>
#include "mdp3_common.h"
namespace bts::spartan::mdp3 {
class InstrumentDefinition;
using InstrumentMap =
std::unordered_map<MDP3SecurityID, InstrumentDefinition const*>;
}
@@ -4,7 +4,7 @@
namespace bts::spartan::mdp3 {
struct TransactionDeducer::PImpl {
std::vector<Transaction> events;
std::vector<ExchangeTransaction> events;
std::unordered_set<MDP3SecurityID> ready_secs;
};
@@ -2,22 +2,13 @@
#include "../../common/mdp3_common.h"
#include "../../handler/IChannelObserver.h"
#include "../../back_testing/simulator/ExchangeTransaction.h"
namespace bts::spartan::mdp3 {
// Generate a transaction stream that explains the pcap
struct Transaction {
size_t event_id;
MDP3TimeStamp transact_time;
MDP3TimeStamp first_packet_send_time;
MDP3TimeStamp last_packet_send_time;
uint32_t first_packet_sn;
uint32_t last_packet_sn;
std::vector<OrderChange> order_changes;
};
struct TransactionDeducer : IChannelObserver {
std::vector<Transaction> result;
std::vector<ExchangeTransaction> result;
TransactionDeducer();
~TransactionDeducer();