Changed sniper, md

This commit is contained in:
Pedro Pinto committed 2020-10-10 10:15:09 -04:00
1 parent 64f35f869e
commit 3a73b6cb9e
12 files changed
+183 -133

No files matched your search

+4 -2
View File
@@ -22,6 +22,8 @@ create table run(
/* Clock time when we started / ended this run */
begin_time text,
end_time text,
realized_pandl real,
position_pandl real,
foreign key(session_id) references session(session_id)
);
@@ -59,8 +61,8 @@ specific. Some of the standard entries are:
- order_update ->* status,symbol, side, last_fills, last_price, ok, sk
*/
drop table if exists log_entry;
create table log_entry(
drop table if exists log;
create table log(
log_entry_id integer primary_key,
run_id numeric,
t_time numeric,
+3 -3
View File
@@ -71,7 +71,7 @@ int load_sim_input(std::string const &sim_input) {
}
}
int run_sim(std::string const &input_file) {
int sim(std::string const &input_file) {
spartan::protos::SimSessionOptions options;
std::ifstream proto_in(input_file);
std::string str((std::istreambuf_iterator<char>(proto_in)),
@@ -257,9 +257,9 @@ void validate_fifo(std::string const &pcap) {
std::map<std::string, std::function<int(std::vector<std::string> const &)>>
extensions = {
{"run_sim",
{"sim",
[](auto const &args) {
run_sim(args[1]);
sim(args[1]);
return 0;
}},
@@ -5,8 +5,8 @@
#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/common/InstrumentDefinition.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>
@@ -15,7 +15,6 @@ namespace bts::spartan::mdp3 {
enum class Sniper::State { WATCH, WAIT };
struct Sniper::Target {
constexpr static double Worthless = -1.0;
@@ -23,13 +22,9 @@ struct Sniper::Target {
std::string const descriptor;
double value = Worthless;
void mark_as_worthless() {
value = Worthless;
}
void mark_as_worthless() { value = Worthless; }
bool has_value () const {
return value > securities::market_epsilon();
}
bool has_value() const { return value > securities::market_epsilon(); }
};
struct Sniper::Shot {
@@ -41,7 +36,7 @@ struct Sniper::Shot {
struct Sniper::DependentTargets {
MBPUpdate book;
std::vector<Target*> dependencies;
std::vector<Target *> dependencies;
};
struct Sniper::PImpl {
@@ -82,17 +77,17 @@ void Sniper::init(ISimServices &s, protos::SimRunOptions const &options) {
me->observable_time = MDP3TimeStamp{0};
for (auto &p : s.polygons()) {
auto buy_t = Target{
p.second, polygon_descriptor(p.second, me->sim->instruments())};
auto sell_t = Target{
flip(p.second),
polygon_descriptor(flip(p.second), me->sim->instruments())};
auto buy_t = Target{p.second,
polygon_descriptor(p.second, me->sim->instruments())};
auto sell_t =
Target{flip(p.second),
polygon_descriptor(flip(p.second), me->sim->instruments())};
me->targets.emplace_back(buy_t);
me->targets.emplace_back(sell_t);
}
for (auto & t:me->targets) {
for (auto &t : me->targets) {
for (auto &pp : t.polygon.parts) {
auto &dependencies = me->books[pp.sec_id].dependencies;
dependencies.push_back(&t);
@@ -113,7 +108,7 @@ api::Side side_from_ratio(int ratio) {
return api::Side::SIDE_SELL;
}
void Sniper::eval(std::vector<Target*> const&targets) {
void Sniper::eval(std::vector<Target *> const &targets) {
for (auto &t : targets) {
t->value = 0;
for (auto &l : t->polygon.parts) {
@@ -122,8 +117,7 @@ void Sniper::eval(std::vector<Target*> const&targets) {
if (mkt[0].n_orders < me->min_orders_at_top) {
t->mark_as_worthless();
break;
}
else {
} else {
auto cash = me->sim->get_sec(l.sec_id).to_cash(mkt[0].price);
t->value += -l.ratio * cash;
}
@@ -131,7 +125,7 @@ void Sniper::eval(std::vector<Target*> const&targets) {
}
}
Sniper::MaybeShot Sniper::pick(std::vector<Target*> &targets) {
Sniper::MaybeShot Sniper::pick(std::vector<Target *> &targets) {
std::sort(targets.begin(), targets.end(),
[](Target const *ptr1, Target const *ptr2) {
if (std::abs(ptr1->value - ptr2->value) >
@@ -145,10 +139,12 @@ Sniper::MaybeShot Sniper::pick(std::vector<Target*> &targets) {
if (targets[0]->has_value()) {
for (auto t : targets)
if (t->has_value())
me->sim->log("opportunity",t->descriptor,t->value);
return Shot{*targets[0], me->sim->now(), targets[0]->value, {}};
}
else
me->sim->log("opportunity", t->descriptor, t->value);
if (me->order_qty)
return Shot{*targets[0], me->sim->now(), targets[0]->value, {}};
else
return {};
} else
return {};
}
@@ -181,9 +177,11 @@ void Sniper::fire(Shot const &s) {
order_change.change = OrderChange::removed;
me->sim->send(order_change);
}
me->sim->log("shot", shot.target.descriptor, shot.observed_ts.value(),
shot.value_at_observation_ts);
}
void Sniper::eval_pick_fire(std::vector<Target*> &targets) {
void Sniper::eval_pick_fire(std::vector<Target *> &targets) {
bts_assert(me->state == State::WATCH);
eval(targets);
auto best_shot = pick(targets);
@@ -195,13 +193,13 @@ void Sniper::eval_pick_fire(std::vector<Target*> &targets) {
void Sniper::operator()(const MBPUpdate &mbp) {
auto it = me->books.find(mbp.security_id) ;
auto it = me->books.find(mbp.security_id);
if (it == me->books.end())
return;
auto &book = it->second;
book.book = mbp;
switch (me->state) {
case State::WATCH:
eval_pick_fire(book.dependencies);
@@ -218,35 +216,34 @@ void Sniper::operator()(const OrderUpdate &update) {
bts_assert(in_flight.fills.count(update.key));
in_flight.fills[update.key].push_back(update);
auto all_done = true;
auto pending_orders = in_flight.fills.size();
for (auto const &p : in_flight.fills) {
auto &these_fills = p.second;
if (these_fills.empty() ||
these_fills.back().status == OrderUpdate::PARTIALLY_FILLED) {
all_done = false;
break;
}
for (auto &f : these_fills)
if (f.status == OrderUpdate::CANCELED ||
f.status == OrderUpdate::FILLED) {
pending_orders--;
break;
}
}
if (all_done) {
if (pending_orders == 0) {
std::vector<OrderUpdate> all_fills;
for (auto const &p : in_flight.fills) {
auto &these_fills = p.second;
all_fills.insert(all_fills.end(), these_fills.begin(),
these_fills.end());
}
PandL pandl (me->sim->instruments());
me->sim->log("shot",
in_flight.target.descriptor,
PandL pandl(me->sim->instruments());
me->sim->log("shot_outcome", in_flight.target.descriptor,
in_flight.observed_ts.value(),
in_flight.value_at_observation_ts,
me->sim->now().value(),
in_flight.value_at_observation_ts, me->sim->now().value(),
pandl.net_cash(all_fills));
me->sim->log_pandl();
me->in_flight.reset();
transition(State::WATCH);
std::vector<Target*> all_targets_ptr;
std::vector<Target *> all_targets_ptr;
for (auto &t : me->targets)
all_targets_ptr.push_back(&t);
eval_pick_fire(all_targets_ptr);
@@ -39,10 +39,8 @@ FifoSecurityMatcher::_find_order(MDP3OrderKey key) {
auto map_it = _order_map.find(key);
if(map_it != _order_map.end()){
auto& ord = map_it->second;
if(ord.is_active){
FifoPriceLevelMatcher* p_level = _find_price_level(ord.side,ord.price);
return std::make_tuple(p_level,&ord);
}
FifoPriceLevelMatcher* p_level = _find_price_level(ord.side,ord.price);
return std::make_tuple(p_level,&ord);
}
return std::tuple<FifoPriceLevelMatcher *, MatcherOrder *>(
nullptr, nullptr);
@@ -68,7 +66,7 @@ void FifoSecurityMatcher::_cleanup() {
});
}
void FifoSecurityMatcher::_on_new_order(const MDP3Order &ord,
std::vector<MatchEffect>* res) {
std::vector<MatchEffect>* res) {
_log_trace_order(ord,"FifoSecurityMatcher::_on_new_order begin");
auto& opp_book = _book(_opp_side(ord.side));
uint32_t new_ord_rem = total_quantity(ord).value();
@@ -116,16 +114,6 @@ void FifoSecurityMatcher::_on_new_order(const MDP3Order &ord,
ord_corrected.quantity = MDP3Quantity(new_ord_rem);
price_level->on_new_same_side_order(ord_corrected);
}
else{
// since _on_new_order is also the same code path
// thats called when an order changes price levels
auto it = _order_map.find(ord.key);
if(it != _order_map.end()){
MatcherOrder& ex_ord = it->second;
ex_ord.quantity = MDP3Quantity(0);
ex_ord.is_active = false;
}
}
}
void FifoSecurityMatcher::_on_order_update(const MDP3Order &ord,
@@ -156,8 +156,8 @@ void SimOrchestrator::run() {
db.log(rid, ts, entry_type, args);
};
ExchangeSim sim(logger, r->inputs);
sim.run(*algo, r->options);
db.end_run(rid);
auto pandl = sim.run(*algo, r->options);
db.end_run(rid, pandl.cash, pandl.position);
}
});
}
@@ -13,12 +13,20 @@
namespace bts::spartan::mdp3 {
struct SimDB::PendingLog {
RunID rid;
MDP3TimeStamp ts;
std::string log_entry_type;
std::vector<std::string> args;
};
struct SimDB::PImpl {
PImpl(std::string const &loc)
: db(loc, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE), lock() {}
SQLite::Database db;
std::mutex lock;
std::vector<PendingLog> pending;
};
SimDB::SimDB(std::string const &loc) {
@@ -101,53 +109,73 @@ SimDB::RunID SimDB::begin_run(SessionID sid,
return run_id;
}
void SimDB::end_run(RunID run_id) {
void SimDB::end_run(RunID run_id, double realized_pandl,
double position_pandl) {
std::lock_guard<std::mutex> l(me->lock);
auto update_run = "update run "
"set "
"end_time=:end_time "
"end_time=:end_time, "
"realized_pandl=:realized_pandl,"
"position_pandl=:position_pandl "
"where "
"run_id=:run_id";
SQLite::Statement statement(me->db, update_run);
statement.bind(":end_time", datetime::time_string(datetime::now()));
statement.bind(":run_id", run_id.value());
statement.bind(":realized_pandl", realized_pandl);
statement.bind(":position_pandl", position_pandl);
statement.exec();
if (me->pending.size())
_log(true);
}
void SimDB::log(RunID rid, MDP3TimeStamp now, std::string const &entry_type,
std::initializer_list<std::string> const &log_args) {
bts_assert(log_args.size());
return;
std::lock_guard<std::mutex> l(me->lock);
me->pending.emplace_back(PendingLog{rid, now, entry_type, log_args});
_log(false);
}
std::ostringstream insert_log_entry;
insert_log_entry << "insert into log_entry("
"run_id,"
"t_time,"
"h_t_time,"
"entry_type";
for (size_t i = 0; i < log_args.size(); i++)
insert_log_entry << ",val" << i + 1;
insert_log_entry << ") values (:run_id,:t_time,:h_t_time,:entry_type";
for (size_t i = 0; i < log_args.size(); i++)
insert_log_entry << ",:val" << i + 1;
insert_log_entry << ")";
void SimDB::_log(bool flush) {
if (me->pending.size() > 100 || flush) {
me->db.exec("begin");
for (auto &l : me->pending) {
auto rid = l.rid;
auto now = l.ts;
auto &entry_type = l.log_entry_type;
auto &log_args = l.args;
SQLite::Statement statement(me->db, insert_log_entry.str());
statement.bind(":run_id", rid.value());
statement.bind(":t_time", (long long)now.value());
statement.bind(":h_t_time", to_time_string(now));
statement.bind(":entry_type", entry_type);
std::ostringstream insert_log_entry;
insert_log_entry << "insert into log("
"run_id,"
"t_time,"
"h_t_time,"
"entry_type";
for (size_t i = 0; i < log_args.size(); i++)
insert_log_entry << ",val" << i + 1;
insert_log_entry << ") values (:run_id,:t_time,:h_t_time,:entry_type";
for (size_t i = 0; i < log_args.size(); i++)
insert_log_entry << ",:val" << i + 1;
insert_log_entry << ")";
size_t i = 1;
for (auto &arg : log_args) {
statement.bind(fmt::format(":val{}", i++), arg);
SQLite::Statement statement(me->db, insert_log_entry.str());
statement.bind(":run_id", rid.value());
statement.bind(":t_time", (long long)now.value());
statement.bind(":h_t_time", to_time_string(now));
statement.bind(":entry_type", entry_type);
size_t i = 1;
for (auto &arg : log_args) {
statement.bind(fmt::format(":val{}", i++), arg);
}
statement.exec();
}
me->db.exec("commit");
me->pending.clear();
}
statement.exec();
}
void SimDB::_add_param(RunID run_id, std::string const &param_name,
@@ -23,7 +23,7 @@ class SimDB {
using RunID = utils::ValueWrapper<struct RunIDTag, int>;
RunID begin_run(SessionID, protos::SimRunOptions const &);
void end_run(RunID);
void end_run(RunID,double realized_pandl, double position_pandl);
void log(RunID, MDP3TimeStamp, std::string const &log_entry_type,
std::initializer_list<std::string> const &log_args);
@@ -41,7 +41,9 @@ class SimDB {
void _add_param(RunID, std::string const &param_name,
std::string const &param_value);
void _log(bool flush = false);
int last_insert_rowid();
struct PendingLog;
struct PImpl;
std::unique_ptr<PImpl> me;
};
@@ -61,8 +61,9 @@ ExchangeSim::ExchangeSim(SimLogger const &logger,
ExchangeSim::~ExchangeSim() {}
void ExchangeSim::run(IAlgoModel &algo, const protos::SimRunOptions &options) {
run({&algo}, options);
PandLSummary ExchangeSim::run(IAlgoModel &algo,
const protos::SimRunOptions &options) {
return run({&algo}, options);
}
void ExchangeSim::prepare_run(std::vector<IAlgoModel *> const &algos,
@@ -107,15 +108,17 @@ void ExchangeSim::prepare_run(std::vector<IAlgoModel *> const &algos,
me->run_state->simulator.emit_event<TimerEvent>(first_ev_ts.value() - 1);
}
void ExchangeSim::run(std::vector<IAlgoModel *> const &algos,
const protos::SimRunOptions &options) {
PandLSummary ExchangeSim::run(std::vector<IAlgoModel *> const &algos,
const protos::SimRunOptions &options) {
prepare_run(algos, options);
while (has_work())
do_work();
PandLSummary summary;
for (auto &a : me->run_state->algos)
a.finit();
summary += a.finit();
return summary;
}
bool ExchangeSim::has_work() const {
@@ -147,19 +150,16 @@ void ExchangeSim::do_work() {
completed_events - me->run_state->last_completed_count;
size_t elapsed_secs = sec_diff(now, me->run_state->run_start);
auto eta = (remaining_events * elapsed_secs / completed_events) ;
auto eta = (remaining_events * elapsed_secs / completed_events);
auto TPS = completed_this_cycle / log_period_s;
me->run_state->last_time_log = now;
me->run_state->last_sim_time = sim_time;
me->run_state->last_completed_count = completed_events;
LOG(Spartan, Info,
"Simulation Status -> Time: {} TPS: {} Complete: {}% ETA: {} secs",
to_time_string(MDP3TimeStamp{sim_time}),
TPS,
p_complete,
eta);
to_time_string(MDP3TimeStamp{sim_time}), TPS, p_complete, eta);
}
}
@@ -3,6 +3,7 @@
#include "../../common/mdp3_common.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 "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/PandLSummary.h"
#include "IExchangeServices.h"
#include "SimInput.h"
@@ -21,12 +22,12 @@ class ExchangeSim : public IExchangeServices {
~ExchangeSim();
// Run a single algo through all the SimInput events
void run(IAlgoModel &,
const protos::SimRunOptions & = protos::SimRunOptions());
PandLSummary run(IAlgoModel &,
const protos::SimRunOptions & = protos::SimRunOptions());
// Run a set of algos
void run(std::vector<IAlgoModel *> const &,
const protos::SimRunOptions & = protos::SimRunOptions());
PandLSummary run(std::vector<IAlgoModel *> const &,
const protos::SimRunOptions & = protos::SimRunOptions());
private:
struct PImpl;
@@ -0,0 +1,15 @@
#pragma once
namespace bts::spartan::mdp3 {
struct PandLSummary {
double cash = 0;
double position = 0;
PandLSummary &operator+=(PandLSummary const &rhs) {
cash += rhs.cash;
position += rhs.position;
return *this;
}
};
} // namespace bts::spartan::mdp3
@@ -66,39 +66,46 @@ void AlgoAgent::handle(sim::Event const &ev) {
}
}
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) {
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;
return price_at_market(id, qty, s);
};
PandL pl{me->instruments, mkt_pricer};
auto outright_pos = pl.outright_position(me->my_fills) ;
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)
log("outright_pos",get_sec(id).symbol(),pos);
if (pos)
log("outright_pos", get_sec(id).symbol(), pos);
auto per_symbol_pnl = pl.symbol_net_cash(me->my_fills) ;
auto per_symbol_pnl = pl.symbol_net_cash(me->my_fills);
for (auto &[symbol, cash] : per_symbol_pnl)
log("symbol_pnl", symbol, cash);
if (std::abs(cash) > securities::market_epsilon())
log("symbol_pnl", symbol, cash);
}
AlgoAgent::~AlgoAgent() {}
@@ -136,13 +143,25 @@ void AlgoAgent::send(OrderChange const &change, uint64_t propagation_delay) {
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 << " ";
str << to_time_string(now()) << " " << log_entry_type << " ";
for (auto const &a : log_args)
str << a << " ";
LOG(Spartan,Info,"{}", str.str());
LOG(Spartan, Info, "{}", str.str());
me->services.log(now(), log_entry_type, log_args);
}
void AlgoAgent::finit() { log_pandl(); }
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
@@ -5,6 +5,7 @@
#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>
@@ -30,8 +31,7 @@ class AlgoAgent : ISimServices, public sim::AgentBase {
// From IAgent
void init() override;
void handle(sim::Event const &) override;
void finit();
PandLSummary finit();
private:
// From ISimservices
@@ -47,9 +47,7 @@ private:
void send(OrderChange const &, uint64_t propagation_delay) override;
// End ISimServices
double price_to_cash(MDP3SecurityID, double) const;
double net_cash_flow(std::vector<OrderUpdate> const&fills) const;
std::unordered_map<std::string,double> per_symbol_net_cash_flow() const;
double price_at_market(MDP3SecurityID id, MDP3Quantity qty, api::Side) const;
struct PImpl;
std::unique_ptr<PImpl> me;
};