Merged
This commit is contained in:
commit
aa975ea1c5
179 files changed
+4312
-3795
No files matched your search
@@ -28,3 +28,5 @@ src/server/sparkd/orderman/perf_analysis/trace_data/
|
||||
*.fsx.lock
|
||||
src/lib/orderman/perf_analysis/logs/latency_data/
|
||||
trace_data/
|
||||
/src/lib/orderman/perf_analysis/last_pretty_log.csv
|
||||
/src/lib/orderman/perf_analysis/log.csv
|
||||
@@ -9,7 +9,9 @@
|
||||
- The Left Pane can now grown and shrunk horizontally via the splitter between the left pane and the main ladder area
|
||||
### Bug Fixes
|
||||
- The Market View form now honors filters when new securities are added
|
||||
|
||||
### Performance Improvements
|
||||
- Requoting and hedging latency has been reduced by up to 30%
|
||||
|
||||
## 1.4.2
|
||||
|
||||
### User Interface Improvements
|
||||
|
||||
+1
-1
Submodule docker/linux updated: 8ee9692597...f6d8d59860.
+1
-1
Submodule externals updated: 5f21fd13e7...69f8639235.
@@ -1 +1 @@
|
||||
set(sources files/Onixs.BTS.lic;files/cert_secure_logon.txt;files/perf_trace/compiled.css;files/perf_trace/index.html;files/perf_trace/jquery-3.3.1.min.js;files/perf_trace/main.js;files/perf_trace/plotly-latest.min.js)
|
||||
set(sources files/Onixs.BTS.lic;files/cert_secure_logon.txt;files/perf_trace/compiled.css;files/perf_trace/index.html;files/perf_trace/jquery-3.3.1.min.js;files/perf_trace/main.js;files/perf_trace/perf_log.sql;files/perf_trace/perf_trace_analysis.sql;files/perf_trace/plotly-latest.min.js)
|
||||
@@ -1,4 +1,6 @@
|
||||
$( document ).ready(function (){
|
||||
$.ajaxSetup({ timeout: 30000 });
|
||||
|
||||
let urlParams = new URLSearchParams(window.location.search);
|
||||
num_rows = urlParams.get('r');
|
||||
let show_rows = urlParams.get('showrows');
|
||||
@@ -111,6 +113,9 @@ function ajax_err (jqXHR, textStatus,errorThrown) {
|
||||
|
||||
console.log(textStatus);
|
||||
console.log(errorThrown);
|
||||
|
||||
console.log(jqXHR);
|
||||
|
||||
}
|
||||
|
||||
function first_upper (str)
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
create table if not exists
|
||||
session (
|
||||
version TEXT,
|
||||
host TEXT,
|
||||
build_type TEXT,
|
||||
session_start TIMESTAMP PRIMARY KEY
|
||||
);
|
||||
|
||||
create table if not exists
|
||||
trace (
|
||||
session_start TIMESTAMP,
|
||||
ts INTEGER,
|
||||
hts timestamp,
|
||||
channel TEXT,
|
||||
entry_kind TEXT,
|
||||
txt_1 TEXT,
|
||||
uint64_1 INTEGER,
|
||||
thread TEXT,
|
||||
cor_event INTEGER,
|
||||
sn INTEGER,
|
||||
uint32_1 INTEGER
|
||||
);
|
||||
|
||||
drop view if exists recent_trace;
|
||||
create view recent_trace as
|
||||
select * from trace order by rowid desc limit 140123;
|
||||
|
||||
create index if not exists trace_idx on trace(session_start,channel,sn,ts);
|
||||
|
||||
drop view if exists ordered_trace;
|
||||
create view ordered_trace as
|
||||
select
|
||||
recent_trace.*,
|
||||
row_number() over (order by session_start,ts) entry_rank
|
||||
from
|
||||
recent_trace;
|
||||
|
||||
drop view if exists delta_trace;
|
||||
create view delta_trace as
|
||||
select
|
||||
current.*,
|
||||
(current.ts - previous.ts) / 1000.0 delta
|
||||
from
|
||||
ordered_trace current,
|
||||
ordered_trace previous
|
||||
where
|
||||
current.entry_rank = previous.entry_rank+1;
|
||||
|
||||
drop view if exists log;
|
||||
create view log as
|
||||
select
|
||||
delta_trace.*,
|
||||
case cor_event>0 when 1 then (ts-cor_event)/1000.0 else 0 end scope_delta,
|
||||
case cor_event>0 when 1 then "end of " || entry_kind else entry_kind end scoped_entry
|
||||
from
|
||||
delta_trace;
|
||||
|
||||
drop view if exists events;
|
||||
create view events as
|
||||
select
|
||||
fix_reads.session_start,
|
||||
"hedge" event_type,
|
||||
fix_reads.ts start,
|
||||
fix_writes.ts stop,
|
||||
(fix_writes.ts - fix_reads.ts) / 1000.0 duration,
|
||||
row_number() over (order by fix_reads.session_start,fix_reads.ts,fix_writes.ts) event_rank ,
|
||||
fix_reads.channel,
|
||||
fix_reads.sn
|
||||
from
|
||||
(select session_start, channel, sn, ts from recent_trace where entry_kind='fix_msg_r') fix_reads,
|
||||
(select session_start, channel, sn, ts from recent_trace where entry_kind='before_fix_socket_w') fix_writes
|
||||
where
|
||||
fix_reads.session_start = fix_writes.session_start and
|
||||
fix_reads.channel = fix_writes.channel and
|
||||
fix_reads.sn = fix_writes.sn
|
||||
union
|
||||
select
|
||||
md_reads.session_start,
|
||||
"requote" event_type,
|
||||
md_reads.ts start,
|
||||
fix_writes.ts stop,
|
||||
(fix_writes.ts - md_reads.ts) / 1000.0 duration,
|
||||
row_number() over (order by md_reads.session_start,md_reads.ts,fix_writes.ts) event_rank ,
|
||||
md_reads.channel,
|
||||
md_reads.sn
|
||||
from
|
||||
(select session_start, channel, sn, ts from recent_trace where entry_kind='md') md_reads,
|
||||
(select session_start, channel, sn, ts from recent_trace where entry_kind='before_fix_socket_w') fix_writes
|
||||
where
|
||||
md_reads.session_start = fix_writes.session_start and
|
||||
md_reads.channel = fix_writes.channel and
|
||||
md_reads.sn = fix_writes.sn;
|
||||
|
||||
drop view if exists event_log;
|
||||
create view event_log as
|
||||
select
|
||||
log.*,
|
||||
event_type,
|
||||
event_rank,
|
||||
events.duration event_duration,
|
||||
events.start event_start,
|
||||
events.stop event_stop
|
||||
from
|
||||
log,
|
||||
events
|
||||
where
|
||||
log.session_start = events.session_start and
|
||||
log.ts >= events.start and log.ts <= events.stop;
|
||||
|
||||
drop view if exists pretty_log;
|
||||
create view pretty_log as
|
||||
select
|
||||
scoped_entry entry_kind,
|
||||
delta,
|
||||
scope_delta ,
|
||||
event_duration,
|
||||
event_type,
|
||||
event_rank,
|
||||
hts,
|
||||
ts,
|
||||
session_start,
|
||||
entry_rank
|
||||
from
|
||||
event_log;
|
||||
|
||||
drop view if exists last_pretty_log;
|
||||
create view last_pretty_log as
|
||||
select * from pretty_log where session_start=(select max(session_start) from session);
|
||||
|
||||
-- drop view if exists summary;
|
||||
-- create view summary as
|
||||
-- select session_start,event_type,count(*) count, avg(duration) avg, max(duration) max, min(duration) min, median(duration) median, stdev(duration) stdev from events group by session_start,event_type;
|
||||
|
||||
|
||||
drop view if exists hedge_trace;
|
||||
create view hedge_trace as
|
||||
select
|
||||
session.version,
|
||||
session.host,
|
||||
session.build_type,
|
||||
session.session_start,
|
||||
recent_trace.hts trace_time,
|
||||
recent_trace.uint64_1 order_key,
|
||||
hedge_events.duration total,
|
||||
hedge_events.event_rank rank
|
||||
from
|
||||
recent_trace,
|
||||
(select * from events where event_type='hedge') hedge_events,
|
||||
session
|
||||
where
|
||||
entry_kind='NativeOrder::on_activate' and
|
||||
recent_trace.cor_event = 0 and
|
||||
recent_trace.channel = hedge_events.channel and
|
||||
recent_trace.session_start = hedge_events.session_start and
|
||||
recent_trace.sn = hedge_events.sn and
|
||||
recent_trace.session_start = session.session_start;
|
||||
|
||||
drop view if exists requote_trace;
|
||||
create view requote_trace as
|
||||
select
|
||||
session.version,
|
||||
session.host,
|
||||
session.build_type,
|
||||
session.session_start,
|
||||
recent_trace.hts trace_time,
|
||||
recent_trace.uint64_1 order_key,
|
||||
requote_events.duration total,
|
||||
requote_events.event_rank rank
|
||||
from
|
||||
recent_trace,
|
||||
(select * from events where event_type='requote') requote_events,
|
||||
session
|
||||
where
|
||||
entry_kind='NativeOrder::on_update' and
|
||||
recent_trace.cor_event = 0 and
|
||||
recent_trace.channel = requote_events.channel and
|
||||
recent_trace.session_start = requote_events.session_start and
|
||||
recent_trace.sn = requote_events.sn and
|
||||
recent_trace.session_start = session.session_start ;
|
||||
|
||||
delete from trace where
|
||||
rowid in (select rowid from trace order by rowid desc limit -1 offset 10000123);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
drop table if exists pretty_log_table;
|
||||
create table pretty_log_table as select * from pretty_log;
|
||||
|
||||
create view if not exists broken_down_hedges as
|
||||
select
|
||||
socket_r.event_id,
|
||||
socket_r.event_duration,
|
||||
(om.ts - socket_r.ts) / 1000.0 fix_parsing,
|
||||
(jf.ts - om.ts) / 1000.0 om,
|
||||
(socket_w.ts - jf.ts) / 1000.0 fix_gen
|
||||
from
|
||||
(select event_id,event_duration,ts from pretty_log_table where entry_kind = 'fix_socket_r') socket_r,
|
||||
(select event_id, ts from pretty_log_table where entry_kind='NativeOrder::on_exe_report') om,
|
||||
(select event_id, ts from pretty_log_table where entry_kind='ExecutionGateway::send_fix') jf,
|
||||
(select event_id, ts from pretty_log_table where entry_kind='before_fix_socket_w') socket_w
|
||||
where
|
||||
socket_r.event_id = om.event_id and om.event_id = jf.event_id and jf.event_id = socket_w.event_id
|
||||
and event_duration < 100
|
||||
order by socket_r.event_id;
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
syntax='proto2';
|
||||
package bts.config;
|
||||
|
||||
message PerfLogCfg {
|
||||
optional bool active = 1 [default=true];
|
||||
optional uint32 recent_trades_size = 2 [default=140123];
|
||||
optional uint32 max_rows = 3 [default=10000123];
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "lib/jetfix/codec/IFIXMsg.h"
|
||||
#include "lib/jetfix/common/InMsgBuffer.h"
|
||||
#include "lib/utils/meta_programming.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTrace.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace jetfix {
|
||||
@@ -73,8 +73,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void set_trace (PerfTrace const&trace) {_trace = trace;}
|
||||
PerfTrace const&trace() const {return _trace;}
|
||||
void set_trace (trace::CorID const&trace) {_trace = trace;}
|
||||
trace::CorID const&trace() const {return _trace;}
|
||||
|
||||
private:
|
||||
typedef std::tuple<Fields...> FieldTuple;
|
||||
@@ -103,7 +103,7 @@ private:
|
||||
FieldTuple _fields;
|
||||
std::bitset<std::tuple_size<FieldTuple>::value> _has_value;
|
||||
std::vector<std::pair<uint32_t,IFieldReader*>> _field_idx;
|
||||
PerfTrace _trace;
|
||||
trace::CorID _trace;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <vector>
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/jetfix/codec/MessageTypes.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTrace.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace jetfix {
|
||||
@@ -114,16 +114,16 @@ public:
|
||||
std::string get_test_req_id () const
|
||||
{return get_string_field ("\001112=",sizeof("\001112=")-1);}
|
||||
|
||||
PerfTrace const&trace() const {return _trace;}
|
||||
trace::CorID const&trace() const {return _trace;}
|
||||
|
||||
void set_trace(PerfTrace const&t) {_trace = t;}
|
||||
void set_trace(trace::CorID const&t) {_trace = t;}
|
||||
|
||||
private:
|
||||
const char *_data;
|
||||
size_t _size;
|
||||
MessageTypes _type;
|
||||
std::shared_ptr<std::string> _buffer;
|
||||
PerfTrace _trace;
|
||||
trace::CorID _trace;
|
||||
|
||||
MessageTypes get_type () const
|
||||
{
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
#ifndef WIN32
|
||||
#include "AsyncReader.h"
|
||||
|
||||
#include <thread>
|
||||
#include "IConnection.h"
|
||||
#include "lib/jetfix/gateway/ICoreGatewayEventSink.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/tracing/bts_trace.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTrace.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/thread_manager.h"
|
||||
#include <thread>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
@@ -18,18 +17,17 @@ namespace jetfix {
|
||||
// frame FIX messages contained in a raw buffer, storing their
|
||||
// location in a vector InMsgBuffer. Returns how many bytes were
|
||||
// successfully framed.
|
||||
size_t extract (char *start, char *end,
|
||||
std::shared_ptr<std::string> const &buffer,
|
||||
std::vector<InMsgBuffer> *msgs)
|
||||
{
|
||||
size_t extract(char *start, char *end,
|
||||
std::shared_ptr<std::string> const &buffer,
|
||||
std::vector<InMsgBuffer> *msgs) {
|
||||
auto first_unprocessed_byte = 0;
|
||||
auto current = start;
|
||||
|
||||
for (;;) {
|
||||
// consume prefix
|
||||
auto this_msg_begin= current;
|
||||
auto this_msg_begin = current;
|
||||
constexpr const char *prefix = "8=FIX.4.2\0019=";
|
||||
for (int i = 0; i < 12;i++)
|
||||
for (int i = 0; i < 12; i++)
|
||||
if (current == end)
|
||||
return first_unprocessed_byte;
|
||||
else if (*current != prefix[i])
|
||||
@@ -42,48 +40,38 @@ size_t extract (char *start, char *end,
|
||||
for (;;) {
|
||||
if (current == end)
|
||||
return first_unprocessed_byte;
|
||||
else if (*current == 1)
|
||||
{
|
||||
current ++;
|
||||
else if (*current == 1) {
|
||||
current++;
|
||||
break;
|
||||
} else {
|
||||
body_length *= 10;
|
||||
body_length += *current - '0';
|
||||
current ++;
|
||||
current++;
|
||||
}
|
||||
}
|
||||
bts_assert (body_length);
|
||||
bts_assert(body_length);
|
||||
|
||||
// consume body
|
||||
current += body_length + sizeof("10=000");
|
||||
if ( current > end)
|
||||
if (current > end)
|
||||
return first_unprocessed_byte;
|
||||
else {
|
||||
msgs->emplace_back (this_msg_begin,
|
||||
current - this_msg_begin,
|
||||
buffer);
|
||||
msgs->emplace_back(this_msg_begin, current - this_msg_begin, buffer);
|
||||
first_unprocessed_byte = current - start;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AsyncReader::PImpl
|
||||
{
|
||||
PImpl(IConnection &connection,
|
||||
std::string const&connection_name,
|
||||
ICoreGatewayEventSink &sink,
|
||||
size_t buffer_sz,
|
||||
struct AsyncReader::PImpl {
|
||||
PImpl(IConnection &connection, std::string const &connection_name,
|
||||
ICoreGatewayEventSink &sink, trace::PerfLog &l, size_t buffer_sz,
|
||||
size_t high_water_mark)
|
||||
:
|
||||
connection (connection),
|
||||
working_buffer (std::make_shared<std::string> (buffer_sz,0)),
|
||||
first_pending_msg (0),
|
||||
write_cursor (0),
|
||||
reader_thread (),
|
||||
sink (sink),
|
||||
high_water_mark (high_water_mark),
|
||||
connection_name(connection_name)
|
||||
{}
|
||||
: connection(connection),
|
||||
working_buffer(std::make_shared<std::string>(buffer_sz, 0)),
|
||||
first_pending_msg(0), write_cursor(0), reader_thread(), sink(sink),
|
||||
perf_log(l), high_water_mark(high_water_mark),
|
||||
connection_name(connection_name),
|
||||
channel(new std::string(connection_name)) {}
|
||||
|
||||
// abstraction for source of raw bytes
|
||||
IConnection &connection;
|
||||
@@ -96,90 +84,93 @@ struct AsyncReader::PImpl
|
||||
size_t write_cursor;
|
||||
// the thread that will be pulling bytes out of the connection
|
||||
std::thread reader_thread;
|
||||
// an observer that will process the messages we frame
|
||||
// an observer that will process the messages we frame
|
||||
ICoreGatewayEventSink &sink;
|
||||
// For performance analysis
|
||||
trace::PerfLog &perf_log;
|
||||
// a position in the current buffer that if reached signals the
|
||||
// need to increase the buffer size
|
||||
uint32_t high_water_mark;
|
||||
const std::string connection_name;
|
||||
|
||||
// we leak this because we do not have a good way to pass a pointer
|
||||
// to the perf_log object and guarantee that pointer is valid by
|
||||
// the time the string is written to disk
|
||||
const std::string *channel;
|
||||
};
|
||||
|
||||
AsyncReader::AsyncReader (IConnection &connection,
|
||||
std::string const&connection_name,
|
||||
ICoreGatewayEventSink &sink,
|
||||
size_t buffer_sz, size_t high_water_mark):
|
||||
me (new PImpl(connection,connection_name,sink,buffer_sz,high_water_mark))
|
||||
{}
|
||||
AsyncReader::AsyncReader(IConnection &connection,
|
||||
std::string const &connection_name,
|
||||
ICoreGatewayEventSink &sink, trace::PerfLog &log,
|
||||
size_t buffer_sz, size_t high_water_mark)
|
||||
: me(new PImpl(connection, connection_name, sink, log, buffer_sz,
|
||||
high_water_mark)) {}
|
||||
|
||||
void AsyncReader::start ()
|
||||
{
|
||||
auto thread_name = fmt::format ("jf_ar_{}", me->connection_name);
|
||||
me->reader_thread = threading::make_thread (thread_name,[this] () {
|
||||
dispatch();
|
||||
});
|
||||
void AsyncReader::start() {
|
||||
auto thread_name = fmt::format("jf_ar_{}", me->connection_name);
|
||||
me->reader_thread =
|
||||
threading::make_thread(thread_name, [this]() { dispatch(); });
|
||||
}
|
||||
|
||||
char* AsyncReader::cursor ()
|
||||
{
|
||||
char *AsyncReader::cursor() {
|
||||
return &((*me->working_buffer)[me->write_cursor]);
|
||||
}
|
||||
|
||||
size_t AsyncReader::free_bytes ()
|
||||
{
|
||||
return me->working_buffer->size () - me->write_cursor;
|
||||
size_t AsyncReader::free_bytes() {
|
||||
return me->working_buffer->size() - me->write_cursor;
|
||||
}
|
||||
|
||||
void AsyncReader::dispatch ()
|
||||
{
|
||||
void AsyncReader::dispatch() {
|
||||
std::vector<InMsgBuffer> framed_msgs;
|
||||
framed_msgs.reserve (100);
|
||||
|
||||
framed_msgs.reserve(100);
|
||||
|
||||
for (;;) {
|
||||
// read as many bytes as we have buffer space for
|
||||
auto read_count = me->connection.read (cursor (),free_bytes ());
|
||||
PerfTrace trace;
|
||||
trace.fix_socket_r = utils::TimeStamp::now();
|
||||
|
||||
auto read_count = me->connection.read(cursor(), free_bytes());
|
||||
me->perf_log.log(trace::CorID{me->channel->c_str(), 0}, "fix_socket_r");
|
||||
auto socket_read_time = utils::TimeStamp::now();
|
||||
trace::CorID trace;
|
||||
trace.channel = me->channel->c_str();
|
||||
|
||||
// signal a disconnect if the connection broke
|
||||
if (read_count <= 0) {
|
||||
me->sink.reader_disconnected ();
|
||||
me->sink.reader_disconnected();
|
||||
return;
|
||||
} else {
|
||||
// move the write cursor
|
||||
me->write_cursor += read_count;
|
||||
|
||||
// see what we can parse with the just read bytes
|
||||
framed_msgs.clear ();
|
||||
framed_msgs.clear();
|
||||
|
||||
// extract as many messages as we can from the last unprocess
|
||||
// byte to the current write cursor, return offset of first
|
||||
// byte in the buffer that we could not process (possibly the
|
||||
// first byte of an incomplete msg)
|
||||
auto first_unprocessed_offset = extract (&((*me->working_buffer)[me->first_pending_msg]),
|
||||
&((*me->working_buffer)[me->write_cursor]),
|
||||
me->working_buffer,
|
||||
&framed_msgs);
|
||||
auto first_unprocessed_offset = extract(
|
||||
&((*me->working_buffer)[me->first_pending_msg]),
|
||||
&((*me->working_buffer)[me->write_cursor]), me->working_buffer,
|
||||
&framed_msgs);
|
||||
me->first_pending_msg += first_unprocessed_offset;
|
||||
|
||||
for (auto &msg : framed_msgs) {
|
||||
trace.sn = msg.get_seq_num();
|
||||
socket_read_time.shift_ns(1);
|
||||
me->perf_log.log(trace, socket_read_time,"fix_msg_r");
|
||||
msg.set_trace(trace);
|
||||
LOG(Latency, Diag, "perf_event AsyncReader.on_fix");
|
||||
tracepoint(bts_trace, trace_order, (char *)"AsyncReader.on_fix", 0,
|
||||
msg.get_seq_num(), 0);
|
||||
me->sink.in_msg_raw (msg);
|
||||
me->sink.in_msg_raw(msg);
|
||||
}
|
||||
|
||||
|
||||
// do we need a new buffer?
|
||||
if (me->write_cursor >= me->working_buffer->size () - me->high_water_mark) {
|
||||
if (me->write_cursor >=
|
||||
me->working_buffer->size() - me->high_water_mark) {
|
||||
auto old_buffer = me->working_buffer;
|
||||
auto pending_size = me->write_cursor - me->first_pending_msg;
|
||||
|
||||
me->working_buffer = std::make_shared<std::string> (std::max (2*pending_size,
|
||||
me->working_buffer->size ()),0);
|
||||
memcpy (&((*me->working_buffer)[0]),
|
||||
&((*old_buffer)[me->first_pending_msg]),
|
||||
pending_size);
|
||||
|
||||
me->working_buffer = std::make_shared<std::string>(
|
||||
std::max(2 * pending_size, me->working_buffer->size()), 0);
|
||||
memcpy(&((*me->working_buffer)[0]),
|
||||
&((*old_buffer)[me->first_pending_msg]), pending_size);
|
||||
me->first_pending_msg = 0;
|
||||
me->write_cursor = pending_size;
|
||||
}
|
||||
@@ -187,12 +178,11 @@ void AsyncReader::dispatch ()
|
||||
}
|
||||
}
|
||||
|
||||
AsyncReader::~AsyncReader ()
|
||||
{
|
||||
if (me->reader_thread.joinable ())
|
||||
me->reader_thread.join ();
|
||||
AsyncReader::~AsyncReader() {
|
||||
if (me->reader_thread.joinable())
|
||||
me->reader_thread.join();
|
||||
}
|
||||
|
||||
}}
|
||||
} // namespace jetfix
|
||||
} // namespace bts
|
||||
#endif
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace trace {class PerfLog;}
|
||||
namespace jetfix {
|
||||
|
||||
class IConnection;
|
||||
@@ -16,6 +17,7 @@ public:
|
||||
AsyncReader(IConnection &connection,
|
||||
std::string const&connection_name,
|
||||
ICoreGatewayEventSink &sink,
|
||||
trace::PerfLog &l,
|
||||
size_t buffer_sz, // initial buffer size to use
|
||||
size_t high_water_mark); // when do we allocate a new buffer
|
||||
void start ();
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include "lib/orderman/orderman_utils/PerfTrace.h"
|
||||
|
||||
namespace bts {
|
||||
namespace trace {struct CorID;}
|
||||
namespace jetfix {
|
||||
|
||||
class IConnection
|
||||
@@ -14,7 +14,7 @@ public:
|
||||
virtual bool connect () = 0;
|
||||
virtual void shutdown () = 0;
|
||||
virtual int read (char* to_buffer,size_t read_count) = 0;
|
||||
virtual int write (WriteVec const&vec,size_t vec_size,PerfTrace const&trace) = 0;
|
||||
virtual int write (WriteVec const&vec,size_t vec_size,trace::CorID trace) = 0;
|
||||
virtual ~IConnection() {};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "SocketConnection.h"
|
||||
#include "lib/jetfix/store/session_name.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTraceLog.h"
|
||||
#include "lib/tracing/bts_trace.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <fstream>
|
||||
@@ -19,7 +19,7 @@ namespace bts {
|
||||
namespace jetfix {
|
||||
|
||||
SocketConnection::SocketConnection(bts::config::JetFIX::Connection const &cfg,
|
||||
PerfTraceLog &perf_log)
|
||||
trace::PerfLog &perf_log)
|
||||
: _fd(-1), _cfg(cfg), _log(cfg.log_fix_stream()), _in_log(), _out_log(),
|
||||
_perf_log(perf_log) {
|
||||
if (_log) {
|
||||
@@ -83,7 +83,6 @@ int SocketConnection::read(char *to_buffer, size_t read_count) {
|
||||
|
||||
bts_assert(_fd >= 0);
|
||||
auto result = ::read(_fd, to_buffer, read_count);
|
||||
LOG(Latency, Diag, "perf_event after_socket_read");
|
||||
if (_log && result > 0) {
|
||||
_in_log.write(to_buffer, result);
|
||||
_in_log.flush();
|
||||
@@ -98,7 +97,7 @@ int SocketConnection::read(char *to_buffer, size_t read_count) {
|
||||
}
|
||||
|
||||
int SocketConnection::write(WriteVec const &vec, size_t vec_size,
|
||||
PerfTrace const &trace) {
|
||||
trace::CorID trace) {
|
||||
bts_assert(_fd >= 0);
|
||||
|
||||
struct iovec iv[MaxWriteVecSz];
|
||||
@@ -107,65 +106,9 @@ int SocketConnection::write(WriteVec const &vec, size_t vec_size,
|
||||
iv[i].iov_len = vec[i].second;
|
||||
}
|
||||
|
||||
auto trace_cpy = trace;
|
||||
trace_cpy.fix_socket_w = utils::TimeStamp::now();
|
||||
|
||||
utils::TimeStamp::Interval s2s;
|
||||
if (trace.md.to_uint64()) {
|
||||
s2s = (trace_cpy.fix_socket_w - trace_cpy.md);
|
||||
tracepoint(bts_trace, trace_order,
|
||||
(char *)"SocketConnection.tick_to_order.pre_write", 0,
|
||||
trace.md_sn, s2s.microseconds());
|
||||
} else {
|
||||
s2s = (trace_cpy.fix_socket_w - trace_cpy.fix_socket_r);
|
||||
tracepoint(bts_trace, trace_order,
|
||||
(char *)"SocketConnection.fill_to_order.pre_write", 0,
|
||||
trace.md_sn, s2s.microseconds());
|
||||
}
|
||||
|
||||
LOG(Latency, Diag, "perf_event before_socket_write");
|
||||
_perf_log.log(trace,"before_fix_socket_w");
|
||||
auto result = ::writev(_fd, iv, (int)vec_size);
|
||||
|
||||
if (trace.md.to_uint64()) {
|
||||
LOG(Latency, Info,
|
||||
"perf_event:tick_to_order s2s_us:{} do_work_begin:{} fix_socket_r:{} "
|
||||
"md:{} "
|
||||
"md_src:{} spreader_on_book:{} spreader_modify_order:{} "
|
||||
"spreader_on_child_order_update:{} "
|
||||
"order_update:{} exe_gw:{} fix_socket_w:{} md_sn:{} order_key:{} "
|
||||
"send_fix_buffer:{}",
|
||||
s2s.microseconds(), trace_cpy.do_work_begin.to_uint64(),
|
||||
trace_cpy.fix_socket_r.to_uint64(), trace_cpy.md.to_uint64(),
|
||||
trace_cpy.md_src.to_uint64(), trace_cpy.spreader_on_book.to_uint64(),
|
||||
trace_cpy.spreader_modify_order.to_uint64(),
|
||||
trace_cpy.spreader_on_child_order_update.to_uint64(),
|
||||
trace_cpy.order_update.to_uint64(), trace_cpy.exe_gw.to_uint64(),
|
||||
trace_cpy.fix_socket_w.to_uint64(), trace_cpy.md_sn,
|
||||
trace_cpy.order_key, trace.send_fix_buffer.to_uint64());
|
||||
_perf_log.log(trace_cpy);
|
||||
}
|
||||
|
||||
if (trace.fix_socket_r.to_uint64()) {
|
||||
LOG(Latency, Info,
|
||||
"perf_event:fill_to_order s2s_us:{} do_work_begin:{} fix_socket_r:{} "
|
||||
"md:{} md_src:{} "
|
||||
"spreader_on_book:{} spreader_modify_order:{} "
|
||||
"spreader_on_child_order_update:{} "
|
||||
"order_update:{} exe_gw:{} fix_socket_w:{} md_sn:{} order_key:{} "
|
||||
"send_fix_buffer:{} ",
|
||||
s2s.microseconds(), trace_cpy.do_work_begin.to_uint64(),
|
||||
trace_cpy.fix_socket_r.to_uint64(), trace_cpy.md.to_uint64(),
|
||||
trace_cpy.md_src.to_uint64(), trace_cpy.spreader_on_book.to_uint64(),
|
||||
trace_cpy.spreader_modify_order.to_uint64(),
|
||||
trace_cpy.spreader_on_child_order_update.to_uint64(),
|
||||
trace_cpy.order_update.to_uint64(), trace_cpy.exe_gw.to_uint64(),
|
||||
trace_cpy.fix_socket_w.to_uint64(), trace_cpy.md_sn, trace_cpy.order_key,
|
||||
trace_cpy.send_fix_buffer.to_uint64());
|
||||
_perf_log.log(trace_cpy);
|
||||
}
|
||||
|
||||
tracepoint(bts_trace, trace_order, (char *)"SocketConnection.post_write", 0,
|
||||
trace.md_sn, 0);
|
||||
_perf_log.log(trace,"after_fix_socket_w");
|
||||
|
||||
if (_log && result > 0) {
|
||||
for (size_t i = 0; i < vec_size; i++)
|
||||
|
||||
@@ -7,19 +7,19 @@
|
||||
#include "lib/metrics/metrics.h"
|
||||
|
||||
namespace bts {
|
||||
class PerfTraceLog;
|
||||
namespace trace {class PerfLog;struct CorID;}
|
||||
|
||||
namespace jetfix {
|
||||
|
||||
class SocketConnection : public IConnection
|
||||
{
|
||||
public:
|
||||
SocketConnection(bts::config::JetFIX::Connection const &cfgl,PerfTraceLog &log);
|
||||
SocketConnection(bts::config::JetFIX::Connection const &cfgl,trace::PerfLog &log);
|
||||
~SocketConnection();
|
||||
bool connect () override;
|
||||
void shutdown () override;
|
||||
int read (char* to_buffer,size_t read_count) override;
|
||||
int write (WriteVec const&vec,size_t vec_size,const PerfTrace &trace) override;
|
||||
int write (WriteVec const&vec,size_t vec_size,trace::CorID ) override;
|
||||
|
||||
private:
|
||||
int _fd;
|
||||
@@ -27,7 +27,7 @@ private:
|
||||
bool _log;
|
||||
std::ofstream _in_log;
|
||||
std::ofstream _out_log;
|
||||
PerfTraceLog &_perf_log;
|
||||
trace::PerfLog &_perf_log;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -1,38 +1,38 @@
|
||||
#include "CoreGateway.h"
|
||||
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/jetfix/common/OutFIX.h"
|
||||
#include "lib/jetfix/connection/AsyncReader.h"
|
||||
#include "lib/jetfix/connection/IConnection.h"
|
||||
#include "lib/jetfix/gateway/ICoreGatewayObserver.h"
|
||||
#include "lib/jetfix/gateway/Session.h"
|
||||
#include "lib/jetfix/store/IFIXStore.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/tracing/bts_trace.h"
|
||||
#include <boost/lockfree/queue.hpp>
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/utils/thread_manager.h"
|
||||
#include <boost/lockfree/queue.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <fmt/format.h>
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
#include <tbb/concurrent_queue.h>
|
||||
#include <thread>
|
||||
|
||||
namespace bts {
|
||||
namespace jetfix {
|
||||
|
||||
enum class CoreGateway::State {
|
||||
OFF,
|
||||
PAUSED,
|
||||
CONNECTING,
|
||||
READY,
|
||||
WREADER_DISCONNECT,
|
||||
WSESSION_TERMINATE
|
||||
};
|
||||
PAUSED,
|
||||
CONNECTING,
|
||||
READY,
|
||||
WREADER_DISCONNECT,
|
||||
WSESSION_TERMINATE
|
||||
};
|
||||
|
||||
struct CoreGateway::SessionInfo {
|
||||
SessionInfo() : connection(), store(), reader(), session() {}
|
||||
@@ -47,25 +47,21 @@ struct CoreGateway::EventData {
|
||||
typedef void (CoreGateway::*Handler)(EventData const &ev);
|
||||
|
||||
EventData(const EventData &src)
|
||||
: handler(src.handler), out_msgs(src.out_msgs), in_msg(src.in_msg),
|
||||
trace(src.trace), ts(src.ts) {}
|
||||
: handler(src.handler), out_msgs(src.out_msgs), in_msg(src.in_msg),
|
||||
trace(src.trace), ts(src.ts) {}
|
||||
|
||||
EventData()
|
||||
: handler(), out_msgs(), in_msg(), trace(),
|
||||
ts() {}
|
||||
EventData() : handler(), out_msgs(), in_msg(), trace(), ts() {}
|
||||
EventData(Handler handler)
|
||||
: handler(handler), out_msgs(), in_msg(), trace(), ts() {}
|
||||
EventData(Handler handler, PerfTrace const &trace,
|
||||
: handler(handler), out_msgs(), in_msg(), trace(), ts() {}
|
||||
EventData(Handler handler, trace::CorID const &trace,
|
||||
OutFIXPtr const &out_msgs)
|
||||
: handler(handler), out_msgs(out_msgs), in_msg(), trace(trace), ts() {}
|
||||
: handler(handler), out_msgs(out_msgs), in_msg(), trace(trace), ts() {}
|
||||
|
||||
EventData(Handler handler, InMsgBuffer const &in_msg)
|
||||
: handler(handler), out_msgs(), in_msg(in_msg),
|
||||
trace(), ts() {}
|
||||
: handler(handler), out_msgs(), in_msg(in_msg), trace(), ts() {}
|
||||
|
||||
EventData(Handler handler, std::shared_ptr<std::string> const &ts)
|
||||
: handler(handler), out_msgs(), in_msg(),
|
||||
trace(), ts(ts) {}
|
||||
: handler(handler), out_msgs(), in_msg(), trace(), ts(ts) {}
|
||||
|
||||
EventData &operator=(EventData &src) {
|
||||
handler = src.handler;
|
||||
@@ -87,19 +83,19 @@ struct CoreGateway::EventData {
|
||||
Handler handler;
|
||||
OutFIXPtr out_msgs;
|
||||
InMsgBuffer in_msg;
|
||||
PerfTrace trace;
|
||||
trace::CorID trace;
|
||||
std::shared_ptr<std::string> ts;
|
||||
};
|
||||
|
||||
struct CoreGateway::PImpl {
|
||||
PImpl(config::JetFIX::Connection const &cfg, ICoreGatewayObserver &observer,
|
||||
ConnectionFactory const &conn_factory,
|
||||
StoreFactory const &store_factory)
|
||||
: cfg(cfg), observer(observer), make_connection(conn_factory),
|
||||
make_store(store_factory), state(State::OFF), start_of_pause(),
|
||||
session_info(), event_queue(), timer_thread(), destroy(false),
|
||||
is_on(false), has_replayed(false), send_batch(), recv_batch(),
|
||||
connection_attempts(0) {
|
||||
StoreFactory const &store_factory, trace::PerfLog &l)
|
||||
: cfg(cfg), observer(observer), make_connection(conn_factory),
|
||||
make_store(store_factory), state(State::OFF), start_of_pause(),
|
||||
session_info(), event_queue(), timer_thread(), destroy(false),
|
||||
is_on(false), has_replayed(false), send_batch(), recv_batch(),
|
||||
connection_attempts(0), perf_log(l) {
|
||||
send_batch.reserve(1024);
|
||||
recv_batch.reserve(1024);
|
||||
}
|
||||
@@ -112,7 +108,7 @@ struct CoreGateway::PImpl {
|
||||
State state;
|
||||
datetime::PTime start_of_pause;
|
||||
std::unique_ptr<SessionInfo> session_info;
|
||||
tbb::concurrent_queue <EventData> event_queue;
|
||||
tbb::concurrent_queue<EventData> event_queue;
|
||||
std::thread timer_thread;
|
||||
std::atomic<bool> destroy;
|
||||
bool is_on;
|
||||
@@ -120,18 +116,21 @@ struct CoreGateway::PImpl {
|
||||
std::vector<EventData> send_batch;
|
||||
std::vector<EventData> recv_batch;
|
||||
uint32_t connection_attempts;
|
||||
trace::PerfLog &perf_log;
|
||||
};
|
||||
|
||||
std::thread CoreGateway::make_timer_thread() {
|
||||
auto thread_name = fmt::format ("jf_tmr_{}", me->cfg.name());
|
||||
return threading::make_thread(thread_name, std::bind(&CoreGateway::call_time, this));
|
||||
auto thread_name = fmt::format("jf_tmr_{}", me->cfg.name());
|
||||
return threading::make_thread(thread_name,
|
||||
std::bind(&CoreGateway::call_time, this));
|
||||
}
|
||||
|
||||
CoreGateway::CoreGateway(config::JetFIX::Connection const &cfg,
|
||||
ICoreGatewayObserver &observer,
|
||||
ConnectionFactory const &conn_factory,
|
||||
StoreFactory const &store_factory)
|
||||
: me(new PImpl(cfg, observer, conn_factory, store_factory)) {
|
||||
StoreFactory const &store_factory,
|
||||
trace::PerfLog &perf_trace)
|
||||
: me(new PImpl(cfg, observer, conn_factory, store_factory, perf_trace)) {
|
||||
me->timer_thread = make_timer_thread();
|
||||
}
|
||||
|
||||
@@ -145,8 +144,7 @@ CoreGateway::~CoreGateway() {
|
||||
me->timer_thread.join();
|
||||
}
|
||||
|
||||
void CoreGateway::send(PerfTrace const &trace, OutFIXPtr const &fix) {
|
||||
tracepoint(bts_trace, trace_order, (char *)"CoreGateway.send", 0, trace.md_sn,0);
|
||||
void CoreGateway::send(OutFIXPtr const &fix, trace::CorID trace) {
|
||||
me->event_queue.push(EventData(&CoreGateway::sync_send, trace, fix));
|
||||
}
|
||||
|
||||
@@ -179,10 +177,13 @@ void CoreGateway::reader_disconnected() {
|
||||
}
|
||||
|
||||
void CoreGateway::in_msg_raw(InMsgBuffer const &msg) {
|
||||
trace::ScopedPerfLogEntry _(me->perf_log, msg.trace(),
|
||||
"CoreGateway::in_msg_raw");
|
||||
me->event_queue.push(EventData(&CoreGateway::sync_in_msg_raw, msg));
|
||||
}
|
||||
|
||||
void CoreGateway::in_msg_sequenced(InMsgBuffer const &msg) {
|
||||
trace::ScopedPerfLogEntry _(me->perf_log,msg.trace(), "CoreGateway::in_msg_sequenced");
|
||||
me->event_queue.push(EventData(&CoreGateway::sync_in_msg_sequenced, msg));
|
||||
}
|
||||
|
||||
@@ -200,7 +201,7 @@ void CoreGateway::dispatch_events() {
|
||||
if (me->send_batch.size() || me->recv_batch.size()) {
|
||||
for (auto &ev : me->send_batch)
|
||||
(this->*ev.handler)(ev);
|
||||
for (auto &ev : me->recv_batch)
|
||||
for (auto &ev : me->recv_batch)
|
||||
(this->*ev.handler)(ev);
|
||||
me->send_batch.clear();
|
||||
me->recv_batch.clear();
|
||||
@@ -211,7 +212,7 @@ void CoreGateway::call_time() {
|
||||
while (!me->destroy) {
|
||||
std::this_thread::sleep_for(10ms);
|
||||
auto ts = std::make_shared<std::string>(
|
||||
datetime::FIX_timestamp(datetime::now()));
|
||||
datetime::FIX_timestamp(datetime::now()));
|
||||
me->event_queue.push(EventData(&CoreGateway::sync_time, ts)); // NOLINT
|
||||
}
|
||||
}
|
||||
@@ -261,8 +262,6 @@ void CoreGateway::sync_stop(EventData const &) {
|
||||
}
|
||||
|
||||
void CoreGateway::sync_send(EventData const &ev) {
|
||||
tracepoint(bts_trace, trace_order, (char *)"CoreGateway.sync_send", 0,
|
||||
ev.trace.md_sn,0);
|
||||
switch (me->state) {
|
||||
case State::OFF:
|
||||
bts_assert(false);
|
||||
@@ -273,7 +272,7 @@ void CoreGateway::sync_send(EventData const &ev) {
|
||||
case State::WSESSION_TERMINATE:
|
||||
break;
|
||||
case State::READY:
|
||||
me->session_info->session->send(ev.trace, ev.out_msgs);
|
||||
me->session_info->session->send(ev.out_msgs, ev.trace);
|
||||
break;
|
||||
default:
|
||||
bts_assert(false);
|
||||
@@ -394,16 +393,17 @@ void CoreGateway::sync_reader_disconnected(EventData const &) {
|
||||
}
|
||||
|
||||
void CoreGateway::sync_in_msg_raw(EventData const &ev) {
|
||||
LOG(Latency, Diag, "perf_event sync_in_msg_raw");
|
||||
switch (me->state) {
|
||||
case State::OFF:
|
||||
case State::PAUSED:
|
||||
bts_assert(false);
|
||||
break;
|
||||
case State::CONNECTING:
|
||||
case State::READY:
|
||||
case State::READY: {
|
||||
trace::ScopedPerfLogEntry _(me->perf_log, ev.in_msg.trace(),
|
||||
"CoreGateway::sync_in_msg_raw");
|
||||
me->session_info->session->receive(ev.in_msg);
|
||||
break;
|
||||
} break;
|
||||
case State::WREADER_DISCONNECT:
|
||||
case State::WSESSION_TERMINATE:
|
||||
break;
|
||||
@@ -413,7 +413,6 @@ void CoreGateway::sync_in_msg_raw(EventData const &ev) {
|
||||
}
|
||||
|
||||
void CoreGateway::sync_in_msg_sequenced(EventData const &ev) {
|
||||
LOG(Latency, Diag, "perf_event sync_in_msg_sequenced");
|
||||
switch (me->state) {
|
||||
case State::OFF:
|
||||
case State::PAUSED:
|
||||
@@ -422,6 +421,7 @@ void CoreGateway::sync_in_msg_sequenced(EventData const &ev) {
|
||||
break;
|
||||
case State::CONNECTING:
|
||||
case State::READY:
|
||||
me->perf_log.log (ev.in_msg.trace(), "CoreGateway::sync_in_msg_sequenced");
|
||||
me->observer.msg(ev.in_msg);
|
||||
break;
|
||||
case State::WSESSION_TERMINATE:
|
||||
@@ -471,13 +471,14 @@ void CoreGateway::sync_time(EventData const &ev) {
|
||||
LOG(Jetfix, Warn, "Connected to exchange gateway for {}",
|
||||
me->cfg.name());
|
||||
|
||||
me->session_info->session.reset(
|
||||
new Session(me->cfg, *me->session_info->connection,
|
||||
*me->session_info->store, *this,
|
||||
[]() { return utils::TimeStamp::now(); }));
|
||||
me->session_info->session.reset(new Session(
|
||||
me->cfg, *me->session_info->connection,
|
||||
*me->session_info->store, *this,
|
||||
[]() { return utils::TimeStamp::now(); }, me->perf_log));
|
||||
me->session_info->reader.reset(new AsyncReader(
|
||||
*me->session_info->connection, me->cfg.name(), *this,
|
||||
me->cfg.msg_buffer_sz(), me->cfg.msg_high_water_mark()));
|
||||
*me->session_info->connection, me->cfg.name(), *this,
|
||||
me->perf_log, me->cfg.msg_buffer_sz(),
|
||||
me->cfg.msg_high_water_mark()));
|
||||
me->session_info->reader->start();
|
||||
me->session_info->session->start();
|
||||
transition(State::CONNECTING);
|
||||
@@ -573,5 +574,5 @@ const char *CoreGateway::state_name(State st) {
|
||||
bts_assert(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace jetfix
|
||||
} // namespace bts
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
|
||||
namespace bts {
|
||||
namespace trace {class PerfLog;}
|
||||
namespace jetfix {
|
||||
class ICoreGatewayObserver;
|
||||
class IConnection;
|
||||
@@ -25,12 +26,13 @@ public:
|
||||
CoreGateway(config::JetFIX::Connection const &cfg,
|
||||
ICoreGatewayObserver &observer,
|
||||
ConnectionFactory const &conn_factory,
|
||||
StoreFactory const &store_factory);
|
||||
StoreFactory const &store_factory,
|
||||
trace::PerfLog &);
|
||||
|
||||
~CoreGateway();
|
||||
void start ();
|
||||
void stop ();
|
||||
void send (PerfTrace const&, OutFIXPtr const &msgs);
|
||||
void send (OutFIXPtr const &msgs, trace::CorID trace);
|
||||
void dispatch_events ();
|
||||
|
||||
// for certification use only
|
||||
|
||||
@@ -8,11 +8,12 @@
|
||||
#include "lib/jetfix/store/FIXFileStore.h"
|
||||
#include "lib/utils/meta_programming.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
|
||||
namespace bts {
|
||||
class PerfTraceLog;
|
||||
namespace trace {class PerfLog;}
|
||||
namespace jetfix {
|
||||
|
||||
// A parsing layer over CoreGateway
|
||||
@@ -20,7 +21,7 @@ template <typename... Messages>
|
||||
class JetFIXGateway : ICoreGatewayObserver
|
||||
{
|
||||
public:
|
||||
JetFIXGateway(config::JetFIX::Connection const &cfg, PerfTraceLog &log)
|
||||
JetFIXGateway(config::JetFIX::Connection const &cfg, trace::PerfLog &log)
|
||||
:
|
||||
_core (cfg,*this,
|
||||
|
||||
@@ -28,24 +29,29 @@ public:
|
||||
{return new SocketConnection (cfg,log);},
|
||||
|
||||
[] (config::JetFIX::Connection const &cfg)
|
||||
{ return new FIXFileStore (cfg);}),
|
||||
{ return new FIXFileStore (cfg);},
|
||||
|
||||
log),
|
||||
_dispatchers (),
|
||||
_dispatcher_idx (),
|
||||
_on_ready ([](){}),
|
||||
_on_disconnect ([](){}),
|
||||
_on_begin_replay ([](){}),
|
||||
_on_end_replay ([](){})
|
||||
_on_end_replay ([](){}),
|
||||
_perf_log(log)
|
||||
{
|
||||
std::vector<IDispatcher*> disp_vec;
|
||||
meta::collect_tuple_items (_dispatchers,&disp_vec);
|
||||
for (auto d : disp_vec)
|
||||
for (auto d : disp_vec) {
|
||||
d->set_log(_perf_log);
|
||||
_dispatcher_idx.emplace_back (d->tag (),d);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void start () {_core.start ();}
|
||||
void stop () {_core.stop ();}
|
||||
void send (PerfTrace const &trace, OutFIXPtr const &msgs) {_core.send (trace,msgs);}
|
||||
void send (OutFIXPtr const &msgs,trace::CorID trace ) {_core.send (msgs,trace);}
|
||||
void dispatch_events () {_core.dispatch_events ();}
|
||||
|
||||
|
||||
@@ -70,6 +76,7 @@ private:
|
||||
{
|
||||
virtual MessageTypes tag () const = 0;
|
||||
virtual void dispatch (InMsgBuffer const&msg) = 0;
|
||||
virtual void set_log(trace::PerfLog &) = 0;
|
||||
};
|
||||
|
||||
template <class MT>
|
||||
@@ -80,18 +87,24 @@ private:
|
||||
Dispatcher () : msg (), handler ([](MT const&){}) {}
|
||||
|
||||
MessageTypes tag () const override {return MT::tag;}
|
||||
|
||||
|
||||
void set_log(trace::PerfLog &l) override {
|
||||
log = &l;
|
||||
}
|
||||
|
||||
void dispatch (InMsgBuffer const&in_msg) override
|
||||
{
|
||||
LOG(Latency, Diag, "perf_event JetFIXGateway::before_parse");
|
||||
msg.read (in_msg.data (),in_msg.size ());
|
||||
{
|
||||
auto _ = trace::ScopedPerfLogEntry(*log,in_msg.trace(),"JetFIXGateway::dispatch");
|
||||
msg.read (in_msg.data (),in_msg.size ());
|
||||
}
|
||||
msg.set_trace(in_msg.trace());
|
||||
LOG(Latency, Diag, "perf_event JetFIXGateway::after_parse");
|
||||
handler (msg);
|
||||
}
|
||||
|
||||
MT msg;
|
||||
Handler handler;
|
||||
trace::PerfLog *log;
|
||||
};
|
||||
|
||||
typedef std::tuple<Dispatcher<Messages>...> DispatcherTuple;
|
||||
@@ -102,6 +115,7 @@ private:
|
||||
|
||||
void msg (InMsgBuffer const&msg) override
|
||||
{
|
||||
auto scoped = trace::ScopedPerfLogEntry(_perf_log,msg.trace(),"JetFIXGateway::msg");
|
||||
auto t = msg.type ();
|
||||
for (auto &d : _dispatcher_idx)
|
||||
if (d.first == t)
|
||||
@@ -123,6 +137,7 @@ private:
|
||||
std::function<void()> _on_disconnect;
|
||||
std::function<void()> _on_begin_replay;
|
||||
std::function<void()> _on_end_replay;
|
||||
trace::PerfLog &_perf_log;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -1,10 +1,6 @@
|
||||
#include "Session.h"
|
||||
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/SecureLogon.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/jetfix/codec/FIXEncodingStream.h"
|
||||
#include "lib/jetfix/codec/FieldDefinitions.h"
|
||||
#include "lib/jetfix/codec/MessageTypes.h"
|
||||
@@ -12,7 +8,11 @@
|
||||
#include "lib/jetfix/connection/IConnection.h"
|
||||
#include "lib/jetfix/gateway/ICoreGatewayEventSink.h"
|
||||
#include "lib/jetfix/store/IFIXStore.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/tracing/bts_trace.h"
|
||||
#include "lib/utils/SecureLogon.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
|
||||
@@ -57,12 +57,13 @@ struct Session::RecoveryState {
|
||||
|
||||
struct Session::PImpl {
|
||||
PImpl(config::JetFIX::Connection const &cfg, IConnection &connection,
|
||||
IFIXStore &store, ICoreGatewayEventSink &sink, Clock const &clock)
|
||||
IFIXStore &store, ICoreGatewayEventSink &sink, Clock const &clock,
|
||||
trace::PerfLog &perf_log)
|
||||
: cfg(cfg), state(State::TERMINATED), connection(connection),
|
||||
store(store), event_sink(sink), now(clock), begin_of_this_session(),
|
||||
last_seq_num(), last_seq_num_processed(), last_sent_msg_timestamp(),
|
||||
last_sent_sync_timestamp(), last_rcvd_msg_timestamp(),
|
||||
start_of_logon(), start_of_logout(),
|
||||
store(store), event_sink(sink), now(clock), perf_log(perf_log),
|
||||
begin_of_this_session(), last_seq_num(), last_seq_num_processed(),
|
||||
last_sent_msg_timestamp(), last_sent_sync_timestamp(),
|
||||
last_rcvd_msg_timestamp(), start_of_logon(), start_of_logout(),
|
||||
current_time(std::make_shared<std::string>(
|
||||
datetime::FIX_timestamp(datetime::now()))),
|
||||
recovery_state() {}
|
||||
@@ -73,6 +74,7 @@ struct Session::PImpl {
|
||||
IFIXStore &store;
|
||||
ICoreGatewayEventSink &event_sink;
|
||||
Clock now;
|
||||
trace::PerfLog &perf_log;
|
||||
uint32_t begin_of_this_session;
|
||||
uint32_t last_seq_num;
|
||||
uint32_t last_seq_num_processed;
|
||||
@@ -87,8 +89,8 @@ struct Session::PImpl {
|
||||
|
||||
Session::Session(config::JetFIX::Connection const &cfg, IConnection &connection,
|
||||
IFIXStore &store, ICoreGatewayEventSink &sink,
|
||||
Clock const &clock)
|
||||
: me(new PImpl(cfg, connection, store, sink, clock)) {}
|
||||
Clock const &clock, trace::PerfLog &perf_log)
|
||||
: me(new PImpl(cfg, connection, store, sink, clock, perf_log)) {}
|
||||
|
||||
Session::~Session() {
|
||||
if (me->state != State::TERMINATED)
|
||||
@@ -106,8 +108,7 @@ void Session::start() {
|
||||
*me->current_time, me->cfg));
|
||||
}
|
||||
|
||||
void Session::send(PerfTrace const &trace, OutFIXPtr const &msgs) {
|
||||
tracepoint(bts_trace, trace_order, (char *)"Session.send", 0, trace.md_sn, 0);
|
||||
void Session::send(OutFIXPtr const &msgs, trace::CorID trace) {
|
||||
switch (me->state) {
|
||||
case State::READY:
|
||||
case State::CERT_MODE:
|
||||
@@ -467,7 +468,6 @@ void Session::on_hb(InMsgBuffer const &msg) {
|
||||
}
|
||||
|
||||
void Session::on_app(InMsgBuffer const &msg) {
|
||||
LOG(Latency, Diag, "perf_event on_app");
|
||||
switch (me->state) {
|
||||
case State::WLOGON:
|
||||
terminate_session(
|
||||
@@ -547,10 +547,10 @@ void Session::buffer_or_deliver(InMsgBuffer const &msg) {
|
||||
}
|
||||
|
||||
void Session::internal_send(OutFIXPtr const &msgs) {
|
||||
internal_send(PerfTrace(), msgs);
|
||||
internal_send(trace::CorID{}, msgs);
|
||||
}
|
||||
|
||||
void Session::internal_send(PerfTrace const &trace, OutFIXPtr const &msgs) {
|
||||
void Session::internal_send(trace::CorID trace, OutFIXPtr const &msgs) {
|
||||
IConnection::WriteVec data_vec;
|
||||
bts_assert(msgs->fix.size() < IConnection::MaxWriteVecSz);
|
||||
|
||||
@@ -562,13 +562,10 @@ void Session::internal_send(PerfTrace const &trace, OutFIXPtr const &msgs) {
|
||||
data_vec[i].first = s.data();
|
||||
data_vec[i].second = s.size();
|
||||
}
|
||||
tracepoint(bts_trace, trace_order, (char *)"Session.internal_send.pre_write", 0, trace.md_sn,0);
|
||||
if (me->connection.write(data_vec, msgs->fix.size(), trace) < 0) {
|
||||
terminate_session(JetFIXError::CONNECTION_ERROR);
|
||||
return;
|
||||
}
|
||||
tracepoint(bts_trace, trace_order,
|
||||
(char *)"Session.internal_send.post_write", 0, trace.md_sn, 0);
|
||||
me->last_sent_msg_timestamp = me->now();
|
||||
me->store.store(msgs);
|
||||
msgs->set_ts(OutFIX::POST_STORE, utils::TimeStamp::now());
|
||||
@@ -612,7 +609,7 @@ bool match_with_outgoing_sn_too_small(std::string const &msg, uint32_t *sn) {
|
||||
|
||||
bool match_with_incoming_sn_too_small(std::string const &msg, uint32_t *sn) {
|
||||
std::string rx_str = ".*Incoming sequence number was (\\d+) "
|
||||
"but the last one we processed was .*";
|
||||
"but the last one we processed was .*";
|
||||
|
||||
std::regex rx(rx_str);
|
||||
|
||||
@@ -648,8 +645,7 @@ void Session::terminate_session(JetFIXError::Code error,
|
||||
me->cfg.name(), fixed_sn);
|
||||
me->store.set_last_seq_num(fixed_sn - 1);
|
||||
}
|
||||
}
|
||||
else if (error == JetFIXError::INCOMING_SN_TOO_SMALL) {
|
||||
} else if (error == JetFIXError::INCOMING_SN_TOO_SMALL) {
|
||||
uint32_t fixed_sn;
|
||||
if (match_with_incoming_sn_too_small(extra_info, &fixed_sn)) {
|
||||
LOG(Jetfix, Warn,
|
||||
@@ -681,7 +677,7 @@ void Session::send_seq_reset(uint32_t sn, uint32_t new_sn) {
|
||||
IConnection::WriteVec data_vec;
|
||||
data_vec[0].first = stream.data();
|
||||
data_vec[0].second = stream.size();
|
||||
if (me->connection.write(data_vec, 1, PerfTrace()) < 0) {
|
||||
if (me->connection.write(data_vec, 1, trace::CorID()) < 0) {
|
||||
terminate_session(JetFIXError::CONNECTION_ERROR);
|
||||
return;
|
||||
} else {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/jetfix/common/OutFIXPtr.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
|
||||
@@ -27,11 +28,12 @@ public:
|
||||
IConnection &connection,
|
||||
IFIXStore &store,
|
||||
ICoreGatewayEventSink &sink,
|
||||
Clock const &clock);
|
||||
Clock const &clock,
|
||||
trace::PerfLog &perf_log);
|
||||
~Session();
|
||||
|
||||
void start ();
|
||||
void send (PerfTrace const &, OutFIXPtr const&);
|
||||
void send (OutFIXPtr const&, trace::CorID);
|
||||
void receive (InMsgBuffer const&);
|
||||
void shutdown ();
|
||||
void time (std::shared_ptr<std::string> const&time_str);
|
||||
@@ -59,7 +61,7 @@ private:
|
||||
void buffer (InMsgBuffer const &msg);
|
||||
void buffer_or_deliver (InMsgBuffer const&msg);
|
||||
|
||||
void internal_send (PerfTrace const&, OutFIXPtr const&);
|
||||
void internal_send (trace::CorID , OutFIXPtr const&);
|
||||
void internal_send (OutFIXPtr const&);
|
||||
void logout (JetFIXError::Code reason);
|
||||
void logout (JetFIXError::Code reason,std::string const &extra_info);
|
||||
|
||||
@@ -111,7 +111,7 @@ struct Connection : public IConnection
|
||||
return str.gcount ();
|
||||
}
|
||||
|
||||
int write (WriteVec const&,size_t, PerfTrace const&)
|
||||
int write (WriteVec const&,size_t, trace::CorID )
|
||||
{return -1;}
|
||||
|
||||
std::istream &str;
|
||||
@@ -119,6 +119,8 @@ struct Connection : public IConnection
|
||||
|
||||
TEST_CASE ("JetFIX AsyncReaderTests", "[jetfix]")
|
||||
{
|
||||
trace::PerfLog l;
|
||||
|
||||
SECTION ("jeftix_smoke")
|
||||
{
|
||||
std::stringstream str;
|
||||
@@ -128,7 +130,7 @@ TEST_CASE ("JetFIX AsyncReaderTests", "[jetfix]")
|
||||
MyGWObserver ob;
|
||||
|
||||
{
|
||||
AsyncReader r (c,"test",ob,10,5);
|
||||
AsyncReader r (c,"test",ob,l,10,5);
|
||||
r.start ();
|
||||
}
|
||||
|
||||
@@ -144,7 +146,7 @@ TEST_CASE ("JetFIX AsyncReaderTests", "[jetfix]")
|
||||
MyGWObserver ob;
|
||||
|
||||
{
|
||||
AsyncReader r (c,"test",ob,10,5);
|
||||
AsyncReader r (c,"test",ob,l,10,5);
|
||||
r.start ();
|
||||
}
|
||||
REQUIRE (ob.got_eof);
|
||||
|
||||
@@ -37,6 +37,10 @@ class Actor {
|
||||
// Implement if you need special cleanup at actor destruction time
|
||||
virtual void finit(){};
|
||||
|
||||
// Implement this if you want to be called before/after an event is dispatched
|
||||
virtual void before_event() {}
|
||||
virtual void after_event() {}
|
||||
|
||||
// subscribe to events using a specific tag
|
||||
template <class EV, class CB>
|
||||
SubHandle subscribe(typename EV::Tag const &tag, CB const &cb) {
|
||||
@@ -67,10 +71,12 @@ class Actor {
|
||||
tracepoint(bts_trace, trace_event, utils::type_name(typeid(*this)),
|
||||
utils::type_name<EV>(), _broker_id, sid.value(),
|
||||
0);
|
||||
before_event();
|
||||
if (payload)
|
||||
cb(UpdateType{sid, type, &(payload->unwrap<typename EV::Array>())});
|
||||
else
|
||||
cb(UpdateType{sid, type, nullptr});
|
||||
after_event();
|
||||
};
|
||||
|
||||
auto result = _subscribe(filter, handler);
|
||||
@@ -107,10 +113,12 @@ class Actor {
|
||||
tracepoint(bts_trace, trace_event, utils::type_name(typeid(*this)),
|
||||
utils::type_name<EV>(), _broker_id, sid.value(),
|
||||
0);
|
||||
before_event();
|
||||
if (payload)
|
||||
cb(UpdateType{sid, type, &(payload->unwrap<typename EV::Array>())});
|
||||
else
|
||||
cb(UpdateType{sid, type, nullptr});
|
||||
after_event();
|
||||
};
|
||||
|
||||
auto result = _subscribe(filter, handler);
|
||||
@@ -146,10 +154,12 @@ class Actor {
|
||||
tracepoint(bts_trace, trace_event, utils::type_name(typeid(*this)),
|
||||
utils::type_name<EV>(), _broker_id, sid.value(),
|
||||
0);
|
||||
before_event();
|
||||
if (payload)
|
||||
cb(UpdateType{sid, type, &(payload->unwrap<typename EV::Array>())});
|
||||
else
|
||||
cb(UpdateType{sid, type, nullptr});
|
||||
after_event();
|
||||
};
|
||||
|
||||
auto result = _subscribe(filter, handler);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
@@ -29,6 +30,9 @@ template <typename EV, typename KE = KeyExtractor> class ContentSubscription {
|
||||
using Key = decltype((*((KE *)nullptr))(*((EV *)nullptr)));
|
||||
using CB = std::function<void(const EV &)>;
|
||||
struct SubID {
|
||||
SubID() : k(), sid(0) {}
|
||||
SubID(Key k, uint32_t sid) : k(k), sid(sid) {}
|
||||
|
||||
Key k;
|
||||
uint32_t sid;
|
||||
bool operator==(SubID const &rhs) const {
|
||||
@@ -39,7 +43,7 @@ template <typename EV, typename KE = KeyExtractor> class ContentSubscription {
|
||||
ContentSubscription(Actor &actor)
|
||||
: _actor(&actor), _sub(_actor->subscribe<EV>(
|
||||
[this](auto const &u) { this->on_update(u); })),
|
||||
_my_subs(), _dispatching(false), _current_sub_id(0),
|
||||
_my_subs(), _dispatching(false), _current_sub_id(1),
|
||||
_pre_sub([](auto) {}) {}
|
||||
|
||||
ContentSubscription(Actor &actor, std::function<void(Key)> const &pscb)
|
||||
@@ -52,7 +56,7 @@ template <typename EV, typename KE = KeyExtractor> class ContentSubscription {
|
||||
: _actor(&actor),
|
||||
_sub(_actor->subscribe<EV>(
|
||||
tag, [this](auto const &u) { this->on_update(u); })),
|
||||
_my_subs(), _dispatching(false), _current_sub_id(0),
|
||||
_my_subs(), _dispatching(false), _current_sub_id(1),
|
||||
_pre_sub([](auto) {}) {}
|
||||
|
||||
template <class Tag>
|
||||
@@ -61,7 +65,7 @@ template <typename EV, typename KE = KeyExtractor> class ContentSubscription {
|
||||
: _actor(&actor),
|
||||
_sub(_actor->subscribe<EV>(
|
||||
tag, [this](auto const &u) { this->on_update(u); })),
|
||||
_my_subs(), _dispatching(false), _current_sub_id(0),
|
||||
_my_subs(), _dispatching(false), _current_sub_id(1),
|
||||
_pre_sub(pscb) {}
|
||||
|
||||
template <class CBT> SubID sub(Key k, CBT const &cb) {
|
||||
@@ -97,7 +101,7 @@ template <typename EV, typename KE = KeyExtractor> class ContentSubscription {
|
||||
CB cb;
|
||||
};
|
||||
|
||||
using SubList = std::vector<Subscriber>;
|
||||
using SubList = boost::container::small_vector<Subscriber,5>;
|
||||
using Subscriptions = std::unordered_map<Key, SubList>;
|
||||
|
||||
Actor *_actor;
|
||||
|
||||
@@ -1,29 +1,24 @@
|
||||
#include "AsyncFactory.h"
|
||||
|
||||
#include "lib/orderman/events/CreateOrderRequestEvent.h"
|
||||
#include "lib/orderman/events/CreateOrderReplyEvent.h"
|
||||
#include "lib/orderman/events/CreateFIXBufferRequestEvent.h"
|
||||
#include "lib/orderman/events/CreateFIXBufferReplyEvent.h"
|
||||
#include "lib/orderman/order_factory/OrderFactory.h"
|
||||
#include "lib/orderman/events/NewSecuritiesEvent.h"
|
||||
#include "lib/jetfix/common/OutFIX.h"
|
||||
#include "lib/orderman/securities/MapSecurityDB.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include "lib/orderman/order_router/OrderRouter.h"
|
||||
#include "lib/jetfix/common/OutFIX.h"
|
||||
#include "lib/orderman/events/CreateFIXBufferReplyEvent.h"
|
||||
#include "lib/orderman/events/CreateFIXBufferRequestEvent.h"
|
||||
#include "lib/orderman/events/CreateOrderReplyEvent.h"
|
||||
#include "lib/orderman/events/CreateOrderRequestEvent.h"
|
||||
#include "lib/orderman/events/NewAdminDataEvent.h"
|
||||
#include "lib/orderman/events/NewSecuritiesEvent.h"
|
||||
#include "lib/orderman/order_factory/OrderFactory.h"
|
||||
#include "lib/orderman/order_router/OrderRouter.h"
|
||||
#include "lib/orderman/securities/MapSecurityDB.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
struct AsyncFactory::PImpl
|
||||
{
|
||||
PImpl(DynamicOrderRegistry const&b) :
|
||||
orders_stream(),
|
||||
fix_buffer_stream(),
|
||||
db(),
|
||||
order_router(),
|
||||
order_factory(db,order_router,b)
|
||||
{}
|
||||
struct AsyncFactory::PImpl {
|
||||
PImpl(DynamicOrderRegistry const &b)
|
||||
: orders_stream(), fix_buffer_stream(), db(), order_router(),
|
||||
order_factory(db, order_router, b) {}
|
||||
|
||||
mercury::Stream<CreateOrderReplyEvent> orders_stream;
|
||||
mercury::Stream<CreateFIXBufferReplyEvent> fix_buffer_stream;
|
||||
@@ -32,48 +27,49 @@ struct AsyncFactory::PImpl
|
||||
OrderFactory order_factory;
|
||||
};
|
||||
|
||||
AsyncFactory::AsyncFactory(DynamicOrderRegistry const&b) : me(new PImpl(b)) {}
|
||||
AsyncFactory::~AsyncFactory () {}
|
||||
AsyncFactory::AsyncFactory(DynamicOrderRegistry const &b) : me(new PImpl(b)) {}
|
||||
AsyncFactory::~AsyncFactory() {}
|
||||
|
||||
void AsyncFactory::init()
|
||||
{
|
||||
void AsyncFactory::init() {
|
||||
me->orders_stream = make_stream<CreateOrderReplyEvent>();
|
||||
me->fix_buffer_stream = make_stream<CreateFIXBufferReplyEvent> ();
|
||||
me->fix_buffer_stream = make_stream<CreateFIXBufferReplyEvent>();
|
||||
|
||||
subscribe<NewAdminDataEvent>([this](auto const &u) {
|
||||
if (u.events)
|
||||
for (auto ev : *(u.events))
|
||||
me->order_router.update(*ev.admin_data);
|
||||
});
|
||||
if (u.events)
|
||||
for (auto ev : *(u.events))
|
||||
me->order_router.update(*ev.admin_data);
|
||||
});
|
||||
|
||||
subscribe<NewSecuritiesEvent>([this] (auto const&u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *(u.events))
|
||||
me->db.securities[SecurityKey(ev.security->id())] = ev.security;
|
||||
});
|
||||
|
||||
subscribe<CreateOrderRequestEvent>([this] (const auto &u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *u.events) {
|
||||
std::vector<Order*> response;
|
||||
response.reserve(ev.n_orders);
|
||||
while (response.size() < ev.n_orders)
|
||||
response.push_back(&me->order_factory.create(ev.parent_key,*ev.def));
|
||||
this->publish(me->orders_stream,ev.request_id,std::move(response));
|
||||
}
|
||||
});
|
||||
subscribe<NewSecuritiesEvent>([this](auto const &u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *(u.events))
|
||||
me->db.securities[SecurityKey(ev.security->id())] = ev.security;
|
||||
});
|
||||
|
||||
subscribe<CreateFIXBufferRequestEvent>([this] (const auto &u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *u.events) {
|
||||
std::vector<jetfix::OutFIXPtr> response;
|
||||
response.reserve(ev.n_buffers);
|
||||
while (response.size() < ev.n_buffers)
|
||||
response.push_back(std::make_shared<jetfix::OutFIX> (*ev.cfg));
|
||||
this->publish(me->fix_buffer_stream,std::move(response),ev.cfg);
|
||||
}
|
||||
});
|
||||
subscribe<CreateOrderRequestEvent>([this](const auto &u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *u.events) {
|
||||
std::vector<Order *> response;
|
||||
response.reserve(ev.n_orders);
|
||||
while (response.size() < ev.n_orders)
|
||||
response.push_back(
|
||||
&me->order_factory.create(ev.parent_key, *ev.def));
|
||||
this->publish(me->orders_stream, ev.request_id,
|
||||
std::move(response));
|
||||
}
|
||||
});
|
||||
|
||||
subscribe<CreateFIXBufferRequestEvent>([this](const auto &u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *u.events) {
|
||||
std::vector<jetfix::OutFIXPtr> response;
|
||||
response.reserve(ev.n_buffers);
|
||||
while (response.size() < ev.n_buffers)
|
||||
response.push_back(std::make_shared<jetfix::OutFIX>(*ev.cfg));
|
||||
this->publish(me->fix_buffer_stream, std::move(response), ev.cfg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -24,11 +24,10 @@ using JFGw =
|
||||
fix::RejectMsg, fix::CancelRejectMsg>;
|
||||
|
||||
struct ExecutionGateway::PImpl {
|
||||
PImpl(config::JetFIX::Connection const &cfg, PerfTraceLog &log)
|
||||
: gw(cfg, log), exe_report_pool(), state(State::WAITING_FOR_SECURITIES),
|
||||
securities(), securities_by_descriptor(), exe_report_stream(),
|
||||
gw_status_change_stream(), cancel_reject_stream(), gw_name(cfg.name()),
|
||||
cfg(cfg) {}
|
||||
PImpl(config::JetFIX::Connection const &cfg, trace::PerfLog &l)
|
||||
: gw(cfg, l), exe_report_pool(), state(State::WAITING_FOR_SECURITIES),
|
||||
securities(), securities_by_descriptor(), gw_name(cfg.name()),
|
||||
cfg(cfg), perf_log(l) {}
|
||||
|
||||
JFGw gw;
|
||||
boost::object_pool<ExecutionReport> exe_report_pool;
|
||||
@@ -36,25 +35,20 @@ struct ExecutionGateway::PImpl {
|
||||
std::unordered_map<SecurityKey::Value, api::Security const *> securities;
|
||||
std::unordered_map<std::string, api::Security const *>
|
||||
securities_by_descriptor;
|
||||
mercury::Stream<NewExecutionReportEvent> exe_report_stream;
|
||||
mercury::Stream<GatewayStatusChangeEvent> gw_status_change_stream;
|
||||
mercury::Stream<CancelRejectEvent> cancel_reject_stream;
|
||||
GatewayName gw_name;
|
||||
config::JetFIX::Connection cfg;
|
||||
OnExeReport on_exe_report;
|
||||
OnGwStatusChange on_gw_status_change;
|
||||
OnCancelReject on_cancel_reject;
|
||||
trace::PerfLog &perf_log;
|
||||
};
|
||||
|
||||
ExecutionGateway::ExecutionGateway(config::JetFIX::Connection const &cfg,
|
||||
PerfTraceLog &log)
|
||||
: me(new PImpl(cfg, log)) {}
|
||||
ExecutionGateway::~ExecutionGateway() {}
|
||||
|
||||
void ExecutionGateway::init() {
|
||||
trace::PerfLog &log)
|
||||
: me(new PImpl(cfg, log)) {
|
||||
me->state = State::WAITING_FOR_SECURITIES;
|
||||
subscribe<NewSecuritiesEvent>(
|
||||
[this](auto const &sec_update) { this->on_securities(sec_update); });
|
||||
subscribe<OutFIXMsgEvent>(
|
||||
me->gw_name, [this](auto const &fix) { this->on_outgoing_fix(fix); });
|
||||
}
|
||||
ExecutionGateway::~ExecutionGateway() {}
|
||||
|
||||
void ExecutionGateway::on_securities(
|
||||
const mercury::Update<NewSecuritiesEvent> &update) {
|
||||
@@ -78,7 +72,6 @@ void ExecutionGateway::on_securities(
|
||||
|
||||
// subscribe to gateway events
|
||||
me->gw.on<fix::ExecutionReportMsg>() = [this](auto const &msg) {
|
||||
LOG(Latency, Diag, "perf_event ExecutionGateway::after_parse");
|
||||
this->on_exe_report(msg);
|
||||
};
|
||||
me->gw.on<fix::CancelRejectMsg>() = [this](auto const &msg) {
|
||||
@@ -96,27 +89,25 @@ void ExecutionGateway::on_securities(
|
||||
me->gw.on<fix::RejectMsg>() = [this](auto const &m) {
|
||||
this->on_session_reject(m);
|
||||
};
|
||||
|
||||
// stream of execution reports
|
||||
me->exe_report_stream =
|
||||
make_stream<NewExecutionReportEvent>(me->gw_name);
|
||||
|
||||
// stream for gateway status
|
||||
me->gw_status_change_stream =
|
||||
make_stream<GatewayStatusChangeEvent>(me->gw_name);
|
||||
|
||||
// stream for cancel rejects
|
||||
me->cancel_reject_stream = make_stream<CancelRejectEvent>(me->gw_name);
|
||||
|
||||
// start the gateway
|
||||
me->gw.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExecutionGateway::start() { me->gw.start(); }
|
||||
|
||||
OnExeReport &ExecutionGateway::on_exe_report() { return me->on_exe_report; }
|
||||
|
||||
OnGwStatusChange &ExecutionGateway::on_gw_status_change() {
|
||||
return me->on_gw_status_change;
|
||||
}
|
||||
|
||||
OnCancelReject &ExecutionGateway::on_cancel_reject() {
|
||||
return me->on_cancel_reject;
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_exe_report(
|
||||
const jetfix::fix::ExecutionReportMsg &msg) {
|
||||
auto start = Timer::now();
|
||||
trace::ScopedPerfLogEntry scoped_log(me->perf_log,msg.trace(), "ExecutionGateway::on_exe_report");
|
||||
OrderKey ok;
|
||||
SecurityKey sk;
|
||||
decode_clorid(msg.get<fix::ClOrdID>(), &ok, &sk);
|
||||
@@ -194,8 +185,6 @@ void ExecutionGateway::on_exe_report(
|
||||
|
||||
data->exec_type = api::orderman::ExecType(msg.get<fix::ExecType>());
|
||||
data->msg_seq_num = msg.get<fix::MsgSeqNum>();
|
||||
tracepoint(bts_trace, trace_order,
|
||||
(char *)"ExecutionGateway.on_exe_report", 0, 0, 0);
|
||||
|
||||
// copy data from fix msg to our structure
|
||||
if (msg.has<fix::Account>())
|
||||
@@ -258,12 +247,7 @@ void ExecutionGateway::on_exe_report(
|
||||
str_to(msg.get<fix::TransactTimeStr>(), &data->transact_time);
|
||||
data->cfg = &me->cfg;
|
||||
data->msg = text_msg;
|
||||
auto trace = msg.trace();
|
||||
trace.exe_gw = utils::TimeStamp::now();
|
||||
publish(me->exe_report_stream, data, trace);
|
||||
LOG(Latency, Info,
|
||||
"Finished handling of execution report for order {}. {}ns",
|
||||
ok.value(), ns_diff(Timer::now(), start));
|
||||
me->on_exe_report(*data, msg.trace());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,7 +263,7 @@ void ExecutionGateway::on_cancel_reject(
|
||||
else if (msg.has<fix::CXlRejReason>())
|
||||
reject_msg = fmt::format("Order cancel was rejected with error code {}",
|
||||
msg.get<fix::CXlRejReason>());
|
||||
publish(me->cancel_reject_stream, ok, reject_msg);
|
||||
me->on_cancel_reject(ok, reject_msg);
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_connection_status_change(bool is_connected) {
|
||||
@@ -287,8 +271,7 @@ void ExecutionGateway::on_connection_status_change(bool is_connected) {
|
||||
me->gw_name.value(),
|
||||
is_connected ? std::string("connected") : std::string("disconnected"));
|
||||
|
||||
publish(me->gw_status_change_stream,
|
||||
GatewayStatusChangeEvent(me->gw_name, is_connected));
|
||||
me->on_gw_status_change(is_connected);
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_business_reject(
|
||||
@@ -308,21 +291,15 @@ void ExecutionGateway::on_session_reject(jetfix::fix::RejectMsg const &m) {
|
||||
m.has<fix::Text>() ? m.get<fix::Text>() : std::string("Unknown"));
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_poll() { me->gw.dispatch_events(); }
|
||||
void ExecutionGateway::dispatch_incoming_fix_messages() {
|
||||
me->gw.dispatch_events();
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_outgoing_fix(
|
||||
mercury::Update<OutFIXMsgEvent> const &msg) {
|
||||
if (msg.events)
|
||||
for (auto const &ev : *(msg.events)) {
|
||||
LOG(Latency, Info, "on_outgoing_fix sn: {} ok: {}", ev.trace.md_sn,
|
||||
ev.trace.order_key);
|
||||
tracepoint(bts_trace, trace_order, (char *)"on_outgoing_fix", 0,
|
||||
ev.trace.md_sn, 0);
|
||||
auto trace = ev.trace;
|
||||
trace.exe_gw = utils::TimeStamp::now();
|
||||
me->gw.send(trace, ev.msg);
|
||||
me->gw.dispatch_events();
|
||||
}
|
||||
void ExecutionGateway::send_fix(jetfix::OutFIXPtr const &fix,
|
||||
trace::CorID trace) {
|
||||
trace::ScopedPerfLogEntry scoped_log (me->perf_log,trace,"ExecutionGateway::send_fix");
|
||||
me->gw.send(fix,trace);
|
||||
me->gw.dispatch_events();
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
|
||||
@@ -1,44 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include "lib/mercury/actors/Actor.h"
|
||||
#include "lib/jetfix/codec/MessageDefinitions.h"
|
||||
#include "lib/jetfix/common/OutFIXPtr.h"
|
||||
#include "lib/mercury/events/Update.h"
|
||||
#include "lib/orderman/events/OutFIXMsgEvent.h"
|
||||
#include "lib/mercury/actors/IPollableActor.h"
|
||||
#include "lib/mercury/actors/PingPong.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
class PerfTraceLog;
|
||||
namespace mercury { class PollEvent;}
|
||||
namespace trace {struct CorID;class PerfLog;}
|
||||
namespace orderman {
|
||||
class NewSecuritiesEvent;
|
||||
class ExecutionReport;
|
||||
|
||||
using OnExeReport =
|
||||
std::function<void(ExecutionReport const &, trace::CorID const &)>;
|
||||
using OnGwStatusChange = std::function<void(bool)>;
|
||||
using OnCancelReject = std::function<void(OrderKey, std::string const &)>;
|
||||
|
||||
// fix execution gateway for a single FIX session
|
||||
class ExecutionGateway : public mercury::Actor,
|
||||
public mercury::IPollableActor
|
||||
{
|
||||
public:
|
||||
ExecutionGateway(config::JetFIX::Connection const&, PerfTraceLog& );
|
||||
class ExecutionGateway {
|
||||
public:
|
||||
ExecutionGateway(config::JetFIX::Connection const &, trace::PerfLog &);
|
||||
~ExecutionGateway();
|
||||
void init() override;
|
||||
|
||||
private:
|
||||
enum class State {WAITING_FOR_SECURITIES,READY};
|
||||
void start();
|
||||
void dispatch_incoming_fix_messages();
|
||||
void on_securities(const mercury::Update<NewSecuritiesEvent> &);
|
||||
void send_fix(jetfix::OutFIXPtr const &fix, trace::CorID trace);
|
||||
|
||||
OnExeReport& on_exe_report();
|
||||
OnGwStatusChange& on_gw_status_change();
|
||||
OnCancelReject &on_cancel_reject();
|
||||
|
||||
private:
|
||||
enum class State { WAITING_FOR_SECURITIES, READY };
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
|
||||
void on_poll() override;
|
||||
|
||||
void on_securities(const mercury::Update<NewSecuritiesEvent> &);
|
||||
void on_exe_report(jetfix::fix::ExecutionReportMsg const&);
|
||||
void on_cancel_reject(jetfix::fix::CancelRejectMsg const&);
|
||||
// fix gateway events
|
||||
void on_exe_report(jetfix::fix::ExecutionReportMsg const &);
|
||||
void on_cancel_reject(jetfix::fix::CancelRejectMsg const &);
|
||||
void on_connection_status_change(bool connected);
|
||||
void on_business_reject(jetfix::fix::BusinessMessageRejectMsg const&);
|
||||
void on_session_reject(jetfix::fix::RejectMsg const&);
|
||||
void on_outgoing_fix(const mercury::Update<OutFIXMsgEvent> &);
|
||||
void on_business_reject(jetfix::fix::BusinessMessageRejectMsg const &);
|
||||
void on_session_reject(jetfix::fix::RejectMsg const &);
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -19,7 +19,7 @@ enum class FillStore::State { WAIT_SEC, READY };
|
||||
|
||||
struct FillStore::UnresolvedFills {
|
||||
std::vector<api::orderman::Fill> fills;
|
||||
api::Security const* security;
|
||||
api::Security const *security;
|
||||
};
|
||||
|
||||
struct FillStore::PImpl {
|
||||
@@ -145,13 +145,13 @@ void FillStore::on_execution_report(
|
||||
fill.mutable_timestamp()->set_seconds(tv.tv_sec);
|
||||
fill.mutable_timestamp()->set_nanos(tv.tv_usec * 1000);
|
||||
|
||||
|
||||
auto order_def = me->known_orders[er.order_key];
|
||||
if (order_def)
|
||||
fill.set_account(order_def->account());
|
||||
else
|
||||
me->fills_with_unresolved_accounts[er.order_key].fills.push_back(fill);
|
||||
|
||||
me->fills_with_unresolved_accounts[er.order_key]
|
||||
.fills.push_back(fill);
|
||||
|
||||
auto fv = utils::pb::to_string(fill);
|
||||
me->store->insert(fills_ns, fk, fv);
|
||||
publish(me->fill_stream, fill, *er.security);
|
||||
@@ -167,7 +167,7 @@ void FillStore::on_execution_report(
|
||||
void FillStore::on_order_state_update(
|
||||
mercury::Update<OrderStateUpdateEvent> const &u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *u.events) {
|
||||
for (auto const &ev : *u.events) {
|
||||
me->known_orders[ev.key] = ev.definition;
|
||||
|
||||
// see if we can resolve the accounts for any fills that we
|
||||
@@ -177,11 +177,12 @@ void FillStore::on_order_state_update(
|
||||
f.set_account(ev.definition->account());
|
||||
auto fv = utils::pb::to_string(f);
|
||||
me->store->update(fills_ns, f.fill_key(), fv);
|
||||
publish(me->fill_stream,f, *unresolved_fills.security);
|
||||
publish(me->fill_stream, f, *unresolved_fills.security);
|
||||
}
|
||||
unresolved_fills.fills.clear();
|
||||
// end fill resolution
|
||||
}
|
||||
}
|
||||
|
||||
}} // namespace orderman
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -3,11 +3,13 @@
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/md/MDConsumer.h"
|
||||
#include "lib/md/MDConsumerHandle.h"
|
||||
#include "lib/md/cme/MDP3Utils.h"
|
||||
#include "lib/mercury/actors/Actor.h"
|
||||
#include "lib/mercury/actors/IPollableActor.h"
|
||||
#include "lib/mercury/events/TimerEvent.h"
|
||||
#include "lib/orderman/events/BookUpdateEvent.h"
|
||||
#include "lib/orderman/events/MDRequestEvent.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
#include "lib/tracing/bts_trace.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
|
||||
@@ -21,11 +23,23 @@ namespace orderman {
|
||||
|
||||
class MDSource : public mercury::Actor, public mercury::IPollableActor {
|
||||
public:
|
||||
MDSource(md::MDConsumer &consumer, metrics::MetricsContainer &stats)
|
||||
: _handles(), _md_consumer(consumer), _book_update(), _stats(stats), _loop_timer() {}
|
||||
MDSource(md::MDConsumer &consumer, metrics::MetricsContainer &stats,
|
||||
trace::PerfLog &log)
|
||||
: _handles(), _md_consumer(consumer), _book_update(), _perf_log(log),
|
||||
_stats(stats), _loop_timer() {
|
||||
|
||||
auto cme_channels = md::get_configured_cme_channels();
|
||||
for (auto const &c : cme_channels)
|
||||
for (auto const &s : c.symbols)
|
||||
_symbol_to_channel[s] = c.id;
|
||||
}
|
||||
|
||||
void init() override {
|
||||
LOG(OrderMan, Warn, "Starting MDSource actor");
|
||||
bts_assert_msg(_md_consumer.all_heartbeats_ok(), "Marketdata heartbeats are bad. MDConsumer will not recover from this condition. Please run 'sparkd md cleanup' and restart sparkd");
|
||||
bts_assert_msg(
|
||||
_md_consumer.all_heartbeats_ok(),
|
||||
"Marketdata heartbeats are bad. MDConsumer will not recover from "
|
||||
"this condition. Please run 'sparkd md cleanup' and restart sparkd");
|
||||
|
||||
std::array<double, 30> buckets;
|
||||
auto i = 1;
|
||||
@@ -63,7 +77,13 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor {
|
||||
LOG(OrderMan, Warn,
|
||||
"Starting to publish book updates for security key: {}", k);
|
||||
auto handle = _md_consumer.get_handle(k);
|
||||
_handles.push_back({utils::TimeStamp(), handle});
|
||||
|
||||
auto &channel = _symbol_to_channel[handle.get_usersymbol()];
|
||||
if (channel.empty())
|
||||
channel = "sim_md";
|
||||
|
||||
_handles.push_back(
|
||||
{utils::TimeStamp(), handle, channel.c_str()});
|
||||
if (handle.is_synthetic()) {
|
||||
synthetic_count_metric->increment();
|
||||
}
|
||||
@@ -77,38 +97,35 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor {
|
||||
void on_poll() override {
|
||||
Book book;
|
||||
|
||||
PerfTrace trace;
|
||||
auto start = utils::TimeStamp::now();
|
||||
trace.do_work_begin = start;
|
||||
|
||||
for (auto &h : _handles) {
|
||||
auto &handle = h.handle;
|
||||
if (handle.get_book(h.last, std::tuple_size<BookSide>(),
|
||||
&book.bid()[0], &book.ask()[0]) ==
|
||||
md::HandleReadResult::SUCCESS) {
|
||||
&book.bid()[0],
|
||||
&book.ask()[0]) == md::HandleReadResult::SUCCESS) {
|
||||
book._security_key = SecurityKey(handle.key());
|
||||
trace.md_src = utils::TimeStamp::now();
|
||||
trace.md_sn = trace.md_src.to_uint64();
|
||||
trace.md = h.last;
|
||||
|
||||
tracepoint(bts_trace, trace_order, "MDSource", 0, trace.md_sn, 0);
|
||||
trace::CorID trace{h.channel, handle.sequence()};
|
||||
_perf_log.log(trace, h.last, "md", handle.key());
|
||||
publish(_book_update, book, trace);
|
||||
}
|
||||
}
|
||||
auto stop = utils::TimeStamp::now();
|
||||
_loop_timer->observe((start-stop).nanoseconds());
|
||||
_loop_timer->observe((start - stop).nanoseconds());
|
||||
}
|
||||
|
||||
struct HandleRecord {
|
||||
utils::TimeStamp last;
|
||||
md::MDConsumerHandle handle;
|
||||
const char *channel;
|
||||
};
|
||||
|
||||
std::vector<HandleRecord> _handles;
|
||||
md::MDConsumer _md_consumer;
|
||||
mercury::Stream<BookUpdateEvent> _book_update;
|
||||
trace::PerfLog &_perf_log;
|
||||
metrics::MetricsContainer &_stats;
|
||||
std::shared_ptr<metrics::HistogramMetric<30>> _loop_timer;
|
||||
std::map<md::UserSymbol, std::string> _symbol_to_channel;
|
||||
};
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,10 +1,9 @@
|
||||
#include "MDSupplementarySource.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/md/MDConsumer.h"
|
||||
#include "lib/md/MDConsumerHandle.h"
|
||||
#include "lib/orderman/events/SettlementRequestEvent.h"
|
||||
#include "lib/orderman/events/SettlementUpdateEvent.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/md/MDConsumerHandle.h"
|
||||
#include "lib/md/MDConsumer.h"
|
||||
#include "lib/md/MDConsumer.h"
|
||||
#include "lib/os/os.h"
|
||||
|
||||
namespace bts {
|
||||
@@ -15,45 +14,36 @@ struct HandleRecord {
|
||||
md::IntPrice settlement_price;
|
||||
};
|
||||
|
||||
struct MDSupplementarySource::PImpl
|
||||
{
|
||||
PImpl(md::MDConsumer& md_consumer)
|
||||
:
|
||||
md_consumer(md_consumer),
|
||||
handle_map(),
|
||||
settlement_update(){
|
||||
}
|
||||
struct MDSupplementarySource::PImpl {
|
||||
PImpl(md::MDConsumer &md_consumer)
|
||||
: md_consumer(md_consumer), handle_map(), settlement_update() {}
|
||||
md::MDConsumer md_consumer;
|
||||
// list of sec subscriptions vs their handles
|
||||
std::map<uint32_t, HandleRecord> handle_map;
|
||||
mercury::Stream<SettlementUpdateEvent> settlement_update;
|
||||
};
|
||||
|
||||
MDSupplementarySource::MDSupplementarySource(md::MDConsumer& md_consumer)
|
||||
:
|
||||
me(new PImpl(md_consumer)){
|
||||
}
|
||||
MDSupplementarySource::MDSupplementarySource(md::MDConsumer &md_consumer)
|
||||
: me(new PImpl(md_consumer)) {}
|
||||
|
||||
MDSupplementarySource::~MDSupplementarySource(){
|
||||
}
|
||||
MDSupplementarySource::~MDSupplementarySource() {}
|
||||
|
||||
void MDSupplementarySource::init(){
|
||||
me->settlement_update=make_stream<SettlementUpdateEvent>();
|
||||
void MDSupplementarySource::init() {
|
||||
me->settlement_update = make_stream<SettlementUpdateEvent>();
|
||||
subscribe<SettlementRequestEvent>([this](auto const &update) {
|
||||
if (update.events) {
|
||||
for (auto const &ev : *update.events) {
|
||||
auto k = ev.security_key.value();
|
||||
auto it = me->handle_map.find(k);
|
||||
if(it ==me->handle_map.end()){
|
||||
if (it == me->handle_map.end()) {
|
||||
LOG(OrderMan, Info, "MDSupplementarySource::init key:{}", k);
|
||||
me->handle_map[k]={me->md_consumer.get_handle(k),md::INT_NAN};
|
||||
}
|
||||
else {
|
||||
me->handle_map[k] = {me->md_consumer.get_handle(k), md::INT_NAN};
|
||||
} else {
|
||||
// late joiners
|
||||
auto const &hr = it->second;
|
||||
if (!hr.settlement_price.is_nan()) {
|
||||
SettlementUpdateEvent ev(hr.settlement_price,SecurityKey(k));
|
||||
publish(me->settlement_update,ev);
|
||||
SettlementUpdateEvent ev(hr.settlement_price, SecurityKey(k));
|
||||
publish(me->settlement_update, ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,21 +51,22 @@ void MDSupplementarySource::init(){
|
||||
});
|
||||
}
|
||||
|
||||
void MDSupplementarySource::on_poll(){
|
||||
for(auto& kvp : me->handle_map){
|
||||
SecurityKey key(kvp.first);
|
||||
HandleRecord& hr=kvp.second;
|
||||
void MDSupplementarySource::on_poll() {
|
||||
for (auto &kvp : me->handle_map) {
|
||||
SecurityKey key(kvp.first);
|
||||
HandleRecord &hr = kvp.second;
|
||||
auto settlement_price = hr.handle.settlement_price();
|
||||
if(!settlement_price.is_nan() &&
|
||||
settlement_price!=hr.settlement_price){
|
||||
if (!settlement_price.is_nan() &&
|
||||
settlement_price != hr.settlement_price) {
|
||||
LOG(OrderMan, Info,
|
||||
"MDSupplementarySource::on_poll new settle key:{} price:{}",
|
||||
key, hr.settlement_price.price());
|
||||
hr.settlement_price=settlement_price;
|
||||
SettlementUpdateEvent ev(settlement_price,key);
|
||||
publish(me->settlement_update,ev);
|
||||
"MDSupplementarySource::on_poll new settle key:{} price:{}", key,
|
||||
hr.settlement_price.price());
|
||||
hr.settlement_price = settlement_price;
|
||||
SettlementUpdateEvent ev(settlement_price, key);
|
||||
publish(me->settlement_update, ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,134 +1,134 @@
|
||||
#include "lib/orderman/actors/SODPosition.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/config/protos/toggles.pb.h"
|
||||
#include "lib/config/protos/orders_fills_cleanup.pb.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/orders_fills_cleanup.pb.h"
|
||||
#include "lib/config/protos/toggles.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include <sys/time.h>
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include <sstream>
|
||||
#include <sys/time.h>
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
SODPosition::SODPosition()
|
||||
:
|
||||
_enabled (config::get<config::toggles::FillsSoftCleanup>().enabled()),
|
||||
_sod_time(_get_fills_sod_time()){
|
||||
LOG(persist, Info, "SODPosition enabled:{} sec:{}",
|
||||
_enabled, _sod_time.tv_sec);
|
||||
: _enabled(config::get<config::toggles::FillsSoftCleanup>().enabled()),
|
||||
_sod_time(_get_fills_sod_time()) {
|
||||
LOG(persist, Info, "SODPosition enabled:{} sec:{}", _enabled,
|
||||
_sod_time.tv_sec);
|
||||
}
|
||||
|
||||
// returns the timeval for today's hh,mm,ss
|
||||
// timeval is the microsecons from epoch)
|
||||
timeval SODPosition::_to_timeval(uint16_t hh, uint16_t mm, uint16_t ss){
|
||||
bts_assert(hh<24);
|
||||
bts_assert(mm<60);
|
||||
bts_assert(ss<60);
|
||||
timeval SODPosition::_to_timeval(uint16_t hh, uint16_t mm, uint16_t ss) {
|
||||
bts_assert(hh < 24);
|
||||
bts_assert(mm < 60);
|
||||
bts_assert(ss < 60);
|
||||
time_t secs;
|
||||
time (&secs); // seconds since epoch (utc)
|
||||
struct tm* ptm = gmtime (&secs);
|
||||
ptm->tm_hour=hh;
|
||||
ptm->tm_min=mm;
|
||||
ptm->tm_sec=ss;
|
||||
ptm->tm_isdst=-1;
|
||||
secs=mktime(ptm);
|
||||
time(&secs); // seconds since epoch (utc)
|
||||
struct tm *ptm = gmtime(&secs);
|
||||
ptm->tm_hour = hh;
|
||||
ptm->tm_min = mm;
|
||||
ptm->tm_sec = ss;
|
||||
ptm->tm_isdst = -1;
|
||||
secs = mktime(ptm);
|
||||
timeval rv;
|
||||
rv.tv_sec=secs;
|
||||
rv.tv_usec=0;
|
||||
rv.tv_sec = secs;
|
||||
rv.tv_usec = 0;
|
||||
return rv;
|
||||
}
|
||||
|
||||
timeval SODPosition::_subtract_days(const timeval& tv, uint32_t n_days){
|
||||
uint32_t secs_in_day=86400*n_days;
|
||||
timeval rv=tv;
|
||||
rv.tv_sec=(rv.tv_sec > secs_in_day) ? rv.tv_sec-secs_in_day : 0;
|
||||
timeval SODPosition::_subtract_days(const timeval &tv, uint32_t n_days) {
|
||||
uint32_t secs_in_day = 86400 * n_days;
|
||||
timeval rv = tv;
|
||||
rv.tv_sec = (rv.tv_sec > secs_in_day) ? rv.tv_sec - secs_in_day : 0;
|
||||
return rv;
|
||||
}
|
||||
|
||||
timeval SODPosition::_get_fills_sod_time(){
|
||||
const auto& sod_time = config::get<config::SODTime>();
|
||||
timeval sod=_to_timeval(sod_time.sod_hh(),sod_time.sod_mm(),0);
|
||||
timeval SODPosition::_get_fills_sod_time() {
|
||||
const auto &sod_time = config::get<config::SODTime>();
|
||||
timeval sod = _to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0);
|
||||
timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
// if the server started before the sod for today
|
||||
// we go back to yesterday
|
||||
if(sod.tv_sec >= now.tv_sec)
|
||||
sod=_subtract_days(sod);
|
||||
if (sod.tv_sec >= now.tv_sec)
|
||||
sod = _subtract_days(sod);
|
||||
return sod;
|
||||
}
|
||||
|
||||
timeval SODPosition::get_orders_start_time(){
|
||||
const auto& sod_time = config::get<config::SODTime>();
|
||||
timeval sod=_to_timeval(sod_time.sod_hh(),sod_time.sod_mm(),0);
|
||||
const auto& orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
|
||||
timeval SODPosition::get_orders_start_time() {
|
||||
const auto &sod_time = config::get<config::SODTime>();
|
||||
timeval sod = _to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0);
|
||||
const auto &orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
|
||||
timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
// if the server started before the sod for today
|
||||
// we go back to yesterday
|
||||
if(sod.tv_sec >= now.tv_sec)
|
||||
sod=_subtract_days(sod);
|
||||
sod=_subtract_days(sod,orders_cleanup.extra_days());
|
||||
if (sod.tv_sec >= now.tv_sec)
|
||||
sod = _subtract_days(sod);
|
||||
sod = _subtract_days(sod, orders_cleanup.extra_days());
|
||||
return sod;
|
||||
}
|
||||
|
||||
timeval SODPosition::get_order_cutoff_time(
|
||||
const std::map<std::string,std::string>& order_map){
|
||||
//returns true is t1 >= t2
|
||||
auto compare_ts = [&](google::protobuf::Timestamp t1, google::protobuf::Timestamp t2) -> bool
|
||||
{
|
||||
const std::map<std::string, std::string> &order_map) {
|
||||
// returns true is t1 >= t2
|
||||
auto compare_ts = [&](google::protobuf::Timestamp t1,
|
||||
google::protobuf::Timestamp t2) -> bool {
|
||||
if (t1.seconds() > t2.seconds())
|
||||
return true;
|
||||
else if (t1.seconds() < t2.seconds())
|
||||
return false;
|
||||
else
|
||||
return t1.nanos() >= t2.nanos();
|
||||
};
|
||||
};
|
||||
google::protobuf::Timestamp max_ts;
|
||||
max_ts.set_seconds(0);
|
||||
max_ts.set_nanos(0);
|
||||
|
||||
for (auto const &kvp : order_map){
|
||||
for (auto const &kvp : order_map) {
|
||||
OrderRecord trec;
|
||||
if (utils::pb::from_string<api::orderman::OrderRecord>(kvp.second, &trec)){
|
||||
const auto& ts = trec.state().created();
|
||||
if(compare_ts(ts,max_ts))
|
||||
if (utils::pb::from_string<api::orderman::OrderRecord>(kvp.second,
|
||||
&trec)) {
|
||||
const auto &ts = trec.state().created();
|
||||
if (compare_ts(ts, max_ts))
|
||||
(&max_ts)->CopyFrom(ts);
|
||||
}
|
||||
else
|
||||
} else
|
||||
LOG(OrderMan, Error,
|
||||
"Could not recover order {} from storage. "
|
||||
"This should be investigated",
|
||||
kvp.first);
|
||||
}
|
||||
timeval rv;
|
||||
rv.tv_sec=max_ts.seconds();
|
||||
rv.tv_usec=static_cast<double>(max_ts.nanos())/1000;
|
||||
const auto& orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
|
||||
return _subtract_days(rv,orders_cleanup.extra_days());
|
||||
rv.tv_sec = max_ts.seconds();
|
||||
rv.tv_usec = static_cast<double>(max_ts.nanos()) / 1000;
|
||||
const auto &orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
|
||||
return _subtract_days(rv, orders_cleanup.extra_days());
|
||||
}
|
||||
|
||||
std::string SODPosition::_sod_fill_id(const Fill& fill,api::Side side){
|
||||
//sod:acct:long/short:sec
|
||||
std::string SODPosition::_sod_fill_id(const Fill &fill, api::Side side) {
|
||||
// sod:acct:long/short:sec
|
||||
std::ostringstream oss;
|
||||
oss << "sod:" << fill.account() << (side==api::Side::SIDE_BUY ? ":L:" : ":S:")
|
||||
<< fill.security_key();
|
||||
return oss.str();
|
||||
oss << "sod:" << fill.account()
|
||||
<< (side == api::Side::SIDE_BUY ? ":L:" : ":S:") << fill.security_key();
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void SODPosition::_merge_pos(Fill& pos, const Fill& fill){
|
||||
bts_assert(pos.security_key()==fill.security_key());
|
||||
bts_assert(pos.account()==fill.account());
|
||||
bts_assert(pos.side()==fill.side());
|
||||
double total_qty=pos.last_shares()+fill.last_shares();
|
||||
double avg_price=(pos.avg_price()*pos.last_shares()+
|
||||
fill.last_price()*fill.last_shares())/total_qty;
|
||||
void SODPosition::_merge_pos(Fill &pos, const Fill &fill) {
|
||||
bts_assert(pos.security_key() == fill.security_key());
|
||||
bts_assert(pos.account() == fill.account());
|
||||
bts_assert(pos.side() == fill.side());
|
||||
double total_qty = pos.last_shares() + fill.last_shares();
|
||||
double avg_price = (pos.avg_price() * pos.last_shares() +
|
||||
fill.last_price() * fill.last_shares()) /
|
||||
total_qty;
|
||||
pos.set_avg_price(avg_price);
|
||||
pos.set_last_price(avg_price);
|
||||
pos.set_last_shares(total_qty);
|
||||
}
|
||||
|
||||
void SODPosition::_zero_pos(Fill& pos, const Fill& fill, api::Side side){
|
||||
void SODPosition::_zero_pos(Fill &pos, const Fill &fill, api::Side side) {
|
||||
pos.set_security_key(fill.security_key());
|
||||
pos.set_account(fill.account());
|
||||
pos.set_side(side);
|
||||
@@ -136,60 +136,61 @@ void SODPosition::_zero_pos(Fill& pos, const Fill& fill, api::Side side){
|
||||
pos.mutable_timestamp()->set_nanos(0);
|
||||
pos.set_fill_kind(Fill::FILL_KIND_SOD);
|
||||
pos.set_order_key(0);
|
||||
pos.set_fill_key(_sod_fill_id(fill,side));
|
||||
pos.set_fill_key(_sod_fill_id(fill, side));
|
||||
pos.set_last_shares(0);
|
||||
}
|
||||
|
||||
bool SODPosition::add_fill_to_sod_pos(const Fill& fill){
|
||||
if (_enabled && fill.timestamp().seconds() < _sod_time.tv_sec){
|
||||
uint32_t seckey=fill.security_key();
|
||||
const std::string& acct=fill.account();
|
||||
auto acct_it=_sod_map.find(acct);
|
||||
if(acct_it==_sod_map.end())
|
||||
_sod_map[acct]=SecPosMap();
|
||||
acct_it=_sod_map.find(fill.account());
|
||||
bts_assert(acct_it!=_sod_map.end());
|
||||
SecPosMap& sec_map=acct_it->second;
|
||||
auto sec_it=sec_map.find(seckey);
|
||||
if(sec_it==sec_map.end()){
|
||||
bool SODPosition::add_fill_to_sod_pos(const Fill &fill) {
|
||||
if (_enabled && fill.timestamp().seconds() < _sod_time.tv_sec) {
|
||||
uint32_t seckey = fill.security_key();
|
||||
const std::string &acct = fill.account();
|
||||
auto acct_it = _sod_map.find(acct);
|
||||
if (acct_it == _sod_map.end())
|
||||
_sod_map[acct] = SecPosMap();
|
||||
acct_it = _sod_map.find(fill.account());
|
||||
bts_assert(acct_it != _sod_map.end());
|
||||
SecPosMap &sec_map = acct_it->second;
|
||||
auto sec_it = sec_map.find(seckey);
|
||||
if (sec_it == sec_map.end()) {
|
||||
Fill long_pos, short_pos;
|
||||
_zero_pos(long_pos,fill,api::Side::SIDE_BUY);
|
||||
_zero_pos(short_pos,fill,api::Side::SIDE_SELL);
|
||||
sec_map[seckey]=std::pair<Fill,Fill>(long_pos,short_pos);
|
||||
_zero_pos(long_pos, fill, api::Side::SIDE_BUY);
|
||||
_zero_pos(short_pos, fill, api::Side::SIDE_SELL);
|
||||
sec_map[seckey] = std::pair<Fill, Fill>(long_pos, short_pos);
|
||||
}
|
||||
sec_it=sec_map.find(seckey);
|
||||
bts_assert(sec_it!=sec_map.end());
|
||||
std::pair<Fill,Fill>& pos=sec_it->second;
|
||||
if(fill.side()==api::Side::SIDE_BUY)
|
||||
_merge_pos(pos.first,fill);
|
||||
sec_it = sec_map.find(seckey);
|
||||
bts_assert(sec_it != sec_map.end());
|
||||
std::pair<Fill, Fill> &pos = sec_it->second;
|
||||
if (fill.side() == api::Side::SIDE_BUY)
|
||||
_merge_pos(pos.first, fill);
|
||||
else
|
||||
_merge_pos(pos.second,fill);
|
||||
_merge_pos(pos.second, fill);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
void SODPosition::add_sod_pos_as_fill_events(std::vector<StoredFillEvent>& fe,
|
||||
std::map<SecurityKey, const api::Security*> sec_map){
|
||||
if(_enabled)
|
||||
for (auto& acct_p : _sod_map){
|
||||
for(auto& sec_p : acct_p.second){
|
||||
Fill& long_pos=sec_p.second.first;
|
||||
Fill& short_pos=sec_p.second.second;
|
||||
bts_assert(long_pos.security_key()==short_pos.security_key());
|
||||
auto sec_it=sec_map.find(SecurityKey(long_pos.security_key()));
|
||||
if(sec_it==sec_map.end())
|
||||
void SODPosition::add_sod_pos_as_fill_events(
|
||||
std::vector<StoredFillEvent> &fe,
|
||||
std::map<SecurityKey, const api::Security *> sec_map) {
|
||||
if (_enabled)
|
||||
for (auto &acct_p : _sod_map) {
|
||||
for (auto &sec_p : acct_p.second) {
|
||||
Fill &long_pos = sec_p.second.first;
|
||||
Fill &short_pos = sec_p.second.second;
|
||||
bts_assert(long_pos.security_key() == short_pos.security_key());
|
||||
auto sec_it = sec_map.find(SecurityKey(long_pos.security_key()));
|
||||
if (sec_it == sec_map.end())
|
||||
LOG(persist, Error,
|
||||
"Could not recover fill for key:{} from storage. "
|
||||
"This should be investigated",
|
||||
long_pos.security_key());
|
||||
else{
|
||||
fe.emplace_back(long_pos,*sec_it->second);
|
||||
fe.emplace_back(short_pos,*sec_it->second);
|
||||
"Could not recover fill for key:{} from storage. "
|
||||
"This should be investigated",
|
||||
long_pos.security_key());
|
||||
else {
|
||||
fe.emplace_back(long_pos, *sec_it->second);
|
||||
fe.emplace_back(short_pos, *sec_it->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "SecuritySrc.h"
|
||||
#include "lib/md/MDConsumer.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/mercury/events/TimerEvent.h"
|
||||
#include "lib/orderman/events/NewSecuritiesEvent.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
struct SecuritySrc::PImpl {
|
||||
PImpl(md::MDConsumer& md) : last_scan(Timer::now()),md_consumer(md) {}
|
||||
PImpl(md::MDConsumer &md) : last_scan(Timer::now()), md_consumer(md) {}
|
||||
TimeStamp last_scan;
|
||||
mercury::Stream<NewSecuritiesEvent> new_securities_stream;
|
||||
std::vector<const api::Security *> to_publish;
|
||||
@@ -18,18 +18,19 @@ struct SecuritySrc::PImpl {
|
||||
std::vector<std::unique_ptr<api::Security>> securities_keep_alive;
|
||||
};
|
||||
|
||||
SecuritySrc::SecuritySrc(md::MDConsumer& md) : me(new PImpl(md)) {}
|
||||
SecuritySrc::SecuritySrc(md::MDConsumer &md) : me(new PImpl(md)) {}
|
||||
SecuritySrc::~SecuritySrc() {}
|
||||
|
||||
void SecuritySrc::init() {
|
||||
me->new_securities_stream = make_stream<NewSecuritiesEvent>();
|
||||
me->md_consumer.on_security([this](api::Security const &s, md::MDConsumerHandle) {
|
||||
auto sec = std::make_unique<api::Security> (s);
|
||||
auto sec_ptr = sec.get();
|
||||
me->securities_keep_alive.push_back(std::move(sec));
|
||||
me->to_publish.push_back(sec_ptr);
|
||||
});
|
||||
|
||||
me->md_consumer.on_security(
|
||||
[this](api::Security const &s, md::MDConsumerHandle) {
|
||||
auto sec = std::make_unique<api::Security>(s);
|
||||
auto sec_ptr = sec.get();
|
||||
me->securities_keep_alive.push_back(std::move(sec));
|
||||
me->to_publish.push_back(sec_ptr);
|
||||
});
|
||||
|
||||
subscribe<mercury::TimerEvent>([this](auto) { this->on_scan_securities(); });
|
||||
}
|
||||
|
||||
@@ -47,5 +48,5 @@ void SecuritySrc::on_scan_securities() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "lib/orderman/order_router/OrderRouter.h"
|
||||
#include "lib/orderman/securities/MapSecurityDB.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
@@ -26,7 +27,9 @@ namespace orderman {
|
||||
|
||||
struct ServiceBridge::PImpl {
|
||||
PImpl(OnReady const &on_ready, OnOrders const &on_orders,
|
||||
OnFills const &on_fills, DynamicOrderRegistry const &dyn_order_registry, mercury::BrokerProxy &service_call_marshaller)
|
||||
OnFills const &on_fills,
|
||||
DynamicOrderRegistry const &dyn_order_registry,
|
||||
mercury::BrokerProxy &service_call_marshaller)
|
||||
: on_ready(on_ready), on_orders(on_orders), on_fills(on_fills),
|
||||
have_orders(false), have_securities(false), order_manager_ready(false),
|
||||
sec_db(), order_router(),
|
||||
@@ -56,8 +59,9 @@ struct ServiceBridge::PImpl {
|
||||
ServiceBridge::ServiceBridge(OnReady const &on_ready, OnOrders const &on_orders,
|
||||
OnFills const &on_fills,
|
||||
DynamicOrderRegistry const &dyn_order_registry,
|
||||
mercury::BrokerProxy &service_call_marshaller)
|
||||
: me(new PImpl(on_ready, on_orders, on_fills, dyn_order_registry, service_call_marshaller)) {}
|
||||
mercury::BrokerProxy &service_call_marshaller)
|
||||
: me(new PImpl(on_ready, on_orders, on_fills, dyn_order_registry,
|
||||
service_call_marshaller)) {}
|
||||
|
||||
ServiceBridge::~ServiceBridge() {}
|
||||
|
||||
@@ -148,7 +152,7 @@ ServiceBridge::create_order(api::orderman::Create const &c) {
|
||||
me->order_factory.create(null_order_key(), c.new_order());
|
||||
me->known_orders.insert(order.key());
|
||||
publish(me->activate_order_pub, order, Price(c.price()),
|
||||
Quantity(c.quantity()), PerfTrace());
|
||||
Quantity(c.quantity()), trace::CorID());
|
||||
r.set_order_key(order.key().value());
|
||||
r.set_error_code(api::orderman::ORDER_ERROR_OK);
|
||||
} else
|
||||
|
||||
@@ -49,7 +49,9 @@ class ServiceBridge : public mercury::Actor {
|
||||
std::function<void(std::vector<api::orderman::Fill> const &)>;
|
||||
|
||||
ServiceBridge(OnReady const &, OnOrders const &, OnFills const &,
|
||||
DynamicOrderRegistry const &, mercury::BrokerProxy &service_call_marshaller);
|
||||
DynamicOrderRegistry const &,
|
||||
|
||||
mercury::BrokerProxy &service_call_marshaller);
|
||||
void init() override;
|
||||
~ServiceBridge();
|
||||
|
||||
|
||||
@@ -1,96 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderServices.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
class ChildOrder{
|
||||
public:
|
||||
ChildOrder ()
|
||||
: _order(nullptr),
|
||||
_order_services(nullptr),
|
||||
_active(false)
|
||||
{}
|
||||
class ChildOrder {
|
||||
public:
|
||||
ChildOrder()
|
||||
: _order(nullptr), _parent_order_services(nullptr), _active(false) {}
|
||||
|
||||
ChildOrder (Order &o, OrderServices &services)
|
||||
: _order(&o) ,
|
||||
_order_services(&services),
|
||||
_active(false)
|
||||
{}
|
||||
ChildOrder(Order &o, OrderServices &services)
|
||||
: _order(&o), _parent_order_services(&services),
|
||||
_child_order_services(nullptr), _active(false) {}
|
||||
|
||||
ChildOrder (ChildOrder &&src)
|
||||
{
|
||||
operator=(std::move(src));
|
||||
}
|
||||
ChildOrder(ChildOrder &&src) { operator=(std::move(src)); }
|
||||
|
||||
ChildOrder& operator=(ChildOrder &&src)
|
||||
{
|
||||
ChildOrder &operator=(ChildOrder &&src) {
|
||||
_order = src._order;
|
||||
_order_services = src._order_services;
|
||||
_parent_order_services = src._parent_order_services;
|
||||
_child_order_services = src._child_order_services;
|
||||
_active = src._active;
|
||||
|
||||
|
||||
src._order = nullptr;
|
||||
src._order_services = nullptr;
|
||||
src._parent_order_services = nullptr;
|
||||
src._child_order_services = nullptr;
|
||||
src._active = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
OrderKey key() const
|
||||
{
|
||||
bts_assert(_order);
|
||||
OrderKey key() const {
|
||||
bts_assert_d(_order);
|
||||
return _order->key();
|
||||
}
|
||||
|
||||
bool is_active() const {return _active;}
|
||||
|
||||
void activate(Price p, Quantity q, PerfTrace const &trace = PerfTrace())
|
||||
{
|
||||
bool is_active() const { return _active; }
|
||||
|
||||
void activate(Price p, Quantity q, trace::CorID const &trace = trace::CorID()) {
|
||||
bts_assert(_order);
|
||||
bts_assert(!_active);
|
||||
|
||||
_order_services->send_activate(*_order,p,q, trace);
|
||||
_child_order_services =
|
||||
&_parent_order_services->activate_child(*_order, p, q, trace);
|
||||
_active = true;
|
||||
}
|
||||
|
||||
void update(Price p, Quantity q)
|
||||
{
|
||||
PerfTrace trace;
|
||||
update(p,q,trace);
|
||||
}
|
||||
|
||||
void update(Price p, Quantity q, PerfTrace &trace)
|
||||
{
|
||||
void update(Price p, Quantity q, trace::CorID const &trace = trace::CorID()) {
|
||||
bts_assert(_order);
|
||||
bts_assert(_active);
|
||||
_order_services->send_update(_order->key(),p,q,trace);
|
||||
_child_order_services->update(p, q, trace);
|
||||
}
|
||||
|
||||
void crack(PerfTrace const &trace = PerfTrace())
|
||||
{
|
||||
void crack(trace::CorID const &trace = trace::CorID()) {
|
||||
bts_assert(_order);
|
||||
bts_assert(_active);
|
||||
_order_services->send_crack(_order->key(),trace);
|
||||
_child_order_services->crack(trace);
|
||||
}
|
||||
|
||||
void cancel(PerfTrace const &trace = PerfTrace())
|
||||
{
|
||||
void cancel(trace::CorID const &trace = trace::CorID()) {
|
||||
bts_assert(_order);
|
||||
bts_assert(_active);
|
||||
_order_services->send_cancel(_order->key(),trace);
|
||||
_child_order_services->cancel(trace);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
friend class OrderBuilder;
|
||||
Order* _order;
|
||||
OrderServices *_order_services;
|
||||
Order *_order;
|
||||
OrderServices *_parent_order_services;
|
||||
OrderServices *_child_order_services;
|
||||
bool _active;
|
||||
};
|
||||
|
||||
|
||||
}}
|
||||
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -4,5 +4,5 @@ namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
std::atomic<size_t> OrderBuilder::_current_builder_id(0);
|
||||
|
||||
}}
|
||||
}
|
||||
} // namespace bts
|
||||
@@ -1,93 +1,85 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/orderman/orders/PreActivationOrderServices.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderServices.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/orderman/actors/order_manager/ChildOrder.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderServices.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/orderman/orders/PreActivationOrderServices.h"
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
class OrderBuilder
|
||||
{
|
||||
public:
|
||||
class OrderBuilder {
|
||||
public:
|
||||
OrderBuilder()
|
||||
: _parent_key(null_order_key()),
|
||||
_definition(),
|
||||
_reserve_sz(0),
|
||||
_order_stash(),
|
||||
_builder_id(),
|
||||
_order_services(nullptr)
|
||||
{}
|
||||
: _parent_key(null_order_key()), _definition(), _reserve_sz(0),
|
||||
_order_stash(), _builder_id(), _order_services(nullptr) {}
|
||||
|
||||
OrderBuilder(OrderBuilder&&src) {operator=(std::move(src));}
|
||||
OrderBuilder(OrderBuilder &&src) { operator=(std::move(src)); }
|
||||
|
||||
OrderBuilder(OrderKey parent_key, api::orderman::OrderDefinition const &def,
|
||||
PreActivationOrderServices const &srv, size_t reserve_sz = 1)
|
||||
: _parent_key(parent_key), _definition(def), _reserve_sz(reserve_sz),
|
||||
_order_stash(), _builder_id(++_current_builder_id),
|
||||
_order_services(nullptr)
|
||||
|
||||
OrderBuilder(OrderKey parent_key,
|
||||
api::orderman::OrderDefinition const&def,
|
||||
PreActivationOrderServices const &srv,
|
||||
size_t reserve_sz = 1)
|
||||
: _parent_key(parent_key),
|
||||
_definition(def) ,
|
||||
_reserve_sz(reserve_sz),
|
||||
_order_stash(),
|
||||
_builder_id(++_current_builder_id),
|
||||
_order_services(nullptr)
|
||||
|
||||
{
|
||||
bts_assert(parent_key != null_order_key());
|
||||
while (_order_stash.size() != reserve_sz)
|
||||
_order_stash.push_back(&srv.order_factory.create(parent_key,_definition));
|
||||
_order_stash.push_back(
|
||||
&srv.order_factory.create(parent_key, _definition));
|
||||
}
|
||||
|
||||
void activate(OrderServices &order_services)
|
||||
{
|
||||
void activate(OrderServices &order_services) {
|
||||
bts_assert(!_order_services);
|
||||
_order_services = &order_services;
|
||||
_order_services->order_creations.sub(_builder_id,[this] (auto const&order_ev) {
|
||||
_order_stash.insert(_order_stash.end(),order_ev.orders.begin(),order_ev.orders.end());
|
||||
});
|
||||
_order_services->subscribe_to_order_creation_reply(
|
||||
_builder_id, [this](auto const &order_ev) {
|
||||
_order_stash.insert(_order_stash.end(), order_ev.orders.begin(),
|
||||
order_ev.orders.end());
|
||||
});
|
||||
}
|
||||
|
||||
ChildOrder build()
|
||||
{
|
||||
bts_assert_msg(_order_services,"OrderBuilder::activate() must be called before orders can be built");
|
||||
|
||||
ChildOrder build() {
|
||||
bts_assert_msg(
|
||||
_order_services,
|
||||
"OrderBuilder::activate() must be called before orders can be built");
|
||||
|
||||
ChildOrder result;
|
||||
if (_order_stash.size()) {
|
||||
result = ChildOrder( *_order_stash.back(),
|
||||
*_order_services);
|
||||
result = ChildOrder(*_order_stash.back(), *_order_services);
|
||||
_order_stash.pop_back();
|
||||
}
|
||||
else {
|
||||
LOG(OrderMan,Warn,
|
||||
"Ran out of pre-allocated child orders for order {}. Will allocate directly. "
|
||||
"This will have an impact on performance", _parent_key);
|
||||
result = ChildOrder (_order_services->order_factory().create(_parent_key,_definition),
|
||||
*_order_services);
|
||||
} else {
|
||||
LOG(OrderMan, Warn,
|
||||
"Ran out of pre-allocated child orders for order {}. Will "
|
||||
"allocate directly. "
|
||||
"This will have an impact on performance",
|
||||
_parent_key);
|
||||
result = ChildOrder(
|
||||
_order_services->order_factory().create(_parent_key, _definition),
|
||||
*_order_services);
|
||||
}
|
||||
if (_reserve_sz > _order_stash.size()) {
|
||||
auto need = _reserve_sz - _order_stash.size();
|
||||
_order_services->send_order_create_req(_builder_id,_parent_key,_definition,need);
|
||||
_order_services->send_order_create_req(_builder_id, _parent_key,
|
||||
_definition, need);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
OrderBuilder& operator=(OrderBuilder &&src)
|
||||
{
|
||||
|
||||
OrderBuilder &operator=(OrderBuilder &&src) {
|
||||
move_here(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
void move_here(OrderBuilder &src)
|
||||
{
|
||||
private:
|
||||
void move_here(OrderBuilder &src) {
|
||||
bts_assert(!_order_services);
|
||||
_parent_key = src._parent_key;
|
||||
_definition= src._definition;
|
||||
_definition = src._definition;
|
||||
_reserve_sz = src._reserve_sz;
|
||||
_order_stash = src._order_stash;
|
||||
_builder_id = src._builder_id;
|
||||
@@ -96,15 +88,16 @@ private:
|
||||
src._order_services = nullptr;
|
||||
bts_assert(_parent_key != null_order_key());
|
||||
}
|
||||
|
||||
|
||||
OrderKey _parent_key;
|
||||
api::orderman::OrderDefinition _definition;
|
||||
size_t _reserve_sz;
|
||||
std::vector<Order*> _order_stash;
|
||||
size_t _builder_id ;
|
||||
std::vector<Order *> _order_stash;
|
||||
size_t _builder_id;
|
||||
OrderServices *_order_services;
|
||||
|
||||
|
||||
static std::atomic<size_t> _current_builder_id;
|
||||
};
|
||||
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -14,8 +14,8 @@
|
||||
#include "lib/utils/inplace_function.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <unordered_map>
|
||||
#include <boost/algorithm/string/trim.hpp>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -30,37 +30,20 @@ enum class OrderManager::State {
|
||||
READY
|
||||
};
|
||||
|
||||
struct OrderManager::OrderInfo {
|
||||
OrderInfo(Order &o, OrderServicesData &data)
|
||||
: order(&o), order_services(std::make_unique<OrderServices>(o, data)) {}
|
||||
|
||||
OrderInfo() = default;
|
||||
OrderInfo(OrderInfo &&src) = default;
|
||||
OrderInfo &operator=(OrderInfo &&src) = default;
|
||||
|
||||
Order *order;
|
||||
std::unique_ptr<OrderServices> order_services;
|
||||
};
|
||||
|
||||
struct OrderManager::PImpl {
|
||||
PImpl(OrderManager &owner, DynamicOrderRegistry const &b)
|
||||
: state(State::WAIT_ADMIN), services_data(owner, b) {
|
||||
pending_order_events.reserve(1000);
|
||||
}
|
||||
PImpl(OrderManager &owner, DynamicOrderRegistry const &b, trace::PerfLog &l)
|
||||
: state(State::WAIT_ADMIN), services_data(owner, b, l) {}
|
||||
|
||||
State state;
|
||||
std::unordered_map<OrderKey, OrderInfo> orders;
|
||||
|
||||
std::vector<PendingOrderEvent> pending_order_events;
|
||||
|
||||
std::unordered_map<OrderKey, Order *> orders;
|
||||
mercury::Actor::SubHandle stored_order_sub;
|
||||
mercury::Actor::SubHandle stored_fills_sub;
|
||||
OrderServicesData services_data;
|
||||
mercury::Stream<OrderManagerReadyEvent> pub_ready;
|
||||
};
|
||||
|
||||
OrderManager::OrderManager(DynamicOrderRegistry const &b)
|
||||
: me(new PImpl(*this, b)) {}
|
||||
OrderManager::OrderManager(DynamicOrderRegistry const &b, trace::PerfLog &l)
|
||||
: me(new PImpl(*this, b, l)) {}
|
||||
OrderManager::~OrderManager() {}
|
||||
|
||||
void OrderManager::init() {
|
||||
@@ -90,6 +73,11 @@ void OrderManager::on_admin_data(const mercury::Update<NewAdminDataEvent> &m) {
|
||||
|
||||
void OrderManager::on_securities(
|
||||
const mercury::Update<NewSecuritiesEvent> &update) {
|
||||
|
||||
// forward to execution gateways
|
||||
for (auto &osd : me->services_data.sessions)
|
||||
osd->gw.on_securities(update);
|
||||
|
||||
if (update.events) {
|
||||
|
||||
// update our security db and accumulate for the sake of the limiter
|
||||
@@ -125,8 +113,10 @@ void OrderManager::on_fill_recovery(
|
||||
me->state = State::WAIT_ORDERS;
|
||||
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto trimmed_account_name = boost::algorithm::trim_copy(ev.fill.account());
|
||||
me->services_data.limiter.add(*ev.security, trimmed_account_name,ev.fill.side(),
|
||||
auto trimmed_account_name =
|
||||
boost::algorithm::trim_copy(ev.fill.account());
|
||||
me->services_data.limiter.add(*ev.security, trimmed_account_name,
|
||||
ev.fill.side(),
|
||||
Quantity(ev.fill.last_shares()));
|
||||
}
|
||||
|
||||
@@ -158,13 +148,14 @@ void OrderManager::on_order_recovery(
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto &order =
|
||||
me->services_data.order_factory.recover(ev.order_record);
|
||||
order.init_order_services(me->services_data);
|
||||
|
||||
auto r = me->orders.emplace(order.key(), &order);
|
||||
bts_assert(r.second);
|
||||
auto const &ostate = ev.order_record.state();
|
||||
OrderInfo oi{order, me->services_data};
|
||||
auto serv_ptr = oi.order_services.get();
|
||||
me->orders[order.key()] = std::move(oi);
|
||||
order.on_activate(Price(ostate.price()),
|
||||
Quantity(ostate.quantity()), *serv_ptr,
|
||||
PerfTrace());
|
||||
Quantity(ostate.quantity()), *order._services,
|
||||
trace::CorID());
|
||||
}
|
||||
|
||||
// signal that we are ready
|
||||
@@ -194,91 +185,73 @@ void OrderManager::on_execution_report(
|
||||
auto order =
|
||||
&me->services_data.order_factory.recreate_from_exe_report(
|
||||
*report.data);
|
||||
OrderInfo oi{*order, me->services_data};
|
||||
auto serv_ptr = oi.order_services.get();
|
||||
me->orders[order->key()] = std::move(oi);
|
||||
order->init_order_services(me->services_data);
|
||||
auto r = me->orders.emplace(order->key(), order);
|
||||
bts_assert(r.second);
|
||||
order->on_activate(report.data->price, report.data->quantity,
|
||||
*serv_ptr, PerfTrace());
|
||||
*order->_services, trace::CorID());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::queue_order_event(PendingOrderEvent const &event_handler) {
|
||||
me->pending_order_events.push_back(event_handler);
|
||||
}
|
||||
|
||||
void OrderManager::on_activate_order(
|
||||
const mercury::Update<ActivateOrderEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events)
|
||||
queue_order_event(ev);
|
||||
}
|
||||
|
||||
void OrderManager::handle_order_event(ActivateOrderEvent const &ev) {
|
||||
auto order = ev.order;
|
||||
auto k = order->key();
|
||||
bts_assert(me->orders.find(k) == me->orders.end());
|
||||
OrderInfo oi{*order, me->services_data};
|
||||
auto serv_ptr = oi.order_services.get();
|
||||
me->orders[k] = std::move(oi);
|
||||
order->on_activate(ev.price, ev.quantity, *serv_ptr, ev.trace);
|
||||
for (auto const &ev : *m.events) {
|
||||
auto order = ev.order;
|
||||
order->init_order_services(me->services_data);
|
||||
auto r = me->orders.emplace(order->key(), order);
|
||||
bts_assert(r.second);
|
||||
order->on_activate(ev.price, ev.quantity, *order->_services, ev.trace);
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_update_order(const mercury::Update<UpdateOrderEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events)
|
||||
queue_order_event(ev);
|
||||
}
|
||||
|
||||
void OrderManager::handle_order_event(UpdateOrderEvent const &ev) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second.order->on_update(ev.price, ev.quantity, ev.trace);
|
||||
else {
|
||||
LOG(OrderMan, Error,
|
||||
"Received an update request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second->on_update(ev.price, ev.quantity, ev.trace);
|
||||
else {
|
||||
LOG(OrderMan, Error,
|
||||
"Received an update request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_cancel_order(const mercury::Update<CancelOrderEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events)
|
||||
queue_order_event(ev);
|
||||
}
|
||||
|
||||
void OrderManager::handle_order_event(CancelOrderEvent const &ev) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second.order->on_cancel(ev.trace);
|
||||
else {
|
||||
LOG(OrderMan, Error,
|
||||
"Received a cancel request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second->on_cancel(ev.trace);
|
||||
else {
|
||||
LOG(OrderMan, Error,
|
||||
"Received a cancel request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_crack_order(const mercury::Update<CrackOrderEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events)
|
||||
queue_order_event(ev);
|
||||
}
|
||||
|
||||
void OrderManager::handle_order_event(CrackOrderEvent const &ev) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second.order->on_crack(ev.trace);
|
||||
else {
|
||||
LOG(OrderMan, Error,
|
||||
"Received a crack request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second->on_crack(ev.trace);
|
||||
else {
|
||||
LOG(OrderMan, Error,
|
||||
"Received a crack request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_gateway_state(
|
||||
@@ -290,6 +263,16 @@ void OrderManager::on_gateway_state(
|
||||
}
|
||||
}
|
||||
|
||||
OrderServices &OrderManager::activate_child(OrderServices &parent, Order &order,
|
||||
Price p, Quantity q,
|
||||
trace::CorID const &trace) {
|
||||
order.init_order_services(me->services_data, parent);
|
||||
auto r = me->orders.emplace(order.key(), &order);
|
||||
bts_assert(r.second);
|
||||
order.on_activate(p, q, *order._services, trace);
|
||||
return *order._services;
|
||||
}
|
||||
|
||||
void OrderManager::on_new_fix_buffer(
|
||||
const mercury::Update<CreateFIXBufferReplyEvent> &m) {
|
||||
if (m.events)
|
||||
@@ -370,8 +353,24 @@ void OrderManager::init_order_services() {
|
||||
s->gateway_status_subs = std::make_unique<
|
||||
ContentSubscription<GatewayStatusChangeEvent, NoKey>>(*this, gw_name);
|
||||
|
||||
// FIX output
|
||||
s->out_fix_pub = make_stream<OutFIXMsgEvent>(gw_name);
|
||||
// publishers for execution reports, gateway status and cancel
|
||||
// reject evets
|
||||
auto s_ptr = s.get();
|
||||
s->exe_report_pub = make_stream<NewExecutionReportEvent>(gw_name);
|
||||
s->gw.on_exe_report() = [s_ptr,this] (auto const &er, auto const &t) {
|
||||
publish(s_ptr->exe_report_pub, &er,t);
|
||||
};
|
||||
s->gw_status_change_pub = make_stream<GatewayStatusChangeEvent>(gw_name);
|
||||
s->gw.on_gw_status_change() = [s_ptr,this,gw_name] (bool b) {
|
||||
publish(s_ptr->gw_status_change_pub, gw_name,b);
|
||||
};
|
||||
s->cancel_reject_pub = make_stream<CancelRejectEvent>(gw_name);
|
||||
s->gw.on_cancel_reject() = [s_ptr,this] (auto const &k,auto const&msg) {
|
||||
publish(s_ptr->cancel_reject_pub, k,msg);
|
||||
};
|
||||
// handlers are ready so we can start processing fix events
|
||||
s->gw.start();
|
||||
|
||||
reload_fix_buffer(*s);
|
||||
}
|
||||
|
||||
@@ -394,11 +393,6 @@ void OrderManager::init_order_services() {
|
||||
[this](auto const &ev) { this->on_new_fix_buffer(ev); });
|
||||
|
||||
// Outputs
|
||||
me->services_data.activate_order_pub = make_stream<ActivateOrderEvent>();
|
||||
me->services_data.update_order_pub = make_stream<UpdateOrderEvent>();
|
||||
me->services_data.cancel_order_pub = make_stream<CancelOrderEvent>();
|
||||
me->services_data.crack_order_pub = make_stream<CrackOrderEvent>();
|
||||
|
||||
me->services_data.create_order_req_pub =
|
||||
make_stream<CreateOrderRequestEvent>();
|
||||
me->services_data.create_fix_req_pub =
|
||||
@@ -407,12 +401,9 @@ void OrderManager::init_order_services() {
|
||||
me->services_data.settlement_request_pub =
|
||||
make_stream<SettlementRequestEvent>();
|
||||
me->services_data.report_order_state_pub =
|
||||
make_stream<OrderStateUpdateEvent>();
|
||||
make_stream<OrderStateUpdateEvent>();
|
||||
me->pub_ready = make_stream<OrderManagerReadyEvent>();
|
||||
|
||||
me->services_data.child_order_subs =
|
||||
std::make_unique<ContentSubscription<OrderStateUpdateEvent, ParentKey>>(
|
||||
*this);
|
||||
// md subs are peculiar in that we try to make them on demand, so
|
||||
// we publish an event announcing the subscription
|
||||
std::function<void(SecurityKey)> on_md_sub = [this](SecurityKey k) {
|
||||
@@ -432,35 +423,36 @@ void OrderManager::init_order_services() {
|
||||
std::make_unique<ContentSubscription<CreateOrderReplyEvent>>(*this);
|
||||
}
|
||||
|
||||
void OrderManager::on_poll() {
|
||||
void OrderManager::after_event() {
|
||||
if (me->state == State::READY) {
|
||||
for (size_t i = 0; i < me->pending_order_events.size(); i++)
|
||||
me->pending_order_events[i](*this);
|
||||
me->pending_order_events.clear();
|
||||
for (auto &osd : me->services_data.sessions) {
|
||||
auto &fix_stream = osd->current_buffer->first_stream();
|
||||
if (fix_stream.n_msgs()) {
|
||||
fix_stream.close();
|
||||
publish(osd->out_fix_pub, osd->current_buffer, osd->current_traces);
|
||||
osd->current_traces.clear();
|
||||
osd->gw.send_fix(osd->current_buffer, osd->current_trace);
|
||||
reload_fix_buffer(*osd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::reload_fix_buffer(SessionServicesData &dt) {
|
||||
if (dt.free_buffers.size()) {
|
||||
dt.current_buffer = dt.free_buffers.back();
|
||||
dt.free_buffers.pop_back();
|
||||
void OrderManager::reload_fix_buffer(SessionServicesData &osd) {
|
||||
if (osd.free_buffers.size()) {
|
||||
osd.current_buffer = osd.free_buffers.back();
|
||||
osd.free_buffers.pop_back();
|
||||
} else {
|
||||
LOG(OrderMan, Warn,
|
||||
"Ran out of fix buffers in {}. Will allocate directly. "
|
||||
"This will have an impact on performance",
|
||||
dt.cfg.name());
|
||||
dt.current_buffer = std::make_shared<jetfix::OutFIX>(dt.cfg);
|
||||
osd.cfg.name());
|
||||
osd.current_buffer = std::make_shared<jetfix::OutFIX>(osd.cfg);
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_poll() {
|
||||
for (auto &osd : me->services_data.sessions)
|
||||
osd->gw.dispatch_incoming_fix_messages();
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -4,16 +4,17 @@
|
||||
#include "lib/jetfix/common/OutFIXPtr.h"
|
||||
#include "lib/mercury/actors/Actor.h"
|
||||
#include "lib/mercury/actors/IPollableActor.h"
|
||||
#include "lib/utils/inplace_function.h"
|
||||
#include "lib/orderman/events/ActivateOrderEvent.h"
|
||||
#include "lib/orderman/events/UpdateOrderEvent.h"
|
||||
#include "lib/orderman/events/CancelOrderEvent.h"
|
||||
#include "lib/orderman/events/CrackOrderEvent.h"
|
||||
#include "lib/orderman/events/UpdateOrderEvent.h"
|
||||
#include "lib/utils/inplace_function.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
// fwd
|
||||
namespace bts {
|
||||
namespace trace {class PerfLog;}
|
||||
namespace mercury {
|
||||
class TimerEvent;
|
||||
}
|
||||
@@ -26,13 +27,14 @@ class CancelOrderEvent;
|
||||
class CrackOrderEvent;
|
||||
class StoredOrderEvent;
|
||||
class StoredFillEvent;
|
||||
class OrderServices;
|
||||
class CreateFIXBufferReplyEvent;
|
||||
class GatewayStatusChangeEvent;
|
||||
class NewAdminDataEvent;
|
||||
struct SessionServicesData;
|
||||
class DynamicOrderRegistry;
|
||||
}
|
||||
}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -49,8 +51,6 @@ namespace orderman {
|
||||
// - security creation events
|
||||
// - timer updates
|
||||
// - order state changes
|
||||
// - order update/cancel/crack events
|
||||
//
|
||||
//
|
||||
// Additionally handles order maintenance events:
|
||||
// - create
|
||||
@@ -58,16 +58,16 @@ namespace orderman {
|
||||
// - crack
|
||||
// - update
|
||||
|
||||
class OrderManager : public mercury::Actor,
|
||||
public mercury::IPollableActor
|
||||
{
|
||||
class OrderManager : public mercury::Actor, public mercury::IPollableActor {
|
||||
public:
|
||||
OrderManager(DynamicOrderRegistry const&dyn_order_builder);
|
||||
OrderManager(DynamicOrderRegistry const &dyn_order_builder, trace::PerfLog &);
|
||||
~OrderManager();
|
||||
void init() override;
|
||||
|
||||
private:
|
||||
friend class OrderServices;
|
||||
|
||||
// external events
|
||||
void on_securities(const mercury::Update<NewSecuritiesEvent> &);
|
||||
void on_fill_recovery(const mercury::Update<StoredFillEvent> &);
|
||||
void on_order_recovery(const mercury::Update<StoredOrderEvent> &);
|
||||
@@ -80,34 +80,22 @@ class OrderManager : public mercury::Actor,
|
||||
void on_gateway_state(const mercury::Update<GatewayStatusChangeEvent> &);
|
||||
void on_timer(const mercury::Update<mercury::TimerEvent> &);
|
||||
void on_admin_data(const mercury::Update<NewAdminDataEvent> &);
|
||||
void on_poll() override;
|
||||
// end external events
|
||||
|
||||
void init_order_services();
|
||||
|
||||
void on_poll() override;
|
||||
|
||||
using PendingOrderEvent = stdext::inplace_function<
|
||||
void(OrderManager &),
|
||||
std::max({128ul, sizeof(ActivateOrderEvent), sizeof(UpdateOrderEvent),
|
||||
sizeof(CancelOrderEvent), sizeof(CrackOrderEvent)})>;
|
||||
|
||||
template <class EV>
|
||||
void queue_order_event(EV const&ev) {
|
||||
queue_order_event(PendingOrderEvent( [ev] (auto &om) {
|
||||
om.handle_order_event(ev);
|
||||
}));
|
||||
}
|
||||
void queue_order_event(PendingOrderEvent const&ev);
|
||||
void handle_order_event(ActivateOrderEvent const &);
|
||||
void handle_order_event(UpdateOrderEvent const &);
|
||||
void handle_order_event(CancelOrderEvent const &);
|
||||
void handle_order_event(CrackOrderEvent const&);
|
||||
void reload_fix_buffer(SessionServicesData &dt);
|
||||
|
||||
std::vector<jetfix::OutFIXPtr> &free_buffers(std::string const&);
|
||||
|
||||
void after_event() override;
|
||||
OrderServices &activate_child(OrderServices &parent, Order &, Price,
|
||||
Quantity, trace::CorID const &);
|
||||
|
||||
std::vector<jetfix::OutFIXPtr> &free_buffers(std::string const &);
|
||||
enum class State;
|
||||
struct OrderInfo;
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -8,143 +8,353 @@
|
||||
#include "lib/orderman/governor/Governor.h"
|
||||
#include "lib/orderman/orderman_utils/EventStream.h"
|
||||
#include "lib/orderman/orders/Order.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
class OrderServices;
|
||||
class SessionServices {
|
||||
friend class OrderServices;
|
||||
EventStream<NewExecutionReportEvent> _exe_reports;
|
||||
EventStream<CancelRejectEvent> _cancel_rejects;
|
||||
EventStream<GatewayStatusChangeEvent, mercury::NoKey> _gateway_status;
|
||||
SessionServicesData *_session_data;
|
||||
EventStream<GatewayStatusChangeEvent, mercury::NoKey>::SubID
|
||||
_gateway_status_sub;
|
||||
|
||||
public:
|
||||
SessionServices(SessionServicesData &session_data)
|
||||
: exe_reports(*session_data.exe_report_subs),
|
||||
cancel_rejects(*session_data.cancel_reject_subs),
|
||||
gateway_status(*session_data.gateway_status_subs),
|
||||
_session_data(&session_data) {}
|
||||
SessionServices(SessionServices &&ss) { operator=(std::move(ss)); }
|
||||
|
||||
SessionServices(SessionServices const &ss) { operator=(ss); }
|
||||
|
||||
SessionServices(SessionServicesData &session_data)
|
||||
: _exe_reports(*session_data.exe_report_subs),
|
||||
_cancel_rejects(*session_data.cancel_reject_subs),
|
||||
_gateway_status(*session_data.gateway_status_subs),
|
||||
_session_data(&session_data), _gateway_status_sub() {}
|
||||
|
||||
SessionServices &operator=(SessionServices &&src) {
|
||||
_exe_reports = std::move(src._exe_reports);
|
||||
_cancel_rejects = std::move(src._cancel_rejects);
|
||||
_gateway_status = std::move(src._gateway_status);
|
||||
_session_data = src._session_data;
|
||||
src._session_data = nullptr;
|
||||
_gateway_status_sub = src._gateway_status_sub;
|
||||
return *this;
|
||||
}
|
||||
|
||||
SessionServices &operator=(SessionServices const &src) {
|
||||
_exe_reports = src._exe_reports;
|
||||
_cancel_rejects = src._cancel_rejects;
|
||||
_gateway_status = src._gateway_status;
|
||||
_session_data = src._session_data;
|
||||
_gateway_status_sub = src._gateway_status_sub;
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::string const &session_name() const { return _session_data->session; }
|
||||
|
||||
config::JetFIX::Connection const &session_cfg() const {
|
||||
return _session_data->cfg;
|
||||
}
|
||||
|
||||
void queue_fix(jetfix::MessageTypes mt, jetfix::FIXFragment const&f,PerfTrace const&trace) {
|
||||
void queue_fix(jetfix::MessageTypes mt, jetfix::FIXFragment const &f,
|
||||
trace::CorID const &trace) {
|
||||
auto &stream = _session_data->current_buffer->first_stream();
|
||||
stream.new_msg(mt).append(f);
|
||||
_session_data->current_traces.push_back(trace);
|
||||
|
||||
if (stream.n_msgs() == 1)
|
||||
_session_data->current_trace = trace;
|
||||
}
|
||||
|
||||
|
||||
bool is_gateway_connected() const { return _session_data->gw_connected; }
|
||||
Governor &governor() { return _session_data->governor; }
|
||||
|
||||
EventStream<NewExecutionReportEvent> exe_reports;
|
||||
EventStream<CancelRejectEvent> cancel_rejects;
|
||||
EventStream<GatewayStatusChangeEvent, mercury::NoKey> gateway_status;
|
||||
// Subscriptions
|
||||
using ExeReportSubID = EventStream<NewExecutionReportEvent>::SubID;
|
||||
using CancelRejectSubID = EventStream<CancelRejectEvent>::SubID;
|
||||
|
||||
private:
|
||||
SessionServicesData *_session_data;
|
||||
template <class CB>
|
||||
ExeReportSubID subscribe_to_exe_report(OrderKey k, CB &&cb) {
|
||||
return _exe_reports.sub(k, std::forward<CB>(cb));
|
||||
}
|
||||
void unsubscribe_from_exe_report(ExeReportSubID sid) {
|
||||
_exe_reports.unsub(sid);
|
||||
}
|
||||
|
||||
template <class CB>
|
||||
CancelRejectSubID subscribe_to_cancel_reject(OrderKey k, CB &&cb) {
|
||||
return _cancel_rejects.sub(k, std::forward<CB>(cb));
|
||||
}
|
||||
void unsubscribe_from_cancel_reject(CancelRejectSubID sid) {
|
||||
_cancel_rejects.unsub(sid);
|
||||
}
|
||||
|
||||
template <class CB> void subscribe_to_gateway_status(CB &&cb) {
|
||||
_gateway_status_sub = _gateway_status.sub(std::forward<CB>(cb));
|
||||
}
|
||||
void unsubscribe_from_gateway_status() {
|
||||
_gateway_status.unsub(_gateway_status_sub);
|
||||
}
|
||||
};
|
||||
|
||||
class OrderServices {
|
||||
public:
|
||||
OrderServices(Order &owner, OrderServicesData &data)
|
||||
: book_updates(*data.md_subs), settlement_updates(*data.settlement_subs),
|
||||
child_updates(*data.child_order_subs), time_change(*data.time_subs),
|
||||
order_creations(*data.order_creation_subs), _owner(owner), _data(data),
|
||||
_sessions() {
|
||||
for (auto &sd : data.sessions)
|
||||
_sessions.push_back(std::make_unique<SessionServices>(*sd));
|
||||
OrderServices(Order &owner)
|
||||
: _busy(false), _pending_calls(), _owner(&owner), _data(),
|
||||
_parent(nullptr), _child_update_cb([](auto const &) {}),
|
||||
_book_updates(), _settlement_updates(), _time_change(), _sessions(),
|
||||
_create_order_reply() {}
|
||||
|
||||
void init(OrderServicesData &data, OrderServices &parent) {
|
||||
_parent = &parent;
|
||||
init(data);
|
||||
}
|
||||
|
||||
EventStream<BookUpdateEvent> book_updates;
|
||||
EventStream<SettlementUpdateEvent> settlement_updates;
|
||||
EventStream<OrderStateUpdateEvent, ParentKey> child_updates;
|
||||
EventStream<mercury::TimerEvent, mercury::NoKey> time_change;
|
||||
void init(OrderServicesData &data) {
|
||||
_data = &data;
|
||||
_book_updates.init(*data.md_subs);
|
||||
_settlement_updates.init(*data.settlement_subs);
|
||||
_time_change.init(*data.time_subs);
|
||||
for (auto &sd : data.sessions)
|
||||
_sessions.emplace_back(*sd);
|
||||
_create_order_reply.init(*data.order_creation_subs);
|
||||
}
|
||||
|
||||
securities::ISecurityDB &sec_db() { return _data.sec_db; }
|
||||
securities::ISecurityDB &sec_db() { return _data->sec_db; }
|
||||
|
||||
mercury::TimeStampStr const &get_time_str() const {
|
||||
return _data.last_timer_ev.ts_str;
|
||||
return _data->last_timer_ev.ts_str;
|
||||
}
|
||||
|
||||
TimeStamp const &get_time() const { return _data.last_timer_ev.ts; }
|
||||
int price_encoding_precision(SessionServices &ss) {
|
||||
return ss.session_cfg().price_encoding_precision();
|
||||
}
|
||||
|
||||
bool is_gateway_connected(SessionServices &ss) {
|
||||
return ss.is_gateway_connected();
|
||||
}
|
||||
|
||||
Governor &governor(SessionServices &ss) { return ss.governor(); }
|
||||
|
||||
void queue_fix(SessionServices &ss, jetfix::MessageTypes mt,
|
||||
jetfix::FIXFragment const &f, trace::CorID const &trace) {
|
||||
ss.queue_fix(mt, f, trace);
|
||||
}
|
||||
|
||||
void log(trace::CorID cid, const char *entry_kind,
|
||||
uint64_t uint64_1 = 0,const char *txt_1 = "",uint32_t uint32_1 = 0) {
|
||||
_data->log.log(cid, entry_kind, uint64_1, txt_1, uint32_1);
|
||||
}
|
||||
|
||||
trace::ScopedPerfLogEntry log_scope(trace::CorID cid, const char *entry_kind,
|
||||
uint64_t uint64_1 = 0,const char *txt_1 = "",uint32_t uint32_1 = 0)
|
||||
{
|
||||
return trace::ScopedPerfLogEntry(_data->log,cid,entry_kind,uint64_1,txt_1,uint32_1);
|
||||
}
|
||||
|
||||
void report_state(const api::orderman::OrderState &state,
|
||||
PerfTrace const &trace = PerfTrace()) {
|
||||
trace::CorID const &trace = trace::CorID()) {
|
||||
|
||||
OrderStateUpdateEvent ev(_owner->key(), _owner->parent_key(),
|
||||
_owner->definition(), state, trace);
|
||||
_data->owner.publish(_data->report_order_state_pub, ev);
|
||||
|
||||
_data.owner.publish(_data.report_order_state_pub, _owner.key(),
|
||||
_owner.parent_key(), _owner.definition(), state,
|
||||
trace);
|
||||
if (orderman::in_terminal_state(state.status())) {
|
||||
LOG(OrderMan, Info,
|
||||
"Order {} transitioned to a terminal state. Canceling all "
|
||||
"subscriptions",
|
||||
_owner.key().value());
|
||||
book_updates.unsub_all();
|
||||
child_updates.unsub_all();
|
||||
time_change.unsub_all();
|
||||
_owner->key().value());
|
||||
unsubscribe_from_child_updates();
|
||||
_book_updates.unsub_all();
|
||||
_settlement_updates.unsub_all();
|
||||
_time_change.unsub_all();
|
||||
_create_order_reply.unsub_all();
|
||||
}
|
||||
|
||||
if (_parent)
|
||||
_parent->on_child_update(ev);
|
||||
}
|
||||
|
||||
Limiter &limiter() { return _data.limiter; }
|
||||
Limiter &limiter() { return _data->limiter; }
|
||||
|
||||
SessionServices &get_session_services(std::string const &for_session) {
|
||||
for (auto &s : _sessions)
|
||||
if (s->session_name() == for_session)
|
||||
return *s;
|
||||
if (s.session_name() == for_session)
|
||||
return s;
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
void schedule_action(std::function<void()> const &action) {
|
||||
_data.owner.schedule_action(action);
|
||||
// Subscriptions ------------------------------
|
||||
|
||||
// Book
|
||||
using BookSubID = EventStream<BookUpdateEvent>::SubID;
|
||||
template <class CB> BookSubID subscribe_to_book(SecurityKey k, CB &&cb) {
|
||||
return _book_updates.sub(
|
||||
k, [this, cb](auto const &ev) { this->on_external_event(ev, cb); });
|
||||
}
|
||||
void unsubscribe_from_book(BookSubID sid) { _book_updates.unsub(sid); }
|
||||
|
||||
// Settlement
|
||||
using SettlementSubID = EventStream<SettlementUpdateEvent>::SubID;
|
||||
template <class CB>
|
||||
SettlementSubID subscribe_to_settlements(SecurityKey k, CB &&cb) {
|
||||
return _settlement_updates.sub(
|
||||
k, [this, cb](auto const &ev) { this->on_external_event(ev, cb); });
|
||||
}
|
||||
void unsubscribe_from_settlements(SettlementSubID sid) {
|
||||
_settlement_updates.unsub(sid);
|
||||
}
|
||||
// Settlement
|
||||
|
||||
// Time
|
||||
template <class CB> void subscribe_to_time(CB &&cb) {
|
||||
_time_change_sub = _time_change.sub(
|
||||
[this, cb](auto const &ev) { this->on_external_event(ev, cb); });
|
||||
}
|
||||
void unsubscribe_from_time() { _time_change.unsub(_time_change_sub); }
|
||||
// Time
|
||||
|
||||
// Execution Reports
|
||||
using ExeReportSubID = EventStream<NewExecutionReportEvent>::SubID;
|
||||
template <class CB>
|
||||
void subscribe_to_exe_report(SessionServices &ss, OrderKey k, CB &&cb) {
|
||||
ss.subscribe_to_exe_report(
|
||||
k, [this, cb](auto const &ev) { this->on_external_event(ev, cb); });
|
||||
}
|
||||
void unsubscribe_from_exe_report(SessionServices &ss, ExeReportSubID sid) {
|
||||
ss.unsubscribe_from_exe_report(sid);
|
||||
}
|
||||
// Execution Reports
|
||||
|
||||
// Cancel Reject
|
||||
using CancelRejectSubID = EventStream<CancelRejectEvent>::SubID;
|
||||
template <class CB>
|
||||
CancelRejectSubID subscribe_to_cancel_reject(SessionServices &ss, OrderKey k,
|
||||
CB &&cb) {
|
||||
return ss.subscribe_to_cancel_reject(
|
||||
k, [this, cb](auto const &ev) { this->on_external_event(ev, cb); });
|
||||
}
|
||||
void unsubscribe_from_cancel_reject(SessionServices &ss,
|
||||
CancelRejectSubID sid) {
|
||||
ss.unsubscribe_from_cancel_reject(sid);
|
||||
}
|
||||
// Cancel Reject
|
||||
|
||||
// Gateway Status
|
||||
template <class CB>
|
||||
void subscribe_to_gateway_status(SessionServices &ss, CB &&cb) {
|
||||
ss.subscribe_to_gateway_status(
|
||||
[this, cb](auto const &ev) { this->on_external_event(ev, cb); });
|
||||
}
|
||||
void unsubscribe_from_gateway_status(SessionServices &ss) {
|
||||
ss.unsubscribe_from_gateway_status();
|
||||
}
|
||||
// Gateway Status
|
||||
|
||||
template <class CB> void subscribe_to_child_updates(CB &&cb) {
|
||||
_child_update_cb = cb;
|
||||
}
|
||||
void unsubscribe_from_child_updates() {
|
||||
_child_update_cb = [](auto const &) {};
|
||||
}
|
||||
|
||||
// End Subscriptions ------------------------------
|
||||
|
||||
private:
|
||||
friend class OrderBuilder;
|
||||
IOrderFactory &order_factory() { return _data.order_factory; }
|
||||
|
||||
EventStream<CreateOrderReplyEvent> order_creations;
|
||||
template <class EV, class C>
|
||||
void on_external_event(EV const &ev, C const &c) {
|
||||
bts_assert(!_busy);
|
||||
_busy = true;
|
||||
c(ev);
|
||||
for (size_t i = 0; i < _pending_calls.size(); i++) {
|
||||
auto c = _pending_calls[i];
|
||||
c(*this);
|
||||
}
|
||||
_pending_calls.clear();
|
||||
_busy = false;
|
||||
}
|
||||
|
||||
template <class C> void on_inter_order_call(C const &c) {
|
||||
if (_busy)
|
||||
_pending_calls.emplace_back(c);
|
||||
else {
|
||||
_busy = true;
|
||||
c(*this);
|
||||
for (size_t i = 0; i < _pending_calls.size(); i++) {
|
||||
auto c = _pending_calls[i];
|
||||
c(*this);
|
||||
}
|
||||
_pending_calls.clear();
|
||||
_busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
IOrderFactory &order_factory() { return _data->order_factory; }
|
||||
|
||||
using CreateOrderReplySubID = EventStream<CreateOrderReplyEvent>::SubID;
|
||||
|
||||
template <class CB>
|
||||
CreateOrderReplySubID subscribe_to_order_creation_reply(size_t req_id,
|
||||
CB &&cb) {
|
||||
return _create_order_reply.sub(req_id, std::forward<CB>(cb));
|
||||
}
|
||||
void unsubscribe_from_order_creation_reply(CreateOrderReplySubID sid) {
|
||||
_create_order_reply.unsub(sid);
|
||||
}
|
||||
|
||||
void send_order_create_req(size_t builder_id, OrderKey pk,
|
||||
api::orderman::OrderDefinition const &def,
|
||||
size_t n_orders) {
|
||||
_data.owner.publish(_data.create_order_req_pub, builder_id, pk, def,
|
||||
n_orders);
|
||||
_data->owner.publish(_data->create_order_req_pub, builder_id, pk, def,
|
||||
n_orders);
|
||||
}
|
||||
|
||||
friend class ChildOrder;
|
||||
|
||||
template <class OP> void queue_order_op(const OP &op) {
|
||||
_data.owner.queue_order_event(op);
|
||||
OrderServices &activate_child(Order &o, Price p, Quantity q,
|
||||
trace::CorID const &trace) {
|
||||
return _data->owner.activate_child(*this, o, p, q, trace);
|
||||
}
|
||||
void send_activate(Order &o, Price p, Quantity q, PerfTrace const &trace) {
|
||||
queue_order_op(ActivateOrderEvent{o, p, q, trace});
|
||||
void update(Price p, Quantity q, trace::CorID const &trace) {
|
||||
on_inter_order_call([p, q, trace](OrderServices &self) {
|
||||
self._owner->on_update(p, q, trace);
|
||||
});
|
||||
}
|
||||
void send_activate(Order &o, Price p, Quantity q) {
|
||||
queue_order_op(ActivateOrderEvent{o, p, q, PerfTrace()});
|
||||
void crack(trace::CorID const &trace) {
|
||||
on_inter_order_call(
|
||||
[trace](OrderServices &self) { self._owner->on_crack(trace); });
|
||||
}
|
||||
void send_update(OrderKey k, Price p, Quantity q, PerfTrace const &trace) {
|
||||
queue_order_op(UpdateOrderEvent{k, p, q, trace});
|
||||
}
|
||||
void send_update(OrderKey k, Price p, Quantity q) {
|
||||
queue_order_op(UpdateOrderEvent{k, p, q, PerfTrace()});
|
||||
}
|
||||
void send_crack(OrderKey k, PerfTrace const &trace) {
|
||||
queue_order_op(CrackOrderEvent{k, trace});
|
||||
}
|
||||
void send_crack(OrderKey k) {
|
||||
queue_order_op(CrackOrderEvent{k, PerfTrace()});
|
||||
}
|
||||
void send_cancel(OrderKey k, PerfTrace const &trace) {
|
||||
queue_order_op(CancelOrderEvent{k, trace});
|
||||
}
|
||||
void send_cancel(OrderKey k) {
|
||||
queue_order_op(CancelOrderEvent{k, PerfTrace()});
|
||||
void cancel(trace::CorID const &trace) {
|
||||
on_inter_order_call(
|
||||
[trace](OrderServices &self) { self._owner->on_cancel(trace); });
|
||||
}
|
||||
|
||||
Order &_owner;
|
||||
OrderServicesData &_data;
|
||||
std::vector<std::unique_ptr<SessionServices>> _sessions;
|
||||
void on_child_update(OrderStateUpdateEvent const &ev) {
|
||||
on_inter_order_call(
|
||||
[ev](OrderServices &self) { self._child_update_cb(ev); });
|
||||
}
|
||||
|
||||
bool _busy;
|
||||
boost::container::small_vector<std::function<void(OrderServices &)>, 10>
|
||||
_pending_calls;
|
||||
Order *_owner;
|
||||
OrderServicesData *_data;
|
||||
OrderServices *_parent;
|
||||
std::function<void(OrderStateUpdateEvent const &)> _child_update_cb;
|
||||
EventStream<BookUpdateEvent> _book_updates;
|
||||
EventStream<SettlementUpdateEvent> _settlement_updates;
|
||||
EventStream<mercury::TimerEvent, mercury::NoKey> _time_change;
|
||||
boost::container::small_vector<SessionServices, 20> _sessions;
|
||||
EventStream<mercury::TimerEvent, mercury::NoKey>::SubID _time_change_sub;
|
||||
EventStream<CreateOrderReplyEvent> _create_order_reply;
|
||||
};
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -5,15 +5,18 @@
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
OrderServicesData::OrderServicesData(OrderManager &owner,DynamicOrderRegistry const&b)
|
||||
: owner(owner), order_router(), order_factory(sec_db, order_router,b),
|
||||
OrderServicesData::OrderServicesData(OrderManager &owner,
|
||||
DynamicOrderRegistry const &b,
|
||||
trace::PerfLog &l)
|
||||
: owner(owner), log(l), order_router(),
|
||||
order_factory(sec_db, order_router, b),
|
||||
last_timer_ev(
|
||||
Timer::now(),
|
||||
to<mercury::TimeStampStr>(datetime::FIX_timestamp(datetime::now()))) {
|
||||
|
||||
auto &all_connections = config::get<config::JetFIX>().connection();
|
||||
for (auto const &c : all_connections)
|
||||
sessions.push_back (std::make_unique<SessionServicesData>(c));
|
||||
}
|
||||
}
|
||||
for (auto const &c : all_connections)
|
||||
sessions.push_back(std::make_unique<SessionServicesData>(c, l));
|
||||
}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -7,11 +7,10 @@
|
||||
#include "lib/orderman/order_factory/OrderFactory.h"
|
||||
#include "lib/orderman/securities/MapSecurityDB.h"
|
||||
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/mercury/events/TimerEvent.h"
|
||||
#include "lib/orderman/actors/ExecutionGateway.h"
|
||||
#include "lib/orderman/events/ActivateOrderEvent.h"
|
||||
#include "lib/orderman/events/BookUpdateEvent.h"
|
||||
#include "lib/orderman/events/SettlementUpdateEvent.h"
|
||||
#include "lib/orderman/events/CancelOrderEvent.h"
|
||||
#include "lib/orderman/events/CancelRejectEvent.h"
|
||||
#include "lib/orderman/events/CrackOrderEvent.h"
|
||||
@@ -21,15 +20,16 @@
|
||||
#include "lib/orderman/events/CreateOrderRequestEvent.h"
|
||||
#include "lib/orderman/events/GatewayStatusChangeEvent.h"
|
||||
#include "lib/orderman/events/MDRequestEvent.h"
|
||||
#include "lib/orderman/events/SettlementRequestEvent.h"
|
||||
#include "lib/orderman/events/NewExecutionReportEvent.h"
|
||||
#include "lib/orderman/events/NewSecuritiesEvent.h"
|
||||
#include "lib/orderman/events/OrderStateUpdateEvent.h"
|
||||
#include "lib/orderman/events/OutFIXMsgEvent.h"
|
||||
#include "lib/orderman/events/SettlementRequestEvent.h"
|
||||
#include "lib/orderman/events/SettlementUpdateEvent.h"
|
||||
#include "lib/orderman/events/UpdateOrderEvent.h"
|
||||
#include "lib/orderman/governor/Governor.h"
|
||||
#include "lib/orderman/limiter/Limiter.h"
|
||||
#include "lib/orderman/order_router/OrderRouter.h"
|
||||
#include "lib/orderman/governor/Governor.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
@@ -44,19 +44,13 @@ struct ParentKey {
|
||||
};
|
||||
|
||||
struct SessionServicesData {
|
||||
SessionServicesData(config::JetFIX::Connection const&cfg)
|
||||
: session(cfg.name()),
|
||||
gw_connected(false),
|
||||
cfg(cfg),
|
||||
governor(cfg.max_order_msgs_per_window(),
|
||||
cfg.window_duration_msecs())
|
||||
{
|
||||
current_traces.reserve(1000);
|
||||
}
|
||||
SessionServicesData(config::JetFIX::Connection const &cfg, trace::PerfLog &log)
|
||||
: session(cfg.name()), gw_connected(false), cfg(cfg),
|
||||
governor(cfg.max_order_msgs_per_window(), cfg.window_duration_msecs()),
|
||||
gw(cfg, log) {}
|
||||
|
||||
std::string session;
|
||||
std::vector<jetfix::OutFIXPtr> free_buffers;
|
||||
mercury::Stream<OutFIXMsgEvent> out_fix_pub;
|
||||
bool gw_connected;
|
||||
config::JetFIX::Connection cfg;
|
||||
std::unique_ptr<mercury::ContentSubscription<NewExecutionReportEvent>>
|
||||
@@ -69,34 +63,34 @@ struct SessionServicesData {
|
||||
|
||||
Governor governor;
|
||||
jetfix::OutFIXPtr current_buffer;
|
||||
std::vector<PerfTrace> current_traces;
|
||||
trace::CorID current_trace;
|
||||
ExecutionGateway gw;
|
||||
mercury::Stream<NewExecutionReportEvent> exe_report_pub;
|
||||
mercury::Stream<GatewayStatusChangeEvent> gw_status_change_pub;
|
||||
mercury::Stream<CancelRejectEvent> cancel_reject_pub;
|
||||
};
|
||||
|
||||
class OrderServicesData {
|
||||
public:
|
||||
OrderServicesData(OrderManager &owner,DynamicOrderRegistry const&b);
|
||||
public:
|
||||
|
||||
OrderServicesData(OrderManager &owner, DynamicOrderRegistry const &b,
|
||||
trace::PerfLog &l);
|
||||
|
||||
OrderManager &owner;
|
||||
trace::PerfLog &log;
|
||||
securities::MapSecurityDB sec_db;
|
||||
OrderRouter order_router;
|
||||
Limiter limiter;
|
||||
OrderFactory order_factory;
|
||||
|
||||
std::unique_ptr<mercury::ContentSubscription<BookUpdateEvent>> md_subs;
|
||||
std::unique_ptr<mercury::ContentSubscription<SettlementUpdateEvent>> settlement_subs;
|
||||
std::unique_ptr<
|
||||
mercury::ContentSubscription<OrderStateUpdateEvent, ParentKey>>
|
||||
child_order_subs;
|
||||
|
||||
std::unique_ptr<mercury::ContentSubscription<SettlementUpdateEvent>>
|
||||
settlement_subs;
|
||||
std::unique_ptr<
|
||||
mercury::ContentSubscription<mercury::TimerEvent, mercury::NoKey>>
|
||||
time_subs;
|
||||
std::unique_ptr<mercury::ContentSubscription<CreateOrderReplyEvent>>
|
||||
order_creation_subs;
|
||||
|
||||
mercury::Stream<ActivateOrderEvent> activate_order_pub;
|
||||
mercury::Stream<UpdateOrderEvent> update_order_pub;
|
||||
mercury::Stream<CancelOrderEvent> cancel_order_pub;
|
||||
mercury::Stream<CrackOrderEvent> crack_order_pub;
|
||||
mercury::Stream<CreateOrderRequestEvent> create_order_req_pub;
|
||||
mercury::Stream<CreateFIXBufferRequestEvent> create_fix_req_pub;
|
||||
mercury::Stream<MDRequestEvent> md_request_pub;
|
||||
@@ -113,5 +107,5 @@ public:
|
||||
bts_assert(false);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/mercury/events/Event.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -18,14 +19,14 @@ public:
|
||||
order (&o),price(p),quantity(q), trace()
|
||||
{}
|
||||
|
||||
ActivateOrderEvent (Order &o,Price p, Quantity q,PerfTrace trace) :
|
||||
ActivateOrderEvent (Order &o,Price p, Quantity q,trace::CorID trace) :
|
||||
order (&o),price(p),quantity(q), trace(trace)
|
||||
{}
|
||||
|
||||
Order* order;
|
||||
Price price;
|
||||
Quantity quantity;
|
||||
PerfTrace trace;
|
||||
trace::CorID trace;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "lib/mercury/events/Event.h"
|
||||
#include "lib/orderman/orderman_utils/Book.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -13,10 +14,10 @@ class BookUpdateEvent : public mercury::Event<BookUpdateEvent> {
|
||||
public:
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
Book book;
|
||||
PerfTrace trace;
|
||||
trace::CorID trace;
|
||||
SecurityKey key() const { return book.security_key(); }
|
||||
|
||||
BookUpdateEvent(const Book &b, PerfTrace const &trace)
|
||||
BookUpdateEvent(const Book &b, trace::CorID const &trace)
|
||||
: book(b), trace(trace) {}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/mercury/events/Event.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -14,12 +15,12 @@ public:
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
CancelOrderEvent (OrderKey k) : k (k), trace() {}
|
||||
CancelOrderEvent (OrderKey k,PerfTrace const&trace) : k (k), trace(trace) {}
|
||||
CancelOrderEvent (OrderKey k,trace::CorID const&trace) : k (k), trace(trace) {}
|
||||
|
||||
// used by ContentSubscription
|
||||
OrderKey key() const {return k;}
|
||||
OrderKey k;
|
||||
PerfTrace trace;
|
||||
trace::CorID trace;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "CancelRejectEvent.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
}}
|
||||
namespace orderman {}
|
||||
} // namespace bts
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "lib/mercury/events/Event.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -15,13 +16,13 @@ class CrackOrderEvent : public mercury::Event<CrackOrderEvent> {
|
||||
CrackOrderEvent(OrderKey k)
|
||||
: k(k), trace() {}
|
||||
|
||||
CrackOrderEvent(OrderKey k, PerfTrace const &trace)
|
||||
CrackOrderEvent(OrderKey k, trace::CorID const &trace)
|
||||
: k(k), trace(trace) {}
|
||||
|
||||
// used by ContentSubscription
|
||||
OrderKey key() const { return k; }
|
||||
OrderKey k;
|
||||
PerfTrace trace;
|
||||
trace::CorID trace;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
#include "NewExecutionReportEvent.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
|
||||
}}
|
||||
namespace orderman {}
|
||||
} // namespace bts
|
||||
@@ -7,6 +7,8 @@
|
||||
#include "lib/mercury/synopsis/CacheAll.h"
|
||||
#include "lib/orderman/orderman_utils/ExecutionReport.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include <memory>
|
||||
@@ -19,10 +21,10 @@ class NewExecutionReportEvent : public mercury::Event<NewExecutionReportEvent> {
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::CacheAll<NewExecutionReportEvent>;
|
||||
ExecutionReport const *data;
|
||||
PerfTrace trace;
|
||||
trace::CorID trace;
|
||||
// used by ContentSubscription
|
||||
OrderKey key() const { return data->order_key; }
|
||||
NewExecutionReportEvent(const ExecutionReport *dt, PerfTrace const &trace)
|
||||
NewExecutionReportEvent(const ExecutionReport *dt, trace::CorID const &trace)
|
||||
: data(dt), trace(trace){};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/mercury/events/Event.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTrace.h"
|
||||
#include <memory>
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -15,7 +15,7 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
OrderStateUpdateEvent(OrderKey key, OrderKey parent_key,
|
||||
api::orderman::OrderDefinition const &def,
|
||||
api::orderman::OrderState const &state,
|
||||
PerfTrace const &trace = PerfTrace())
|
||||
trace::CorID const &trace = trace::CorID())
|
||||
: key(key), parent_key(parent_key), trace(trace), definition(&def) {
|
||||
|
||||
status = state.status();
|
||||
@@ -51,7 +51,7 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
double avg_price;
|
||||
std::string last_msg;
|
||||
|
||||
PerfTrace trace;
|
||||
trace::CorID trace;
|
||||
api::orderman::OrderState::CustomCase custom;
|
||||
|
||||
// native order state
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
#include "lib/jetfix/common/OutFIXPtr.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/mercury/events/Event.h"
|
||||
#include "lib/mercury/synopsis/CacheAll.h"
|
||||
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
class OutFIXMsgEvent : public mercury::Event<OutFIXMsgEvent> {
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
|
||||
// there is no need to store synopsis for this event because no one
|
||||
// should be sending fix events (OutFIXMsgEvent) until they receive
|
||||
// an event from the FIX gateway regarding the status of the FIX
|
||||
// connection. Receipt of this connection event guarantees to
|
||||
// potential publishers of OutFIXMsgEvent that the gateway is
|
||||
// already subscribed to OutFIXMsgEvent and so no events will be
|
||||
// missed.
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
OutFIXMsgEvent (jetfix::OutFIXPtr const&fix,std::vector<PerfTrace> const&traces) :
|
||||
msg(fix),trace(traces[0])
|
||||
{}
|
||||
|
||||
jetfix::OutFIXPtr msg;
|
||||
PerfTrace trace;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/mercury/events/Event.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -17,7 +18,7 @@ public:
|
||||
k(k), price(p), quantity(q), trace()
|
||||
{}
|
||||
|
||||
UpdateOrderEvent (OrderKey k, Price p, Quantity q,PerfTrace const&trace) :
|
||||
UpdateOrderEvent (OrderKey k, Price p, Quantity q,trace::CorID const&trace) :
|
||||
k(k), price(p), quantity(q), trace(trace)
|
||||
{}
|
||||
|
||||
@@ -27,7 +28,7 @@ public:
|
||||
OrderKey k;
|
||||
Price price;
|
||||
Quantity quantity;
|
||||
PerfTrace trace;
|
||||
trace::CorID trace;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -1,77 +1,91 @@
|
||||
#include "LimitStore.h"
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
LimitStore::LimitStore(const bts::api::admin::AdminData& admin_data,
|
||||
const std::map<uint32_t,api::Security const*>& sec_map)
|
||||
:
|
||||
_admin_data (bts::admin::AdminServiceUtils::sort_limits(admin_data)),
|
||||
_account_map(bts::admin::AdminServiceUtils::accounts_map(_admin_data))
|
||||
{
|
||||
for(auto it=sec_map.begin(); it!=sec_map.end(); it++){
|
||||
const api::Security& security = *(it->second);
|
||||
LimitStore::LimitStore(const bts::api::admin::AdminData &admin_data,
|
||||
const std::map<uint32_t, api::Security const *> &sec_map)
|
||||
: _admin_data(bts::admin::AdminServiceUtils::sort_limits(admin_data)),
|
||||
_account_map(bts::admin::AdminServiceUtils::accounts_map(_admin_data)) {
|
||||
for (auto it = sec_map.begin(); it != sec_map.end(); it++) {
|
||||
const api::Security &security = *(it->second);
|
||||
_add_product_entry(security, _admin_data);
|
||||
}
|
||||
for(auto it=sec_map.begin(); it!=sec_map.end(); it++){
|
||||
}
|
||||
for (auto it = sec_map.begin(); it != sec_map.end(); it++) {
|
||||
// pos limit
|
||||
const api::Security& security = *(it->second);
|
||||
ProductLimitTree* product_tree_ptr = _find_product_for_security (security);
|
||||
bts_assert(product_tree_ptr!=nullptr);
|
||||
_sec_map.insert(std::pair<uint32_t,SecurityLimitTree>(security.id(),SecurityLimitTree(security, *product_tree_ptr, _admin_data, _zero_security_limit)));
|
||||
const api::Security &security = *(it->second);
|
||||
ProductLimitTree *product_tree_ptr = _find_product_for_security(security);
|
||||
bts_assert(product_tree_ptr != nullptr);
|
||||
_sec_map.insert(std::pair<uint32_t, SecurityLimitTree>(
|
||||
security.id(), SecurityLimitTree(security, *product_tree_ptr,
|
||||
_admin_data, _zero_security_limit)));
|
||||
}
|
||||
_set_user_data();
|
||||
}
|
||||
void LimitStore::copy_positions_from(LimitStore& source){
|
||||
void LimitStore::copy_positions_from(LimitStore &source) {
|
||||
// copy securities
|
||||
for (auto it=_sec_map.begin(); it!=_sec_map.end(); it++){
|
||||
SecurityLimitTree* source_security_limit_tree = source._find_security(it->first);
|
||||
if(source_security_limit_tree != nullptr)
|
||||
for (auto it = _sec_map.begin(); it != _sec_map.end(); it++) {
|
||||
SecurityLimitTree *source_security_limit_tree =
|
||||
source._find_security(it->first);
|
||||
if (source_security_limit_tree != nullptr)
|
||||
it->second.copy_positions_from(*source_security_limit_tree);
|
||||
}
|
||||
}
|
||||
void LimitStore::_add_product_entry(const api::Security& security,
|
||||
const bts::api::admin::AdminData& admin_data){
|
||||
ProductLimitTree* product_tree_ptr = _find_product_for_security(security);
|
||||
if (product_tree_ptr==nullptr){
|
||||
void LimitStore::_add_product_entry(
|
||||
const api::Security &security,
|
||||
const bts::api::admin::AdminData &admin_data) {
|
||||
ProductLimitTree *product_tree_ptr = _find_product_for_security(security);
|
||||
if (product_tree_ptr == nullptr) {
|
||||
api::Product product;
|
||||
product.set_exchange(security.exchange()); product.set_type(bts::admin::AdminServiceUtils::security_type(security)); product.set_symbol(security.symbol());
|
||||
_prod_map.emplace_back(product, admin_data, _zero_product_limit);
|
||||
product.set_exchange(security.exchange());
|
||||
product.set_type(bts::admin::AdminServiceUtils::security_type(security));
|
||||
product.set_symbol(security.symbol());
|
||||
_prod_map.emplace_back(product, admin_data, _zero_product_limit);
|
||||
}
|
||||
}
|
||||
void LimitStore::_set_user_data (){
|
||||
for(const auto& user : _admin_data.users()){
|
||||
if (user.is_present()){
|
||||
void LimitStore::_set_user_data() {
|
||||
for (const auto &user : _admin_data.users()) {
|
||||
if (user.is_present()) {
|
||||
UserData user_data;
|
||||
user_data.user = &user;
|
||||
bts::admin::AdminServiceUtils::find_user_trading_accounts(_admin_data, user.id(), user_data.accounts);
|
||||
_user_data_map.insert(std::pair<uint32_t, UserData>(user.id(), user_data));
|
||||
bts::admin::AdminServiceUtils::find_user_trading_accounts(
|
||||
_admin_data, user.id(), user_data.accounts);
|
||||
_user_data_map.insert(
|
||||
std::pair<uint32_t, UserData>(user.id(), user_data));
|
||||
}
|
||||
}
|
||||
}
|
||||
ProductLimitTree* LimitStore::_find_product_for_security(const api::Security& security){
|
||||
api::Product product;
|
||||
product.set_exchange(security.exchange()); product.set_type(bts::admin::AdminServiceUtils::security_type(security)); product.set_symbol(security.symbol());
|
||||
return _find_product(product);
|
||||
}
|
||||
ProductLimitTree* LimitStore::_find_product(const api::Product& product){
|
||||
for(ProductLimitTree& plt : _prod_map)
|
||||
if(product.exchange() == plt.product().exchange() && product.type() == plt.product().type() && product.symbol() == plt.product().symbol())
|
||||
}
|
||||
ProductLimitTree *
|
||||
LimitStore::_find_product_for_security(const api::Security &security) {
|
||||
api::Product product;
|
||||
product.set_exchange(security.exchange());
|
||||
product.set_type(bts::admin::AdminServiceUtils::security_type(security));
|
||||
product.set_symbol(security.symbol());
|
||||
return _find_product(product);
|
||||
}
|
||||
ProductLimitTree *LimitStore::_find_product(const api::Product &product) {
|
||||
for (ProductLimitTree &plt : _prod_map)
|
||||
if (product.exchange() == plt.product().exchange() &&
|
||||
product.type() == plt.product().type() &&
|
||||
product.symbol() == plt.product().symbol())
|
||||
return &plt;
|
||||
return nullptr;
|
||||
}
|
||||
const SecurityLimitNode* LimitStore::test_only_func_find_security_node(AccountID account_id,
|
||||
uint32_t security_id){
|
||||
const SecurityLimitTree* sec_data = _find_security(security_id);
|
||||
if(sec_data != nullptr)
|
||||
const SecurityLimitNode *
|
||||
LimitStore::test_only_func_find_security_node(AccountID account_id,
|
||||
uint32_t security_id) {
|
||||
const SecurityLimitTree *sec_data = _find_security(security_id);
|
||||
if (sec_data != nullptr)
|
||||
return sec_data->find_account(account_id.value());
|
||||
return nullptr;
|
||||
}
|
||||
const ProductLimitNode* LimitStore::test_only_func_find_product_node(AccountID account_id,
|
||||
const api::Product& product){
|
||||
ProductLimitTree* prod_data = _find_product(product);
|
||||
if(prod_data != nullptr)
|
||||
const ProductLimitNode *
|
||||
LimitStore::test_only_func_find_product_node(AccountID account_id,
|
||||
const api::Product &product) {
|
||||
ProductLimitTree *prod_data = _find_product(product);
|
||||
if (prod_data != nullptr)
|
||||
return prod_data->find_account(account_id.value());
|
||||
return nullptr;
|
||||
}
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,144 +1,131 @@
|
||||
#include "Limiter.h"
|
||||
#include "LimitStore.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/api/services/AdminService.server.pb.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
struct Limiter::PImpl {
|
||||
std::map<uint32_t,api::Security const*> sec_map;
|
||||
std::map<uint32_t, api::Security const *> sec_map;
|
||||
std::unique_ptr<LimitStore> limit_store;
|
||||
PImpl()
|
||||
:
|
||||
sec_map(),
|
||||
limit_store(std::make_unique<LimitStore>(bts::api::admin::AdminData(),
|
||||
sec_map))
|
||||
{
|
||||
}
|
||||
: sec_map(), limit_store(std::make_unique<LimitStore>(
|
||||
bts::api::admin::AdminData(), sec_map)) {}
|
||||
};
|
||||
Limiter::Limiter() : me(new PImpl()) {}
|
||||
Limiter::~Limiter() {}
|
||||
|
||||
void Limiter::update(api::admin::AdminData const& admin_data) {
|
||||
void Limiter::update(api::admin::AdminData const &admin_data) {
|
||||
LOG(OrderMan, Info, "Limiter::update");
|
||||
_reset(admin_data);
|
||||
}
|
||||
void Limiter::add_securities(std::vector<api::Security const*> const& new_securities){
|
||||
void Limiter::add_securities(
|
||||
std::vector<api::Security const *> const &new_securities) {
|
||||
LOG(OrderMan, Info, "Limiter::add_securities {} securities",
|
||||
me->sec_map.size());
|
||||
for(const api::Security* s : new_securities)
|
||||
me->sec_map.size());
|
||||
for (const api::Security *s : new_securities)
|
||||
_add_security(s);
|
||||
_reset(me->limit_store->admin_data());
|
||||
}
|
||||
void Limiter::add_security(api::Security const & security) {
|
||||
void Limiter::add_security(api::Security const &security) {
|
||||
_add_security(&security);
|
||||
_reset(me->limit_store->admin_data());
|
||||
}
|
||||
void Limiter::add(api::Security const &security,
|
||||
AccountID account_id,
|
||||
api::Side side,
|
||||
Quantity quantity){
|
||||
void Limiter::add(api::Security const &security, AccountID account_id,
|
||||
api::Side side, Quantity quantity) {
|
||||
if (!_contains_security(&security))
|
||||
add_security(security);
|
||||
if (!me->limit_store->has_account(account_id)){
|
||||
if (!me->limit_store->has_account(account_id)) {
|
||||
// add a temporary account, so trades can go in there
|
||||
_add_account_and_reset(account_id);
|
||||
}
|
||||
me->limit_store->add_position(security, account_id, side, quantity);
|
||||
}
|
||||
void Limiter::remove(api::Security const &security,
|
||||
AccountID account_id,
|
||||
api::Side side,
|
||||
Quantity quantity) {
|
||||
void Limiter::remove(api::Security const &security, AccountID account_id,
|
||||
api::Side side, Quantity quantity) {
|
||||
if (!_contains_security(&security))
|
||||
add_security(security);
|
||||
if (!me->limit_store->has_account(account_id)){
|
||||
if (!me->limit_store->has_account(account_id)) {
|
||||
_add_account_and_reset(account_id);
|
||||
}
|
||||
me->limit_store->remove_position(security, account_id, side, quantity);
|
||||
}
|
||||
void Limiter::add(const api::Security& security,
|
||||
const std::string& account_name,
|
||||
api::Side side,
|
||||
Quantity quantity){
|
||||
const auto* acct_ptr=
|
||||
bts::admin::AdminServiceUtils::find_account(me->limit_store->admin_data(),
|
||||
account_name);
|
||||
if(acct_ptr==nullptr)
|
||||
void Limiter::add(const api::Security &security,
|
||||
const std::string &account_name, api::Side side,
|
||||
Quantity quantity) {
|
||||
const auto *acct_ptr = bts::admin::AdminServiceUtils::find_account(
|
||||
me->limit_store->admin_data(), account_name);
|
||||
if (acct_ptr == nullptr)
|
||||
LOG(OrderMan, Error, "Limiter::add can not find account:{}",
|
||||
account_name);
|
||||
account_name);
|
||||
else
|
||||
add(security,AccountID(acct_ptr->id()),side,quantity);
|
||||
add(security, AccountID(acct_ptr->id()), side, quantity);
|
||||
}
|
||||
void Limiter::remove(const api::Security& security,
|
||||
const std::string& account_name,
|
||||
api::Side side,
|
||||
Quantity quantity){
|
||||
const auto* acct_ptr=
|
||||
bts::admin::AdminServiceUtils::find_account(me->limit_store->admin_data(),
|
||||
account_name);
|
||||
if(acct_ptr==nullptr)
|
||||
void Limiter::remove(const api::Security &security,
|
||||
const std::string &account_name, api::Side side,
|
||||
Quantity quantity) {
|
||||
const auto *acct_ptr = bts::admin::AdminServiceUtils::find_account(
|
||||
me->limit_store->admin_data(), account_name);
|
||||
if (acct_ptr == nullptr)
|
||||
LOG(OrderMan, Error, "Limiter::remove can not find account:{}",
|
||||
account_name);
|
||||
account_name);
|
||||
else
|
||||
remove(security,AccountID(acct_ptr->id()),side,quantity);
|
||||
remove(security, AccountID(acct_ptr->id()), side, quantity);
|
||||
}
|
||||
Limiter::LimiterQueryResult Limiter::can_trade(UserID user_id,
|
||||
AccountID account_id) const {
|
||||
LimiterQueryResult rv = me->limit_store->can_trade(user_id, account_id);
|
||||
return rv;
|
||||
}
|
||||
Limiter::LimiterQueryResult Limiter::can_send_new_order(AccountID account_id,
|
||||
api::Security const & security,
|
||||
api::Side side,
|
||||
Quantity quantity) const {
|
||||
LimiterQueryResult rv = me->limit_store->can_send_new_order(account_id,
|
||||
security,
|
||||
side,
|
||||
quantity);
|
||||
//rv.ok = true;
|
||||
Limiter::LimiterQueryResult
|
||||
Limiter::can_send_new_order(AccountID account_id, api::Security const &security,
|
||||
api::Side side, Quantity quantity) const {
|
||||
LimiterQueryResult rv = me->limit_store->can_send_new_order(
|
||||
account_id, security, side, quantity);
|
||||
// rv.ok = true;
|
||||
return rv;
|
||||
}
|
||||
Limiter::LimiterQueryResult Limiter::can_update_order(AccountID account_id,
|
||||
api::Security const & security,
|
||||
api::Side side,
|
||||
Quantity from,
|
||||
Quantity to) const {
|
||||
Limiter::LimiterQueryResult
|
||||
Limiter::can_update_order(AccountID account_id, api::Security const &security,
|
||||
api::Side side, Quantity from, Quantity to) const {
|
||||
LimiterQueryResult rv;
|
||||
if (from == to)
|
||||
rv.ok = true ;
|
||||
else
|
||||
rv = me->limit_store->can_update_order(account_id, security, side, from, to);
|
||||
if (from == to)
|
||||
rv.ok = true;
|
||||
else
|
||||
rv = me->limit_store->can_update_order(account_id, security, side, from,
|
||||
to);
|
||||
return rv;
|
||||
}
|
||||
void Limiter::_add_account_and_reset(AccountID account_id){
|
||||
bts::api::admin::AdminData admin_data_catchup = me->limit_store->admin_data();
|
||||
bts::api::admin::BTSAccount* new_account = admin_data_catchup.mutable_accounts()->Add();
|
||||
void Limiter::_add_account_and_reset(AccountID account_id) {
|
||||
bts::api::admin::AdminData admin_data_catchup =
|
||||
me->limit_store->admin_data();
|
||||
bts::api::admin::BTSAccount *new_account =
|
||||
admin_data_catchup.mutable_accounts()->Add();
|
||||
new_account->set_id(account_id.value());
|
||||
new_account->set_is_active(true);
|
||||
update(admin_data_catchup);
|
||||
}
|
||||
void Limiter::_reset (const bts::api::admin::AdminData& admin_data){
|
||||
LimitStore* new_store = new LimitStore(admin_data, me->sec_map);
|
||||
void Limiter::_reset(const bts::api::admin::AdminData &admin_data) {
|
||||
LimitStore *new_store = new LimitStore(admin_data, me->sec_map);
|
||||
new_store->copy_positions_from(*me->limit_store);
|
||||
me->limit_store.reset(new_store);
|
||||
}
|
||||
|
||||
void Limiter::_add_security (const api::Security* sec){
|
||||
if (!_contains_security(sec)){
|
||||
me->sec_map.insert(std::pair<uint32_t,const api::Security*>(sec->id(), sec));
|
||||
void Limiter::_add_security(const api::Security *sec) {
|
||||
if (!_contains_security(sec)) {
|
||||
me->sec_map.insert(
|
||||
std::pair<uint32_t, const api::Security *>(sec->id(), sec));
|
||||
if (sec->has_spread())
|
||||
for(const auto& leg : sec->spread().legs())
|
||||
for (const auto &leg : sec->spread().legs())
|
||||
_add_security(&leg.security());
|
||||
}
|
||||
}
|
||||
|
||||
bool Limiter::_contains_security (const api::Security* security){
|
||||
bool Limiter::_contains_security(const api::Security *security) {
|
||||
return me->sec_map.find(security->id()) != me->sec_map.end();
|
||||
}
|
||||
|
||||
LimitStore& Limiter::limit_store(){
|
||||
return *me->limit_store;
|
||||
}
|
||||
}}
|
||||
LimitStore &Limiter::limit_store() { return *me->limit_store; }
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,21 +1,18 @@
|
||||
#include "ProductLimitNode.h"
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
|
||||
ProductLimitNode::ProductLimitNode(uint32_t account_id,
|
||||
uint32_t, // parent_account_id,
|
||||
const bts::api::admin::AccountProductPositionLimitUnit& limit,
|
||||
bool enforce_net)
|
||||
:
|
||||
_account_id(account_id),
|
||||
//_parent_account_id(parent_account_id),
|
||||
_limit(limit),
|
||||
_enforce_net(enforce_net),
|
||||
_long_pos_limit (limit.long_position_limit()),
|
||||
_short_pos_limit (limit.short_position_limit()),
|
||||
_long_pos(0),
|
||||
_short_pos(0)
|
||||
{}
|
||||
}}
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
ProductLimitNode::ProductLimitNode(
|
||||
uint32_t account_id,
|
||||
uint32_t, // parent_account_id,
|
||||
const bts::api::admin::AccountProductPositionLimitUnit &limit,
|
||||
bool enforce_net)
|
||||
: _account_id(account_id),
|
||||
//_parent_account_id(parent_account_id),
|
||||
_limit(limit), _enforce_net(enforce_net),
|
||||
_long_pos_limit(limit.long_position_limit()),
|
||||
_short_pos_limit(limit.short_position_limit()), _long_pos(0),
|
||||
_short_pos(0) {}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,23 +1,27 @@
|
||||
#include "ProductLimitTree.h"
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
ProductLimitTree::ProductLimitTree(const api::Product& product,
|
||||
const bts::api::admin::AdminData& admin_data,
|
||||
const bts::api::admin::AccountProductPositionLimitUnit& zero_limit)
|
||||
:
|
||||
_product (product)
|
||||
{
|
||||
for(const auto& account : admin_data.accounts()){
|
||||
bool enforce_net=true; // default is net
|
||||
if(account.has_risk_limit_enforcement())
|
||||
enforce_net=account.risk_limit_enforcement().product_limit_in_net_terms();
|
||||
const bts::api::admin::AccountProductPositionLimitUnit* limit = bts::admin::AdminServiceUtils::find_account_product_position_limit(account, _product);
|
||||
_tree.emplace_back(account.id(),
|
||||
bts::admin::AdminServiceUtils::has_id(account.parent_account_id()) ? account.parent_account_id() : 0,
|
||||
limit != nullptr ? *limit : zero_limit,
|
||||
enforce_net);
|
||||
ProductLimitTree::ProductLimitTree(
|
||||
const api::Product &product, const bts::api::admin::AdminData &admin_data,
|
||||
const bts::api::admin::AccountProductPositionLimitUnit &zero_limit)
|
||||
: _product(product) {
|
||||
for (const auto &account : admin_data.accounts()) {
|
||||
bool enforce_net = true; // default is net
|
||||
if (account.has_risk_limit_enforcement())
|
||||
enforce_net =
|
||||
account.risk_limit_enforcement().product_limit_in_net_terms();
|
||||
const bts::api::admin::AccountProductPositionLimitUnit *limit =
|
||||
bts::admin::AdminServiceUtils::find_account_product_position_limit(
|
||||
account, _product);
|
||||
_tree.emplace_back(
|
||||
account.id(),
|
||||
bts::admin::AdminServiceUtils::has_id(account.parent_account_id())
|
||||
? account.parent_account_id()
|
||||
: 0,
|
||||
limit != nullptr ? *limit : zero_limit, enforce_net);
|
||||
}
|
||||
}
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -3,22 +3,18 @@
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
SecurityLimitNode::SecurityLimitNode(uint32_t account_id,
|
||||
uint32_t, // parent_account_id,
|
||||
const bts::api::admin::AccountSecurityPositionLimitUnit& limit,
|
||||
bool apply_sec_position_limits,
|
||||
ProductLimitNode& product_node,
|
||||
uint32_t clip_size)
|
||||
:
|
||||
_account_id(account_id),
|
||||
//_parent_account_id(parent_account_id),
|
||||
_limit(limit),
|
||||
_long_pos_limit (limit.long_position_limit()),
|
||||
_short_pos_limit (-1 * limit.short_position_limit()),
|
||||
_apply_sec_position_limits (apply_sec_position_limits),
|
||||
_product_node(product_node),
|
||||
_clip_size(clip_size),
|
||||
_net_pos(0)
|
||||
{}
|
||||
SecurityLimitNode::SecurityLimitNode(
|
||||
uint32_t account_id,
|
||||
uint32_t, // parent_account_id,
|
||||
const bts::api::admin::AccountSecurityPositionLimitUnit &limit,
|
||||
bool apply_sec_position_limits, ProductLimitNode &product_node,
|
||||
uint32_t clip_size)
|
||||
: _account_id(account_id),
|
||||
//_parent_account_id(parent_account_id),
|
||||
_limit(limit), _long_pos_limit(limit.long_position_limit()),
|
||||
_short_pos_limit(-1 * limit.short_position_limit()),
|
||||
_apply_sec_position_limits(apply_sec_position_limits),
|
||||
_product_node(product_node), _clip_size(clip_size), _net_pos(0) {}
|
||||
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,20 +1,30 @@
|
||||
#include "lib/orderman/limiter/SecurityLimitTree.h"
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
SecurityLimitTree::SecurityLimitTree(const api::Security& security,
|
||||
ProductLimitTree& product_tree,
|
||||
const bts::api::admin::AdminData& admin_data,
|
||||
const bts::api::admin::AccountSecurityPositionLimitUnit& zero_limit)
|
||||
{
|
||||
for(const auto& account : admin_data.accounts()){
|
||||
ProductLimitNode* product_node_ptr = product_tree.find_account(account.id());
|
||||
SecurityLimitTree::SecurityLimitTree(
|
||||
const api::Security &security, ProductLimitTree &product_tree,
|
||||
const bts::api::admin::AdminData &admin_data,
|
||||
const bts::api::admin::AccountSecurityPositionLimitUnit &zero_limit) {
|
||||
for (const auto &account : admin_data.accounts()) {
|
||||
ProductLimitNode *product_node_ptr =
|
||||
product_tree.find_account(account.id());
|
||||
bts_assert(product_node_ptr != nullptr);
|
||||
const bts::api::admin::AccountSecurityPositionLimitUnit* limit = bts::admin::AdminServiceUtils::find_account_security_position_limit(account, security);
|
||||
uint32_t clip_size = bts::admin::AdminServiceUtils::find_account_security_clip_size(account, security);
|
||||
_tree.emplace_back(account.id(), bts::admin::AdminServiceUtils::has_id(account.parent_account_id()) ? account.parent_account_id() : 0,
|
||||
limit != nullptr ? *limit : zero_limit, limit != nullptr, *product_node_ptr, clip_size);
|
||||
const bts::api::admin::AccountSecurityPositionLimitUnit *limit =
|
||||
bts::admin::AdminServiceUtils::find_account_security_position_limit(
|
||||
account, security);
|
||||
uint32_t clip_size =
|
||||
bts::admin::AdminServiceUtils::find_account_security_clip_size(
|
||||
account, security);
|
||||
_tree.emplace_back(
|
||||
account.id(),
|
||||
bts::admin::AdminServiceUtils::has_id(account.parent_account_id())
|
||||
? account.parent_account_id()
|
||||
: 0,
|
||||
limit != nullptr ? *limit : zero_limit, limit != nullptr,
|
||||
*product_node_ptr, clip_size);
|
||||
}
|
||||
}
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,63 +1,71 @@
|
||||
#include "catch/catch.hpp"
|
||||
#include <memory>
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "lib/api/services/AdminService.server.pb.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/orderman/limiter/LimitStore.h"
|
||||
#include "lib/orderman/limiter/Limiter.h"
|
||||
#include "lib/api/services/AdminService.server.pb.h"
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
#include "lib/config/config.h"
|
||||
#include <memory>
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
void add_account_prod_pos_limit(bts::api::admin::AdminData& admin_data,
|
||||
const std::string& account_name,
|
||||
void add_account_prod_pos_limit(bts::api::admin::AdminData &admin_data,
|
||||
const std::string &account_name,
|
||||
api::Venue prod_exchange,
|
||||
api::SecurityType prod_type,
|
||||
const std::string& prod_symbol,
|
||||
const std::string &prod_symbol,
|
||||
uint32_t long_pos_limit,
|
||||
uint32_t short_pos_limit){
|
||||
auto account_ptr = bts::admin::AdminServiceUtils::find_account(admin_data, account_name);
|
||||
REQUIRE(account_ptr!=nullptr);
|
||||
uint32_t short_pos_limit) {
|
||||
auto account_ptr =
|
||||
bts::admin::AdminServiceUtils::find_account(admin_data, account_name);
|
||||
REQUIRE(account_ptr != nullptr);
|
||||
bts::api::admin::ProductScope prod_scope;
|
||||
prod_scope.set_exchange(prod_exchange);
|
||||
prod_scope.set_security_type(prod_type);
|
||||
prod_scope.set_security_symbol(prod_symbol);
|
||||
auto new_prod_pos_limit_ptr = account_ptr->mutable_product_position_limits()->Add();
|
||||
prod_scope.set_security_symbol(prod_symbol);
|
||||
auto new_prod_pos_limit_ptr =
|
||||
account_ptr->mutable_product_position_limits()->Add();
|
||||
new_prod_pos_limit_ptr->mutable_product()->CopyFrom(prod_scope);
|
||||
new_prod_pos_limit_ptr->set_long_position_limit(long_pos_limit);
|
||||
new_prod_pos_limit_ptr->set_short_position_limit(short_pos_limit);
|
||||
}
|
||||
|
||||
void add_account_security_pos_limit(bts::api::admin::AdminData& admin_data,
|
||||
const std::string& account_name,
|
||||
void add_account_security_pos_limit(bts::api::admin::AdminData &admin_data,
|
||||
const std::string &account_name,
|
||||
api::Venue sec_exchange,
|
||||
api::SecurityType sec_type,
|
||||
const std::string& sec_symbol,
|
||||
uint32_t sec_id, //0 for unset
|
||||
const std::string &sec_symbol,
|
||||
uint32_t sec_id, // 0 for unset
|
||||
uint32_t long_pos_limit,
|
||||
uint32_t short_pos_limit){
|
||||
auto account_ptr = bts::admin::AdminServiceUtils::find_account(admin_data, account_name);
|
||||
REQUIRE(account_ptr!=nullptr);
|
||||
uint32_t short_pos_limit) {
|
||||
auto account_ptr =
|
||||
bts::admin::AdminServiceUtils::find_account(admin_data, account_name);
|
||||
REQUIRE(account_ptr != nullptr);
|
||||
bts::api::admin::SecurityScope sec_scope;
|
||||
sec_scope.set_exchange(sec_exchange);
|
||||
sec_scope.set_security_type(sec_type);
|
||||
sec_scope.set_security_symbol(sec_symbol);
|
||||
if(sec_id != 0)
|
||||
if (sec_id != 0)
|
||||
sec_scope.set_security_id(sec_id);
|
||||
auto new_sec_pos_limit_ptr = account_ptr->mutable_security_position_limits()->Add();
|
||||
auto new_sec_pos_limit_ptr =
|
||||
account_ptr->mutable_security_position_limits()->Add();
|
||||
new_sec_pos_limit_ptr->mutable_security()->CopyFrom(sec_scope);
|
||||
new_sec_pos_limit_ptr->set_long_position_limit(long_pos_limit);
|
||||
new_sec_pos_limit_ptr->set_short_position_limit(short_pos_limit);
|
||||
}
|
||||
|
||||
void add_account_sec_pos_limit(bts::api::admin::BTSAccount& account,
|
||||
api::Venue exchange, api::SecurityType sec_type, const std::string& sec_symbol, uint32_t sec_id, bool sec_id_set,
|
||||
uint32_t long_pos_limit, uint32_t short_pos_limit){
|
||||
void add_account_sec_pos_limit(bts::api::admin::BTSAccount &account,
|
||||
api::Venue exchange, api::SecurityType sec_type,
|
||||
const std::string &sec_symbol, uint32_t sec_id,
|
||||
bool sec_id_set, uint32_t long_pos_limit,
|
||||
uint32_t short_pos_limit) {
|
||||
bts::api::admin::SecurityScope scope;
|
||||
scope.set_exchange(exchange); scope.set_security_type(sec_type); scope.set_security_symbol(sec_symbol);
|
||||
scope.set_exchange(exchange);
|
||||
scope.set_security_type(sec_type);
|
||||
scope.set_security_symbol(sec_symbol);
|
||||
if (sec_id_set)
|
||||
scope.set_security_id(sec_id);
|
||||
auto new_limit = account.add_security_position_limits();
|
||||
@@ -66,11 +74,14 @@ void add_account_sec_pos_limit(bts::api::admin::BTSAccount& account,
|
||||
new_limit->set_short_position_limit(short_pos_limit);
|
||||
}
|
||||
|
||||
void add_account_sec_clip_size(bts::api::admin::BTSAccount& account,
|
||||
api::Venue exchange, api::SecurityType sec_type, const std::string& sec_symbol, uint32_t sec_id, bool sec_id_set,
|
||||
uint32_t clip_size){
|
||||
void add_account_sec_clip_size(bts::api::admin::BTSAccount &account,
|
||||
api::Venue exchange, api::SecurityType sec_type,
|
||||
const std::string &sec_symbol, uint32_t sec_id,
|
||||
bool sec_id_set, uint32_t clip_size) {
|
||||
bts::api::admin::SecurityScope scope;
|
||||
scope.set_exchange(exchange); scope.set_security_type(sec_type); scope.set_security_symbol(sec_symbol);
|
||||
scope.set_exchange(exchange);
|
||||
scope.set_security_type(sec_type);
|
||||
scope.set_security_symbol(sec_symbol);
|
||||
if (sec_id_set)
|
||||
scope.set_security_id(sec_id);
|
||||
auto new_limit = account.mutable_security_clip_limits()->add_limit();
|
||||
@@ -78,69 +89,95 @@ void add_account_sec_clip_size(bts::api::admin::BTSAccount& account,
|
||||
new_limit->set_clip_size(clip_size);
|
||||
}
|
||||
|
||||
void fill_future_security (api::Security& sec,
|
||||
uint32_t key, const std::string& symbol, api::Venue exchange,
|
||||
uint32_t expiration_year, uint32_t expiration_month, uint32_t expiration_day,
|
||||
double tick_size)
|
||||
{
|
||||
sec.set_id (key);
|
||||
sec.set_symbol (symbol);
|
||||
sec.set_exchange (exchange);
|
||||
void fill_future_security(api::Security &sec, uint32_t key,
|
||||
const std::string &symbol, api::Venue exchange,
|
||||
uint32_t expiration_year, uint32_t expiration_month,
|
||||
uint32_t expiration_day, double tick_size) {
|
||||
sec.set_id(key);
|
||||
sec.set_symbol(symbol);
|
||||
sec.set_exchange(exchange);
|
||||
api::FutureContract fc;
|
||||
fc.set_expiration_year(expiration_year);
|
||||
fc.set_expiration_month(expiration_month);
|
||||
fc.set_expiration_day(expiration_day);
|
||||
sec.mutable_future()->MergeFrom (fc);
|
||||
sec.mutable_future()->MergeFrom(fc);
|
||||
api::ContractSpecification c_spec;
|
||||
c_spec.set_low_tick (tick_size);
|
||||
c_spec.set_high_tick (tick_size);
|
||||
sec.mutable_contract_spec()->MergeFrom (c_spec);
|
||||
c_spec.set_low_tick(tick_size);
|
||||
c_spec.set_high_tick(tick_size);
|
||||
sec.mutable_contract_spec()->MergeFrom(c_spec);
|
||||
}
|
||||
|
||||
void verify_security_limit_data(SecurityLimitNode const* acc_sec_state,
|
||||
uint32_t expected_long_limit, uint32_t expected_short_limit,
|
||||
int expected_net_pos){
|
||||
void verify_security_limit_data(SecurityLimitNode const *acc_sec_state,
|
||||
uint32_t expected_long_limit,
|
||||
uint32_t expected_short_limit,
|
||||
int expected_net_pos) {
|
||||
REQUIRE(acc_sec_state != nullptr);
|
||||
REQUIRE(acc_sec_state->test_only_func_limit().long_position_limit() == expected_long_limit);
|
||||
REQUIRE(acc_sec_state->test_only_func_limit().short_position_limit() == expected_short_limit);
|
||||
REQUIRE(acc_sec_state->test_only_func_limit().long_position_limit() ==
|
||||
expected_long_limit);
|
||||
REQUIRE(acc_sec_state->test_only_func_limit().short_position_limit() ==
|
||||
expected_short_limit);
|
||||
REQUIRE(acc_sec_state->net_pos() == expected_net_pos);
|
||||
}
|
||||
|
||||
void verify_product_limit_data(ProductLimitNode const* acc_prod_state,
|
||||
uint32_t expected_long_limit, uint32_t expected_short_limit,
|
||||
int expected_net_pos){
|
||||
void verify_product_limit_data(ProductLimitNode const *acc_prod_state,
|
||||
uint32_t expected_long_limit,
|
||||
uint32_t expected_short_limit,
|
||||
int expected_net_pos) {
|
||||
REQUIRE(acc_prod_state != nullptr);
|
||||
REQUIRE(acc_prod_state->test_only_func_limit().long_position_limit() == expected_long_limit);
|
||||
REQUIRE(acc_prod_state->test_only_func_limit().short_position_limit() == expected_short_limit);
|
||||
REQUIRE(acc_prod_state->test_only_func_limit().long_position_limit() ==
|
||||
expected_long_limit);
|
||||
REQUIRE(acc_prod_state->test_only_func_limit().short_position_limit() ==
|
||||
expected_short_limit);
|
||||
REQUIRE(acc_prod_state->net_pos() == expected_net_pos);
|
||||
}
|
||||
|
||||
|
||||
// (bts::api::admin::BTSAccount& account,
|
||||
// api::Venue exchange, api::SecurityType sec_type, const std::string& sec_symbol, uint32_t sec_id, bool sec_id_set,
|
||||
// uint32_t long_pos_limit, uint32_t short_pos_limit){
|
||||
// api::Venue exchange, api::SecurityType
|
||||
// sec_type, const std::string& sec_symbol,
|
||||
// uint32_t sec_id, bool sec_id_set, uint32_t
|
||||
// long_pos_limit, uint32_t short_pos_limit){
|
||||
|
||||
TEST_CASE ("Limit Rule Res", "[limiter]"){
|
||||
TEST_CASE("Limit Rule Res", "[limiter]") {
|
||||
api::Security sec_1, sec_2, sec_3;
|
||||
fill_future_security(sec_1, 1000, std::string("ES"), api::Venue::VENUE_GLBX, 2020, 1, 1, 0.25);
|
||||
fill_future_security(sec_2, 2000, std::string("SI"), api::Venue::VENUE_GLBX, 2021, 1, 1, 0.05);
|
||||
fill_future_security(sec_3, 3000, std::string("SI"), api::Venue::VENUE_GLBX, 2022, 1, 1, 0.05);
|
||||
fill_future_security(sec_1, 1000, std::string("ES"), api::Venue::VENUE_GLBX,
|
||||
2020, 1, 1, 0.25);
|
||||
fill_future_security(sec_2, 2000, std::string("SI"), api::Venue::VENUE_GLBX,
|
||||
2021, 1, 1, 0.05);
|
||||
fill_future_security(sec_3, 3000, std::string("SI"), api::Venue::VENUE_GLBX,
|
||||
2022, 1, 1, 0.05);
|
||||
// add multiple rules and check sorting
|
||||
bts::api::admin::AdminData admin_data;
|
||||
bts::api::admin::AdminServiceCreateAccountResult result;
|
||||
bts::admin::AdminServiceUtils::create_account(admin_data, 0, "ACC A1", "desc", true, result);
|
||||
auto account_ptr = bts::admin::AdminServiceUtils::find_account(admin_data, "ACC A1");
|
||||
add_account_sec_pos_limit(*account_ptr, api::Venue::VENUE_GLBX, api::SecurityType::SECURITYTYPE_FUTURE, "*", 0, false, 10, 21);
|
||||
add_account_sec_pos_limit(*account_ptr, api::Venue::VENUE_GLBX, api::SecurityType::SECURITYTYPE_FUTURE, "ES", 1000, true, 1000, 2001);
|
||||
add_account_sec_pos_limit(*account_ptr, api::Venue::VENUE_GLBX, api::SecurityType::SECURITYTYPE_FUTURE, "ES", 0, false, 100, 201);
|
||||
bts::admin::AdminServiceUtils::create_account(admin_data, 0, "ACC A1",
|
||||
"desc", true, result);
|
||||
auto account_ptr =
|
||||
bts::admin::AdminServiceUtils::find_account(admin_data, "ACC A1");
|
||||
add_account_sec_pos_limit(*account_ptr, api::Venue::VENUE_GLBX,
|
||||
api::SecurityType::SECURITYTYPE_FUTURE, "*", 0,
|
||||
false, 10, 21);
|
||||
add_account_sec_pos_limit(*account_ptr, api::Venue::VENUE_GLBX,
|
||||
api::SecurityType::SECURITYTYPE_FUTURE, "ES", 1000,
|
||||
true, 1000, 2001);
|
||||
add_account_sec_pos_limit(*account_ptr, api::Venue::VENUE_GLBX,
|
||||
api::SecurityType::SECURITYTYPE_FUTURE, "ES", 0,
|
||||
false, 100, 201);
|
||||
// add clip sizes
|
||||
add_account_sec_clip_size(*account_ptr, api::Venue::VENUE_GLBX, api::SecurityType::SECURITYTYPE_FUTURE, "SI", 0, false, 11);
|
||||
add_account_sec_clip_size(*account_ptr, api::Venue::VENUE_GLBX, api::SecurityType::SECURITYTYPE_FUTURE, "ES", sec_1.id(), true, 1001);
|
||||
add_account_sec_clip_size(*account_ptr, api::Venue::VENUE_GLBX, api::SecurityType::SECURITYTYPE_FUTURE, "SI", sec_2.id(), true, 101);
|
||||
add_account_sec_clip_size(*account_ptr, api::Venue::VENUE_GLBX,
|
||||
api::SecurityType::SECURITYTYPE_FUTURE, "SI", 0,
|
||||
false, 11);
|
||||
add_account_sec_clip_size(*account_ptr, api::Venue::VENUE_GLBX,
|
||||
api::SecurityType::SECURITYTYPE_FUTURE, "ES",
|
||||
sec_1.id(), true, 1001);
|
||||
add_account_sec_clip_size(*account_ptr, api::Venue::VENUE_GLBX,
|
||||
api::SecurityType::SECURITYTYPE_FUTURE, "SI",
|
||||
sec_2.id(), true, 101);
|
||||
// validate pos limits went in
|
||||
REQUIRE(account_ptr->security_position_limits(0).long_position_limit() == 10);
|
||||
REQUIRE(account_ptr->security_position_limits(1).long_position_limit() == 1000);
|
||||
REQUIRE(account_ptr->security_position_limits(2).long_position_limit() == 100);
|
||||
REQUIRE(account_ptr->security_position_limits(0).long_position_limit() ==
|
||||
10);
|
||||
REQUIRE(account_ptr->security_position_limits(1).long_position_limit() ==
|
||||
1000);
|
||||
REQUIRE(account_ptr->security_position_limits(2).long_position_limit() ==
|
||||
100);
|
||||
// validate clip sizes went in
|
||||
REQUIRE(account_ptr->security_clip_limits().limit(0).clip_size() == 11);
|
||||
REQUIRE(account_ptr->security_clip_limits().limit(1).clip_size() == 1001);
|
||||
@@ -148,22 +185,36 @@ TEST_CASE ("Limit Rule Res", "[limiter]"){
|
||||
// sort order
|
||||
bts::admin::AdminServiceUtils::sort_limits(*account_ptr);
|
||||
// validate pos limits order
|
||||
REQUIRE(account_ptr->security_position_limits(0).long_position_limit() == 1000);
|
||||
REQUIRE(account_ptr->security_position_limits(1).long_position_limit() == 100);
|
||||
REQUIRE(account_ptr->security_position_limits(2).long_position_limit() == 10);
|
||||
REQUIRE(account_ptr->security_position_limits(0).long_position_limit() ==
|
||||
1000);
|
||||
REQUIRE(account_ptr->security_position_limits(1).long_position_limit() ==
|
||||
100);
|
||||
REQUIRE(account_ptr->security_position_limits(2).long_position_limit() ==
|
||||
10);
|
||||
// validate clip sizes order
|
||||
REQUIRE(account_ptr->security_clip_limits().limit(2).clip_size() == 11);
|
||||
// resolve pos
|
||||
auto sec_1_limit = bts::admin::AdminServiceUtils::find_account_security_position_limit (*account_ptr, sec_1);
|
||||
// resolve pos
|
||||
auto sec_1_limit =
|
||||
bts::admin::AdminServiceUtils::find_account_security_position_limit(
|
||||
*account_ptr, sec_1);
|
||||
REQUIRE(sec_1_limit->long_position_limit() == 1000);
|
||||
auto sec_2_limit = bts::admin::AdminServiceUtils::find_account_security_position_limit (*account_ptr, sec_2);
|
||||
auto sec_2_limit =
|
||||
bts::admin::AdminServiceUtils::find_account_security_position_limit(
|
||||
*account_ptr, sec_2);
|
||||
REQUIRE(sec_2_limit->long_position_limit() == 10);
|
||||
// resolve clip sizes
|
||||
auto sec_1_clip = bts::admin::AdminServiceUtils::find_account_security_clip_size (*account_ptr, sec_1);
|
||||
auto sec_1_clip =
|
||||
bts::admin::AdminServiceUtils::find_account_security_clip_size(
|
||||
*account_ptr, sec_1);
|
||||
REQUIRE(sec_1_clip == 1001);
|
||||
auto sec_2_clip = bts::admin::AdminServiceUtils::find_account_security_clip_size (*account_ptr, sec_2);
|
||||
auto sec_2_clip =
|
||||
bts::admin::AdminServiceUtils::find_account_security_clip_size(
|
||||
*account_ptr, sec_2);
|
||||
REQUIRE(sec_2_clip == 101);
|
||||
auto sec_3_clip = bts::admin::AdminServiceUtils::find_account_security_clip_size (*account_ptr, sec_3);
|
||||
auto sec_3_clip =
|
||||
bts::admin::AdminServiceUtils::find_account_security_clip_size(
|
||||
*account_ptr, sec_3);
|
||||
REQUIRE(sec_3_clip == 11);
|
||||
}
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -40,6 +40,5 @@ DynamicOrderRegistry::get_all_dynamic_order_types() const {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -17,8 +17,8 @@
|
||||
#include "lib/orderman/orders/book_algos/CoverOrder.h"
|
||||
#include "lib/orderman/orders/book_algos/JoinOrder.h"
|
||||
#include "lib/orderman/orders/book_algos/LeanOrder.h"
|
||||
#include "lib/orderman/orders/book_algos/TickOrder.h"
|
||||
#include "lib/orderman/orders/book_algos/OffsetOrder.h"
|
||||
#include "lib/orderman/orders/book_algos/TickOrder.h"
|
||||
#include "lib/orderman/orders/book_algos/TopOrder.h"
|
||||
#include "lib/orderman/orders/spreader_orders/SpreaderOrder.h"
|
||||
|
||||
@@ -153,7 +153,7 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk,
|
||||
break;
|
||||
case CustomOrderDefinition::kOffset:
|
||||
order_ptr.reset(new orderman::OffsetOrder());
|
||||
break;
|
||||
break;
|
||||
case CustomOrderDefinition::kDynamic: {
|
||||
order_ptr = me->dynamic_order_registry.build(
|
||||
def.custom_def().dynamic().dynamic_order_type());
|
||||
|
||||
@@ -66,5 +66,5 @@ bool OrderRouter::is_valid_session(std::string const &session) const {
|
||||
}
|
||||
|
||||
OrderRouter::~OrderRouter() {}
|
||||
}
|
||||
}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -13,27 +13,50 @@ struct EventStream
|
||||
using CS = mercury::ContentSubscription<EV,K>;
|
||||
using SubID = typename CS::SubID;
|
||||
|
||||
EventStream (CS&cs) : cs (cs) {}
|
||||
EventStream () : cs (nullptr) {}
|
||||
EventStream (CS&cs) : cs (&cs) {}
|
||||
|
||||
void init(CS&csub)
|
||||
{
|
||||
bts_assert(!cs);
|
||||
cs = ⫏
|
||||
}
|
||||
|
||||
template <class CB>
|
||||
SubID sub(typename CS::Key k, CB&&cb) {
|
||||
auto s = cs.sub(k,std::forward<CB> (cb));
|
||||
auto s = cs->sub(k,std::forward<CB> (cb));
|
||||
active_subs.push_back(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
template <class CB>
|
||||
SubID sub(CB&&cb) {
|
||||
auto s = cs.sub(std::forward<CB> (cb));
|
||||
auto s = cs->sub(std::forward<CB> (cb));
|
||||
active_subs.push_back(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
EventStream& operator=(EventStream &&src)
|
||||
{
|
||||
cs = src.cs;
|
||||
src.cs = nullptr;
|
||||
active_subs = src.active_subs;
|
||||
src.active_subs.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
EventStream& operator=(EventStream const &src)
|
||||
{
|
||||
cs = src.cs;
|
||||
active_subs = src.active_subs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void unsub(SubID sid) {
|
||||
auto it = std::find(active_subs.begin(), active_subs.end(),sid);
|
||||
if (it != active_subs.end()) {
|
||||
active_subs.erase(it);
|
||||
cs.unsub(sid);
|
||||
cs->unsub(sid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +69,7 @@ struct EventStream
|
||||
}
|
||||
|
||||
private:
|
||||
CS &cs;
|
||||
CS *cs;
|
||||
boost::container::small_vector<SubID,10> active_subs;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
|
||||
namespace bts {
|
||||
|
||||
class PerfTrace {
|
||||
public:
|
||||
utils::TimeStamp do_work_begin;
|
||||
utils::TimeStamp fix_socket_r;
|
||||
utils::TimeStamp md ;
|
||||
utils::TimeStamp md_src ;
|
||||
utils::TimeStamp spreader_on_book ;
|
||||
utils::TimeStamp spreader_on_child_order_update ;
|
||||
utils::TimeStamp order_update ;
|
||||
utils::TimeStamp exe_gw ;
|
||||
utils::TimeStamp fix_socket_w ;
|
||||
utils::TimeStamp spreader_modify_order ;
|
||||
utils::TimeStamp send_fix_buffer ;
|
||||
uint32_t order_key = 0;
|
||||
uint16_t md_sn = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,541 +0,0 @@
|
||||
#include "PerfTraceLog.h"
|
||||
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/system.pb.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTrace.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTraceLog.h"
|
||||
#include "lib/persistence/persistence_utils.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/utils/file_utils.h"
|
||||
#include "lib/utils/stats.h"
|
||||
|
||||
#include <SQLiteCpp/SQLiteCpp.h>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/range/adaptor/reversed.hpp>
|
||||
#include <fmt/format.h>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <tbb/concurrent_queue.h>
|
||||
|
||||
namespace bts {
|
||||
|
||||
auto current_schema_version = 3;
|
||||
|
||||
auto insert_trace = "insert into trace ("
|
||||
"version,"
|
||||
"host,"
|
||||
"build_type,"
|
||||
"session_start,"
|
||||
"trace_time,"
|
||||
"do_work_begin,"
|
||||
"fix_socket_r,"
|
||||
"md,"
|
||||
"md_src,"
|
||||
"spreader_on_book,"
|
||||
"spreader_on_child_order_update,"
|
||||
"order_update,"
|
||||
"exe_gw,"
|
||||
"fix_socket_w,"
|
||||
"spreader_modify_order,"
|
||||
"send_fix_buffer,"
|
||||
"order_key,"
|
||||
"md_sn"
|
||||
") values ("
|
||||
":version,"
|
||||
":host,"
|
||||
":build_type,"
|
||||
":session_start,"
|
||||
":trace_time,"
|
||||
":do_work_begin,"
|
||||
":fix_socket_r,"
|
||||
":md,"
|
||||
":md_src,"
|
||||
":spreader_on_book,"
|
||||
":spreader_on_child_order_update,"
|
||||
":order_update,"
|
||||
":exe_gw,"
|
||||
":fix_socket_w,"
|
||||
":spreader_modify_order,"
|
||||
":send_fix_buffer,"
|
||||
":order_key,"
|
||||
":md_sn"
|
||||
")";
|
||||
|
||||
auto create_trace_table = "drop table if exists trace;create table trace ("
|
||||
"version TEXT, "
|
||||
"host TEXT, "
|
||||
"build_type TEXT, "
|
||||
"session_start TEXT,"
|
||||
"trace_time TEXT,"
|
||||
"do_work_begin INTEGER,"
|
||||
"fix_socket_r INTEGER,"
|
||||
"md INTEGER,"
|
||||
"md_src INTEGER,"
|
||||
"spreader_on_book INTEGER,"
|
||||
"spreader_on_child_order_update INTEGER,"
|
||||
"order_update INTEGER,"
|
||||
"exe_gw INTEGER,"
|
||||
"fix_socket_w INTEGER,"
|
||||
"spreader_modify_order INTEGER,"
|
||||
"send_fix_buffer INTEGER,"
|
||||
"order_key INTEGER,"
|
||||
"md_sn INTEGER );";
|
||||
|
||||
auto create_schema_version_table =
|
||||
"create table schema_version(version INTEGER); insert into "
|
||||
"schema_version(version) values(0);";
|
||||
|
||||
auto query_schema_version = "select version from schema_version limit 1";
|
||||
auto delete_version_record = "delete from schema_version;";
|
||||
auto update_version = "insert into schema_version(version) values(:version);";
|
||||
|
||||
auto requote_trace_view = "drop view if exists requote_trace; "
|
||||
"create view requote_trace as select "
|
||||
"version, "
|
||||
"host, "
|
||||
"build_type, "
|
||||
"session_start,"
|
||||
"trace_time,"
|
||||
"order_key,"
|
||||
"(fix_socket_w-md) / 1000.0 total,"
|
||||
"0 md,"
|
||||
"0 spreader,"
|
||||
"0 fix "
|
||||
"from "
|
||||
"trace "
|
||||
"where md<>0 and (md_src-md)<1000000";
|
||||
|
||||
auto hedge_trace_view =
|
||||
"drop view if exists hedge_trace; "
|
||||
"CREATE VIEW hedge_trace as "
|
||||
"select version, host, build_type, session_start,trace_time,order_key, "
|
||||
"(fix_socket_w-fix_socket_r) / 1000.0 total, "
|
||||
"0 fix_in, "
|
||||
"0 spreader, "
|
||||
"0 fix_out "
|
||||
"from trace where fix_socket_r<>0 ";
|
||||
|
||||
auto requote_query =
|
||||
"select * from requote_trace order by trace_time desc limit ?";
|
||||
|
||||
auto hedge_query = "select * from hedge_trace order by trace_time desc limit ?";
|
||||
|
||||
struct PerfTraceLog::PImpl {
|
||||
PImpl(std::string const &loc)
|
||||
: db(loc, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE),
|
||||
insert_statement(), pending_traces() {}
|
||||
SQLite::Database db;
|
||||
std::unique_ptr<SQLite::Statement> insert_statement;
|
||||
std::unique_ptr<SQLite::Statement> requote_query_statement;
|
||||
std::unique_ptr<SQLite::Statement> hedge_query_statement;
|
||||
tbb::concurrent_queue<PerfTrace> pending_traces;
|
||||
std::string session_start;
|
||||
std::mutex lock;
|
||||
};
|
||||
|
||||
PerfTraceLog::PerfTraceLog() : me() {
|
||||
auto loc = init_storage_location();
|
||||
try {
|
||||
me = std::make_unique<PImpl>(loc);
|
||||
if (!me->db.tableExists("schema_version")) {
|
||||
LOG(persist, Warn, "{} database does not exist yet. Creating it ",
|
||||
std::string(me->db.getFilename()));
|
||||
me->db.exec(create_schema_version_table);
|
||||
}
|
||||
|
||||
if (schema_out_of_date()) {
|
||||
me->db.exec(create_trace_table);
|
||||
me->db.exec(requote_trace_view);
|
||||
me->db.exec(hedge_trace_view);
|
||||
|
||||
SQLite::Statement(me->db, delete_version_record).exec();
|
||||
SQLite::Statement update_version_statement(me->db, update_version);
|
||||
update_version_statement.bind(":version", current_schema_version);
|
||||
update_version_statement.exec();
|
||||
}
|
||||
|
||||
me->insert_statement =
|
||||
std::make_unique<SQLite::Statement>(me->db, insert_trace);
|
||||
me->requote_query_statement =
|
||||
std::make_unique<SQLite::Statement>(me->db, requote_query);
|
||||
me->hedge_query_statement =
|
||||
std::make_unique<SQLite::Statement>(me->db, hedge_query);
|
||||
me->session_start = datetime::FIX_timestamp(datetime::local_now());
|
||||
} catch (SQLite::Exception const &e) {
|
||||
auto msg = fmt::format("Unable to create {}. Error: {}", loc,
|
||||
std::string(e.getErrorStr()));
|
||||
bts_assert_msg(false, msg);
|
||||
}
|
||||
}
|
||||
|
||||
PerfTraceLog::~PerfTraceLog() {}
|
||||
|
||||
std::string PerfTraceLog::init_storage_location() {
|
||||
auto loc = persistence::storage_location("perf_trace");
|
||||
auto parent = boost::filesystem::path(loc).parent_path();
|
||||
|
||||
if (!boost::filesystem::is_directory(parent))
|
||||
bts_assert(boost::filesystem::create_directories(parent));
|
||||
return loc;
|
||||
}
|
||||
|
||||
void PerfTraceLog::log(const PerfTrace &trace) {
|
||||
me->pending_traces.push(trace);
|
||||
}
|
||||
|
||||
int PerfTraceLog::persist_traces() {
|
||||
std::vector<PerfTrace> traces;
|
||||
PerfTrace trace;
|
||||
while (me->pending_traces.try_pop(trace))
|
||||
traces.push_back(trace);
|
||||
store(traces);
|
||||
return traces.size();
|
||||
}
|
||||
|
||||
void PerfTraceLog::store(std::vector<PerfTrace> const &traces) {
|
||||
std::lock_guard<std::mutex> l(me->lock);
|
||||
try {
|
||||
for (auto const &t : traces) {
|
||||
me->insert_statement->bind(":version",
|
||||
config::get<config::System>().version());
|
||||
me->insert_statement->bind(":host",
|
||||
config::get<config::System>().host_name());
|
||||
#ifdef DEBUG
|
||||
me->insert_statement->bind(":build_type", "debug");
|
||||
#else
|
||||
me->insert_statement->bind(":build_type", "release");
|
||||
#endif
|
||||
me->insert_statement->bind(":session_start", me->session_start);
|
||||
if (t.md_src.to_uint64())
|
||||
me->insert_statement->bind(
|
||||
":trace_time", datetime::FIX_timestamp(
|
||||
datetime::timestamp_to_ptime(t.md_src)));
|
||||
else
|
||||
me->insert_statement->bind(
|
||||
":trace_time",
|
||||
datetime::FIX_timestamp(
|
||||
datetime::timestamp_to_ptime(t.fix_socket_r)));
|
||||
|
||||
me->insert_statement->bind(":do_work_begin",
|
||||
(long long)t.do_work_begin.to_uint64());
|
||||
me->insert_statement->bind(":fix_socket_r",
|
||||
(long long)t.fix_socket_r.to_uint64());
|
||||
me->insert_statement->bind(":md", (long long)t.md.to_uint64());
|
||||
me->insert_statement->bind(":md_src", (long long)t.md_src.to_uint64());
|
||||
me->insert_statement->bind(":spreader_on_book",
|
||||
(long long)t.spreader_on_book.to_uint64());
|
||||
me->insert_statement->bind(
|
||||
":spreader_on_child_order_update",
|
||||
(long long)t.spreader_on_child_order_update.to_uint64());
|
||||
me->insert_statement->bind(":order_update",
|
||||
(long long)t.order_update.to_uint64());
|
||||
me->insert_statement->bind(":exe_gw", (long long)t.exe_gw.to_uint64());
|
||||
me->insert_statement->bind(":fix_socket_w",
|
||||
(long long)t.fix_socket_w.to_uint64());
|
||||
me->insert_statement->bind(
|
||||
":spreader_modify_order",
|
||||
(long long)t.spreader_modify_order.to_uint64());
|
||||
me->insert_statement->bind(":send_fix_buffer",
|
||||
(long long)t.send_fix_buffer.to_uint64());
|
||||
me->insert_statement->bind(":order_key", (long long)t.order_key);
|
||||
me->insert_statement->bind(":md_sn", (long long)t.md_sn);
|
||||
me->insert_statement->exec();
|
||||
me->insert_statement->reset();
|
||||
}
|
||||
} catch (SQLite::Exception const &e) {
|
||||
auto msg = fmt::format("Unable to insert {}. Error: {}",
|
||||
std::string(me->db.getFilename()),
|
||||
std::string(e.getErrorStr()));
|
||||
bts_assert_msg(false, msg);
|
||||
}
|
||||
}
|
||||
|
||||
bool PerfTraceLog::schema_out_of_date() {
|
||||
try {
|
||||
SQLite::Statement query_schema_version_statement(me->db,
|
||||
query_schema_version);
|
||||
|
||||
while (query_schema_version_statement.executeStep()) {
|
||||
int version = query_schema_version_statement.getColumn("version");
|
||||
return version != current_schema_version;
|
||||
}
|
||||
} catch (SQLite::Exception const &e) {
|
||||
auto msg = fmt::format("Unable to query schema version in {}. Error: {}",
|
||||
std::string(me->db.getFilename()),
|
||||
std::string(e.getErrorStr()));
|
||||
bts_assert_msg(false, msg);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string encode_string(SQLite::Column const &c) {
|
||||
return fmt::format("\"{}\"", std::string(c));
|
||||
}
|
||||
|
||||
std::string encode_number(SQLite::Column const &c) {
|
||||
return fmt::format("{}", (long long)c);
|
||||
}
|
||||
|
||||
std::vector<std::map<std::string, std::string>>
|
||||
PerfTraceLog::get_latest_requote_traces(size_t n) {
|
||||
std::vector<std::map<std::string, std::string>> result;
|
||||
|
||||
std::lock_guard<std::mutex> l(me->lock);
|
||||
try {
|
||||
me->requote_query_statement->bind(1, (long long)n);
|
||||
|
||||
while (me->requote_query_statement->executeStep()) {
|
||||
auto &row = result.emplace_back();
|
||||
row["version"] =
|
||||
encode_string(me->requote_query_statement->getColumn("version"));
|
||||
row["host"] =
|
||||
encode_string(me->requote_query_statement->getColumn("host"));
|
||||
row["build_type"] = encode_string(
|
||||
me->requote_query_statement->getColumn("build_type"));
|
||||
row["session_start"] = encode_string(
|
||||
me->requote_query_statement->getColumn("session_start"));
|
||||
row["trace_time"] = encode_string(
|
||||
me->requote_query_statement->getColumn("trace_time"));
|
||||
|
||||
row["order_key"] =
|
||||
encode_number(me->requote_query_statement->getColumn("order_key"));
|
||||
row["total"] =
|
||||
encode_number(me->requote_query_statement->getColumn("total"));
|
||||
row["md"] =
|
||||
encode_number(me->requote_query_statement->getColumn("md"));
|
||||
row["spreader"] =
|
||||
encode_number(me->requote_query_statement->getColumn("spreader"));
|
||||
row["fix"] =
|
||||
encode_number(me->requote_query_statement->getColumn("fix"));
|
||||
}
|
||||
me->requote_query_statement->reset();
|
||||
} catch (SQLite::Exception const &e) {
|
||||
auto msg = fmt::format("Unable to quote_traces {}. Error: {}",
|
||||
std::string(me->db.getFilename()),
|
||||
std::string(e.getErrorStr()));
|
||||
bts_assert_msg(false, msg);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::map<std::string, std::string>>
|
||||
PerfTraceLog::get_latest_hedge_traces(size_t n) {
|
||||
std::vector<std::map<std::string, std::string>> result;
|
||||
|
||||
std::lock_guard<std::mutex> l(me->lock);
|
||||
try {
|
||||
me->hedge_query_statement->bind(1, (long long)n);
|
||||
|
||||
while (me->hedge_query_statement->executeStep()) {
|
||||
auto &row = result.emplace_back();
|
||||
row["version"] =
|
||||
encode_string(me->hedge_query_statement->getColumn("version"));
|
||||
row["host"] =
|
||||
encode_string(me->hedge_query_statement->getColumn("host"));
|
||||
row["build_type"] =
|
||||
encode_string(me->hedge_query_statement->getColumn("build_type"));
|
||||
row["session_start"] = encode_string(
|
||||
me->hedge_query_statement->getColumn("session_start"));
|
||||
row["trace_time"] =
|
||||
encode_string(me->hedge_query_statement->getColumn("trace_time"));
|
||||
|
||||
row["order_key"] =
|
||||
encode_number(me->hedge_query_statement->getColumn("order_key"));
|
||||
row["total"] =
|
||||
encode_number(me->hedge_query_statement->getColumn("total"));
|
||||
row["fix_in"] =
|
||||
encode_number(me->hedge_query_statement->getColumn("fix_in"));
|
||||
row["spreader"] =
|
||||
encode_number(me->hedge_query_statement->getColumn("spreader"));
|
||||
row["fix_out"] =
|
||||
encode_number(me->hedge_query_statement->getColumn("fix_out"));
|
||||
}
|
||||
me->hedge_query_statement->reset();
|
||||
} catch (SQLite::Exception const &e) {
|
||||
auto msg = fmt::format("Unable to query hedge_traces. Error: {}",
|
||||
std::string(e.getErrorStr()));
|
||||
bts_assert_msg(false, msg);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
namespace perf_stat {
|
||||
|
||||
using Row = std::map<std::string, std::string>;
|
||||
|
||||
struct Summary {
|
||||
std::string name;
|
||||
double min;
|
||||
double max;
|
||||
double median;
|
||||
double ninenty_nine;
|
||||
double stddev;
|
||||
std::vector<double> values;
|
||||
};
|
||||
|
||||
struct Session {
|
||||
std::string start;
|
||||
std::string host;
|
||||
std::string version;
|
||||
std::string build_type;
|
||||
uint32_t n_rows;
|
||||
std::map<std::string, Summary> summaries;
|
||||
std::vector<Row> rows;
|
||||
};
|
||||
|
||||
} // namespace perf_stat
|
||||
|
||||
std::map<std::string, perf_stat::Session> structure_quote_traces(
|
||||
std::vector<std::map<std::string, std::string>> "es) {
|
||||
|
||||
std::map<std::string, perf_stat::Session> result;
|
||||
|
||||
for (auto &q : quotes) {
|
||||
auto &session = result[q["session_start"]];
|
||||
if (session.start.empty()) {
|
||||
session.start = q["session_start"];
|
||||
session.host = q["host"];
|
||||
session.version = q["version"];
|
||||
session.build_type = q["build_type"];
|
||||
}
|
||||
for (auto st : {"total", "md", "spreader", "fix"}) {
|
||||
auto &summary = session.summaries[st];
|
||||
if (summary.name.empty())
|
||||
summary.name = st;
|
||||
summary.values.push_back(std::stod(q[st]));
|
||||
}
|
||||
session.rows.push_back(q);
|
||||
session.n_rows++;
|
||||
}
|
||||
|
||||
for (auto &session_kvp : result)
|
||||
for (auto &stat_kvp : session_kvp.second.summaries) {
|
||||
auto &summary = stat_kvp.second;
|
||||
summary.min = stats::min(summary.values);
|
||||
summary.max = stats::max(summary.values);
|
||||
summary.median = stats::percentile(summary.values, 0.5);
|
||||
summary.ninenty_nine = stats::percentile(summary.values, 0.99);
|
||||
summary.stddev = stats::stdev(summary.values);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::map<std::string, perf_stat::Session> structure_hedge_traces(
|
||||
std::vector<std::map<std::string, std::string>> "es) {
|
||||
|
||||
std::map<std::string, perf_stat::Session> result;
|
||||
|
||||
for (auto &q : quotes) {
|
||||
auto &session = result[q["session_start"]];
|
||||
if (session.start.empty()) {
|
||||
session.start = q["session_start"];
|
||||
session.host = q["host"];
|
||||
session.version = q["version"];
|
||||
session.build_type = q["build_type"];
|
||||
}
|
||||
for (auto st : {"total", "fix_in", "spreader", "fix_out"}) {
|
||||
auto &summary = session.summaries[st];
|
||||
if (summary.name.empty())
|
||||
summary.name = st;
|
||||
summary.values.push_back(std::stod(q[st]));
|
||||
}
|
||||
session.rows.push_back(q);
|
||||
session.n_rows++;
|
||||
}
|
||||
|
||||
for (auto &session_kvp : result)
|
||||
for (auto &stat_kvp : session_kvp.second.summaries) {
|
||||
auto &summary = stat_kvp.second;
|
||||
summary.min = stats::min(summary.values);
|
||||
summary.max = stats::max(summary.values);
|
||||
summary.median = stats::percentile(summary.values, 0.5);
|
||||
summary.ninenty_nine = stats::percentile(summary.values, 0.99);
|
||||
summary.stddev = stats::stdev(summary.values);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void print_summary(std::ostream &ss, perf_stat::Summary const &s, bool last) {
|
||||
ss << " { "
|
||||
<< "\"value\":"
|
||||
<< " \"" << s.name << "\""
|
||||
<< ", \"min\":" << s.min << ", \"max\":" << s.max
|
||||
<< ", \"median\":" << s.median << ", \"ninenty_nine\":" << s.ninenty_nine
|
||||
<< ", \"stdev\":" << s.stddev;
|
||||
if (!last)
|
||||
ss << " },\n";
|
||||
else
|
||||
ss << " }\n";
|
||||
}
|
||||
|
||||
void print_row(std::ostream &ss, perf_stat::Row const &r, bool last) {
|
||||
ss << " { ";
|
||||
size_t i = 0;
|
||||
for (auto const &kvp : r)
|
||||
if (kvp.first != "version" && kvp.first != "host" &&
|
||||
kvp.first != "build_type" && kvp.first != "session_start") {
|
||||
ss << "\"" << kvp.first << "\":" << kvp.second;
|
||||
if (++i != r.size() - 4)
|
||||
ss << ",";
|
||||
}
|
||||
if (!last)
|
||||
ss << " },\n";
|
||||
else
|
||||
ss << " }\n";
|
||||
}
|
||||
|
||||
void print_session(std::ostream &ss, perf_stat::Session const &s, bool last) {
|
||||
ss << "{\n"
|
||||
<< " \"session_name\": " << s.start << ",\n"
|
||||
<< " \"host\": " << s.host << ",\n"
|
||||
<< " \"version\": " << s.version << ",\n"
|
||||
<< " \"build_type\": " << s.build_type << ",\n"
|
||||
<< " \"n_rows\": " << s.n_rows << ",\n";
|
||||
|
||||
ss << " \"summaries\":\n [\n";
|
||||
size_t i = 0;
|
||||
for (auto &summaries_kvp : s.summaries)
|
||||
print_summary(ss, summaries_kvp.second, ++i == s.summaries.size());
|
||||
ss << " ],\n";
|
||||
|
||||
ss << " \"rows\":\n [\n";
|
||||
i = 0;
|
||||
for (auto &r : s.rows)
|
||||
print_row(ss, r, ++i == s.rows.size());
|
||||
ss << " ]\n";
|
||||
if (!last)
|
||||
ss << "},\n";
|
||||
else
|
||||
ss << "}\n";
|
||||
}
|
||||
|
||||
void PerfTraceLog::print_latest_traces(std::ostream &ss, size_t n) {
|
||||
{
|
||||
auto rows = get_latest_requote_traces(n);
|
||||
auto structured = structure_quote_traces(rows);
|
||||
|
||||
ss << "{\n\"requotes\": [\n";
|
||||
size_t i = 0;
|
||||
for (auto &session_kvp : boost::adaptors::reverse(structured))
|
||||
print_session(ss, session_kvp.second, ++i == structured.size());
|
||||
ss << "],\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto rows = get_latest_hedge_traces(n);
|
||||
auto structured = structure_hedge_traces(rows);
|
||||
|
||||
ss << "\"hedges\": [\n";
|
||||
|
||||
size_t i = 0;
|
||||
for (auto &session_kvp : boost::adaptors::reverse(structured))
|
||||
print_session(ss, session_kvp.second, ++i == structured.size());
|
||||
ss << "]\n}\n";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts
|
||||
@@ -1,34 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
namespace bts {
|
||||
class PerfTrace;
|
||||
|
||||
class PerfTraceLog {
|
||||
public:
|
||||
PerfTraceLog();
|
||||
~PerfTraceLog();
|
||||
void log(const PerfTrace &);
|
||||
|
||||
// returns number of persisted traces
|
||||
int persist_traces();
|
||||
|
||||
// return the N last traces
|
||||
std::vector<std::map<std::string, std::string>> get_latest_requote_traces(size_t n=0);
|
||||
std::vector<std::map<std::string, std::string>> get_latest_hedge_traces(size_t n=0);
|
||||
|
||||
void print_latest_traces(std::ostream &,size_t n = 0);
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
|
||||
std::string init_storage_location();
|
||||
bool schema_out_of_date();
|
||||
void store(std::vector<PerfTrace> const &);
|
||||
};
|
||||
|
||||
} // namespace bts
|
||||
@@ -6,7 +6,6 @@
|
||||
#include <string>
|
||||
|
||||
#include "lib/domain_model/common.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTrace.h"
|
||||
#include "lib/utils/ValueWrapper.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
|
||||
@@ -49,13 +49,13 @@ void DynamicOrder::on_init_recovered(api::orderman::OrderRecord const &rec,
|
||||
}
|
||||
|
||||
void DynamicOrder::on_activate(Price, Quantity, OrderServices &services,
|
||||
PerfTrace const &) {
|
||||
trace::CorID const &) {
|
||||
services.report_state(me->state);
|
||||
}
|
||||
|
||||
void DynamicOrder::on_update(Price, Quantity, PerfTrace const &) {}
|
||||
void DynamicOrder::on_crack(PerfTrace const &) {}
|
||||
void DynamicOrder::on_cancel(PerfTrace const &) {}
|
||||
void DynamicOrder::on_update(Price, Quantity, trace::CorID const &) {}
|
||||
void DynamicOrder::on_crack(trace::CorID const &) {}
|
||||
void DynamicOrder::on_cancel(trace::CorID const &) {}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -19,10 +19,10 @@ public:
|
||||
const PreActivationOrderServices&) override;
|
||||
void on_init_recovered(api::orderman::OrderRecord const&,
|
||||
const PreActivationOrderServices &) override;
|
||||
void on_activate(Price,Quantity,OrderServices &, PerfTrace const&) override ;
|
||||
void on_update(Price , Quantity,PerfTrace const&) override ;
|
||||
void on_crack(PerfTrace const&) override ;
|
||||
void on_cancel(PerfTrace const&) override ;
|
||||
void on_activate(Price,Quantity,OrderServices &, trace::CorID const&) override ;
|
||||
void on_update(Price , Quantity,trace::CorID const&) override ;
|
||||
void on_crack(trace::CorID const&) override ;
|
||||
void on_cancel(trace::CorID const&) override ;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "IcebergOrder.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/orderman_cfg.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/mercury/events/TimerEvent.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/orderman_cfg.pb.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
@@ -104,7 +104,7 @@ void IcebergOrder::on_init_recovered(api::orderman::OrderRecord const &rec,
|
||||
}
|
||||
|
||||
void IcebergOrder::on_activate(Price p, Quantity q, OrderServices &services,
|
||||
PerfTrace const &) {
|
||||
trace::CorID const &) {
|
||||
bts_assert(!me->services);
|
||||
me->services = &services;
|
||||
me->child_builder.activate(services);
|
||||
@@ -114,10 +114,10 @@ void IcebergOrder::on_activate(Price p, Quantity q, OrderServices &services,
|
||||
me->state.set_price(p.value());
|
||||
me->state.set_quantity(q.value());
|
||||
if (me->pause_between_orders)
|
||||
services.time_change.sub(
|
||||
services.subscribe_to_time(
|
||||
[this](auto const &time) { this->on_time(time); });
|
||||
services.child_updates.sub(
|
||||
key(), [this](auto const &os) { this->on_child_order_update(os); });
|
||||
services.subscribe_to_child_updates(
|
||||
[this](auto const &os) { this->on_child_order_update(os); });
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
me->working_sub_status = WorkingSubStatus::WORKING;
|
||||
send_next_child_order();
|
||||
@@ -145,7 +145,7 @@ void IcebergOrder::on_activate(Price p, Quantity q, OrderServices &services,
|
||||
report_state();
|
||||
}
|
||||
|
||||
void IcebergOrder::on_update(Price p, Quantity q, PerfTrace const &) {
|
||||
void IcebergOrder::on_update(Price p, Quantity q, trace::CorID const &) {
|
||||
switch (me->state.status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
@@ -171,9 +171,9 @@ void IcebergOrder::on_update(Price p, Quantity q, PerfTrace const &) {
|
||||
report_state();
|
||||
}
|
||||
|
||||
void IcebergOrder::on_crack(PerfTrace const &) {}
|
||||
void IcebergOrder::on_crack(trace::CorID const &) {}
|
||||
|
||||
void IcebergOrder::on_cancel(PerfTrace const &) {
|
||||
void IcebergOrder::on_cancel(trace::CorID const &) {
|
||||
switch (me->state.status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
LOG(OrderMan, Info, "Canceling Iceberg Order {}", key().value());
|
||||
@@ -255,7 +255,7 @@ void IcebergOrder::send_next_child_order() {
|
||||
key().value(), me->child.order.key().value(), to_send, me->state.price(),
|
||||
me->state.filled_quantity(), me->state.quantity());
|
||||
|
||||
me->child.order.activate(Price(me->state.price()), Quantity(to_send));
|
||||
me->child.order.activate(Price(me->state.price()), Quantity(to_send), trace::CorID());
|
||||
}
|
||||
|
||||
void IcebergOrder::on_child_order_update(OrderStateUpdateEvent const &ev) {
|
||||
|
||||
@@ -20,10 +20,10 @@ public:
|
||||
const PreActivationOrderServices&) override;
|
||||
void on_init_recovered(api::orderman::OrderRecord const&,
|
||||
const PreActivationOrderServices &) override;
|
||||
void on_activate(Price,Quantity,OrderServices &, PerfTrace const&) override ;
|
||||
void on_update(Price , Quantity,PerfTrace const&) override ;
|
||||
void on_crack(PerfTrace const&) override ;
|
||||
void on_cancel(PerfTrace const&) override ;
|
||||
void on_activate(Price,Quantity,OrderServices &, trace::CorID const&) override ;
|
||||
void on_update(Price , Quantity,trace::CorID const&) override ;
|
||||
void on_crack(trace::CorID const&) override ;
|
||||
void on_cancel(trace::CorID const&) override ;
|
||||
|
||||
private:
|
||||
void report_state();
|
||||
|
||||
@@ -30,7 +30,7 @@ struct NativeOrder::PImpl {
|
||||
PImpl()
|
||||
: services(nullptr), session_services(), sec(nullptr),
|
||||
previous_position_contribution(0), definition(), state(),
|
||||
price_encoding_precision(7),have_exchange_order_id(false) {}
|
||||
price_encoding_precision(7), have_exchange_order_id(false) {}
|
||||
|
||||
OrderServices *services;
|
||||
SessionServices *session_services;
|
||||
@@ -42,7 +42,7 @@ struct NativeOrder::PImpl {
|
||||
OrderState state;
|
||||
int price_encoding_precision;
|
||||
bool have_exchange_order_id;
|
||||
|
||||
|
||||
jetfix::FIXFragment update_encoding;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::Price> update_price_bm;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::OrderQty> update_quantity_bm;
|
||||
@@ -274,14 +274,10 @@ NativeOrder::~NativeOrder() {}
|
||||
|
||||
void NativeOrder::on_activate(Price p, Quantity q,
|
||||
bts::orderman::OrderServices &services,
|
||||
PerfTrace const &t) {
|
||||
LOG(Latency,Info, "on_activate start {}", key().value());
|
||||
trace::CorID const &trace) {
|
||||
auto scoped_log =
|
||||
services.log_scope(trace, "NativeOrder::on_activate", key().value());
|
||||
|
||||
auto trace = t;
|
||||
trace.order_key = key().value();
|
||||
trace.order_update = utils::TimeStamp::now();
|
||||
|
||||
tracepoint(bts_trace, trace_order, (char *)"on_activate", 0, trace.md_sn, key().value());
|
||||
bts_assert(!me->services);
|
||||
me->services = &services;
|
||||
|
||||
@@ -299,10 +295,13 @@ void NativeOrder::on_activate(Price p, Quantity q,
|
||||
me->session_services = &me->services->get_session_services(
|
||||
get_native_state(me->state).routing_record().session());
|
||||
me->price_encoding_precision =
|
||||
me->session_services->session_cfg().price_encoding_precision();
|
||||
me->services->price_encoding_precision(*me->session_services);
|
||||
|
||||
auto checks_log = services.log_scope(
|
||||
trace, "NativeOrder::on_activate::checks", key().value());
|
||||
|
||||
// is our gateway connected?
|
||||
if (!me->session_services->is_gateway_connected()) {
|
||||
if (!me->services->is_gateway_connected(*me->session_services)) {
|
||||
LOG(OrderMan, Warn,
|
||||
"Attempt to send order to a disconnected gateway. "
|
||||
"Dropping order {}",
|
||||
@@ -345,7 +344,7 @@ void NativeOrder::on_activate(Price p, Quantity q,
|
||||
}
|
||||
|
||||
// are we under the messaging threshold?
|
||||
if (!me->session_services->governor().can_send()) {
|
||||
if (!me->services->governor(*me->session_services).can_send()) {
|
||||
LOG(OrderMan, Warn,
|
||||
"New order would cause breach of messaging "
|
||||
"limits. Dropping order {}",
|
||||
@@ -355,16 +354,25 @@ void NativeOrder::on_activate(Price p, Quantity q,
|
||||
break;
|
||||
}
|
||||
|
||||
checks_log.finish();
|
||||
|
||||
// if we are here we are good.
|
||||
|
||||
services.log(
|
||||
trace, "NativeOrder::on_activate::post_checks:1", key().value());
|
||||
// subscribe to usefull events
|
||||
me->session_services->exe_reports.sub(
|
||||
key(), [this](auto const &r) { this->on_exe_report(r); });
|
||||
me->session_services->cancel_rejects.sub(
|
||||
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
|
||||
me->session_services->gateway_status.sub(
|
||||
me->services->subscribe_to_exe_report(
|
||||
*me->session_services, key(),
|
||||
[this](auto const &r) { this->on_exe_report(r); });
|
||||
me->services->subscribe_to_cancel_reject(
|
||||
*me->session_services, key(),
|
||||
[this](auto const &cr) { this->on_cancel_reject(cr); });
|
||||
me->services->subscribe_to_gateway_status(
|
||||
*me->session_services,
|
||||
[this](auto const &gw) { this->on_gateway_status(gw); });
|
||||
|
||||
services.log(
|
||||
trace, "NativeOrder::on_activate::post_checks:2", key().value());
|
||||
// send out order
|
||||
auto &encoding = me->create_encoding;
|
||||
|
||||
@@ -384,19 +392,27 @@ void NativeOrder::on_activate(Price p, Quantity q,
|
||||
break;
|
||||
}
|
||||
|
||||
services.log(
|
||||
trace, "NativeOrder::on_activate::post_checks:3", key().value());
|
||||
me->create_quantity_bm =
|
||||
encoding.overwrite(me->create_quantity_bm, q.value(), quantity_sz());
|
||||
auto const &ts = services.get_time_str();
|
||||
me->create_transact_time_bm = encoding.overwrite(
|
||||
me->create_transact_time_bm, &ts.value()[0], ts.value().size());
|
||||
trace.send_fix_buffer = utils::TimeStamp::now();
|
||||
me->session_services->queue_fix(jetfix::MessageTypes::NewOrderSingle,
|
||||
me->create_encoding, trace);
|
||||
me->session_services->governor().record_sent();
|
||||
|
||||
services.log(
|
||||
trace, "NativeOrder::on_activate::post_checks:4", key().value());
|
||||
me->services->queue_fix(*me->session_services,
|
||||
jetfix::MessageTypes::NewOrderSingle,
|
||||
me->create_encoding, trace);
|
||||
|
||||
services.log(
|
||||
trace, "NativeOrder::on_activate::post_checks:5", key().value());
|
||||
me->services->governor(*me->session_services).record_sent();
|
||||
services.log(
|
||||
trace, "NativeOrder::on_activate::post_checks:5", key().value());
|
||||
me->state.set_status(ORDER_STATUS_PENDING_WORKING);
|
||||
|
||||
LOG(Latency,Diag, "on_activate done fix {} t: {}us", key().value(),
|
||||
(utils::TimeStamp::now() - trace.order_update).microseconds());
|
||||
LOG(OrderMan, Info, "Sent new order with key {} to gateway",
|
||||
key().value());
|
||||
// early return so we skip the expensive report_state() call
|
||||
@@ -407,39 +423,48 @@ void NativeOrder::on_activate(Price p, Quantity q,
|
||||
case ORDER_STATUS_WORKING:
|
||||
me->session_services = &me->services->get_session_services(
|
||||
get_native_state(me->state).routing_record().session());
|
||||
me->session_services->exe_reports.sub(
|
||||
key(), [this](auto const &r) { this->on_exe_report(r); });
|
||||
me->session_services->cancel_rejects.sub(
|
||||
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
|
||||
me->session_services->gateway_status.sub(
|
||||
me->services->subscribe_to_exe_report(
|
||||
*me->session_services, key(),
|
||||
[this](auto const &r) { this->on_exe_report(r); });
|
||||
me->services->subscribe_to_cancel_reject(
|
||||
*me->session_services, key(),
|
||||
[this](auto const &cr) { this->on_cancel_reject(cr); });
|
||||
me->services->subscribe_to_gateway_status(
|
||||
*me->session_services,
|
||||
[this](auto const &gw) { this->on_gateway_status(gw); });
|
||||
|
||||
if (!me->session_services->is_gateway_connected())
|
||||
if (!me->services->is_gateway_connected(*me->session_services))
|
||||
transition(ORDER_STATUS_DISCONNECTED);
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
me->session_services = &me->services->get_session_services(
|
||||
get_native_state(me->state).routing_record().session());
|
||||
me->session_services->exe_reports.sub(
|
||||
key(), [this](auto const &r) { this->on_exe_report(r); });
|
||||
me->session_services->cancel_rejects.sub(
|
||||
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
|
||||
me->session_services->gateway_status.sub(
|
||||
me->services->subscribe_to_exe_report(
|
||||
*me->session_services, key(),
|
||||
[this](auto const &r) { this->on_exe_report(r); });
|
||||
me->services->subscribe_to_cancel_reject(
|
||||
*me->session_services, key(),
|
||||
[this](auto const &cr) { this->on_cancel_reject(cr); });
|
||||
me->services->subscribe_to_gateway_status(
|
||||
*me->session_services,
|
||||
[this](auto const &gw) { this->on_gateway_status(gw); });
|
||||
|
||||
if (me->session_services->is_gateway_connected())
|
||||
if (me->services->is_gateway_connected(*me->session_services))
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
me->session_services = &me->services->get_session_services(
|
||||
get_native_state(me->state).routing_record().session());
|
||||
me->session_services->exe_reports.sub(
|
||||
key(), [this](auto const &r) { this->on_exe_report(r); });
|
||||
me->session_services->cancel_rejects.sub(
|
||||
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
|
||||
me->session_services->gateway_status.sub(
|
||||
me->services->subscribe_to_exe_report(
|
||||
*me->session_services, key(),
|
||||
[this](auto const &r) { this->on_exe_report(r); });
|
||||
me->services->subscribe_to_cancel_reject(
|
||||
*me->session_services, key(),
|
||||
[this](auto const &cr) { this->on_cancel_reject(cr); });
|
||||
me->services->subscribe_to_gateway_status(
|
||||
*me->session_services,
|
||||
[this](auto const &gw) { this->on_gateway_status(gw); });
|
||||
break;
|
||||
|
||||
@@ -457,17 +482,16 @@ void NativeOrder::on_activate(Price p, Quantity q,
|
||||
report_state();
|
||||
}
|
||||
|
||||
void NativeOrder::on_update(Price p, Quantity q, PerfTrace const &t) {
|
||||
auto trace = t;
|
||||
trace.order_update = utils::TimeStamp::now();
|
||||
trace.order_key = key().value();
|
||||
tracepoint(bts_trace, trace_order, (char *)"on_update.begin", 0, trace.md_sn,
|
||||
trace.order_key);
|
||||
void NativeOrder::on_update(Price p, Quantity q, trace::CorID const &trace) {
|
||||
auto scoped_log =
|
||||
me->services->log_scope(trace, "NativeOrder::on_update", key().value());
|
||||
|
||||
switch (me->state.status()) {
|
||||
|
||||
case ORDER_STATUS_WORKING: {
|
||||
|
||||
auto checks_perf_log = me->services->log_scope(
|
||||
trace, "NativeOrder::on_update::checks", key().value());
|
||||
auto previous_working_qty = 0;
|
||||
if (me->state.quantity() > me->state.filled_quantity())
|
||||
previous_working_qty =
|
||||
@@ -500,7 +524,7 @@ void NativeOrder::on_update(Price p, Quantity q, PerfTrace const &t) {
|
||||
}
|
||||
|
||||
// are we under the messaging threshold?
|
||||
if (!me->session_services->governor().can_send()) {
|
||||
if (!me->services->governor(*me->session_services).can_send()) {
|
||||
LOG(OrderMan, Warn,
|
||||
"Cannot update order {} because of messaging limits. ",
|
||||
key().value());
|
||||
@@ -514,9 +538,10 @@ void NativeOrder::on_update(Price p, Quantity q, PerfTrace const &t) {
|
||||
break;
|
||||
}
|
||||
|
||||
tracepoint(bts_trace, trace_order, (char *)"on_update.after_limits", 0,
|
||||
trace.md_sn, trace.order_key);
|
||||
checks_perf_log.finish();
|
||||
|
||||
auto post_check = me->services->log_scope(
|
||||
trace, "NativeOrder::on_update::post_check", key().value());
|
||||
// all good
|
||||
auto &encoding = me->update_encoding;
|
||||
me->update_price_bm = encoding.overwrite(
|
||||
@@ -526,17 +551,18 @@ void NativeOrder::on_update(Price p, Quantity q, PerfTrace const &t) {
|
||||
me->update_quantity_bm =
|
||||
encoding.overwrite(me->update_quantity_bm, q.value(), quantity_sz());
|
||||
|
||||
tracepoint(bts_trace, trace_order, (char *)"on_update.half_way", 0,
|
||||
trace.md_sn, trace.order_key);
|
||||
|
||||
auto const &ts = me->services->get_time_str();
|
||||
me->update_transact_time_bm = encoding.overwrite(
|
||||
me->update_transact_time_bm, &ts.value()[0], ts.value().size());
|
||||
|
||||
me->session_services->queue_fix(jetfix::MessageTypes::CancelReplace,
|
||||
me->update_encoding, trace);
|
||||
post_check.finish();
|
||||
me->services->queue_fix(*me->session_services,
|
||||
jetfix::MessageTypes::CancelReplace,
|
||||
me->update_encoding, trace);
|
||||
|
||||
me->session_services->governor().record_sent();
|
||||
auto post_sends = me->services->log_scope(
|
||||
trace, "NativeOrder::on_update::post_send", key().value());
|
||||
me->services->governor(*me->session_services).record_sent();
|
||||
update_working_position();
|
||||
if (me->state.last_msg().size())
|
||||
me->state.set_last_msg("");
|
||||
@@ -563,9 +589,11 @@ void NativeOrder::on_update(Price p, Quantity q, PerfTrace const &t) {
|
||||
report_state();
|
||||
}
|
||||
|
||||
void NativeOrder::on_crack(PerfTrace const &) {}
|
||||
void NativeOrder::on_crack(trace::CorID const &) {}
|
||||
|
||||
void NativeOrder::on_cancel(PerfTrace const &trace) {
|
||||
void NativeOrder::on_cancel(trace::CorID const &trace) {
|
||||
auto scoped_log =
|
||||
me->services->log_scope(trace, "NativeOrder::on_cancel", key().value());
|
||||
switch (me->state.status()) {
|
||||
|
||||
case ORDER_STATUS_WORKING: {
|
||||
@@ -574,8 +602,9 @@ void NativeOrder::on_cancel(PerfTrace const &trace) {
|
||||
me->cancel_transact_time_bm = encoding.overwrite(
|
||||
me->cancel_transact_time_bm, &ts.value()[0], ts.value().size());
|
||||
|
||||
me->session_services->queue_fix(jetfix::MessageTypes::Cancel,
|
||||
me->cancel_encoding, trace);
|
||||
me->services->queue_fix(*me->session_services,
|
||||
jetfix::MessageTypes::Cancel, me->cancel_encoding,
|
||||
trace);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -621,6 +650,9 @@ bool NativeOrder::is_old_exe_report(
|
||||
}
|
||||
|
||||
void NativeOrder::on_exe_report(const NewExecutionReportEvent &er) {
|
||||
auto scoped_log = me->services->log_scope(
|
||||
er.trace, "NativeOrder::on_exe_report", key().value());
|
||||
|
||||
auto &state = me->state;
|
||||
auto &native_state = get_native_state(state);
|
||||
|
||||
@@ -696,7 +728,7 @@ void NativeOrder::on_exe_report(const NewExecutionReportEvent &er) {
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
if (external_status == ORDER_STATUS_WORKING &&
|
||||
!me->session_services->is_gateway_connected())
|
||||
!me->services->is_gateway_connected(*me->session_services))
|
||||
transition(ORDER_STATUS_DISCONNECTED);
|
||||
else
|
||||
transition(external_status);
|
||||
@@ -817,7 +849,7 @@ void NativeOrder::update_working_position() {
|
||||
key().value(), me->definition.security_key());
|
||||
}
|
||||
|
||||
void NativeOrder::report_state(PerfTrace const &trace) {
|
||||
void NativeOrder::report_state(trace::CorID const &trace) {
|
||||
update_working_position();
|
||||
me->services->report_state(me->state, trace);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <memory>
|
||||
#include "lib/orderman/orders/Order.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/orderman/orderman_utils/PerfTrace.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -24,10 +24,10 @@ public:
|
||||
const PreActivationOrderServices&) override;
|
||||
void on_init_recovered(api::orderman::OrderRecord const&,
|
||||
const PreActivationOrderServices &) override;
|
||||
void on_activate(Price,Quantity,OrderServices &, PerfTrace const&) override ;
|
||||
void on_update(Price , Quantity,PerfTrace const&) override ;
|
||||
void on_crack(PerfTrace const&) override ;
|
||||
void on_cancel(PerfTrace const&) override ;
|
||||
void on_activate(Price,Quantity,OrderServices &, trace::CorID const&) override ;
|
||||
void on_update(Price , Quantity,trace::CorID const&) override ;
|
||||
void on_crack(trace::CorID const&) override ;
|
||||
void on_cancel(trace::CorID const&) override ;
|
||||
|
||||
void on_init_from_exe_report(ExecutionReport const&,const PreActivationOrderServices &);
|
||||
private:
|
||||
@@ -36,7 +36,7 @@ private:
|
||||
void on_exe_report(const NewExecutionReportEvent &);
|
||||
void on_cancel_reject(const CancelRejectEvent&);
|
||||
void on_gateway_status(const GatewayStatusChangeEvent&);
|
||||
void report_state(PerfTrace const&trace = PerfTrace());
|
||||
void report_state(trace::CorID const&trace = trace::CorID());
|
||||
|
||||
void init_fix();
|
||||
void add_exchange_order_id_to_fix();
|
||||
|
||||
@@ -1,8 +1,25 @@
|
||||
#include "Order.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderServices.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
Order::Order()
|
||||
{
|
||||
_services = std::make_unique<OrderServices>(*this);
|
||||
}
|
||||
|
||||
void Order::init_order_services(OrderServicesData &osd)
|
||||
{
|
||||
_services->init(osd);
|
||||
}
|
||||
|
||||
void Order::init_order_services(OrderServicesData &osd,OrderServices &parent)
|
||||
{
|
||||
_services->init(osd,parent);
|
||||
}
|
||||
|
||||
Order::~Order() {}
|
||||
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -5,11 +5,13 @@
|
||||
|
||||
// fwd
|
||||
namespace bts {
|
||||
namespace trace {struct CorID;}
|
||||
namespace securities {
|
||||
class ISecurityDB;
|
||||
}
|
||||
namespace orderman {
|
||||
class IOrderFactory;
|
||||
class OrderServicesData;
|
||||
}
|
||||
namespace api {
|
||||
namespace orderman {
|
||||
@@ -29,6 +31,7 @@ class OrderServices;
|
||||
|
||||
class Order {
|
||||
public:
|
||||
Order();
|
||||
virtual ~Order();
|
||||
|
||||
OrderKey key() const { return _key; }
|
||||
@@ -43,17 +46,21 @@ class Order {
|
||||
const PreActivationOrderServices &) = 0;
|
||||
|
||||
// activation - cannot be slow
|
||||
virtual void on_activate(Price, Quantity, OrderServices &, PerfTrace const&) = 0;
|
||||
virtual void on_activate(Price, Quantity, OrderServices &, trace::CorID const&) = 0;
|
||||
|
||||
// order is running, maintenance
|
||||
virtual void on_update(Price, Quantity, PerfTrace const&) = 0;
|
||||
virtual void on_crack(PerfTrace const&) = 0;
|
||||
virtual void on_cancel(PerfTrace const&) = 0;
|
||||
virtual void on_update(Price, Quantity, trace::CorID const&) = 0;
|
||||
virtual void on_crack(trace::CorID const&) = 0;
|
||||
virtual void on_cancel(trace::CorID const&) = 0;
|
||||
|
||||
private:
|
||||
OrderKey _key;
|
||||
OrderKey _parent_key;
|
||||
std::unique_ptr<OrderServices> _services;
|
||||
void init_order_services(OrderServicesData &);
|
||||
void init_order_services(OrderServicesData &,OrderServices &parent);
|
||||
friend class OrderFactory;
|
||||
friend class OrderManager;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "TimeBoxOrder.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
|
||||
namespace bts {
|
||||
@@ -38,7 +38,7 @@ void TimeBoxOrder::on_init_new(api::orderman::OrderDefinition const &def,
|
||||
me->start_time = timebox_def.start();
|
||||
me->stop_time = timebox_def.stop();
|
||||
me->child_state.set_status(ORDER_STATUS_INACTIVE);
|
||||
|
||||
|
||||
auto child_def = def;
|
||||
*child_def.mutable_custom_def() = timebox_def.child();
|
||||
if (me->start_time.seconds() > utils::pb::pb_now().seconds() &&
|
||||
@@ -84,20 +84,21 @@ void TimeBoxOrder::on_init_recovered(api::orderman::OrderRecord const &rec,
|
||||
}
|
||||
}
|
||||
|
||||
void TimeBoxOrder::on_activate(Price p, Quantity q, OrderServices &services,PerfTrace const&) {
|
||||
void TimeBoxOrder::on_activate(Price p, Quantity q, OrderServices &services,
|
||||
trace::CorID const &) {
|
||||
bts_assert(!me->services);
|
||||
me->services = &services;
|
||||
me->child_builder.activate(services);
|
||||
|
||||
|
||||
switch (me->state.status()) {
|
||||
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
me->child = me->child_builder.build();
|
||||
me->state.set_price(p.value());
|
||||
me->state.set_quantity(q.value());
|
||||
services.child_updates.sub(
|
||||
key(), [this](auto const &os) { this->on_child_order_update(os); });
|
||||
services.time_change.sub([this](auto) { this->on_time_change(); });
|
||||
services.subscribe_to_child_updates(
|
||||
[this](auto const &os) { this->on_child_order_update(os); });
|
||||
services.subscribe_to_time([this](auto) { this->on_time_change(); });
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
break;
|
||||
|
||||
@@ -113,7 +114,7 @@ void TimeBoxOrder::on_activate(Price p, Quantity q, OrderServices &services,Perf
|
||||
// state)
|
||||
case ORDER_STATUS_WORKING:
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
@@ -122,7 +123,7 @@ void TimeBoxOrder::on_activate(Price p, Quantity q, OrderServices &services,Perf
|
||||
report_state();
|
||||
}
|
||||
|
||||
void TimeBoxOrder::on_update(Price p, Quantity q,PerfTrace const&) {
|
||||
void TimeBoxOrder::on_update(Price p, Quantity q, trace::CorID const &) {
|
||||
LOG(OrderMan, Info, "Updating TimeBox Order {}", key().value());
|
||||
|
||||
switch (me->state.status()) {
|
||||
@@ -140,7 +141,7 @@ void TimeBoxOrder::on_update(Price p, Quantity q,PerfTrace const&) {
|
||||
// we launched our child, everything is nominal
|
||||
me->state.set_price(p.value());
|
||||
me->state.set_quantity(q.value());
|
||||
me->child.update(p,q);
|
||||
me->child.update(p, q);
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
@@ -151,7 +152,7 @@ void TimeBoxOrder::on_update(Price p, Quantity q,PerfTrace const&) {
|
||||
|
||||
// if the child was in any of these states we couldn't be
|
||||
// in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -182,9 +183,9 @@ void TimeBoxOrder::on_update(Price p, Quantity q,PerfTrace const&) {
|
||||
report_state();
|
||||
}
|
||||
|
||||
void TimeBoxOrder::on_crack(PerfTrace const&) {}
|
||||
void TimeBoxOrder::on_crack(trace::CorID const &) {}
|
||||
|
||||
void TimeBoxOrder::on_cancel(PerfTrace const&) {
|
||||
void TimeBoxOrder::on_cancel(trace::CorID const &) {
|
||||
LOG(OrderMan, Info, "Canceling TimeBox Order {}", key().value());
|
||||
|
||||
switch (me->state.status()) {
|
||||
@@ -210,7 +211,7 @@ void TimeBoxOrder::on_cancel(PerfTrace const&) {
|
||||
break;
|
||||
|
||||
// child cannot be in any of these if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -263,7 +264,7 @@ void TimeBoxOrder::on_time_change() {
|
||||
case ORDER_STATUS_INACTIVE: {
|
||||
// we haven't yet launched the child, check the time and
|
||||
// launch it if appropriate
|
||||
if ( now >= me->start_time.seconds() && now < me->stop_time.seconds()) {
|
||||
if (now >= me->start_time.seconds() && now < me->stop_time.seconds()) {
|
||||
LOG(OrderMan, Info, "TimeBox order {} launching child {}",
|
||||
key().value(), me->child.key().value());
|
||||
me->child.activate(Price(me->state.price()),
|
||||
@@ -285,12 +286,12 @@ void TimeBoxOrder::on_time_change() {
|
||||
report_state();
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -318,34 +319,33 @@ void TimeBoxOrder::on_time_change() {
|
||||
}
|
||||
}
|
||||
|
||||
void TimeBoxOrder::on_child_order_update(OrderStateUpdateEvent const&ev)
|
||||
{
|
||||
void TimeBoxOrder::on_child_order_update(OrderStateUpdateEvent const &ev) {
|
||||
me->child_state = ev.rebuild_state();
|
||||
|
||||
switch (me->state.status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (ev.status) {
|
||||
|
||||
case ORDER_STATUS_WORKING:
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
// nothing to do but report new state
|
||||
break;
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
transition(ORDER_STATUS_COMPLETED);
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
// nothing to do but report new state
|
||||
break;
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
transition(ORDER_STATUS_COMPLETED);
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
transition(ORDER_STATUS_ERROR, "Unexpected child order state");
|
||||
break;
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
transition(ORDER_STATUS_ERROR, "Unexpected child order state");
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -368,12 +368,11 @@ void TimeBoxOrder::on_child_order_update(OrderStateUpdateEvent const&ev)
|
||||
|
||||
void TimeBoxOrder::report_state() {
|
||||
auto merged_state = me->state;
|
||||
merged_state.set_filled_quantity (me->child_state.filled_quantity());
|
||||
merged_state.set_last_price ( me->child_state.last_price());
|
||||
merged_state.set_avg_price ( me->child_state.avg_price());
|
||||
merged_state.set_filled_quantity(me->child_state.filled_quantity());
|
||||
merged_state.set_last_price(me->child_state.last_price());
|
||||
merged_state.set_avg_price(me->child_state.avg_price());
|
||||
me->services->report_state(merged_state);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -19,10 +19,10 @@ public:
|
||||
const PreActivationOrderServices&) override;
|
||||
void on_init_recovered(api::orderman::OrderRecord const&,
|
||||
const PreActivationOrderServices &) override;
|
||||
void on_activate(Price,Quantity,OrderServices &, PerfTrace const&) override ;
|
||||
void on_update(Price , Quantity,PerfTrace const&) override ;
|
||||
void on_crack(PerfTrace const&) override ;
|
||||
void on_cancel(PerfTrace const&) override ;
|
||||
void on_activate(Price,Quantity,OrderServices &, trace::CorID const&) override ;
|
||||
void on_update(Price , Quantity,trace::CorID const&) override ;
|
||||
void on_crack(trace::CorID const&) override ;
|
||||
void on_cancel(trace::CorID const&) override ;
|
||||
|
||||
private:
|
||||
void report_state();
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
#include "TriggerOrder.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
|
||||
#include "lib/orderman/events/BookUpdateEvent.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
struct TriggerOrder::PImpl {
|
||||
OrderServices *services;
|
||||
@@ -42,496 +42,491 @@ struct TriggerOrder::PImpl {
|
||||
bool is_succ_inited;
|
||||
bts::TimeStamp child_order_time;
|
||||
PImpl()
|
||||
:
|
||||
services(nullptr),
|
||||
order_def(),
|
||||
timeout_exit(false),
|
||||
timeout_msecs(std::numeric_limits<uint32_t>::max()),
|
||||
price_cond_exit(false),
|
||||
state(),
|
||||
child_state(),
|
||||
child_order(),
|
||||
order_placed(false),
|
||||
book(),
|
||||
cancel_mode(CancelMode::none),
|
||||
market_observed_once(false),
|
||||
is_recovered(false),
|
||||
is_succ_inited(false){
|
||||
}
|
||||
: services(nullptr), order_def(), timeout_exit(false),
|
||||
timeout_msecs(std::numeric_limits<uint32_t>::max()),
|
||||
price_cond_exit(false), state(), child_state(), child_order(),
|
||||
order_placed(false), book(), cancel_mode(CancelMode::none),
|
||||
market_observed_once(false), is_recovered(false),
|
||||
is_succ_inited(false) {}
|
||||
};
|
||||
|
||||
TriggerOrder::TriggerOrder()
|
||||
:
|
||||
_state(State::START),
|
||||
me(new PImpl()){
|
||||
}
|
||||
TriggerOrder::~TriggerOrder(){
|
||||
}
|
||||
const api::orderman::OrderDefinition& TriggerOrder::definition() const{
|
||||
TriggerOrder::TriggerOrder() : _state(State::START), me(new PImpl()) {}
|
||||
TriggerOrder::~TriggerOrder() {}
|
||||
const api::orderman::OrderDefinition &TriggerOrder::definition() const {
|
||||
return me->order_def;
|
||||
}
|
||||
void TriggerOrder::on_init_new(const api::orderman::OrderDefinition& order_def,
|
||||
const PreActivationOrderServices& pre_os){
|
||||
LOG(OrderMan, Info, "TriggerOrder::on_init_new key:{} state:{}",
|
||||
key(),_state);
|
||||
switch (_state){
|
||||
case State::START:
|
||||
me->is_succ_inited=_on_init_new(order_def,pre_os);
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
case State::END:
|
||||
std::ostringstream oss;
|
||||
oss << "TriggerOrder::on_init_new key:" << key() << "state:" << _state;
|
||||
bts_assert_msg(false, oss.str());
|
||||
break;
|
||||
void TriggerOrder::on_init_new(const api::orderman::OrderDefinition &order_def,
|
||||
const PreActivationOrderServices &pre_os) {
|
||||
LOG(OrderMan, Info, "TriggerOrder::on_init_new key:{} state:{}", key(),
|
||||
_state);
|
||||
switch (_state) {
|
||||
case State::START:
|
||||
me->is_succ_inited = _on_init_new(order_def, pre_os);
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
case State::END:
|
||||
std::ostringstream oss;
|
||||
oss << "TriggerOrder::on_init_new key:" << key() << "state:" << _state;
|
||||
bts_assert_msg(false, oss.str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
void TriggerOrder::on_init_recovered(const api::orderman::OrderRecord& rec,
|
||||
const PreActivationOrderServices& pre_os){
|
||||
LOG(OrderMan, Info, "TriggerOrder::on_init_recovered key:{} state:{}",
|
||||
key(),_state);
|
||||
switch (_state){
|
||||
case State::START:
|
||||
me->is_succ_inited=_on_init_new(rec.def(),pre_os);
|
||||
// get the state from what was recovered (we will only use
|
||||
// it for reporting)
|
||||
me->state=rec.state();
|
||||
// if it was not in a terminal state, we will call it cancelled
|
||||
if (!SpreaderUtils::order_in_terminal_state(me->state.status()))
|
||||
me->state.set_status(api::orderman::OrderStatus::ORDER_STATUS_CANCELED);
|
||||
me->is_recovered=true;
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
case State::END:
|
||||
std::ostringstream oss;
|
||||
oss << "TriggerOrder::on_init_recovered key:" << key() << "state:" << _state;
|
||||
bts_assert_msg(false, oss.str());
|
||||
break;
|
||||
void TriggerOrder::on_init_recovered(const api::orderman::OrderRecord &rec,
|
||||
const PreActivationOrderServices &pre_os) {
|
||||
LOG(OrderMan, Info, "TriggerOrder::on_init_recovered key:{} state:{}", key(),
|
||||
_state);
|
||||
switch (_state) {
|
||||
case State::START:
|
||||
me->is_succ_inited = _on_init_new(rec.def(), pre_os);
|
||||
// get the state from what was recovered (we will only use
|
||||
// it for reporting)
|
||||
me->state = rec.state();
|
||||
// if it was not in a terminal state, we will call it cancelled
|
||||
if (!SpreaderUtils::order_in_terminal_state(me->state.status()))
|
||||
me->state.set_status(
|
||||
api::orderman::OrderStatus::ORDER_STATUS_CANCELED);
|
||||
me->is_recovered = true;
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
case State::END:
|
||||
std::ostringstream oss;
|
||||
oss << "TriggerOrder::on_init_recovered key:" << key()
|
||||
<< "state:" << _state;
|
||||
bts_assert_msg(false, oss.str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
void TriggerOrder::on_activate(Price price,
|
||||
Quantity qty,
|
||||
OrderServices& services,
|
||||
PerfTrace const&){
|
||||
LOG(OrderMan, Info, "TriggerOrder::on_activate key:{} state:{} price:{} qty:{}",
|
||||
key(),_state, price, qty);
|
||||
switch (_state){
|
||||
case State::START:
|
||||
me->services=&services;
|
||||
me->state.set_price(price.value());
|
||||
me->state.set_quantity(qty.value());
|
||||
if(me->is_recovered || !me->is_succ_inited)
|
||||
_switch_state(State::END);
|
||||
else{
|
||||
me->order_builder.activate(services);
|
||||
me->child_order=me->order_builder.build();
|
||||
services.child_updates.sub(key(),
|
||||
[this](const OrderStateUpdateEvent& os)
|
||||
{ this->_on_child_order_update (os);});
|
||||
services.book_updates.sub(SecurityKey(me->order_def.security_key()),
|
||||
[this](const BookUpdateEvent& bk_event)
|
||||
{ this->_on_book_update (bk_event);});
|
||||
services.time_change.sub([this](const bts::mercury::TimerEvent& te)
|
||||
{_on_time(te);});
|
||||
_switch_state(State::WAITING_FOR_TRIGGER);
|
||||
}
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
case State::END:{
|
||||
std::ostringstream oss;
|
||||
oss << "TriggerOrder::on_activate key:" << key() << "state:" << _state;
|
||||
bts_assert_msg(false,oss.str());
|
||||
break;
|
||||
void TriggerOrder::on_activate(Price price, Quantity qty,
|
||||
OrderServices &services, trace::CorID const &) {
|
||||
LOG(OrderMan, Info,
|
||||
"TriggerOrder::on_activate key:{} state:{} price:{} qty:{}", key(),
|
||||
_state, price, qty);
|
||||
switch (_state) {
|
||||
case State::START:
|
||||
me->services = &services;
|
||||
me->state.set_price(price.value());
|
||||
me->state.set_quantity(qty.value());
|
||||
if (me->is_recovered || !me->is_succ_inited)
|
||||
_switch_state(State::END);
|
||||
else {
|
||||
me->order_builder.activate(services);
|
||||
me->child_order = me->order_builder.build();
|
||||
services.subscribe_to_child_updates(
|
||||
[this](const OrderStateUpdateEvent &os) {
|
||||
this->_on_child_order_update(os);
|
||||
});
|
||||
services.subscribe_to_book(SecurityKey(me->order_def.security_key()),
|
||||
[this](const BookUpdateEvent &bk_event) {
|
||||
this->_on_book_update(bk_event);
|
||||
});
|
||||
services.subscribe_to_time(
|
||||
[this](const bts::mercury::TimerEvent &te) { _on_time(te); });
|
||||
_switch_state(State::WAITING_FOR_TRIGGER);
|
||||
}
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
case State::END: {
|
||||
std::ostringstream oss;
|
||||
oss << "TriggerOrder::on_activate key:" << key() << "state:" << _state;
|
||||
bts_assert_msg(false, oss.str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// order price, qty change
|
||||
void TriggerOrder::on_update(Price price,
|
||||
Quantity qty,
|
||||
PerfTrace const&){
|
||||
LOG(OrderMan, Info, "TriggerOrder::on_update key:{} state:{} price:{} qty:{}",
|
||||
key(),_state, price, qty);
|
||||
switch (_state){
|
||||
case State::START:
|
||||
me->state.set_price(price.value());
|
||||
me->state.set_quantity(qty.value());
|
||||
// cant report because does not have order_services yet
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
me->state.set_price(price.value());
|
||||
me->state.set_quantity(qty.value());
|
||||
_report_state();
|
||||
_analyze_trigger_and_shoot();
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
me->state.set_last_msg("Trigger order can not be modified after the trigger has been hit");
|
||||
_report_state();
|
||||
break;
|
||||
case State::END:
|
||||
break;
|
||||
void TriggerOrder::on_update(Price price, Quantity qty, trace::CorID const &) {
|
||||
LOG(OrderMan, Info,
|
||||
"TriggerOrder::on_update key:{} state:{} price:{} qty:{}", key(), _state,
|
||||
price, qty);
|
||||
switch (_state) {
|
||||
case State::START:
|
||||
me->state.set_price(price.value());
|
||||
me->state.set_quantity(qty.value());
|
||||
// cant report because does not have order_services yet
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
me->state.set_price(price.value());
|
||||
me->state.set_quantity(qty.value());
|
||||
_report_state();
|
||||
_analyze_trigger_and_shoot();
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
me->state.set_last_msg(
|
||||
"Trigger order can not be modified after the trigger has been hit");
|
||||
_report_state();
|
||||
break;
|
||||
case State::END:
|
||||
break;
|
||||
}
|
||||
}
|
||||
void TriggerOrder::on_crack(PerfTrace const&){
|
||||
void TriggerOrder::on_crack(trace::CorID const &) {
|
||||
// not supported
|
||||
}
|
||||
void TriggerOrder::on_cancel(PerfTrace const&){
|
||||
LOG(OrderMan, Info, "TriggerOrder::on_cancel key:{} state:{}",
|
||||
key(),_state);
|
||||
me->cancel_mode=CancelMode::external;
|
||||
switch (_state){
|
||||
case State::START:
|
||||
_switch_state(State::END);
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
_switch_state(State::END);
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
// we dont have a WAITING_FOR_ACK_CANCELLED
|
||||
// (like the Quoter), so this message is
|
||||
// rejected and reported
|
||||
me->state.set_last_msg("Order is in flight");
|
||||
_report_state();
|
||||
break;
|
||||
case State::STEADY:
|
||||
_cancel_order(CancelMode::external);
|
||||
break;
|
||||
case State::END:
|
||||
break;
|
||||
void TriggerOrder::on_cancel(trace::CorID const &) {
|
||||
LOG(OrderMan, Info, "TriggerOrder::on_cancel key:{} state:{}", key(),
|
||||
_state);
|
||||
me->cancel_mode = CancelMode::external;
|
||||
switch (_state) {
|
||||
case State::START:
|
||||
_switch_state(State::END);
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
_switch_state(State::END);
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
// we dont have a WAITING_FOR_ACK_CANCELLED
|
||||
// (like the Quoter), so this message is
|
||||
// rejected and reported
|
||||
me->state.set_last_msg("Order is in flight");
|
||||
_report_state();
|
||||
break;
|
||||
case State::STEADY:
|
||||
_cancel_order(CancelMode::external);
|
||||
break;
|
||||
case State::END:
|
||||
break;
|
||||
}
|
||||
}
|
||||
void TriggerOrder::_on_book_update(const BookUpdateEvent& b_event){
|
||||
switch (_state){
|
||||
case State::START:
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
if(b_event.key().value()==me->order_def.security_key()){
|
||||
me->book=b_event.book;
|
||||
_observe_market_once();
|
||||
_analyze_trigger_and_shoot();
|
||||
void TriggerOrder::_on_book_update(const BookUpdateEvent &b_event) {
|
||||
switch (_state) {
|
||||
case State::START:
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
if (b_event.key().value() == me->order_def.security_key()) {
|
||||
me->book = b_event.book;
|
||||
_observe_market_once();
|
||||
_analyze_trigger_and_shoot();
|
||||
}
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
break;
|
||||
case State::STEADY:
|
||||
if (b_event.key().value() == me->order_def.security_key()) {
|
||||
if (me->price_cond_exit) {
|
||||
me->book = b_event.book;
|
||||
_analyze_trigger_and_cancel();
|
||||
}
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
break;
|
||||
case State::STEADY:
|
||||
if(b_event.key().value()==me->order_def.security_key()){
|
||||
if(me->price_cond_exit){
|
||||
me->book=b_event.book;
|
||||
_analyze_trigger_and_cancel();
|
||||
}
|
||||
}
|
||||
case State::END:
|
||||
break;
|
||||
}
|
||||
case State::END:
|
||||
break;
|
||||
}
|
||||
}
|
||||
void TriggerOrder::_on_child_order_update(const OrderStateUpdateEvent& o_ev){
|
||||
if(o_ev.key==me->child_order.key()){
|
||||
void TriggerOrder::_on_child_order_update(const OrderStateUpdateEvent &o_ev) {
|
||||
if (o_ev.key == me->child_order.key()) {
|
||||
auto ev_state = o_ev.rebuild_state();
|
||||
me->child_state=ev_state;
|
||||
switch (_state){
|
||||
case State::START:
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
bts_assert(false);
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
if(SpreaderUtils::order_acknowledged(o_ev.status)){
|
||||
if(SpreaderUtils::order_in_terminal_state(o_ev.status))
|
||||
_switch_state(State::END);
|
||||
else
|
||||
_switch_state(State::STEADY);
|
||||
}
|
||||
break;
|
||||
case State::STEADY:
|
||||
if(SpreaderUtils::order_in_terminal_state(ev_state.status()))
|
||||
me->child_state = ev_state;
|
||||
switch (_state) {
|
||||
case State::START:
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
bts_assert(false);
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
if (SpreaderUtils::order_acknowledged(o_ev.status)) {
|
||||
if (SpreaderUtils::order_in_terminal_state(o_ev.status))
|
||||
_switch_state(State::END);
|
||||
break;
|
||||
case State::END:
|
||||
break;
|
||||
else
|
||||
_switch_state(State::STEADY);
|
||||
}
|
||||
break;
|
||||
case State::STEADY:
|
||||
if (SpreaderUtils::order_in_terminal_state(ev_state.status()))
|
||||
_switch_state(State::END);
|
||||
break;
|
||||
case State::END:
|
||||
break;
|
||||
}
|
||||
_report_state();
|
||||
}
|
||||
}
|
||||
void TriggerOrder::_on_time(const bts::mercury::TimerEvent&){
|
||||
switch (_state){
|
||||
case State::START:
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
break;
|
||||
case State::STEADY:{
|
||||
if(me->timeout_exit)
|
||||
_analyze_trigger_and_cancel();
|
||||
break;
|
||||
}
|
||||
case State::END:
|
||||
break;
|
||||
void TriggerOrder::_on_time(const bts::mercury::TimerEvent &) {
|
||||
switch (_state) {
|
||||
case State::START:
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
break;
|
||||
case State::STEADY: {
|
||||
if (me->timeout_exit)
|
||||
_analyze_trigger_and_cancel();
|
||||
break;
|
||||
}
|
||||
case State::END:
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool TriggerOrder::_on_init_new(const api::orderman::OrderDefinition& order_def,
|
||||
const PreActivationOrderServices& pre_os){
|
||||
me->order_def=order_def;
|
||||
if (!me->order_def.has_custom_def() || !me->order_def.custom_def().has_trigger()){
|
||||
LOG(OrderMan, Error, "TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"OrderDefinition does not have TriggerOrderDefinition",
|
||||
key(),_state);
|
||||
bool TriggerOrder::_on_init_new(const api::orderman::OrderDefinition &order_def,
|
||||
const PreActivationOrderServices &pre_os) {
|
||||
me->order_def = order_def;
|
||||
if (!me->order_def.has_custom_def() ||
|
||||
!me->order_def.custom_def().has_trigger()) {
|
||||
LOG(OrderMan, Error,
|
||||
"TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"OrderDefinition does not have TriggerOrderDefinition",
|
||||
key(), _state);
|
||||
me->state.set_last_msg("Trigger Order not properly formed");
|
||||
return false;
|
||||
}
|
||||
if (pre_os.sec_db.resolve(SecurityKey(order_def.security_key()))==nullptr){
|
||||
LOG(OrderMan, Error, "TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"Could not resolve sec_key:{}",
|
||||
key(),_state, order_def.security_key());
|
||||
if (pre_os.sec_db.resolve(SecurityKey(order_def.security_key())) ==
|
||||
nullptr) {
|
||||
LOG(OrderMan, Error,
|
||||
"TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"Could not resolve sec_key:{}",
|
||||
key(), _state, order_def.security_key());
|
||||
me->state.set_last_msg("Could not resolve Trigger Order security");
|
||||
return false;
|
||||
}
|
||||
const auto& trigger_order_def=order_def.custom_def().trigger();
|
||||
if(pre_os.sec_db.resolve(SecurityKey(trigger_order_def.trigger_action().security_id())) == nullptr){
|
||||
LOG(OrderMan, Error, "TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"Could not resolve child key sec_key:{}",
|
||||
key(),_state, trigger_order_def.trigger_action().security_id());
|
||||
}
|
||||
const auto &trigger_order_def = order_def.custom_def().trigger();
|
||||
if (pre_os.sec_db.resolve(SecurityKey(
|
||||
trigger_order_def.trigger_action().security_id())) == nullptr) {
|
||||
LOG(OrderMan, Error,
|
||||
"TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"Could not resolve child key sec_key:{}",
|
||||
key(), _state, trigger_order_def.trigger_action().security_id());
|
||||
me->state.set_last_msg("Could not resolve Trigger Order child security");
|
||||
return false;
|
||||
}
|
||||
if (!trigger_order_def.trigger_action().has_payup() &&
|
||||
bts::os::is_nan(trigger_order_def.trigger_action().price())){
|
||||
LOG(OrderMan, Error, "TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"illegal child order price",
|
||||
key(),_state);
|
||||
bts::os::is_nan(trigger_order_def.trigger_action().price())) {
|
||||
LOG(OrderMan, Error,
|
||||
"TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"illegal child order price",
|
||||
key(), _state);
|
||||
me->state.set_last_msg("Illegal Child Order price");
|
||||
return false;
|
||||
}
|
||||
me->timeout_exit = trigger_order_def.has_exit_condition() &&
|
||||
trigger_order_def.exit_condition().timeout_enabled();
|
||||
me->timeout_msecs = trigger_order_def.has_exit_condition() ?
|
||||
1000.0*trigger_order_def.exit_condition().timeout_seconds () :
|
||||
std::numeric_limits<uint32_t>::max();
|
||||
me->price_cond_exit = trigger_order_def.has_exit_condition() &&
|
||||
trigger_order_def.exit_condition().conditional_cancel();
|
||||
trigger_order_def.exit_condition().timeout_enabled();
|
||||
me->timeout_msecs =
|
||||
trigger_order_def.has_exit_condition()
|
||||
? 1000.0 * trigger_order_def.exit_condition().timeout_seconds()
|
||||
: std::numeric_limits<uint32_t>::max();
|
||||
me->price_cond_exit =
|
||||
trigger_order_def.has_exit_condition() &&
|
||||
trigger_order_def.exit_condition().conditional_cancel();
|
||||
|
||||
api::orderman::OrderDefinition child_order_def = order_def;
|
||||
child_order_def.set_security_key(trigger_order_def.trigger_action().security_id());
|
||||
child_order_def.set_security_key(
|
||||
trigger_order_def.trigger_action().security_id());
|
||||
child_order_def.mutable_custom_def()->CopyFrom(trigger_order_def.child());
|
||||
me->order_builder=OrderBuilder(key(),child_order_def,pre_os);
|
||||
const auto* sec_ptr = pre_os.sec_db.resolve(SecurityKey(order_def.security_key()));
|
||||
me->tick_size=std::min(sec_ptr->contract_spec().low_tick(),
|
||||
sec_ptr->contract_spec().high_tick());
|
||||
LOG(OrderMan, Info, "TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"trigger_price_type:{} child_order_price:{}",
|
||||
key(),_state,
|
||||
trigger_order_def.trigger_condition().price_type(),
|
||||
trigger_order_def.trigger_action().price());
|
||||
me->order_builder = OrderBuilder(key(), child_order_def, pre_os);
|
||||
const auto *sec_ptr =
|
||||
pre_os.sec_db.resolve(SecurityKey(order_def.security_key()));
|
||||
me->tick_size = std::min(sec_ptr->contract_spec().low_tick(),
|
||||
sec_ptr->contract_spec().high_tick());
|
||||
LOG(OrderMan, Info,
|
||||
"TriggerOrder::_on_init_new key:{} state:{} "
|
||||
"trigger_price_type:{} child_order_price:{}",
|
||||
key(), _state, trigger_order_def.trigger_condition().price_type(),
|
||||
trigger_order_def.trigger_action().price());
|
||||
|
||||
return true;
|
||||
}
|
||||
void TriggerOrder::_report_state(){
|
||||
if(_activated()){
|
||||
void TriggerOrder::_report_state() {
|
||||
if (_activated()) {
|
||||
_compute_state();
|
||||
me->services->report_state(me->state);
|
||||
}
|
||||
}
|
||||
void TriggerOrder::_switch_state(State new_state){
|
||||
switch(new_state){
|
||||
case State::START:
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
bts_assert(_state==State::START);
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
bts_assert(_state==State::WAITING_FOR_TRIGGER);
|
||||
break;
|
||||
case State::STEADY:
|
||||
bts_assert(_state==State::WAITING_FOR_FIRST_ACK);
|
||||
break;
|
||||
case State::END:
|
||||
bts_assert(_state!=State::END);
|
||||
break;
|
||||
void TriggerOrder::_switch_state(State new_state) {
|
||||
switch (new_state) {
|
||||
case State::START:
|
||||
break;
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
bts_assert(_state == State::START);
|
||||
break;
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
bts_assert(_state == State::WAITING_FOR_TRIGGER);
|
||||
break;
|
||||
case State::STEADY:
|
||||
bts_assert(_state == State::WAITING_FOR_FIRST_ACK);
|
||||
break;
|
||||
case State::END:
|
||||
bts_assert(_state != State::END);
|
||||
break;
|
||||
}
|
||||
LOG(OrderMan, Info, "TriggerOrder::_report_state key:{} {}->{}",
|
||||
key(),
|
||||
_state,new_state);
|
||||
_state=new_state;
|
||||
LOG(OrderMan, Info, "TriggerOrder::_report_state key:{} {}->{}", key(),
|
||||
_state, new_state);
|
||||
_state = new_state;
|
||||
_report_state();
|
||||
}
|
||||
void TriggerOrder::_analyze_trigger_and_shoot(){
|
||||
if(me->market_observed_once){
|
||||
double trigger_price=_trigger_price();
|
||||
const md::PriceSize* obs_md=_observed_market();
|
||||
if (_trigger_met(obs_md,trigger_price,
|
||||
me->tick_size,me->trigger_above_market)){
|
||||
me->child_order_time=bts::Timer::now();
|
||||
me->order_placed=true;
|
||||
LOG(OrderMan, Info, "TriggerOrder::_analyze_trigger_and_shoot"
|
||||
"key:{} state:{} "
|
||||
"trigger_above_md:{} trigger_price:{} md:{} "
|
||||
"order_price:{} order_qty:{}",
|
||||
key(),_state,
|
||||
me->trigger_above_market,trigger_price,
|
||||
obs_md->price(),
|
||||
_child_order_price(),
|
||||
me->state.quantity());
|
||||
void TriggerOrder::_analyze_trigger_and_shoot() {
|
||||
if (me->market_observed_once) {
|
||||
double trigger_price = _trigger_price();
|
||||
const md::PriceSize *obs_md = _observed_market();
|
||||
if (_trigger_met(obs_md, trigger_price, me->tick_size,
|
||||
me->trigger_above_market)) {
|
||||
me->child_order_time = bts::Timer::now();
|
||||
me->order_placed = true;
|
||||
LOG(OrderMan, Info,
|
||||
"TriggerOrder::_analyze_trigger_and_shoot"
|
||||
"key:{} state:{} "
|
||||
"trigger_above_md:{} trigger_price:{} md:{} "
|
||||
"order_price:{} order_qty:{}",
|
||||
key(), _state, me->trigger_above_market, trigger_price,
|
||||
obs_md->price(), _child_order_price(), me->state.quantity());
|
||||
me->child_order.activate(Price(_child_order_price()),
|
||||
Quantity(me->state.quantity()));
|
||||
_switch_state(State::WAITING_FOR_FIRST_ACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
void TriggerOrder::_analyze_trigger_and_cancel(){
|
||||
if(me->timeout_exit){
|
||||
int64_t time_diff = ms_diff (bts::Timer::now(), me->child_order_time);
|
||||
if(time_diff > me->timeout_msecs){
|
||||
LOG(OrderMan, Info, "TriggerOrder::_analyze_trigger_and_cancel "
|
||||
"key:{} state:{} msecs:{} "
|
||||
"time out, cancelling order",
|
||||
key(),_state, time_diff);
|
||||
void TriggerOrder::_analyze_trigger_and_cancel() {
|
||||
if (me->timeout_exit) {
|
||||
int64_t time_diff = ms_diff(bts::Timer::now(), me->child_order_time);
|
||||
if (time_diff > me->timeout_msecs) {
|
||||
LOG(OrderMan, Info,
|
||||
"TriggerOrder::_analyze_trigger_and_cancel "
|
||||
"key:{} state:{} msecs:{} "
|
||||
"time out, cancelling order",
|
||||
key(), _state, time_diff);
|
||||
_cancel_order(CancelMode::trigger);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(me->price_cond_exit){
|
||||
double trigger_price=_trigger_price();
|
||||
const md::PriceSize* obs_md=_observed_market();
|
||||
if (!_trigger_met(obs_md,trigger_price,
|
||||
me->tick_size,me->trigger_above_market)){
|
||||
LOG(OrderMan, Info, "TriggerOrder::_analyze_trigger_and_cancel"
|
||||
"key:{} state:{} "
|
||||
"price condition untrue, cancelling order"
|
||||
"trigger_above_md:{} trigger_price:{} md:{} ",
|
||||
key(),_state,
|
||||
me->trigger_above_market,trigger_price,
|
||||
obs_md->price());
|
||||
if (me->price_cond_exit) {
|
||||
double trigger_price = _trigger_price();
|
||||
const md::PriceSize *obs_md = _observed_market();
|
||||
if (!_trigger_met(obs_md, trigger_price, me->tick_size,
|
||||
me->trigger_above_market)) {
|
||||
LOG(OrderMan, Info,
|
||||
"TriggerOrder::_analyze_trigger_and_cancel"
|
||||
"key:{} state:{} "
|
||||
"price condition untrue, cancelling order"
|
||||
"trigger_above_md:{} trigger_price:{} md:{} ",
|
||||
key(), _state, me->trigger_above_market, trigger_price,
|
||||
obs_md->price());
|
||||
_cancel_order(CancelMode::trigger);
|
||||
return;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
api::orderman::OrderStatus TriggerOrder::_status() const{
|
||||
switch(_state){
|
||||
// will only be called once the order
|
||||
// has been activated
|
||||
case State::START:
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
|
||||
case State::END:
|
||||
if(!me->is_succ_inited)
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_ERROR;
|
||||
else{
|
||||
if(me->order_placed){
|
||||
if (!SpreaderUtils::order_in_terminal_state(me->child_state.status()))
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
|
||||
else{
|
||||
if (SpreaderUtils::order_in_error_state(me->child_state.status()))
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_ERROR;
|
||||
else if (SpreaderUtils::order_in_completed_state(me->child_state.status()))
|
||||
api::orderman::OrderStatus TriggerOrder::_status() const {
|
||||
switch (_state) {
|
||||
// will only be called once the order
|
||||
// has been activated
|
||||
case State::START:
|
||||
case State::WAITING_FOR_TRIGGER:
|
||||
case State::WAITING_FOR_FIRST_ACK:
|
||||
case State::STEADY:
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
|
||||
case State::END:
|
||||
if (!me->is_succ_inited)
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_ERROR;
|
||||
else {
|
||||
if (me->order_placed) {
|
||||
if (!SpreaderUtils::order_in_terminal_state(
|
||||
me->child_state.status()))
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
|
||||
else {
|
||||
if (SpreaderUtils::order_in_error_state(
|
||||
me->child_state.status()))
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_ERROR;
|
||||
else if (SpreaderUtils::order_in_completed_state(
|
||||
me->child_state.status()))
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_COMPLETED;
|
||||
else if (SpreaderUtils::order_in_cancelled_state(
|
||||
me->child_state.status())) {
|
||||
switch (me->cancel_mode) {
|
||||
case CancelMode::none: // this means an unscolicted child cxl
|
||||
case CancelMode::external:
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
|
||||
case CancelMode::trigger:
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_COMPLETED;
|
||||
else if (SpreaderUtils::order_in_cancelled_state(me->child_state.status())){
|
||||
switch (me->cancel_mode){
|
||||
case CancelMode::none: //this means an unscolicted child cxl
|
||||
case CancelMode::external:
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
|
||||
case CancelMode::trigger:
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_COMPLETED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
|
||||
}
|
||||
} else
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
|
||||
}
|
||||
}
|
||||
bts_assert(false);
|
||||
return api::orderman::OrderStatus::ORDER_STATUS_INACTIVE;
|
||||
}
|
||||
void TriggerOrder::_compute_state(){
|
||||
if (!me->is_recovered){
|
||||
void TriggerOrder::_compute_state() {
|
||||
if (!me->is_recovered) {
|
||||
me->state.set_status(_status());
|
||||
me->state.set_filled_quantity(me->child_state.filled_quantity());
|
||||
me->state.set_last_price(me->child_state.last_price());
|
||||
me->state.set_avg_price(me->child_state.avg_price());
|
||||
if(_state==State::STEADY)
|
||||
if (_state == State::STEADY)
|
||||
me->state.set_display_status(bts::api::orderman::OrderState::HIDDEN);
|
||||
else
|
||||
me->state.set_display_status(bts::api::orderman::OrderState::VISIBLE);
|
||||
|
||||
}
|
||||
}
|
||||
void TriggerOrder::_observe_market_once(){
|
||||
if(!me->market_observed_once){
|
||||
const md::PriceSize* obs_md=_observed_market();
|
||||
if(obs_md != nullptr && obs_md->size()>0){
|
||||
me->market_observed_once=true;
|
||||
me->trigger_above_market=_trigger_price()>=obs_md->price();
|
||||
LOG(OrderMan, Info, "TriggerOrder::_observe_market_once "
|
||||
"key:{} state:{} "
|
||||
"md_p:{} md_q:{} trigger_above_md:{}",
|
||||
key(),_state,
|
||||
obs_md->price(), obs_md->size(),
|
||||
me->trigger_above_market);
|
||||
void TriggerOrder::_observe_market_once() {
|
||||
if (!me->market_observed_once) {
|
||||
const md::PriceSize *obs_md = _observed_market();
|
||||
if (obs_md != nullptr && obs_md->size() > 0) {
|
||||
me->market_observed_once = true;
|
||||
me->trigger_above_market = _trigger_price() >= obs_md->price();
|
||||
LOG(OrderMan, Info,
|
||||
"TriggerOrder::_observe_market_once "
|
||||
"key:{} state:{} "
|
||||
"md_p:{} md_q:{} trigger_above_md:{}",
|
||||
key(), _state, obs_md->price(), obs_md->size(),
|
||||
me->trigger_above_market);
|
||||
}
|
||||
}
|
||||
}
|
||||
const md::PriceSize* TriggerOrder::_observed_market() const{
|
||||
if(me->order_def.custom_def().trigger().trigger_condition().price_type()==
|
||||
api::orderman::TriggerPriceType::BID){
|
||||
const auto& rv=me->book.bid()[0];
|
||||
return &rv;
|
||||
}
|
||||
else if (me->order_def.custom_def().trigger().trigger_condition().price_type()==
|
||||
api::orderman::TriggerPriceType::ASK){
|
||||
const auto& rv=me->book.ask()[0];
|
||||
const md::PriceSize *TriggerOrder::_observed_market() const {
|
||||
if (me->order_def.custom_def().trigger().trigger_condition().price_type() ==
|
||||
api::orderman::TriggerPriceType::BID) {
|
||||
const auto &rv = me->book.bid()[0];
|
||||
return &rv;
|
||||
}
|
||||
else{
|
||||
} else if (me->order_def.custom_def()
|
||||
.trigger()
|
||||
.trigger_condition()
|
||||
.price_type() == api::orderman::TriggerPriceType::ASK) {
|
||||
const auto &rv = me->book.ask()[0];
|
||||
return &rv;
|
||||
} else {
|
||||
bts_assert(false);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
double TriggerOrder::_trigger_price() const{
|
||||
return me->state.price();
|
||||
}
|
||||
double TriggerOrder::_child_order_price() const{
|
||||
const auto& trigger_action=
|
||||
me->order_def.custom_def().trigger().trigger_action();
|
||||
double rv=bts::os::nan();
|
||||
if(trigger_action.has_payup()){
|
||||
double offset=trigger_action.payup().payup_ticks()*me->tick_size;
|
||||
if(me->order_def.side()==api::Side::SIDE_BUY)
|
||||
rv=_trigger_price()+offset;
|
||||
double TriggerOrder::_trigger_price() const { return me->state.price(); }
|
||||
double TriggerOrder::_child_order_price() const {
|
||||
const auto &trigger_action =
|
||||
me->order_def.custom_def().trigger().trigger_action();
|
||||
double rv = bts::os::nan();
|
||||
if (trigger_action.has_payup()) {
|
||||
double offset = trigger_action.payup().payup_ticks() * me->tick_size;
|
||||
if (me->order_def.side() == api::Side::SIDE_BUY)
|
||||
rv = _trigger_price() + offset;
|
||||
else
|
||||
rv=_trigger_price()-offset;
|
||||
}
|
||||
else
|
||||
rv=trigger_action.price();
|
||||
rv = bts::securities::raw_round_to_quote_price (rv, me->tick_size,
|
||||
me->order_def.side());
|
||||
rv = _trigger_price() - offset;
|
||||
} else
|
||||
rv = trigger_action.price();
|
||||
rv = bts::securities::raw_round_to_quote_price(rv, me->tick_size,
|
||||
me->order_def.side());
|
||||
return rv;
|
||||
}
|
||||
void TriggerOrder::_cancel_order(CancelMode cancel_mode){
|
||||
me->cancel_mode=cancel_mode;
|
||||
void TriggerOrder::_cancel_order(CancelMode cancel_mode) {
|
||||
me->cancel_mode = cancel_mode;
|
||||
me->child_order.cancel();
|
||||
LOG(OrderMan, Info, "TriggerOrder::_cancel_order key:{} state:{} mode:{}",
|
||||
key(),_state,cancel_mode);
|
||||
key(), _state, cancel_mode);
|
||||
_switch_state(State::END);
|
||||
}
|
||||
bool TriggerOrder::_activated() const{
|
||||
return me->services!=nullptr;
|
||||
bool TriggerOrder::_activated() const { return me->services != nullptr; }
|
||||
bool TriggerOrder::_gr_than_eq(double p1, double p2, double tick_size) {
|
||||
return bts::securities::test_price_equality(p1, p2, tick_size) || p1 >= p2;
|
||||
}
|
||||
bool TriggerOrder::_gr_than_eq(double p1, double p2, double tick_size){
|
||||
return bts::securities::test_price_equality(p1,p2,tick_size) || p1>=p2;
|
||||
}
|
||||
bool TriggerOrder::_trigger_met(const md::PriceSize* obs_md,
|
||||
double trpr,
|
||||
double tick_size,
|
||||
bool trigger_above_md){
|
||||
bool TriggerOrder::_trigger_met(const md::PriceSize *obs_md, double trpr,
|
||||
double tick_size, bool trigger_above_md) {
|
||||
if (obs_md != nullptr)
|
||||
return (trigger_above_md && _gr_than_eq(obs_md->price(),trpr, tick_size)) ||
|
||||
(!trigger_above_md && _gr_than_eq(trpr,obs_md->price(),tick_size));
|
||||
return (trigger_above_md &&
|
||||
_gr_than_eq(obs_md->price(), trpr, tick_size)) ||
|
||||
(!trigger_above_md &&
|
||||
_gr_than_eq(trpr, obs_md->price(), tick_size));
|
||||
return false;
|
||||
}
|
||||
}}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -56,10 +56,10 @@ public:
|
||||
const PreActivationOrderServices &) override;
|
||||
void on_activate(Price, Quantity,
|
||||
OrderServices&,
|
||||
PerfTrace const&) override ;
|
||||
void on_update(Price, Quantity, PerfTrace const&) override;
|
||||
void on_crack(PerfTrace const&) override;
|
||||
void on_cancel(PerfTrace const&) override;
|
||||
trace::CorID const&) override ;
|
||||
void on_update(Price, Quantity, trace::CorID const&) override;
|
||||
void on_crack(trace::CorID const&) override;
|
||||
void on_cancel(trace::CorID const&) override;
|
||||
private:
|
||||
// callbacks from book and child order udpates
|
||||
void _on_book_update(const BookUpdateEvent&);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "BookAlgo.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
|
||||
namespace bts {
|
||||
@@ -9,148 +9,113 @@ namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
|
||||
struct BookAlgo::PImpl
|
||||
{
|
||||
PImpl() : services(nullptr), fake_detached_child(false),
|
||||
supports_hedge(false), has_activated_child(false)
|
||||
{
|
||||
}
|
||||
|
||||
OrderServices* services;
|
||||
|
||||
struct BookAlgo::PImpl {
|
||||
PImpl()
|
||||
: services(nullptr), fake_detached_child(false), supports_hedge(false),
|
||||
has_activated_child(false) {}
|
||||
|
||||
OrderServices *services;
|
||||
|
||||
api::orderman::OrderDefinition def;
|
||||
api::orderman::OrderState state;
|
||||
|
||||
|
||||
OrderBuilder child_builder;
|
||||
ChildOrder child;
|
||||
api::orderman::OrderState child_state;
|
||||
ChildOrder child;
|
||||
api::orderman::OrderState child_state;
|
||||
bool fake_detached_child;
|
||||
|
||||
bool supports_hedge;
|
||||
OrderBuilder hedge_builder;
|
||||
|
||||
bool supports_hedge;
|
||||
OrderBuilder hedge_builder;
|
||||
std::vector<ChildOrder> hedges;
|
||||
using HedgeStateMap = boost::container::flat_map<OrderKey, api::orderman::OrderState>;
|
||||
using HedgeStateMap =
|
||||
boost::container::flat_map<OrderKey, api::orderman::OrderState>;
|
||||
HedgeStateMap hedges_state;
|
||||
|
||||
|
||||
double tick_size;
|
||||
bool has_activated_child;
|
||||
};
|
||||
|
||||
BookAlgo::BookAlgo() : me(new PImpl())
|
||||
{
|
||||
}
|
||||
BookAlgo::BookAlgo() : me(new PImpl()) {}
|
||||
|
||||
BookAlgo::~BookAlgo() {}
|
||||
|
||||
|
||||
|
||||
const api::orderman::OrderDefinition& BookAlgo::definition() const
|
||||
{
|
||||
const api::orderman::OrderDefinition &BookAlgo::definition() const {
|
||||
return me->def;
|
||||
}
|
||||
|
||||
const api::orderman::OrderState& BookAlgo::this_state() const
|
||||
{
|
||||
const api::orderman::OrderState &BookAlgo::this_state() const {
|
||||
return me->state;
|
||||
}
|
||||
|
||||
void BookAlgo::set_price(double p)
|
||||
{
|
||||
me->state.set_price(p);
|
||||
}
|
||||
|
||||
void BookAlgo::set_quantity(uint32_t s)
|
||||
{
|
||||
me->state.set_quantity(s);
|
||||
}
|
||||
|
||||
const api::orderman::OrderState& BookAlgo::child_state() const
|
||||
{
|
||||
void BookAlgo::set_price(double p) { me->state.set_price(p); }
|
||||
|
||||
void BookAlgo::set_quantity(uint32_t s) { me->state.set_quantity(s); }
|
||||
|
||||
const api::orderman::OrderState &BookAlgo::child_state() const {
|
||||
return me->child_state;
|
||||
}
|
||||
|
||||
void BookAlgo::set_child_price(double p)
|
||||
{
|
||||
me->child_state.set_price(p);
|
||||
}
|
||||
|
||||
void BookAlgo::set_child_quantity(uint32_t s)
|
||||
{
|
||||
void BookAlgo::set_child_price(double p) { me->child_state.set_price(p); }
|
||||
|
||||
void BookAlgo::set_child_quantity(uint32_t s) {
|
||||
me->child_state.set_quantity(s);
|
||||
}
|
||||
|
||||
void BookAlgo::activate_child(Price p, Quantity q)
|
||||
{
|
||||
//per Pedro, activating the child is not a synchronous calls, and he asserts that this is done only once
|
||||
if(!me->has_activated_child)
|
||||
{
|
||||
void BookAlgo::activate_child(Price p, Quantity q) {
|
||||
// per Pedro, activating the child is not a synchronous calls, and he asserts
|
||||
// that this is done only once
|
||||
if (!me->has_activated_child) {
|
||||
me->child.activate(p, q);
|
||||
me->has_activated_child = true;
|
||||
}
|
||||
}
|
||||
|
||||
void BookAlgo::update_child(Price p, Quantity q)
|
||||
{
|
||||
me->child.update(p, q);
|
||||
}
|
||||
}
|
||||
|
||||
void BookAlgo::cancel_child()
|
||||
{
|
||||
me->child.cancel();
|
||||
}
|
||||
void BookAlgo::update_child(Price p, Quantity q) { me->child.update(p, q); }
|
||||
|
||||
void BookAlgo::build_and_send_hedge(Price p, Quantity q)
|
||||
{
|
||||
auto hedge = me->hedge_builder.build();
|
||||
void BookAlgo::cancel_child() { me->child.cancel(); }
|
||||
|
||||
void BookAlgo::build_and_send_hedge(Price p, Quantity q) {
|
||||
auto hedge = me->hedge_builder.build();
|
||||
me->hedges.push_back(std::move(hedge));
|
||||
me->hedges.back().activate(p, q);
|
||||
me->hedges.back().activate(p, q);
|
||||
}
|
||||
|
||||
double BookAlgo::tick_size() const
|
||||
{
|
||||
return me->tick_size;
|
||||
}
|
||||
double BookAlgo::tick_size() const { return me->tick_size; }
|
||||
|
||||
void BookAlgo::on_init_new(const api::orderman::OrderDefinition& def,
|
||||
const PreActivationOrderServices &services)
|
||||
{
|
||||
void BookAlgo::on_init_new(const api::orderman::OrderDefinition &def,
|
||||
const PreActivationOrderServices &services) {
|
||||
me->services = nullptr;
|
||||
me->def = def;
|
||||
|
||||
me->child_state.set_status(ORDER_STATUS_INACTIVE);
|
||||
|
||||
me->child_state.set_status(ORDER_STATUS_INACTIVE);
|
||||
OrderDefinition child_def = def;
|
||||
setup_child(def, services, me->child_state, &child_def);
|
||||
me->child_builder = OrderBuilder(key(), child_def, services, 1);
|
||||
|
||||
OrderDefinition hedge_def = def;
|
||||
|
||||
OrderDefinition hedge_def = def;
|
||||
me->supports_hedge = setup_hedge(def, services, &hedge_def);
|
||||
if(me->supports_hedge)
|
||||
me->hedge_builder = OrderBuilder(key(), hedge_def, services, 4);
|
||||
|
||||
const bts::api::Security* sec = services.sec_db.resolve(SecurityKey(def.security_key()));
|
||||
if(sec)
|
||||
{
|
||||
if (me->supports_hedge)
|
||||
me->hedge_builder = OrderBuilder(key(), hedge_def, services, 4);
|
||||
|
||||
const bts::api::Security *sec =
|
||||
services.sec_db.resolve(SecurityKey(def.security_key()));
|
||||
if (sec) {
|
||||
me->tick_size = sec->contract_spec().low_tick();
|
||||
transition(ORDER_STATUS_INACTIVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
auto msg = fmt::format("Unknown security: {}", def.security_key());
|
||||
me->state.set_last_msg(msg);
|
||||
transition(ORDER_STATUS_REJECTED_BY_SPARK, msg);
|
||||
transition(ORDER_STATUS_REJECTED_BY_SPARK, msg);
|
||||
}
|
||||
}
|
||||
|
||||
void BookAlgo::on_init_recovered(const api::orderman::OrderRecord& rec,
|
||||
const PreActivationOrderServices &)
|
||||
{
|
||||
void BookAlgo::on_init_recovered(const api::orderman::OrderRecord &rec,
|
||||
const PreActivationOrderServices &) {
|
||||
auto const &def = rec.def();
|
||||
me->def = def;
|
||||
me->state = rec.state();
|
||||
switch (me->state.status())
|
||||
{
|
||||
case ORDER_STATUS_WORKING:
|
||||
{
|
||||
switch (me->state.status()) {
|
||||
case ORDER_STATUS_WORKING: {
|
||||
auto msg = "System restarted with active BookAlgo";
|
||||
me->state.set_last_msg(msg);
|
||||
transition(ORDER_STATUS_ERROR, msg);
|
||||
@@ -174,24 +139,24 @@ void BookAlgo::on_init_recovered(const api::orderman::OrderRecord& rec,
|
||||
}
|
||||
}
|
||||
|
||||
void BookAlgo::on_activate(Price p, Quantity q, OrderServices& services,PerfTrace const&)
|
||||
{
|
||||
void BookAlgo::on_activate(Price p, Quantity q, OrderServices &services,
|
||||
trace::CorID const &) {
|
||||
bts_assert(!me->services);
|
||||
me->services = &services;
|
||||
switch (this_state().status())
|
||||
{
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
me->child_builder.activate(services);
|
||||
if(me->supports_hedge)
|
||||
me->child_builder.activate(services);
|
||||
if (me->supports_hedge)
|
||||
me->hedge_builder.activate(services);
|
||||
|
||||
|
||||
me->child = me->child_builder.build();
|
||||
set_price(p.value());
|
||||
set_quantity(q.value());
|
||||
services.child_updates.sub(key(),
|
||||
[this](auto const &os) { this->on_child_order_update(os); });
|
||||
services.book_updates.sub(SecurityKey(me->def.security_key()),
|
||||
[this](auto const &b) { this->on_book_update(b); });
|
||||
services.subscribe_to_child_updates(
|
||||
[this](auto const &os) { this->on_child_order_update(os); });
|
||||
services.subscribe_to_book(
|
||||
SecurityKey(me->def.security_key()),
|
||||
[this](auto const &b) { this->on_book_update(b); });
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
break;
|
||||
|
||||
@@ -207,7 +172,7 @@ void BookAlgo::on_activate(Price p, Quantity q, OrderServices& services,PerfTrac
|
||||
// state)
|
||||
case ORDER_STATUS_WORKING:
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
@@ -216,14 +181,11 @@ void BookAlgo::on_activate(Price p, Quantity q, OrderServices& services,PerfTrac
|
||||
report_state();
|
||||
}
|
||||
|
||||
void BookAlgo::on_update(Price p, Quantity q,PerfTrace const&)
|
||||
{
|
||||
void BookAlgo::on_update(Price p, Quantity q, trace::CorID const &) {
|
||||
LOG(OrderMan, Info, "Updating BookAlgo {}", key().value());
|
||||
switch (this_state().status())
|
||||
{
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (child_state().status())
|
||||
{
|
||||
switch (child_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
set_price(p.value());
|
||||
set_quantity(q.value());
|
||||
@@ -231,8 +193,8 @@ void BookAlgo::on_update(Price p, Quantity q,PerfTrace const&)
|
||||
case ORDER_STATUS_WORKING:
|
||||
set_price(p.value());
|
||||
set_quantity(q.value());
|
||||
if(!me->fake_detached_child)
|
||||
me->child.update(p,q);
|
||||
if (!me->fake_detached_child)
|
||||
me->child.update(p, q);
|
||||
break;
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
// we launched our child but we are not yet able
|
||||
@@ -242,7 +204,7 @@ void BookAlgo::on_update(Price p, Quantity q,PerfTrace const&)
|
||||
|
||||
// if the child was in any of these states we couldn't be
|
||||
// in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -273,25 +235,22 @@ void BookAlgo::on_update(Price p, Quantity q,PerfTrace const&)
|
||||
report_state();
|
||||
}
|
||||
|
||||
void BookAlgo::on_crack(PerfTrace const&) {}
|
||||
void BookAlgo::on_crack(trace::CorID const &) {}
|
||||
|
||||
void BookAlgo::on_cancel(PerfTrace const&)
|
||||
{
|
||||
void BookAlgo::on_cancel(trace::CorID const &) {
|
||||
LOG(OrderMan, Info, "Canceling BookAlgo Order {}", key().value());
|
||||
|
||||
switch (this_state().status())
|
||||
{
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (child_state().status())
|
||||
{
|
||||
switch (child_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
if(!me->fake_detached_child)
|
||||
if (!me->fake_detached_child)
|
||||
me->child.cancel();
|
||||
for(auto& i : me->hedges)
|
||||
i.crack();
|
||||
for (auto &i : me->hedges)
|
||||
i.crack();
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
break;
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
@@ -300,7 +259,7 @@ void BookAlgo::on_cancel(PerfTrace const&)
|
||||
break;
|
||||
|
||||
// child cannot be in any of these if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -330,53 +289,50 @@ void BookAlgo::on_cancel(PerfTrace const&)
|
||||
report_state();
|
||||
}
|
||||
|
||||
void BookAlgo::transition(api::orderman::OrderStatus new_status, std::string msg)
|
||||
{
|
||||
void BookAlgo::transition(api::orderman::OrderStatus new_status,
|
||||
std::string msg) {
|
||||
auto const &from = OrderStatus_Name(me->state.status());
|
||||
auto const &to = OrderStatus_Name(new_status);
|
||||
LOG(OrderMan, Info, "Transitioning BookAlgo {} from {} to {}. Msg: {}", key().value(), from, to, msg);
|
||||
LOG(OrderMan, Info, "Transitioning BookAlgo {} from {} to {}. Msg: {}",
|
||||
key().value(), from, to, msg);
|
||||
me->state.set_status(new_status);
|
||||
}
|
||||
|
||||
void BookAlgo::on_child_order_update(const OrderStateUpdateEvent& ev)
|
||||
{
|
||||
if(me->fake_detached_child)
|
||||
return;
|
||||
if(ev.key != me->child.key())
|
||||
void BookAlgo::on_child_order_update(const OrderStateUpdateEvent &ev) {
|
||||
if (me->fake_detached_child)
|
||||
return;
|
||||
if (ev.key != me->child.key())
|
||||
me->hedges_state[ev.key] = ev.rebuild_state();
|
||||
else
|
||||
{
|
||||
else {
|
||||
api::orderman::OrderState orig_state = child_state();
|
||||
me->child_state = ev.rebuild_state();
|
||||
switch (this_state().status())
|
||||
{
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (ev.status)
|
||||
{
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
if(me->supports_hedge)
|
||||
send_hedge_if_new_fills(orig_state, ev);
|
||||
break;
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
transition(ORDER_STATUS_COMPLETED);
|
||||
if(me->supports_hedge)
|
||||
send_hedge_if_new_fills(orig_state, ev);
|
||||
break;
|
||||
switch (ev.status) {
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
if (me->supports_hedge)
|
||||
send_hedge_if_new_fills(orig_state, ev);
|
||||
break;
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
transition(ORDER_STATUS_COMPLETED);
|
||||
if (me->supports_hedge)
|
||||
send_hedge_if_new_fills(orig_state, ev);
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
transition(ORDER_STATUS_ERROR, "Unexpected child order state");
|
||||
break;
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
transition(ORDER_STATUS_ERROR, "Unexpected child order state");
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -398,52 +354,56 @@ void BookAlgo::on_child_order_update(const OrderStateUpdateEvent& ev)
|
||||
report_state();
|
||||
}
|
||||
|
||||
void BookAlgo::report_state()
|
||||
{
|
||||
void BookAlgo::report_state() {
|
||||
auto merged_state = me->state;
|
||||
merged_state.set_filled_quantity(child_state().filled_quantity());
|
||||
merged_state.set_last_price(child_state().last_price());
|
||||
merged_state.set_avg_price(child_state().avg_price());
|
||||
merged_state.set_display_status(bts::api::orderman::OrderState_DisplayStatus::OrderState_DisplayStatus_HIDDEN);
|
||||
merged_state.set_display_status(
|
||||
bts::api::orderman::OrderState_DisplayStatus::
|
||||
OrderState_DisplayStatus_HIDDEN);
|
||||
me->services->report_state(merged_state);
|
||||
}
|
||||
|
||||
void BookAlgo::payup(const md::PriceSize& opposite, const bts::orderman::BookUpdateEvent&, uint32_t cross_ticks)
|
||||
{
|
||||
void BookAlgo::payup(const md::PriceSize &opposite,
|
||||
const bts::orderman::BookUpdateEvent &,
|
||||
uint32_t cross_ticks) {
|
||||
double new_price = opposite.price() + me->tick_size * cross_ticks;
|
||||
me->child.update(Price(new_price), Quantity(this_state().quantity()));
|
||||
report_state();
|
||||
}
|
||||
|
||||
void BookAlgo::paydown(const md::PriceSize& opposite, const bts::orderman::BookUpdateEvent&, uint32_t cross_ticks)
|
||||
{
|
||||
void BookAlgo::paydown(const md::PriceSize &opposite,
|
||||
const bts::orderman::BookUpdateEvent &,
|
||||
uint32_t cross_ticks) {
|
||||
double new_price = opposite.price() - me->tick_size * cross_ticks;
|
||||
me->child.update(Price(new_price), Quantity(this_state().quantity()));
|
||||
report_state();
|
||||
}
|
||||
|
||||
void BookAlgo::detach(bts::api::orderman::OrderStatus status_after_detach)
|
||||
{
|
||||
void BookAlgo::detach(bts::api::orderman::OrderStatus status_after_detach) {
|
||||
me->fake_detached_child = true;
|
||||
me->child.crack();
|
||||
transition(status_after_detach);
|
||||
report_state();
|
||||
}
|
||||
|
||||
void BookAlgo::on_book_update(const bts::orderman::BookUpdateEvent&) {}
|
||||
void BookAlgo::on_book_update(const bts::orderman::BookUpdateEvent &) {}
|
||||
|
||||
bool BookAlgo::setup_hedge(const api::orderman::OrderDefinition&, const PreActivationOrderServices&, api::orderman::OrderDefinition*)
|
||||
{
|
||||
return false;
|
||||
bool BookAlgo::setup_hedge(const api::orderman::OrderDefinition &,
|
||||
const PreActivationOrderServices &,
|
||||
api::orderman::OrderDefinition *) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void BookAlgo::send_hedge_if_new_fills(const api::orderman::OrderState& orig_state, const OrderStateUpdateEvent& new_state)
|
||||
{
|
||||
UNUSED(orig_state);UNUSED(new_state);
|
||||
// uint32_t qty = new_state.state.filled_quantity() - orig_state.filled_quantity();
|
||||
// UNUSED(qty);
|
||||
}
|
||||
|
||||
}
|
||||
void BookAlgo::send_hedge_if_new_fills(
|
||||
const api::orderman::OrderState &orig_state,
|
||||
const OrderStateUpdateEvent &new_state) {
|
||||
UNUSED(orig_state);
|
||||
UNUSED(new_state);
|
||||
// uint32_t qty = new_state.state.filled_quantity() -
|
||||
// orig_state.filled_quantity(); UNUSED(qty);
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -24,10 +24,10 @@ public:
|
||||
const PreActivationOrderServices&) override;
|
||||
void on_init_recovered(const api::orderman::OrderRecord&,
|
||||
const PreActivationOrderServices &) override;
|
||||
void on_activate(Price,Quantity,OrderServices &, PerfTrace const&) override ;
|
||||
void on_update(Price , Quantity,PerfTrace const&) override ;
|
||||
void on_crack(PerfTrace const&) override ;
|
||||
void on_cancel(PerfTrace const&) override ;
|
||||
void on_activate(Price,Quantity,OrderServices &, trace::CorID const&) override ;
|
||||
void on_update(Price , Quantity,trace::CorID const&) override ;
|
||||
void on_crack(trace::CorID const&) override ;
|
||||
void on_cancel(trace::CorID const&) override ;
|
||||
|
||||
protected:
|
||||
virtual void setup_child(const api::orderman::OrderDefinition& parent, const PreActivationOrderServices&,
|
||||
|
||||
@@ -10,94 +10,87 @@ namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
|
||||
struct CoverOrder::PImpl
|
||||
{
|
||||
struct CoverOrder::PImpl {
|
||||
PImpl() {}
|
||||
|
||||
int hedge_ticks;
|
||||
OrderDefinition hedge_def;
|
||||
};
|
||||
|
||||
int hedge_ticks;
|
||||
OrderDefinition hedge_def;
|
||||
};
|
||||
|
||||
CoverOrder::CoverOrder() : me(new PImpl()) {}
|
||||
|
||||
CoverOrder::~CoverOrder() {}
|
||||
|
||||
void CoverOrder::setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
||||
api::orderman::OrderState& initial_state, api::orderman::OrderDefinition* out_child)
|
||||
{
|
||||
void CoverOrder::setup_child(const api::orderman::OrderDefinition &this_def,
|
||||
const PreActivationOrderServices &,
|
||||
api::orderman::OrderState &initial_state,
|
||||
api::orderman::OrderDefinition *out_child) {
|
||||
initial_state.mutable_cover();
|
||||
auto const &cover_def = this_def.custom_def().cover();
|
||||
auto const &cover_def = this_def.custom_def().cover();
|
||||
*out_child->mutable_custom_def() = cover_def.child();
|
||||
|
||||
me->hedge_ticks = cover_def.hedge_ticks();
|
||||
|
||||
me->hedge_ticks = cover_def.hedge_ticks();
|
||||
}
|
||||
|
||||
bool CoverOrder::setup_hedge(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
||||
api::orderman::OrderDefinition* out_hedge)
|
||||
{
|
||||
bool CoverOrder::setup_hedge(const api::orderman::OrderDefinition &this_def,
|
||||
const PreActivationOrderServices &,
|
||||
api::orderman::OrderDefinition *out_hedge) {
|
||||
auto const &cover_def = this_def.custom_def().cover();
|
||||
if(cover_def.has_hedge())
|
||||
{
|
||||
*out_hedge->mutable_custom_def() = cover_def.hedge();
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
if (cover_def.has_hedge()) {
|
||||
*out_hedge->mutable_custom_def() = cover_def.hedge();
|
||||
if (definition().side() == api::SIDE_BUY)
|
||||
out_hedge->set_side(api::SIDE_SELL);
|
||||
else
|
||||
out_hedge->set_side(api::SIDE_BUY);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
void CoverOrder::send_hedge_if_new_fills(const api::orderman::OrderState& orig_state, const OrderStateUpdateEvent& new_state)
|
||||
{
|
||||
void CoverOrder::send_hedge_if_new_fills(
|
||||
const api::orderman::OrderState &orig_state,
|
||||
const OrderStateUpdateEvent &new_state) {
|
||||
uint32_t qty = new_state.filled_quantity - orig_state.filled_quantity();
|
||||
if(qty > 0)
|
||||
{
|
||||
if (qty > 0) {
|
||||
double prx = new_state.last_price;
|
||||
if(definition().side() == api::SIDE_BUY) //if this is a buy, I'm going to sell my hedge, nticks higher than the fill price
|
||||
if (definition().side() ==
|
||||
api::SIDE_BUY) // if this is a buy, I'm going to sell my hedge, nticks
|
||||
// higher than the fill price
|
||||
prx = prx + tick_size() * me->hedge_ticks;
|
||||
else
|
||||
prx = prx - tick_size() * me->hedge_ticks;
|
||||
|
||||
|
||||
build_and_send_hedge(Price(prx), Quantity(qty));
|
||||
}
|
||||
}
|
||||
|
||||
void CoverOrder::on_book_update(const bts::orderman::BookUpdateEvent& )
|
||||
{
|
||||
switch (this_state().status())
|
||||
{
|
||||
void CoverOrder::on_book_update(const bts::orderman::BookUpdateEvent &) {
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (child_state().status())
|
||||
{
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
{
|
||||
activate_child(Price(this_state().price()), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
report_state();
|
||||
}
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
{
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
switch (child_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE: {
|
||||
activate_child(Price(this_state().price()),
|
||||
Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
report_state();
|
||||
} break;
|
||||
case ORDER_STATUS_WORKING: {
|
||||
} break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -113,9 +106,8 @@ void CoverOrder::on_book_update(const bts::orderman::BookUpdateEvent& )
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -10,101 +10,90 @@ namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
|
||||
struct JoinOrder::PImpl
|
||||
{
|
||||
struct JoinOrder::PImpl {
|
||||
PImpl() {}
|
||||
|
||||
spark::QuantityMeasure join_threshold;
|
||||
|
||||
md::PriceSize opposite_at_inception;
|
||||
|
||||
spark::QuantityMeasure join_threshold;
|
||||
|
||||
md::PriceSize opposite_at_inception;
|
||||
bool did_join;
|
||||
};
|
||||
|
||||
|
||||
JoinOrder::JoinOrder() : me(new PImpl()) {}
|
||||
|
||||
JoinOrder::~JoinOrder() {}
|
||||
|
||||
void JoinOrder::setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
||||
api::orderman::OrderState& initial_state, api::orderman::OrderDefinition* out_child)
|
||||
{
|
||||
void JoinOrder::setup_child(const api::orderman::OrderDefinition &this_def,
|
||||
const PreActivationOrderServices &,
|
||||
api::orderman::OrderState &initial_state,
|
||||
api::orderman::OrderDefinition *out_child) {
|
||||
initial_state.mutable_join();
|
||||
auto const &join_def = this_def.custom_def().join();
|
||||
auto const &join_def = this_def.custom_def().join();
|
||||
*out_child->mutable_custom_def() = join_def.child();
|
||||
|
||||
|
||||
me->join_threshold = join_def.join_threshold();
|
||||
me->did_join = false;
|
||||
}
|
||||
|
||||
void JoinOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
{
|
||||
switch (this_state().status())
|
||||
{
|
||||
void JoinOrder::on_book_update(const bts::orderman::BookUpdateEvent &book) {
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (child_state().status())
|
||||
{
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
{
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
{
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
activate_child(Price(this_state().price()), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
else
|
||||
{
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
activate_child(Price(this_state().price()), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
report_state();
|
||||
switch (child_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE: {
|
||||
if (definition().side() == api::SIDE_BUY) {
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
activate_child(Price(this_state().price()),
|
||||
Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
} else {
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
activate_child(Price(this_state().price()),
|
||||
Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
{
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(me->join_threshold, Quantity(this_state().quantity()));
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
{
|
||||
const auto& same = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if(me->opposite_at_inception.price() > same.price())
|
||||
{}
|
||||
//we join exactly once, but we can't detach because we have to hang around and look for hedges
|
||||
else if(!me->did_join && same.size() >= thresh.value())
|
||||
{
|
||||
me->did_join = true;
|
||||
update_child(Price(same.price()), Quantity(this_state().quantity()));
|
||||
report_state();
|
||||
}
|
||||
report_state();
|
||||
} break;
|
||||
case ORDER_STATUS_WORKING: {
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(
|
||||
me->join_threshold, Quantity(this_state().quantity()));
|
||||
if (definition().side() == api::SIDE_BUY) {
|
||||
const auto &same = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if (me->opposite_at_inception.price() > same.price()) {
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto& same = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if(me->opposite_at_inception.price() < same.price())
|
||||
{}
|
||||
else if(!me->did_join && same.size() >= thresh.value())
|
||||
{
|
||||
me->did_join = true;
|
||||
update_child(Price(same.price()), Quantity(this_state().quantity()));
|
||||
report_state();
|
||||
}
|
||||
// we join exactly once, but we can't detach because we have to hang
|
||||
// around and look for hedges
|
||||
else if (!me->did_join && same.size() >= thresh.value()) {
|
||||
me->did_join = true;
|
||||
update_child(Price(same.price()),
|
||||
Quantity(this_state().quantity()));
|
||||
report_state();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
} else {
|
||||
const auto &same = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if (me->opposite_at_inception.price() < same.price()) {
|
||||
} else if (!me->did_join && same.size() >= thresh.value()) {
|
||||
me->did_join = true;
|
||||
update_child(Price(same.price()),
|
||||
Quantity(this_state().quantity()));
|
||||
report_state();
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -120,9 +109,8 @@ void JoinOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -10,184 +10,170 @@ namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
|
||||
struct LeanOrder::PImpl
|
||||
{
|
||||
PImpl() : cross_threshold(), max_chase_ticks(0), trade_toward_behavior(LeanOrderDefinition_OnToward_NO_CHANGE), opposite_at_inception(), num_chases(0)
|
||||
{}
|
||||
|
||||
spark::QuantityMeasure cross_threshold;
|
||||
uint32_t max_chase_ticks;
|
||||
struct LeanOrder::PImpl {
|
||||
PImpl()
|
||||
: cross_threshold(), max_chase_ticks(0),
|
||||
trade_toward_behavior(LeanOrderDefinition_OnToward_NO_CHANGE),
|
||||
opposite_at_inception(), num_chases(0) {}
|
||||
|
||||
spark::QuantityMeasure cross_threshold;
|
||||
uint32_t max_chase_ticks;
|
||||
LeanOrderDefinition_OnToward trade_toward_behavior;
|
||||
|
||||
md::PriceSize opposite_at_inception;
|
||||
|
||||
md::PriceSize opposite_at_inception;
|
||||
uint32_t num_chases;
|
||||
};
|
||||
|
||||
|
||||
LeanOrder::LeanOrder() : me(new PImpl()) {}
|
||||
|
||||
LeanOrder::~LeanOrder() {}
|
||||
|
||||
void LeanOrder::setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
||||
api::orderman::OrderState& initial_state, api::orderman::OrderDefinition* out_child)
|
||||
{
|
||||
void LeanOrder::setup_child(const api::orderman::OrderDefinition &this_def,
|
||||
const PreActivationOrderServices &,
|
||||
api::orderman::OrderState &initial_state,
|
||||
api::orderman::OrderDefinition *out_child) {
|
||||
initial_state.mutable_lean();
|
||||
auto const &lean_def = this_def.custom_def().lean();
|
||||
auto const &lean_def = this_def.custom_def().lean();
|
||||
*out_child->mutable_custom_def() = lean_def.child();
|
||||
|
||||
|
||||
me->cross_threshold = lean_def.cross_threshold();
|
||||
me->max_chase_ticks = lean_def.max_chase_ticks();
|
||||
me->trade_toward_behavior = lean_def.trade_toward_behavior();
|
||||
me->max_chase_ticks = lean_def.max_chase_ticks();
|
||||
me->trade_toward_behavior = lean_def.trade_toward_behavior();
|
||||
}
|
||||
|
||||
void LeanOrder::payup(const md::PriceSize&, const bts::orderman::BookUpdateEvent& book, uint32_t cross_ticks /*= 0*/)
|
||||
{
|
||||
double new_price = me->opposite_at_inception.price() + tick_size() * cross_ticks;
|
||||
double limit = me->opposite_at_inception.price() + tick_size() * me->max_chase_ticks;
|
||||
if(new_price <= limit)
|
||||
void LeanOrder::payup(const md::PriceSize &,
|
||||
const bts::orderman::BookUpdateEvent &book,
|
||||
uint32_t cross_ticks /*= 0*/) {
|
||||
double new_price =
|
||||
me->opposite_at_inception.price() + tick_size() * cross_ticks;
|
||||
double limit =
|
||||
me->opposite_at_inception.price() + tick_size() * me->max_chase_ticks;
|
||||
if (new_price <= limit)
|
||||
update_child(Price(new_price), Quantity(this_state().quantity()));
|
||||
else
|
||||
{
|
||||
update_child(Price(limit), Quantity(this_state().quantity()));
|
||||
else {
|
||||
update_child(Price(limit), Quantity(this_state().quantity()));
|
||||
detach();
|
||||
return;
|
||||
}
|
||||
|
||||
const bts::orderman::BookSide& sellbook = book.book.side(api::Side::SIDE_SELL);
|
||||
for(const auto& i : sellbook)
|
||||
{
|
||||
}
|
||||
|
||||
const bts::orderman::BookSide &sellbook =
|
||||
book.book.side(api::Side::SIDE_SELL);
|
||||
for (const auto &i : sellbook) {
|
||||
me->opposite_at_inception = i;
|
||||
if(me->opposite_at_inception.price() > new_price)
|
||||
if (me->opposite_at_inception.price() > new_price)
|
||||
break;
|
||||
}
|
||||
if(me->opposite_at_inception.price() <= new_price)
|
||||
detach();
|
||||
me->num_chases += cross_ticks+1;
|
||||
if(me->num_chases >= me->max_chase_ticks)
|
||||
detach();
|
||||
}
|
||||
if (me->opposite_at_inception.price() <= new_price)
|
||||
detach();
|
||||
me->num_chases += cross_ticks + 1;
|
||||
if (me->num_chases >= me->max_chase_ticks)
|
||||
detach();
|
||||
report_state();
|
||||
}
|
||||
|
||||
void LeanOrder::paydown(const md::PriceSize&, const bts::orderman::BookUpdateEvent& book, uint32_t cross_ticks /*= 0*/)
|
||||
{
|
||||
double new_price = me->opposite_at_inception.price() - tick_size() * cross_ticks;
|
||||
double limit = me->opposite_at_inception.price() - tick_size() * me->max_chase_ticks;
|
||||
if(new_price >= limit)
|
||||
void LeanOrder::paydown(const md::PriceSize &,
|
||||
const bts::orderman::BookUpdateEvent &book,
|
||||
uint32_t cross_ticks /*= 0*/) {
|
||||
double new_price =
|
||||
me->opposite_at_inception.price() - tick_size() * cross_ticks;
|
||||
double limit =
|
||||
me->opposite_at_inception.price() - tick_size() * me->max_chase_ticks;
|
||||
if (new_price >= limit)
|
||||
update_child(Price(new_price), Quantity(this_state().quantity()));
|
||||
else
|
||||
{
|
||||
update_child(Price(limit), Quantity(this_state().quantity()));
|
||||
else {
|
||||
update_child(Price(limit), Quantity(this_state().quantity()));
|
||||
detach();
|
||||
return;
|
||||
}
|
||||
|
||||
const bts::orderman::BookSide& buybook = book.book.side(api::Side::SIDE_BUY);
|
||||
for(const auto& i : buybook)
|
||||
{
|
||||
}
|
||||
|
||||
const bts::orderman::BookSide &buybook = book.book.side(api::Side::SIDE_BUY);
|
||||
for (const auto &i : buybook) {
|
||||
me->opposite_at_inception = i;
|
||||
if(me->opposite_at_inception.price() < new_price)
|
||||
if (me->opposite_at_inception.price() < new_price)
|
||||
break;
|
||||
}
|
||||
if(me->opposite_at_inception.price() >= new_price)
|
||||
detach();
|
||||
me->num_chases += cross_ticks+1;
|
||||
if(me->num_chases >= me->max_chase_ticks)
|
||||
detach();
|
||||
}
|
||||
if (me->opposite_at_inception.price() >= new_price)
|
||||
detach();
|
||||
me->num_chases += cross_ticks + 1;
|
||||
if (me->num_chases >= me->max_chase_ticks)
|
||||
detach();
|
||||
report_state();
|
||||
}
|
||||
|
||||
void LeanOrder::choose_trade_toward_behavior(const md::PriceSize& opposite)
|
||||
{
|
||||
switch (me->trade_toward_behavior)
|
||||
{
|
||||
case LeanOrderDefinition_OnToward_CANCEL:
|
||||
{
|
||||
cancel_child();
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
}
|
||||
void LeanOrder::choose_trade_toward_behavior(const md::PriceSize &opposite) {
|
||||
switch (me->trade_toward_behavior) {
|
||||
case LeanOrderDefinition_OnToward_CANCEL: {
|
||||
cancel_child();
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
} break;
|
||||
case LeanOrderDefinition_OnToward_DETACH: {
|
||||
detach();
|
||||
} break;
|
||||
case LeanOrderDefinition_OnToward_REPRICE: {
|
||||
me->opposite_at_inception = opposite;
|
||||
} break;
|
||||
case LeanOrderDefinition_OnToward_NO_CHANGE:
|
||||
default:
|
||||
break;
|
||||
case LeanOrderDefinition_OnToward_DETACH:
|
||||
{
|
||||
detach();
|
||||
}
|
||||
break;
|
||||
case LeanOrderDefinition_OnToward_REPRICE:
|
||||
{
|
||||
me->opposite_at_inception = opposite;
|
||||
}
|
||||
break;
|
||||
case LeanOrderDefinition_OnToward_NO_CHANGE:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LeanOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
{
|
||||
switch (this_state().status())
|
||||
{
|
||||
void LeanOrder::on_book_update(const bts::orderman::BookUpdateEvent &book) {
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (child_state().status())
|
||||
{
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
{
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
else
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
|
||||
activate_child(Price(this_state().price()), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
report_state();
|
||||
}
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
{
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(me->cross_threshold, Quantity(this_state().quantity()));
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
{
|
||||
const auto& opposite = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if(opposite.price() < me->opposite_at_inception.price())
|
||||
choose_trade_toward_behavior(opposite);
|
||||
else if(opposite.price() > me->opposite_at_inception.price())
|
||||
payup(opposite, book); //you missed, payup/join
|
||||
else
|
||||
{
|
||||
if(opposite.size() <= thresh.value())
|
||||
payup(opposite, book);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto& opposite = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if(opposite.price() > me->opposite_at_inception.price())
|
||||
choose_trade_toward_behavior(opposite);
|
||||
else if(opposite.price() < me->opposite_at_inception.price())
|
||||
paydown(opposite, book);
|
||||
else
|
||||
{
|
||||
if(opposite.size() <= thresh.value())
|
||||
paydown(opposite, book);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
switch (child_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE: {
|
||||
if (definition().side() == api::SIDE_BUY)
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
else
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
activate_child(Price(this_state().price()),
|
||||
Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
report_state();
|
||||
} break;
|
||||
case ORDER_STATUS_WORKING: {
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(
|
||||
me->cross_threshold, Quantity(this_state().quantity()));
|
||||
if (definition().side() == api::SIDE_BUY) {
|
||||
const auto &opposite = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if (opposite.price() < me->opposite_at_inception.price())
|
||||
choose_trade_toward_behavior(opposite);
|
||||
else if (opposite.price() > me->opposite_at_inception.price())
|
||||
payup(opposite, book); // you missed, payup/join
|
||||
else {
|
||||
if (opposite.size() <= thresh.value())
|
||||
payup(opposite, book);
|
||||
}
|
||||
} else {
|
||||
const auto &opposite = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if (opposite.price() > me->opposite_at_inception.price())
|
||||
choose_trade_toward_behavior(opposite);
|
||||
else if (opposite.price() < me->opposite_at_inception.price())
|
||||
paydown(opposite, book);
|
||||
else {
|
||||
if (opposite.size() <= thresh.value())
|
||||
paydown(opposite, book);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -203,9 +189,8 @@ void LeanOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -10,75 +10,65 @@ namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
|
||||
struct OffsetOrder::PImpl
|
||||
{
|
||||
struct OffsetOrder::PImpl {
|
||||
PImpl() {}
|
||||
|
||||
uint32_t tick_offset_on_deploy;
|
||||
};
|
||||
|
||||
uint32_t tick_offset_on_deploy;
|
||||
};
|
||||
|
||||
OffsetOrder::OffsetOrder() : me(new PImpl()) {}
|
||||
|
||||
OffsetOrder::~OffsetOrder() {}
|
||||
|
||||
void OffsetOrder::setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
||||
api::orderman::OrderState& initial_state, api::orderman::OrderDefinition* out_child)
|
||||
{
|
||||
void OffsetOrder::setup_child(const api::orderman::OrderDefinition &this_def,
|
||||
const PreActivationOrderServices &,
|
||||
api::orderman::OrderState &initial_state,
|
||||
api::orderman::OrderDefinition *out_child) {
|
||||
initial_state.mutable_tick();
|
||||
auto const &offset_def = this_def.custom_def().offset();
|
||||
auto const &offset_def = this_def.custom_def().offset();
|
||||
*out_child->mutable_custom_def() = offset_def.child();
|
||||
|
||||
me->tick_offset_on_deploy = offset_def.tick_offset_on_deploy();
|
||||
|
||||
me->tick_offset_on_deploy = offset_def.tick_offset_on_deploy();
|
||||
}
|
||||
|
||||
|
||||
void OffsetOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
{
|
||||
switch (this_state().status())
|
||||
{
|
||||
void OffsetOrder::on_book_update(const bts::orderman::BookUpdateEvent &book) {
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (child_state().status())
|
||||
{
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
{
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
{
|
||||
const auto& same = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
double price = same.price() - tick_size() * me->tick_offset_on_deploy;
|
||||
activate_child(Price(price), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto& same = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
double price = same.price() + tick_size() * me->tick_offset_on_deploy;
|
||||
activate_child(Price(price), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
report_state();
|
||||
switch (child_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE: {
|
||||
if (definition().side() == api::SIDE_BUY) {
|
||||
const auto &same = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
double price =
|
||||
same.price() - tick_size() * me->tick_offset_on_deploy;
|
||||
activate_child(Price(price), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
} else {
|
||||
const auto &same = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
double price =
|
||||
same.price() + tick_size() * me->tick_offset_on_deploy;
|
||||
activate_child(Price(price), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
{
|
||||
detach();
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
report_state();
|
||||
} break;
|
||||
case ORDER_STATUS_WORKING: {
|
||||
detach();
|
||||
} break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -94,9 +84,8 @@ void OffsetOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -10,165 +10,146 @@ namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
|
||||
struct TickOrder::PImpl
|
||||
{
|
||||
struct TickOrder::PImpl {
|
||||
PImpl() {}
|
||||
|
||||
uint32_t tick_offset_on_deploy;
|
||||
spark::QuantityMeasure cross_threshold;
|
||||
uint32_t cross_ticks;
|
||||
uint32_t max_chase_ticks;
|
||||
bool detach_on_toward;
|
||||
|
||||
md::PriceSize opposite_at_inception;
|
||||
};
|
||||
|
||||
uint32_t tick_offset_on_deploy;
|
||||
spark::QuantityMeasure cross_threshold;
|
||||
uint32_t cross_ticks;
|
||||
uint32_t max_chase_ticks;
|
||||
bool detach_on_toward;
|
||||
|
||||
md::PriceSize opposite_at_inception;
|
||||
};
|
||||
|
||||
TickOrder::TickOrder() : me(new PImpl()) {}
|
||||
|
||||
TickOrder::~TickOrder() {}
|
||||
|
||||
void TickOrder::setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
||||
api::orderman::OrderState& initial_state, api::orderman::OrderDefinition* out_child)
|
||||
{
|
||||
void TickOrder::setup_child(const api::orderman::OrderDefinition &this_def,
|
||||
const PreActivationOrderServices &,
|
||||
api::orderman::OrderState &initial_state,
|
||||
api::orderman::OrderDefinition *out_child) {
|
||||
initial_state.mutable_tick();
|
||||
auto const &tick_def = this_def.custom_def().tick();
|
||||
auto const &tick_def = this_def.custom_def().tick();
|
||||
*out_child->mutable_custom_def() = tick_def.child();
|
||||
|
||||
me->tick_offset_on_deploy = tick_def.tick_offset_on_deploy();
|
||||
|
||||
me->tick_offset_on_deploy = tick_def.tick_offset_on_deploy();
|
||||
me->cross_threshold = tick_def.cross_threshold();
|
||||
me->cross_ticks = tick_def.cross_ticks();
|
||||
me->max_chase_ticks = tick_def.max_chase_ticks();
|
||||
me->max_chase_ticks = tick_def.max_chase_ticks();
|
||||
me->cross_threshold = tick_def.cross_threshold();
|
||||
me->detach_on_toward = tick_def.detach_on_toward();
|
||||
me->detach_on_toward = tick_def.detach_on_toward();
|
||||
}
|
||||
|
||||
void TickOrder::payup(const md::PriceSize& opposite, const bts::orderman::BookUpdateEvent&, uint32_t cross_ticks)
|
||||
{
|
||||
void TickOrder::payup(const md::PriceSize &opposite,
|
||||
const bts::orderman::BookUpdateEvent &,
|
||||
uint32_t cross_ticks) {
|
||||
double new_price = opposite.price() + tick_size() * cross_ticks;
|
||||
double limit = me->opposite_at_inception.price() + tick_size() * me->max_chase_ticks;
|
||||
if(new_price <= limit)
|
||||
double limit =
|
||||
me->opposite_at_inception.price() + tick_size() * me->max_chase_ticks;
|
||||
if (new_price <= limit)
|
||||
update_child(Price(new_price), Quantity(this_state().quantity()));
|
||||
else
|
||||
{
|
||||
else {
|
||||
update_child(Price(limit), Quantity(this_state().quantity()));
|
||||
detach();
|
||||
}
|
||||
report_state();
|
||||
}
|
||||
|
||||
void TickOrder::paydown(const md::PriceSize& opposite, const bts::orderman::BookUpdateEvent&, uint32_t cross_ticks)
|
||||
{
|
||||
void TickOrder::paydown(const md::PriceSize &opposite,
|
||||
const bts::orderman::BookUpdateEvent &,
|
||||
uint32_t cross_ticks) {
|
||||
double new_price = opposite.price() - tick_size() * cross_ticks;
|
||||
double limit = me->opposite_at_inception.price() - tick_size() * me->max_chase_ticks;
|
||||
if(new_price >= limit)
|
||||
double limit =
|
||||
me->opposite_at_inception.price() - tick_size() * me->max_chase_ticks;
|
||||
if (new_price >= limit)
|
||||
update_child(Price(new_price), Quantity(this_state().quantity()));
|
||||
else
|
||||
{
|
||||
update_child(Price(limit), Quantity(this_state().quantity()));
|
||||
else {
|
||||
update_child(Price(limit), Quantity(this_state().quantity()));
|
||||
detach();
|
||||
}
|
||||
report_state();
|
||||
}
|
||||
|
||||
void TickOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
{
|
||||
switch (this_state().status())
|
||||
{
|
||||
void TickOrder::on_book_update(const bts::orderman::BookUpdateEvent &book) {
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (child_state().status())
|
||||
{
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
{
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
{
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if(me->tick_offset_on_deploy > 0)
|
||||
{
|
||||
double price = me->opposite_at_inception.price() - tick_size() * me->tick_offset_on_deploy;
|
||||
activate_child(Price(price), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
else
|
||||
{
|
||||
activate_child(Price(this_state().price()), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
switch (child_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE: {
|
||||
if (definition().side() == api::SIDE_BUY) {
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if (me->tick_offset_on_deploy > 0) {
|
||||
double price = me->opposite_at_inception.price() -
|
||||
tick_size() * me->tick_offset_on_deploy;
|
||||
activate_child(Price(price), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
} else {
|
||||
activate_child(Price(this_state().price()),
|
||||
Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
else
|
||||
{
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if(me->tick_offset_on_deploy > 0)
|
||||
{
|
||||
double price = me->opposite_at_inception.price() + tick_size() * me->tick_offset_on_deploy;
|
||||
activate_child(Price(price), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
else
|
||||
{
|
||||
activate_child(Price(this_state().price()), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
} else {
|
||||
me->opposite_at_inception = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if (me->tick_offset_on_deploy > 0) {
|
||||
double price = me->opposite_at_inception.price() +
|
||||
tick_size() * me->tick_offset_on_deploy;
|
||||
activate_child(Price(price), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
} else {
|
||||
activate_child(Price(this_state().price()),
|
||||
Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
}
|
||||
report_state();
|
||||
}
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
{
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(me->cross_threshold, Quantity(this_state().quantity()));
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
{
|
||||
const auto& opposite = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if(opposite.price() < me->opposite_at_inception.price())
|
||||
{
|
||||
if(me->detach_on_toward)
|
||||
detach();
|
||||
else
|
||||
me->opposite_at_inception = opposite;
|
||||
}
|
||||
else if(opposite.price() > me->opposite_at_inception.price())
|
||||
report_state();
|
||||
} break;
|
||||
case ORDER_STATUS_WORKING: {
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(
|
||||
me->cross_threshold, Quantity(this_state().quantity()));
|
||||
if (definition().side() == api::SIDE_BUY) {
|
||||
const auto &opposite = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if (opposite.price() < me->opposite_at_inception.price()) {
|
||||
if (me->detach_on_toward)
|
||||
detach();
|
||||
else
|
||||
me->opposite_at_inception = opposite;
|
||||
} else if (opposite.price() > me->opposite_at_inception.price())
|
||||
payup(opposite, book, me->cross_ticks);
|
||||
else {
|
||||
if (opposite.size() <= thresh.value())
|
||||
payup(opposite, book, me->cross_ticks);
|
||||
else
|
||||
{
|
||||
if(opposite.size() <= thresh.value())
|
||||
payup(opposite, book, me->cross_ticks);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto& opposite = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if(opposite.price() > me->opposite_at_inception.price())
|
||||
{
|
||||
if(me->detach_on_toward)
|
||||
detach();
|
||||
else
|
||||
me->opposite_at_inception = opposite;
|
||||
}
|
||||
else if(opposite.price() < me->opposite_at_inception.price())
|
||||
} else {
|
||||
const auto &opposite = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if (opposite.price() > me->opposite_at_inception.price()) {
|
||||
if (me->detach_on_toward)
|
||||
detach();
|
||||
else
|
||||
me->opposite_at_inception = opposite;
|
||||
} else if (opposite.price() < me->opposite_at_inception.price())
|
||||
paydown(opposite, book, me->cross_ticks);
|
||||
else {
|
||||
if (opposite.size() <= thresh.value())
|
||||
paydown(opposite, book, me->cross_ticks);
|
||||
else
|
||||
{
|
||||
if(opposite.size() <= thresh.value())
|
||||
paydown(opposite, book, me->cross_ticks);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -184,9 +165,8 @@ void TickOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "TopOrder.h"
|
||||
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
|
||||
@@ -11,118 +11,102 @@ namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
|
||||
struct TopOrder::PImpl
|
||||
{
|
||||
struct TopOrder::PImpl {
|
||||
PImpl() {}
|
||||
|
||||
uint32_t watch_level_tick_offset;
|
||||
spark::QuantityMeasure top_threshold;
|
||||
};
|
||||
|
||||
uint32_t watch_level_tick_offset;
|
||||
spark::QuantityMeasure top_threshold;
|
||||
};
|
||||
|
||||
TopOrder::TopOrder() : me(new PImpl()) {}
|
||||
|
||||
TopOrder::~TopOrder() {}
|
||||
|
||||
void TopOrder::setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
||||
api::orderman::OrderState& initial_state, api::orderman::OrderDefinition* out_child)
|
||||
{
|
||||
void TopOrder::setup_child(const api::orderman::OrderDefinition &this_def,
|
||||
const PreActivationOrderServices &,
|
||||
api::orderman::OrderState &initial_state,
|
||||
api::orderman::OrderDefinition *out_child) {
|
||||
initial_state.mutable_top();
|
||||
auto const &top_def = this_def.custom_def().top();
|
||||
*out_child->mutable_custom_def() = top_def.child();
|
||||
|
||||
|
||||
me->watch_level_tick_offset = top_def.watch_level_tick_offset();
|
||||
me->top_threshold = top_def.top_threshold();
|
||||
}
|
||||
|
||||
|
||||
void TopOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
{
|
||||
switch (this_state().status())
|
||||
{
|
||||
void TopOrder::on_book_update(const bts::orderman::BookUpdateEvent &book) {
|
||||
switch (this_state().status()) {
|
||||
case ORDER_STATUS_WORKING:
|
||||
switch (child_state().status())
|
||||
{
|
||||
case ORDER_STATUS_INACTIVE:
|
||||
{
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(me->top_threshold, Quantity(this_state().quantity()));
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
{
|
||||
const auto& same = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if(securities::test_price_equality(this_state().price(), same.price(), tick_size()))
|
||||
{
|
||||
if(same.size() <= thresh.value())
|
||||
{
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
return;
|
||||
}
|
||||
switch (child_state().status()) {
|
||||
case ORDER_STATUS_INACTIVE: {
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(
|
||||
me->top_threshold, Quantity(this_state().quantity()));
|
||||
if (definition().side() == api::SIDE_BUY) {
|
||||
const auto &same = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if (securities::test_price_equality(this_state().price(),
|
||||
same.price(), tick_size())) {
|
||||
if (same.size() <= thresh.value()) {
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto& same = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if(securities::test_price_equality(this_state().price(), same.price(), tick_size()))
|
||||
{
|
||||
if(same.size() <= thresh.value())
|
||||
{
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
activate_child(Price(this_state().price()), Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
report_state();
|
||||
}
|
||||
break;
|
||||
case ORDER_STATUS_WORKING:
|
||||
{
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(me->top_threshold, Quantity(this_state().quantity()));
|
||||
if(definition().side() == api::SIDE_BUY)
|
||||
{
|
||||
const auto& same = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if(securities::test_price_equality(this_state().price(), same.price(), tick_size()))
|
||||
{
|
||||
if(same.size() <= thresh.value())
|
||||
{
|
||||
cancel_child();
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
}
|
||||
} else {
|
||||
const auto &same = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if (securities::test_price_equality(this_state().price(),
|
||||
same.price(), tick_size())) {
|
||||
if (same.size() <= thresh.value()) {
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto& same = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if(securities::test_price_equality(this_state().price(), same.price(), tick_size()))
|
||||
{
|
||||
if(same.size() <= thresh.value())
|
||||
{
|
||||
cancel_child();
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
activate_child(Price(this_state().price()),
|
||||
Quantity(this_state().quantity()));
|
||||
transition(ORDER_STATUS_WORKING);
|
||||
report_state();
|
||||
} break;
|
||||
case ORDER_STATUS_WORKING: {
|
||||
Quantity thresh = SpreaderUtils::_quantity_threshold(
|
||||
me->top_threshold, Quantity(this_state().quantity()));
|
||||
if (definition().side() == api::SIDE_BUY) {
|
||||
const auto &same = book.book.side(api::Side::SIDE_BUY)[0];
|
||||
if (securities::test_price_equality(this_state().price(),
|
||||
same.price(), tick_size())) {
|
||||
if (same.size() <= thresh.value()) {
|
||||
cancel_child();
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const auto &same = book.book.side(api::Side::SIDE_SELL)[0];
|
||||
if (securities::test_price_equality(this_state().price(),
|
||||
same.price(), tick_size())) {
|
||||
if (same.size() <= thresh.value()) {
|
||||
cancel_child();
|
||||
transition(ORDER_STATUS_CANCELED);
|
||||
report_state();
|
||||
}
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
case ORDER_STATUS_PENDING_WORKING:
|
||||
break;
|
||||
|
||||
// child cannot be in any of these states if we are in the working state
|
||||
case ORDER_STATUS_DISCONNECTED:
|
||||
case ORDER_STATUS_ERROR:
|
||||
case ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
case ORDER_STATUS_CANCELED:
|
||||
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ORDER_STATUS_COMPLETED:
|
||||
@@ -138,9 +122,8 @@ void TopOrder::on_book_update(const bts::orderman::BookUpdateEvent& book)
|
||||
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
// 5. called when the user cancels an order
|
||||
virtual void on_cancel() = 0;
|
||||
// 6. on market data update (support late joiners)
|
||||
virtual void on_book(const Book&,PerfTrace &) = 0;
|
||||
virtual void on_book(const Book&,trace::CorID ) = 0;
|
||||
// 15. settlement price update
|
||||
virtual void on_settlement_price(const SettlementUpdateEvent&) = 0;
|
||||
// 7. time event called by the infrastructure
|
||||
@@ -43,7 +43,7 @@ public:
|
||||
virtual void on_quoter_order_error(SpreaderLegData& leg_data) = 0;
|
||||
// 9. on a quoter order fill
|
||||
virtual void on_quoter_order_fill(SpreaderLegData& leg_data,
|
||||
PerfTrace &) = 0;
|
||||
trace::CorID ) = 0;
|
||||
// 10. on a quoter status update without a fill
|
||||
virtual void on_quoter_order_status_update(SpreaderLegData& leg_data) = 0;
|
||||
// 11. when a hedger order ack from the exchange has an error
|
||||
|
||||
@@ -24,6 +24,7 @@ _bookkeeping_data_ptr(),
|
||||
_order_services(nullptr),
|
||||
_curr_timestamp(),
|
||||
_last_check_termination_timestamp(),
|
||||
_perf_ts(utils::TimeStamp::now()),
|
||||
_state_worker(nullptr),
|
||||
_start_state_worker(this),
|
||||
_waiting_for_initial_data_state_worker(this),
|
||||
@@ -54,26 +55,26 @@ void SpreaderOrder::on_init_recovered(const bts::api::orderman::OrderRecord& ord
|
||||
_report_state();
|
||||
}
|
||||
void SpreaderOrder::on_activate(Price price, Quantity quantity,
|
||||
OrderServices& services, const PerfTrace&){
|
||||
OrderServices& services, const trace::CorID&){
|
||||
if (!_active) return;
|
||||
SpreaderOrderServices spreader_order_services (&services);
|
||||
_on_activate(price, quantity, spreader_order_services);
|
||||
_report_state();
|
||||
}
|
||||
void SpreaderOrder::on_update(Price price, Quantity quantity,PerfTrace const&){
|
||||
void SpreaderOrder::on_update(Price price, Quantity quantity,trace::CorID const&){
|
||||
if (!_active) return;
|
||||
LOG(OrderMan, Info, "SpreaderOrder.on_update key:{} price:{} quantity:{}",
|
||||
key().value(), price.value(), quantity.value());
|
||||
_state_worker->on_update(price, quantity);
|
||||
_report_state();
|
||||
}
|
||||
void SpreaderOrder::on_cancel(PerfTrace const&){
|
||||
void SpreaderOrder::on_cancel(trace::CorID const&){
|
||||
if (!_active) return;
|
||||
LOG(OrderMan, Info, "SpreaderOrder.on_cancel key:{}", key().value());
|
||||
_state_worker->on_cancel();
|
||||
_report_state();
|
||||
}
|
||||
void SpreaderOrder::on_crack(PerfTrace const&){
|
||||
void SpreaderOrder::on_crack(trace::CorID const&){
|
||||
if (!_active) return;
|
||||
LOG(OrderMan, Info, "SpreaderOrder.on_crack key: {}", key().value());
|
||||
_state_worker->on_crack();
|
||||
@@ -100,10 +101,10 @@ void SpreaderOrder::_on_time(const bts::mercury::TimerEvent& t_event){
|
||||
}
|
||||
void SpreaderOrder::_on_book_update(const BookUpdateEvent& book_event){
|
||||
if (!_active) return;
|
||||
tracepoint (bts_trace,trace_order,(char*) "_on_book_update", key().value(),book_event.trace.md_sn,0);
|
||||
auto trace = book_event.trace;
|
||||
trace.spreader_on_book = utils::TimeStamp::now();
|
||||
_state_worker->on_book (book_event.book,trace);
|
||||
_order_services->log_scope(book_event.trace,
|
||||
"SpreaderOrder::_on_book_update", key().value());
|
||||
_state_worker->on_book (book_event.book,book_event.trace);
|
||||
//_keep_hedging_path_hot ();
|
||||
}
|
||||
void SpreaderOrder::_on_settlement_update(const SettlementUpdateEvent&
|
||||
settlement_event){
|
||||
@@ -115,10 +116,15 @@ void SpreaderOrder::_on_settlement_update(const SettlementUpdateEvent&
|
||||
settlement_event.settlement_price.price());
|
||||
_state_worker->on_settlement_price(settlement_event);
|
||||
}
|
||||
void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
|
||||
auto trace = ord_ev.trace;
|
||||
trace.spreader_on_child_order_update = utils::TimeStamp::now();
|
||||
void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
|
||||
if (!_active) return;
|
||||
_perf_ts=utils::TimeStamp::now();
|
||||
auto now = _perf_ts;
|
||||
char cb_type='q'; //q=quoter non fill, f=quoter fill, h=hedger
|
||||
auto scoped_log =
|
||||
_order_services->log_scope(ord_ev.trace,
|
||||
"SpreaderOrder::_on_child_order_update",
|
||||
key().value());
|
||||
SecurityKey sec_key = SecurityKey(ord_ev.definition->security_key());
|
||||
OrderKey order_key = ord_ev.key;
|
||||
bts::api::orderman::OrderStatus status = ord_ev.status;
|
||||
@@ -135,6 +141,7 @@ void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
|
||||
return;
|
||||
}
|
||||
SpreaderLegData* leg_data_ptr = _bookkeeping_data().find_leg_from_quoter_orderkey(order_key);
|
||||
_order_services->log(ord_ev.trace,"_on_child_order_update:1", key().value());
|
||||
if (leg_data_ptr != nullptr){
|
||||
SpreaderLegData& leg_data = *leg_data_ptr;
|
||||
ChildOrderError order_error = _get_order_error (ord_ev,
|
||||
@@ -148,19 +155,28 @@ void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
|
||||
filled_qty, order_price, last_price,
|
||||
ord_ev.last_msg, static_cast<int>(order_error));
|
||||
leg_data.order_update_message().on_order_update(ord_ev);
|
||||
_order_services->log(ord_ev.trace,"_on_child_order_update:2", key().value());
|
||||
if (order_error == ChildOrderError::NONE){
|
||||
if (leg_data.order_update_message().last_update_filled_quantity().value() > 0)
|
||||
_state_worker->on_quoter_order_fill (leg_data,trace);
|
||||
else
|
||||
_state_worker->on_quoter_order_status_update (leg_data);
|
||||
if (leg_data.order_update_message().last_update_filled_quantity().value() > 0){
|
||||
cb_type='f';
|
||||
LOG(OrderMan,Info,"h_p f child update {0}",_diff_ns());
|
||||
_state_worker->on_quoter_order_fill (leg_data,ord_ev.trace);
|
||||
}
|
||||
else{
|
||||
cb_type='q';
|
||||
LOG(OrderMan,Info,"h_p q child update {0}",_diff_ns());
|
||||
_state_worker->on_quoter_order_status_update (leg_data);
|
||||
_order_services->log(ord_ev.trace,"_on_child_order_update:4", key().value());
|
||||
}
|
||||
}
|
||||
else
|
||||
else
|
||||
_state_worker->on_quoter_order_error (leg_data);
|
||||
}
|
||||
else{
|
||||
// should be a hedge order then
|
||||
SingleFillHedger* hedger = _bookkeeping_data().find_hedger_from_orderkey(order_key);
|
||||
if (hedger != nullptr){
|
||||
cb_type='h';
|
||||
ChildOrderError order_error = _get_order_error (ord_ev,
|
||||
hedger->expecting_cancel_update());
|
||||
LOG(OrderMan, Info, "SpreaderOrder::_on_child_order_update hedger "
|
||||
@@ -184,10 +200,15 @@ void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
|
||||
key(), sec_key, order_key, status, filled_qty,
|
||||
last_price, ord_ev.last_msg);
|
||||
}
|
||||
LOG(OrderMan,Info,"h_p cb done {} {}",cb_type, _diff_ns());
|
||||
_send_check_termination(true);
|
||||
_order_services->log(ord_ev.trace,"_on_child_order_update:5", key().value());
|
||||
_report_state();
|
||||
LOG(Latency,Diag, "_on_child_order_update k:{} t:{}us", key().value(),
|
||||
(utils::TimeStamp::now() - trace.spreader_on_child_order_update).microseconds());
|
||||
_order_services->log(ord_ev.trace,"end_on_child_order_update", key().value());
|
||||
LOG(OrderMan, Info,"h_p end {} {}",cb_type, _diff_ns());
|
||||
LOG(OrderMan, Info,"_on_child_order_update {} {}",
|
||||
cb_type,
|
||||
(utils::TimeStamp::now()-now).nanoseconds());
|
||||
}
|
||||
// State Transformations ////////////////////
|
||||
/////////////////////////////////////////////
|
||||
@@ -315,15 +336,16 @@ bool SpreaderOrder::_quoters_can_accept_modification (){
|
||||
void SpreaderOrder::_send_hedge(size_t hedge_leg_index,
|
||||
Price hedge_price,
|
||||
Quantity hedge_qty,
|
||||
const PerfTrace &trace){
|
||||
LOG(OrderMan, Info, "SpreaderOrder::_send_hedge key:{} hedge_leg_index:{} "
|
||||
"hedge_price:{} hedge_qty:{} cid:{}",
|
||||
key(), hedge_leg_index, hedge_price, hedge_qty, trace.md_sn);
|
||||
const trace::CorID trace){
|
||||
auto scoped_log =
|
||||
_order_services->log_scope(trace,"SpreaderOrder::_send_hedge");
|
||||
SingleFillHedger& hedger = _bookkeeping_data().add_new_hedger (*this,
|
||||
hedge_leg_index,
|
||||
hedge_price,
|
||||
hedge_qty,
|
||||
_order_services,
|
||||
trace);
|
||||
_order_services->log(trace,"SpreaderOrder::_send_hedge:2");
|
||||
hedger.init ();
|
||||
}
|
||||
void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index,
|
||||
@@ -365,7 +387,12 @@ void SpreaderOrder::_report_state (){
|
||||
if (_activated()){
|
||||
SpreaderBookkeepingData& bookkeeping_data = _bookkeeping_data();
|
||||
auto& reported_state = bookkeeping_data.reported_state();
|
||||
api::orderman::OrderStatus status = _get_order_status ();
|
||||
api::orderman::OrderStatus status =
|
||||
// this is a temporary solution, until we get sniper working.
|
||||
// if there are no quoting legs, we simply mark the order
|
||||
// as cancelled (instead of completed with 0 filled qty)
|
||||
bookkeeping_data.n_quoting_legs() == 0 ? bts::api::orderman::OrderStatus::ORDER_STATUS_CANCELED :
|
||||
_get_order_status ();
|
||||
if(bookkeeping_data.inited_new()){
|
||||
Quantity filled_qty; Price avg_filled_price;
|
||||
bookkeeping_data.total_fills(filled_qty, avg_filled_price);
|
||||
@@ -440,4 +467,31 @@ Price SpreaderOrder::_compute_effective_price (Price input_price){
|
||||
else
|
||||
return input_price;
|
||||
}
|
||||
void SpreaderOrder::_keep_hedging_path_hot(){
|
||||
if (_inited()){
|
||||
SpreaderBookkeepingData& d=_bookkeeping_data();
|
||||
for (size_t i=0; i<d.n_legs(); i++){
|
||||
SpreaderLegData& q_leg = d.get_leg_data(i);
|
||||
if(q_leg.leg_property_data().is_active_quoting()){
|
||||
if(q_leg.order_update_message().out_quantity().value() > 0){
|
||||
for(size_t j=0; j<d.n_legs(); j++){
|
||||
SpreaderLegData& h_leg = d.get_leg_data(j);
|
||||
Price hedge_price;
|
||||
double hedge_qty;
|
||||
_compute_hedge_price_quantity(i,
|
||||
Price(1.0),
|
||||
1.0,
|
||||
Price(1.0),
|
||||
j,
|
||||
hedge_price,
|
||||
hedge_qty);
|
||||
UnhedgedDelta& leg_unhedged_delta = h_leg.unhedged_delta();
|
||||
leg_unhedged_delta.add_unhedged(hedge_price.value(), 0.0);
|
||||
leg_unhedged_delta.subtract_hedge_if_available(0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}}
|
||||
@@ -68,6 +68,7 @@ private:
|
||||
// spreader fires a periodic check
|
||||
bts::TimeStamp _last_check_termination_timestamp;
|
||||
// current state
|
||||
utils::TimeStamp _perf_ts;
|
||||
ISpreaderStateWorker* _state_worker;
|
||||
SpreaderStartStateWorker _start_state_worker;
|
||||
SpreaderWaitingForInitialDataStateWorker _waiting_for_initial_data_state_worker;
|
||||
@@ -99,15 +100,15 @@ public:
|
||||
const PreActivationOrderServices&) override;
|
||||
// this is in the critical path, orders can be submitted now
|
||||
// (this is only called once and after the on_init_new)
|
||||
void on_activate(Price, Quantity, OrderServices&,PerfTrace const&) override;
|
||||
void on_activate(Price, Quantity, OrderServices&,trace::CorID const&) override;
|
||||
// called when the user (or a higher level order)
|
||||
// updates the order's price or quantity
|
||||
void on_update(Price, Quantity,PerfTrace const&) override;
|
||||
void on_update(Price, Quantity,trace::CorID const&) override;
|
||||
// called when the user (or higher level order) cancels an order
|
||||
void on_cancel(PerfTrace const&) override;
|
||||
void on_cancel(trace::CorID const&) override;
|
||||
// crack means to let the lower level orders lose from the higher
|
||||
// level order and cancel the higher level order
|
||||
void on_crack(PerfTrace const&) override;
|
||||
void on_crack(trace::CorID const&) override;
|
||||
// ISpreaderManager ////////////////////
|
||||
////////////////////////////////////////
|
||||
public:
|
||||
@@ -178,7 +179,7 @@ private:
|
||||
void _send_hedge(size_t hedge_leg_index,
|
||||
Price hedge_price,
|
||||
Quantity hedge_qty,
|
||||
PerfTrace const&);
|
||||
trace::CorID const);
|
||||
void _compute_hedge_price_quantity(size_t filled_leg_index,
|
||||
Price filled_price,
|
||||
double filled_quantity,
|
||||
@@ -240,6 +241,14 @@ private:
|
||||
// (if settlements are not available returns nan)
|
||||
// Otherwise effectiv_price==order_price.
|
||||
Price _compute_effective_price (Price order_price);
|
||||
// retuns the nano seconds from last
|
||||
// order callback
|
||||
double _diff_ns(){
|
||||
double rv = (utils::TimeStamp::now()-_perf_ts).nanoseconds();
|
||||
_perf_ts = utils::TimeStamp::now();
|
||||
return rv;
|
||||
}
|
||||
void _keep_hedging_path_hot();
|
||||
// testing only //////////////////
|
||||
// this provides direct way to inject
|
||||
// data into the spreader, as well as
|
||||
@@ -251,7 +260,7 @@ public:
|
||||
SpreaderBookkeepingData& test_only_func_spreader_full_data () {return _bookkeeping_data();}
|
||||
int64_t test_cancel_timeout () {return _cancel_timeout;}
|
||||
void test_only_on_book_update(const bts::orderman::BookUpdateEvent& b) {_on_book_update(b);}
|
||||
void test_only_on_book_update(const bts::orderman::Book& b, PerfTrace const&trace) {_on_book_update(bts::orderman::BookUpdateEvent(b, trace));}
|
||||
void test_only_on_book_update(const bts::orderman::Book& b, trace::CorID const&trace) {_on_book_update(bts::orderman::BookUpdateEvent(b, trace));}
|
||||
void test_only_on_child_order_update (bts::orderman::OrderKey order_key, bts::api::orderman::OrderStatus status, Quantity total_filled_qty, Price avg_price)
|
||||
{
|
||||
bts::api::orderman::OrderDefinition order_def;
|
||||
@@ -260,7 +269,7 @@ public:
|
||||
order_state.set_status(status);
|
||||
order_state.set_filled_quantity(total_filled_qty.value());
|
||||
order_state.set_last_price(avg_price.value());
|
||||
OrderStateUpdateEvent ord_ev (order_key, OrderKey(0), order_def, order_state,PerfTrace());
|
||||
OrderStateUpdateEvent ord_ev (order_key, OrderKey(0), order_def, order_state,trace::CorID());
|
||||
_on_child_order_update (ord_ev);
|
||||
}
|
||||
void test_only_on_init_new(const bts::api::orderman::OrderDefinition& order_def, const SpreaderPreActivationOrderServices& pao_services) {_on_init_new(order_def, pao_services);}
|
||||
|
||||
@@ -14,7 +14,7 @@ SingleFillHedger::SingleFillHedger(ISpreaderManager& spreader_manager,
|
||||
ChildOrder& child_order,
|
||||
bts::Price hedge_price,
|
||||
bts::Quantity hedge_quantity,
|
||||
PerfTrace const &fill_trace,
|
||||
trace::CorID const &fill_trace,
|
||||
bool is_live)
|
||||
:
|
||||
_spreader(spreader_manager),
|
||||
@@ -94,7 +94,7 @@ void SingleFillHedger::on_order_update(const SpreaderChildOrderState& u){
|
||||
u.last_update_filled_quantity());
|
||||
_worker->on_order_update (u);
|
||||
}
|
||||
void SingleFillHedger::on_book(const bts::orderman::Book& book,PerfTrace &trace){
|
||||
void SingleFillHedger::on_book(const bts::orderman::Book& book,trace::CorID &trace){
|
||||
_worker->on_book (book,trace);
|
||||
}
|
||||
void SingleFillHedger::cancel_hedge(){
|
||||
|
||||
@@ -30,7 +30,7 @@ private:
|
||||
const LegPropertyData& _leg_property_data;
|
||||
SpreaderChildOrderState _child_order_state;
|
||||
HedgePriceQuantityState _price_quantity_state;
|
||||
PerfTrace _fill_trace;
|
||||
trace::CorID _fill_trace;
|
||||
IHedgerStateWorker* _worker;
|
||||
HedgerStartStateWorker _start_state_worker;
|
||||
HedgerWaitingForFirstAckStateWorker _waiting_for_first_ack_state_worker;
|
||||
@@ -71,7 +71,7 @@ public:
|
||||
ChildOrder& child_order,
|
||||
bts::Price hedge_price,
|
||||
bts::Quantity hedge_quantity,
|
||||
PerfTrace const &trace,
|
||||
trace::CorID const &trace,
|
||||
bool is_live);
|
||||
SingleFillHedger() = delete;
|
||||
SingleFillHedger (const SingleFillHedger&) = delete;
|
||||
@@ -88,7 +88,7 @@ public:
|
||||
void on_order_update(const SpreaderChildOrderState& order_update_message);
|
||||
//c. called when the market data associated
|
||||
// with this security is changed
|
||||
void on_book(const bts::orderman::Book& book,PerfTrace &);
|
||||
void on_book(const bts::orderman::Book& book,trace::CorID &);
|
||||
//d. called when the hedge order is cancelled, either
|
||||
// manually or automatically
|
||||
void cancel_hedge();
|
||||
|
||||
@@ -90,44 +90,61 @@ void SingleLegQuoter::one_time_activate(Price price,
|
||||
Quantity quantity){
|
||||
if (_active_quoting){
|
||||
Quantity this_leg_quantity = SpreaderUtils::get_leg_quantity_from_spreader_quantity (_this_leg_data, quantity);
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::one_time_activate {} price: {} spread quantity: {} leg quantity: {}", _log_prefix(), price.value(), quantity.value(), this_leg_quantity.value());
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::one_time_activate "
|
||||
"spread_key:{} leg_idx:{}"
|
||||
"price:{} leg_quantity:{}",
|
||||
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
|
||||
price, this_leg_quantity);
|
||||
_worker->one_time_activate (price, this_leg_quantity);
|
||||
}
|
||||
}
|
||||
void SingleLegQuoter::update_order_status(const SpreaderChildOrderState& u){
|
||||
if (_active_quoting){
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::update_order_status {} order status: {} fills: {}", _log_prefix(), static_cast<int>(u.status()), u.last_update_filled_quantity().value());
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::update_order_status "
|
||||
"spread_key:{} leg_idx:{} status:{}",
|
||||
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
|
||||
static_cast<int>(u.status()));
|
||||
_worker->on_order_update (u);
|
||||
}
|
||||
}
|
||||
void SingleLegQuoter::on_book(size_t leg_index,
|
||||
const bts::orderman::Book& book,PerfTrace &trace){
|
||||
const bts::orderman::Book& book,trace::CorID trace){
|
||||
if (_active_quoting)
|
||||
_worker->on_book (leg_index, book,trace);
|
||||
}
|
||||
void SingleLegQuoter::update_other_leg_fills(size_t leg_index,
|
||||
Quantity filled_quantity){
|
||||
if (_active_quoting){
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::update_other_leg_fills {} other_leg_index: {} filled_quantity: {}", _log_prefix(), leg_index, filled_quantity.value());
|
||||
_worker->on_other_leg_quoter_fill (leg_index, filled_quantity);
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::update_other_leg_fills "
|
||||
"spread_key:{} leg_idx:{} fill_idx:{} filled_quantity:{}",
|
||||
_spreader.spreader_order_key(), _this_leg_data.leg_index(),
|
||||
leg_index, filled_quantity);
|
||||
_worker->on_other_leg_quoter_fill (leg_index, filled_quantity);
|
||||
}
|
||||
}
|
||||
void SingleLegQuoter::update_order_price_quantity(Price price,
|
||||
Quantity quantity){
|
||||
if (_active_quoting){
|
||||
Quantity this_leg_quantity = SpreaderUtils::get_leg_quantity_from_spreader_quantity (_this_leg_data, quantity);
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::update_order_price_quantity {} price: {} spread quantity: {} leg quantity: {}", _log_prefix(), price.value(), quantity.value(), this_leg_quantity.value());
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::update_order_price_quantity "
|
||||
"spread_key:{} leg_idx:{} price:{} this_leg_quantity:{}",
|
||||
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
|
||||
price, this_leg_quantity);
|
||||
_worker->update_spreader_order (price, this_leg_quantity);
|
||||
}
|
||||
}
|
||||
void SingleLegQuoter::cancel_order(){
|
||||
_is_permenantly_cancelled = true;
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::cancel_order {}", _log_prefix());
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::cancel_order "
|
||||
"spread_key:{} leg_idx:{}",
|
||||
_spreader.spreader_order_key(),_this_leg_data.leg_index());
|
||||
_worker->cancel ();
|
||||
}
|
||||
void SingleLegQuoter::terminate (){
|
||||
_is_permenantly_cancelled = true;
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::terminate {}", _log_prefix());
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::terminate "
|
||||
"spread_key:{} leg_idx:{}",
|
||||
_spreader.spreader_order_key(),_this_leg_data.leg_index());
|
||||
_worker->terminate ();
|
||||
}
|
||||
void SingleLegQuoter::on_time (const bts::TimeStamp& ts){
|
||||
@@ -241,8 +258,11 @@ void SingleLegQuoter::_transform_to_END(){
|
||||
}
|
||||
void SingleLegQuoter::_switch_state (IQuoterStateWorker* new_state){
|
||||
std::string curr_state = _worker == nullptr ? "NULL" : _worker->state_text();
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::_switch_state {} {} -> {} ",
|
||||
_log_prefix(), curr_state, new_state->state_text());
|
||||
LOG(OrderMan, Info, "SingleLegQuoter::_switch_state "
|
||||
"spread_key:{} leg_idx:{} {} -> {} ",
|
||||
_spreader.spreader_order_key(),
|
||||
_this_leg_data.leg_index(),
|
||||
curr_state, new_state->state_text());
|
||||
_worker = new_state;
|
||||
_worker->on_transition();
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ public:
|
||||
//c. called when the market data associated
|
||||
// with this security is changed
|
||||
void on_book(size_t leg_index,
|
||||
const bts::orderman::Book&,PerfTrace &);
|
||||
const bts::orderman::Book&,trace::CorID );
|
||||
//d. called when the order is cancelled, either
|
||||
// manually or automatically
|
||||
void cancel_order();
|
||||
@@ -150,16 +150,7 @@ public:
|
||||
}
|
||||
private:
|
||||
bts::orderman::OrderKey _key() {return _child_order_state.order_key();}
|
||||
TimeStamp _current_time () const {return _spreader.current_time();}
|
||||
// TODO: state out and move the string to a const
|
||||
std::string _log_prefix(){
|
||||
std::ostringstream oss;
|
||||
oss << " spreader_order_key:" << _spreader.spreader_order_key();
|
||||
oss << " sec_key:" << _this_leg_data.security_key();
|
||||
oss << " leg_idx:" << _this_leg_data.leg_index();
|
||||
oss << " state:" << (_worker == nullptr ? "NULL" : _worker->state_text());
|
||||
return oss.str();
|
||||
}
|
||||
TimeStamp _current_time () const {return _spreader.current_time();}
|
||||
void _set_irrecoverable_error(){
|
||||
_irrecoverable_error=true;
|
||||
}
|
||||
@@ -171,13 +162,13 @@ private:
|
||||
}
|
||||
void _place_order (Price price,
|
||||
Quantity quantity,
|
||||
const PerfTrace &trace) {
|
||||
const trace::CorID &trace) {
|
||||
_child_order_state.place_order(price,
|
||||
quantity,
|
||||
_spreader.current_time(),
|
||||
trace);
|
||||
}
|
||||
void _modify_order (Price price, Quantity quantity, PerfTrace &trace) {
|
||||
void _modify_order (Price price, Quantity quantity, trace::CorID &trace) {
|
||||
_child_order_state.modify_order(price, quantity, trace);
|
||||
}
|
||||
void _cancel_order () {_child_order_state.cancel_order();}
|
||||
|
||||
@@ -12,6 +12,7 @@ SpreaderBookkeepingData::SpreaderBookkeepingData (ISpreaderManager& spreader_man
|
||||
:
|
||||
_is_live(spreader_pao_services.is_live()),
|
||||
_n_legs(legs.size()),
|
||||
_n_quoting_legs(_calc_n_quoting_legs(legs)),
|
||||
_inited_new (inited_new),
|
||||
_order_def(order_def),
|
||||
_reported_state(){
|
||||
@@ -62,6 +63,14 @@ SpreaderBookkeepingData(spreader_manager,
|
||||
false){
|
||||
_reported_state=order_record.state();
|
||||
}
|
||||
size_t SpreaderBookkeepingData::_calc_n_quoting_legs(
|
||||
const std::vector<LegPropertyData>& legs){
|
||||
size_t rv=0;
|
||||
for(const LegPropertyData& l : legs)
|
||||
if(l.is_active_quoting())
|
||||
rv++;
|
||||
return rv;
|
||||
}
|
||||
void SpreaderBookkeepingData::total_fills (Quantity& out_quantity,
|
||||
Price& out_price) const{
|
||||
double avg_price = 0.0;
|
||||
@@ -84,14 +93,22 @@ SingleFillHedger& SpreaderBookkeepingData::add_new_hedger(ISpreaderManager& spre
|
||||
size_t leg_index,
|
||||
bts::Price hedge_price,
|
||||
bts::Quantity hedge_quantity,
|
||||
const PerfTrace &trace){
|
||||
SpreaderOrderServices* serv,
|
||||
const trace::CorID &trace){
|
||||
if (serv)
|
||||
serv->log(trace, "SpreaderBookkeepingData::add_new_hedger");
|
||||
|
||||
LOG(OrderMan, Info, "SpreaderBookkeepingData::add_new_hedger "
|
||||
"leg_index:{} price:{} quantity:{} cid:{}",
|
||||
leg_index, hedge_price, hedge_quantity,trace.md_sn);
|
||||
leg_index, hedge_price, hedge_quantity,trace.sn);
|
||||
bts_assert(leg_index < _n_legs);
|
||||
SpreaderLegData& leg_data = _leg_data[leg_index];
|
||||
std::vector<SingleFillHedger>& leg_hedgers = _hedgers[leg_index];
|
||||
if (serv)
|
||||
serv->log(trace, "SpreaderBookkeepingData::add_new_hedger:1");
|
||||
bts::orderman::ChildOrder hedge_order = leg_data.hedger_order_builder().build();
|
||||
if (serv)
|
||||
serv->log(trace, "SpreaderBookkeepingData::add_new_hedger:2");
|
||||
leg_hedgers.emplace_back (spreader_manager,
|
||||
leg_data.leg_property_data(),
|
||||
hedge_order,
|
||||
@@ -99,6 +116,8 @@ SingleFillHedger& SpreaderBookkeepingData::add_new_hedger(ISpreaderManager& spre
|
||||
hedge_quantity,
|
||||
trace,
|
||||
_is_live);
|
||||
if (serv)
|
||||
serv->log(trace, "SpreaderBookkeepingData::add_new_hedger:3");
|
||||
return leg_hedgers.back();
|
||||
}
|
||||
}}
|
||||
@@ -22,6 +22,7 @@ class SpreaderBookkeepingData
|
||||
private:
|
||||
const bool _is_live;
|
||||
const size_t _n_legs;
|
||||
const size_t _n_quoting_legs;
|
||||
// when the order is not created from the disk
|
||||
// (ie created live) this is set to true
|
||||
const bool _inited_new;
|
||||
@@ -42,6 +43,7 @@ private:
|
||||
Price _input_price;
|
||||
Price _effective_price;
|
||||
Quantity _quantity;
|
||||
size_t _calc_n_quoting_legs(const std::vector<LegPropertyData>& legs);
|
||||
public:
|
||||
SpreaderBookkeepingData (ISpreaderManager& spreader_manager,
|
||||
bts::orderman::OrderKey spreader_order_key,
|
||||
@@ -108,7 +110,8 @@ public:
|
||||
size_t leg_index,
|
||||
bts::Price hedge_price,
|
||||
bts::Quantity hedge_quantity,
|
||||
PerfTrace const &);
|
||||
SpreaderOrderServices *services,
|
||||
trace::CorID const &);
|
||||
// finds the spreader fill, price (ie averages it out, using
|
||||
// the size and price ratio, across all legs)
|
||||
void total_fills (Quantity& out_quantity, Price& out_price) const;
|
||||
@@ -133,5 +136,8 @@ public:
|
||||
bool has_effective_price() const{
|
||||
return !os::is_nan(_effective_price.value());
|
||||
}
|
||||
size_t n_quoting_legs() const{
|
||||
return _n_quoting_legs;
|
||||
}
|
||||
};
|
||||
}}
|
||||
@@ -130,7 +130,7 @@ public:
|
||||
void place_order(Price price,
|
||||
Quantity quantity,
|
||||
bts::TimeStamp curr_time,
|
||||
const PerfTrace& trace){
|
||||
const trace::CorID& trace){
|
||||
_out_price = price;
|
||||
_out_quantity = quantity;
|
||||
_order_place_time=curr_time;
|
||||
@@ -140,15 +140,15 @@ public:
|
||||
"key:{} is_quoting:{} sec_key:{} live:{} "
|
||||
"price:{} qty:{} cid:{}",
|
||||
order_key(), _is_quoting, _sec_key,
|
||||
_is_live, price, quantity, trace.md_sn);
|
||||
_is_live, price, quantity, trace.sn);
|
||||
if (_is_live)
|
||||
_child_order.activate(price, quantity, trace);
|
||||
}
|
||||
void modify_order(Price price, Quantity quantity){
|
||||
PerfTrace dont_care;
|
||||
trace::CorID dont_care;
|
||||
modify_order(price,quantity,dont_care);
|
||||
}
|
||||
void modify_order(Price price, Quantity quantity, PerfTrace &trace){
|
||||
void modify_order(Price price, Quantity quantity, trace::CorID &trace){
|
||||
_out_price = price;
|
||||
_out_quantity = quantity;
|
||||
_n_modify_orders++;
|
||||
@@ -158,7 +158,6 @@ public:
|
||||
order_key(), _is_quoting, _sec_key,
|
||||
_is_live, price, quantity, _n_modify_orders);
|
||||
if (_is_live) {
|
||||
trace.spreader_modify_order = utils::TimeStamp::now();
|
||||
_child_order.update(price, quantity, trace);
|
||||
}
|
||||
}
|
||||
|
||||
+9
-4
@@ -3,6 +3,7 @@
|
||||
#include "lib/orderman/actors/order_manager/ChildOrder.h"
|
||||
#include "lib/orderman/orders/PreActivationOrderServices.h"
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -35,7 +36,7 @@ private:
|
||||
const TestOnlySpreaderPreActivationOrderServices* _test_only_pre_services;
|
||||
public:
|
||||
SpreaderPreActivationOrderServices (const bts::orderman::PreActivationOrderServices* pre_services);
|
||||
SpreaderPreActivationOrderServices (TestOnlySpreaderPreActivationOrderServices* test_only_pre_services);
|
||||
SpreaderPreActivationOrderServices (TestOnlySpreaderPreActivationOrderServices* test_only_pre_services);
|
||||
const bts::api::Security* resolve (SecurityKey key) const;
|
||||
bool is_live () const;
|
||||
const bts::orderman::PreActivationOrderServices& pre_services() const;
|
||||
@@ -51,9 +52,13 @@ private:
|
||||
const bool _is_live;
|
||||
bts::orderman::OrderServices* _order_services;
|
||||
public:
|
||||
SpreaderOrderServices (bts::orderman::OrderServices* order_services);
|
||||
bts::orderman::OrderServices& order_services();
|
||||
void report_state(const api::orderman::OrderState &state);
|
||||
SpreaderOrderServices (bts::orderman::OrderServices* order_services);
|
||||
bts::orderman::OrderServices& order_services();
|
||||
void report_state(const api::orderman::OrderState &state);
|
||||
trace::ScopedPerfLogEntry log_scope(trace::CorID cid, const char *entry_kind,
|
||||
uint64_t uint64_1 = 0,const char *txt_1 = "",uint32_t uint32_1 = 0);
|
||||
void log(trace::CorID cid, const char *entry_kind,
|
||||
uint64_t uint64_1 = 0,const char *txt_1 = "",uint32_t uint32_1 = 0);
|
||||
bool is_live();
|
||||
};
|
||||
|
||||
|
||||
+20
-1
@@ -16,7 +16,26 @@ void SpreaderOrderServices::report_state(const api::orderman::OrderState &state)
|
||||
if (is_live())
|
||||
_order_services->report_state(state);
|
||||
}
|
||||
trace::ScopedPerfLogEntry SpreaderOrderServices::log_scope(trace::CorID cid,
|
||||
const char *entry_kind,
|
||||
uint64_t uint64_1,
|
||||
const char *txt_1,
|
||||
uint32_t uint32_1){
|
||||
if(_is_live)
|
||||
return _order_services->log_scope(cid,entry_kind,uint64_1,txt_1,uint32_1);
|
||||
else
|
||||
return
|
||||
trace::ScopedPerfLogEntry ();
|
||||
}
|
||||
void SpreaderOrderServices::log(trace::CorID cid,
|
||||
const char *entry_kind,
|
||||
uint64_t uint64_1,
|
||||
const char *txt_1,
|
||||
uint32_t uint32_1) {
|
||||
if(_is_live)
|
||||
_order_services->log(cid, entry_kind, uint64_1, txt_1, uint32_1);
|
||||
}
|
||||
bool SpreaderOrderServices::is_live()
|
||||
{return _is_live;}
|
||||
|
||||
}}
|
||||
}}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerState.h"
|
||||
|
||||
namespace bts {
|
||||
namespace trace {struct CorID;}
|
||||
namespace orderman {class Book;}
|
||||
namespace orderman {
|
||||
class SpreaderChildOrderState;
|
||||
@@ -24,7 +25,7 @@ public:
|
||||
virtual void on_order_update (const SpreaderChildOrderState& order_update) = 0;
|
||||
// called only when the security that the hedger is working on
|
||||
// has a market data update
|
||||
virtual void on_book (const bts::orderman::Book& book,PerfTrace &) = 0;
|
||||
virtual void on_book (const bts::orderman::Book& book,trace::CorID &) = 0;
|
||||
// when the spreader wants the hedger to cancel its order
|
||||
virtual void cancel() = 0;
|
||||
// on terminate, the hedger should simply go to its end state
|
||||
|
||||
Loaded 100 of 179 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user