Integrated commission model..

This commit is contained in:
Pedro Pinto committed 2020-12-06 12:41:07 -05:00
1 parent 8687de6fa2
commit 92bdb06ceb
10 files changed
+41 -114

No files matched your search

+1 -99
View File
@@ -13,6 +13,7 @@ create table run(
transaction_latency_ns numeric,
use_first_packet_st numeric,
realized_pandl real,
commission real,
position_pandl real,
/* algo specific parameters */
param1 text,
@@ -61,102 +62,3 @@ create table log(
);
CREATE INDEX log_idx_0 ON log(run_id);
/* Gateway log */
drop view if exists gateway_log;
create view gateway_log as
select
log_entry_order,
t_time,h_t_time,entry_type,
cast (val1 as numeric) eid1,
cast (val2 as numeric) eid2,
val3,val4,val5,val6,val7,
val8,val9
from
log
where
entry_type='gw_order' or
entry_type='gw_algo_order' or
entry_type='gw_fill' or
entry_type='gw_algo_fill'
order by
log_entry_order;
/* sniper specific views */
drop view if exists shot_beginnings;
create view shot_beginnings as
select
run_id,
t_time,
val3 polygon,
cast (val1 as numeric) o_eid,
cast (val2 as numeric) o_eid_2,
cast (val4 as real) target,
cast (val5 as numeric) quantity,
cast (val6 as numeric) streak_id
from
log
where
entry_type='shot_begin';
drop view if exists shot_outcome;
create view shot_outcome as
select
log.run_id,
log.t_time,
val1 polygon,
cast(val2 as numeric) o_eid,
cast(val3 as numeric) o_eid_2,
cast(val4 as numeric) e_eid,
cast(val5 as numeric) e_eid_2,
cast(val6 as real) target,
cast(val7 as real) realized,
cast(val8 as numeric) streak_id
from
log
where
log.entry_type='shot_outcome';
drop view if exists shot_count;
create view shot_count as
select
run_id,
count(*) n_shots,
sum( case when realized > target then 1 else 0 end) lucky_shots,
sum( case when realized = target then 1 else 0 end) good_shots,
sum( case when realized < target then 1 else 0 end) unlucky_shots
from
shot_outcome
group by
run_id ;
drop view if exists sniper_run;
create view sniper_run as
select
run.run_id,
session_id ,
begin_run_time begin_t,
end_run_time end_t,
market_date mkt_date,
channel ,
base_algo_latency_ns algo_latency,
transaction_latency_ns exchange_latency,
realized_pandl ,
position_pandl ,
cast(param1 as numeric) DoubleDip,
cast(param2 as numeric) MaxQty,
cast(param3 as numeric) MaxTransactionLatency,
cast(param4 as numeric) MinDepth,
cast(param5 as numeric) MinOrdersAtTop,
cast(param6 as numeric) MinValue,
cast(param7 as numeric) MsToWorkShot,
shot_count.n_shots,
shot_count.lucky_shots,
shot_count.unlucky_shots
from
run, shot_count
where
run.run_id = shot_count.run_id
order by realized_pandl;
@@ -195,12 +195,15 @@ void Sniper2::on_order_update(EventHeader const &h, const OrderUpdate &update) {
these_fills.end());
}
PandL pandl(me->sim->instruments());
auto cash = pandl.net_cash(all_fills);
auto commission = pandl.commission_cost(all_fills);
auto net = cash - commission;
me->sim->log(
"shot_outcome", in_flight.target.descriptor,
api::Side_Name(in_flight.target.side),
in_flight.observed.event_id.first, in_flight.observed.event_id.second,
in_flight.value_at_observation_ts * in_flight.quantity.value(),
pandl.net_cash(all_fills), me->streak_id, in_flight.quantity.value());
net,commission,in_flight.quantity.value());
me->sim->log_pandl();
if (!me->double_dip)
me->sim->stop_simulation();
@@ -155,7 +155,7 @@ void run_sim(SimDB &db, SimDB::SessionID sid, size_t remaining,
Sim sim(algo, options, source.instruments(), source.icebergs(),
source.polygons(), event_gen, logger);
auto pnl = sim.run();
db.end_run(rid, pnl.cash, pnl.position);
db.end_run(rid, pnl.cash , pnl.commission, pnl.position);
LOG(Spartan, Warn,
"Final PandL for run {}. Cash: {} Pos: {} "
"----------------------------------------------",
@@ -90,6 +90,7 @@ SimDB::RunID SimDB::begin_run(SessionID sid,
"transaction_latency_ns,"
"use_first_packet_st,"
"realized_pandl,"
"commission,"
"position_pandl,"
"param1,"
"param2,"
@@ -112,6 +113,7 @@ SimDB::RunID SimDB::begin_run(SessionID sid,
":transaction_latency_ns,"
":use_first_packet_st,"
":realized_pandl,"
":commission,"
":position_pandl,"
":param1,"
":param2,"
@@ -139,6 +141,7 @@ SimDB::RunID SimDB::begin_run(SessionID sid,
statement.bind(":use_first_packet_st",
(long long)options.use_first_packet_st());
statement.bind(":realized_pandl", 0);
statement.bind(":commission", 0);
statement.bind(":position_pandl", 0);
std::map<std::string, std::string> sorted_params(options.param().begin(),
@@ -158,7 +161,7 @@ SimDB::RunID SimDB::begin_run(SessionID sid,
return run_id;
}
void SimDB::end_run(RunID run_id, double realized_pandl,
void SimDB::end_run(RunID run_id, double realized_pandl, double commission,
double position_pandl) {
std::lock_guard<std::mutex> l(me->db_lock);
@@ -166,6 +169,7 @@ void SimDB::end_run(RunID run_id, double realized_pandl,
"set "
"end_run_time=:end_run_time, "
"realized_pandl=:realized_pandl,"
"commission=:commission,"
"position_pandl=:position_pandl "
"where "
"run_id=:run_id";
@@ -174,6 +178,7 @@ void SimDB::end_run(RunID run_id, double realized_pandl,
statement.bind(":end_run_time", datetime::time_string(datetime::now()));
statement.bind(":run_id", run_id.value());
statement.bind(":realized_pandl", realized_pandl);
statement.bind(":commission", commission);
statement.bind(":position_pandl", position_pandl);
statement.exec();
}
@@ -26,7 +26,7 @@ class SimDB {
using RunID = utils::ValueWrapper<struct RunIDTag, int>;
RunID begin_run(SessionID, protos::SimRunOptions const &);
void end_run(RunID,double realized_pandl, double position_pandl);
void end_run(RunID,double realized_pandl, double commission,double position_pandl);
void log(RunID,
size_t sim_seq,
@@ -23,6 +23,18 @@ double PandL::net_cash(std::vector<OrderUpdate> const &fills) const {
return net_cash_flow;
}
double PandL::commission_cost(std::vector<OrderUpdate> const &fills) const {
double cost = 0;
for (auto const &f : fills) {
auto n_legs = get_sec(f.security_id).legs().size();
if (n_legs == 0)
cost += f.last_fills.value() * 1.23;
else
cost += f.last_fills.value() * n_legs * 0.86;
}
return cost;
}
PandL::SymbolNetCash
PandL::symbol_net_cash(std::vector<OrderUpdate> const &fills) const {
std::unordered_map<std::string, std::vector<OrderUpdate>> fills_per_symbol;
@@ -26,7 +26,7 @@ public:
SymbolNetCash symbol_net_cash(std::vector<OrderUpdate> const &fills) const;
OutrightPosition outright_position(std::vector<OrderUpdate> const &fills) const;
double position_liquidation_value(OutrightPosition const&) const;
double commission_cost(std::vector<OrderUpdate> const &fills) const;
private:
struct PImpl;
InstrumentDefinition const &get_sec(MDP3SecurityID id) const ;
@@ -3,11 +3,13 @@
namespace bts::spartan::mdp3 {
struct PandLSummary {
double cash = 0;
double cash = 0; // net of commission
double position = 0;
double commission = 0;
PandLSummary &operator+=(PandLSummary const &rhs) {
cash += rhs.cash;
position += rhs.position;
commission += rhs.commission;
return *this;
}
};
@@ -102,7 +102,7 @@ Sim::Sim(IAlgo &algo, protos::SimRunOptions const &options,
Sim::~Sim() {}
PandLSummary Sim::run() {
me->algo.init(*this,me->options);
me->algo.init(*this, me->options);
me->algo.on_replay_start({}, {});
while (me->event_generator.top() &&
@@ -190,8 +190,7 @@ void Sim::on_pcap_event_during_replay() {
for (auto const &snap : ev.affected_securities)
me->snapshots[snap.security_id()].update_with(snap);
EventHeader header{
{ev.event_id, 0}, ev.transact_time,me->time};
EventHeader header{{ev.event_id, 0}, ev.transact_time, me->time};
for (auto &snap : ev.affected_securities) {
if (snap.ready_to_trade())
@@ -502,13 +501,14 @@ PandLSummary Sim::pnl() const {
auto outright_pos = pl.outright_position(me->my_fills);
auto cash = pl.net_cash(me->my_fills);
auto commission = pl.commission_cost(me->my_fills);
auto pos = pl.position_liquidation_value(outright_pos);
return {cash, pos};
return {cash-commission, pos, commission};
}
void Sim::log_pandl() const {
auto pnl_summary = pnl();
log("pnl", pnl_summary.cash, pnl_summary.position);
log("pnl", pnl_summary.cash, pnl_summary.commission, pnl_summary.position);
}
MDP3OrderKey Sim::send(SmallOrderChange const &change,
@@ -384,16 +384,19 @@ struct ObsWrapper : mdp3::IChannelObserver {
real_obs.on_recovery_complete(s);
}
void on_real_time_event(mdp3::MDP3Event const &ev) override {
if (ms_diff(Timer::now(), last_print) >= 10000) {
auto period_ms = 10000;
if (ms_diff(Timer::now(), last_print) >= period_ms) {
last_print = Timer::now();
std::stringstream str;
mdp3::cme_ts_to_time_str(ev.transact_time.value(), &str);
std::stringstream str2;
str2 << "Packet time: " << str.str() << " Event ID: " << ev.event_id
<< " (" << (ev.first_packet_sn - previous_packet) << " pkt/sec, "
<< " ("
<< (ev.first_packet_sn - previous_packet) / (period_ms / 1000)
<< " pkt/sec, "
<< (ev.first_packet_send_time.value() - previous_send_ts) /
1000000000
<< "x speedup)" ;
1000000000 / (period_ms / 1000)
<< "x speedup)";
LOG(Spartan, Warn, "{}", str2.str());
previous_packet = ev.first_packet_sn;
previous_send_ts = ev.first_packet_send_time.value();