Trying to speed up polygon valuations.
This commit is contained in:
1 parent
f1c5ba93d3
commit
5ea147efec
6 files changed
+194
-110
No files matched your search
@@ -1,6 +1,6 @@
|
||||
#include "polygon_utils.h"
|
||||
#include <iostream>
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
@@ -22,8 +22,8 @@ PolyInstrument make_outright(mdp3::MDP3SecurityID id) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const InstrumentDefinition& find_instrument(const InstrumentMap& ins_map,
|
||||
MDP3SecurityID sec_id){
|
||||
const InstrumentDefinition &find_instrument(const InstrumentMap &ins_map,
|
||||
MDP3SecurityID sec_id) {
|
||||
auto it = ins_map.find(sec_id);
|
||||
bts_assert(it != ins_map.end());
|
||||
bts_assert(it->second);
|
||||
@@ -209,15 +209,18 @@ std::vector<Polygon> find_polygon_with_fixed_size(
|
||||
std::vector<Polygon>
|
||||
find_polygons(std::vector<PolyInstrument> const &tradeable_instruments,
|
||||
size_t n_vertices, size_t max_ratio) {
|
||||
|
||||
auto clusters = clusterize(tradeable_instruments);
|
||||
LOG(Spartan,Info, "Found {} clusters of securities for polygons", clusters.size());
|
||||
LOG(Spartan, Info, "Found {} clusters in {} securities to build polygons",
|
||||
clusters.size(), tradeable_instruments.size());
|
||||
std::vector<Polygon> result;
|
||||
do {
|
||||
for (auto &universe : clusters) {
|
||||
LOG(Spartan,Info, "Searching for polygons in cluster of size {}",universe.size());
|
||||
LOG(Spartan, Info, "Searching for polygons in cluster of size {}",
|
||||
universe.size());
|
||||
auto these_polygons =
|
||||
find_polygon_with_fixed_size(universe, n_vertices, max_ratio);
|
||||
LOG(Spartan,Info, "Found {} polygons",these_polygons.size());
|
||||
LOG(Spartan, Info, "Found {} polygons", these_polygons.size());
|
||||
result.insert(result.end(), these_polygons.begin(),
|
||||
these_polygons.end());
|
||||
}
|
||||
@@ -225,46 +228,43 @@ find_polygons(std::vector<PolyInstrument> const &tradeable_instruments,
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t abs_contracts (const Polygon& polygon,
|
||||
const InstrumentMap& ins_map){
|
||||
auto find_def = [&](MDP3SecurityID id){
|
||||
uint32_t abs_contracts(const Polygon &polygon, const InstrumentMap &ins_map) {
|
||||
auto find_def = [&](MDP3SecurityID id) {
|
||||
auto it = ins_map.find(id);
|
||||
bts_assert(it != ins_map.end());
|
||||
bts_assert(it->second);
|
||||
return it->second;
|
||||
};
|
||||
std::function<int(const InstrumentDefinition&,int)> comp_abs_contracts;
|
||||
comp_abs_contracts = [&](const InstrumentDefinition& d, int ratio){
|
||||
std::function<int(const InstrumentDefinition &, int)> comp_abs_contracts;
|
||||
comp_abs_contracts = [&](const InstrumentDefinition &d, int ratio) {
|
||||
int sum = 0;
|
||||
switch (d.security_type())
|
||||
{
|
||||
case bts::api::SECURITYTYPE_SPREAD:
|
||||
for (const auto& l : d.legs())
|
||||
sum += comp_abs_contracts(*find_def(l.security_id),
|
||||
std::abs(ratio*l.ratio.value()));
|
||||
break;
|
||||
default:
|
||||
sum = ratio;
|
||||
break;
|
||||
switch (d.security_type()) {
|
||||
case bts::api::SECURITYTYPE_SPREAD:
|
||||
for (const auto &l : d.legs())
|
||||
sum += comp_abs_contracts(*find_def(l.security_id),
|
||||
std::abs(ratio * l.ratio.value()));
|
||||
break;
|
||||
default:
|
||||
sum = ratio;
|
||||
break;
|
||||
};
|
||||
return std::abs(sum);
|
||||
};
|
||||
int rv = 0;
|
||||
for (const InstrumentPart& p : polygon.parts)
|
||||
for (const InstrumentPart &p : polygon.parts)
|
||||
rv += std::abs(comp_abs_contracts(*find_def(p.sec_id), p.ratio));
|
||||
bts_assert(rv > 0);
|
||||
return static_cast<uint32_t>(rv);
|
||||
}
|
||||
|
||||
double tick_size (const Polygon& polygon,
|
||||
const InstrumentMap& ins_map){
|
||||
double tick_size(const Polygon &polygon, const InstrumentMap &ins_map) {
|
||||
double rv = 0;
|
||||
for (const InstrumentPart& p : polygon.parts){
|
||||
auto price_inc = find_instrument(ins_map,p.sec_id).min_price_increment();
|
||||
for (const InstrumentPart &p : polygon.parts) {
|
||||
auto price_inc = find_instrument(ins_map, p.sec_id).min_price_increment();
|
||||
rv = price_inc > rv ? price_inc : rv;
|
||||
}
|
||||
LOG(Spartan,Diag, "Polygon:{} tick_size:{}",
|
||||
polygon_descriptor(polygon, ins_map), rv);
|
||||
LOG(Spartan, Diag, "Polygon:{} tick_size:{}",
|
||||
polygon_descriptor(polygon, ins_map), rv);
|
||||
|
||||
return rv;
|
||||
}
|
||||
@@ -300,68 +300,39 @@ std::ostream &operator<<(std::ostream &s, Polygon const &p) {
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<MDP3SecurityID> get_connected(
|
||||
PolyInstrument const &i,
|
||||
std::unordered_map<MDP3SecurityID, PolyInstrument> const &universe) {
|
||||
bts_assert(i.parts.size() > 0);
|
||||
|
||||
std::vector<MDP3SecurityID> result;
|
||||
if (i.parts.size() > 1)
|
||||
for (auto &p : i.parts)
|
||||
result.push_back(p.sec_id);
|
||||
else
|
||||
for (auto const &[cid, candidate] : universe)
|
||||
for (auto const &p : candidate.parts)
|
||||
if (p.sec_id == i.sec_id)
|
||||
result.push_back(cid);
|
||||
return result;
|
||||
}
|
||||
|
||||
void dfs(PolyInstrument const &i,
|
||||
std::unordered_map<MDP3SecurityID, PolyInstrument> const &universe,
|
||||
std::unordered_set<MDP3SecurityID> &visited) {
|
||||
if (visited.count(i.sec_id))
|
||||
return;
|
||||
visited.insert(i.sec_id);
|
||||
auto connected = get_connected(i, universe);
|
||||
for (auto const &id : connected)
|
||||
if (visited.count(id) == 0) {
|
||||
auto it = universe.find(id);
|
||||
bts_assert(it != universe.end());
|
||||
dfs(it->second, universe, visited);
|
||||
}
|
||||
}
|
||||
|
||||
using InstrumentCluster = std::vector<PolyInstrument>;
|
||||
std::vector<InstrumentCluster>
|
||||
clusterize(std::vector<PolyInstrument> const &universe) {
|
||||
|
||||
// Index all instruments
|
||||
std::unordered_map<MDP3SecurityID, PolyInstrument> universe_idx;
|
||||
for (auto const &p : universe)
|
||||
universe_idx[p.sec_id] = p;
|
||||
std::unordered_map<MDP3SecurityID, PolyInstrument> idx;
|
||||
|
||||
// Discover all instruments reacheable from the first, group them
|
||||
// to form a cluster, remove them from the universe and try again.
|
||||
std::vector<InstrumentCluster> result;
|
||||
std::unordered_set<MDP3SecurityID> cluster;
|
||||
while (universe_idx.size()) {
|
||||
|
||||
cluster.clear();
|
||||
dfs(universe_idx.begin()->second, universe_idx, cluster);
|
||||
|
||||
auto &cl_vec = result.emplace_back();
|
||||
for (auto id : cluster) {
|
||||
auto it = universe_idx.find(id);
|
||||
bts_assert(it != universe_idx.end());
|
||||
cl_vec.push_back(it->second);
|
||||
universe_idx.erase(id);
|
||||
Graph<MDP3SecurityID> g;
|
||||
for (auto const &poly : universe) {
|
||||
idx[poly.sec_id] = poly;
|
||||
g.add_node(poly.sec_id);
|
||||
for (auto const &p : poly.parts) {
|
||||
if (p.sec_id != poly.sec_id)
|
||||
g.add_edge(poly.sec_id, p.sec_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Add edges
|
||||
while (g.size()) {
|
||||
LOG(Spartan, Info, "Graph has {} nodes", g.size());
|
||||
auto connected = g.shortest_path(g.begin()->first);
|
||||
auto &cluster = result.emplace_back();
|
||||
for (auto &n : connected) {
|
||||
cluster.push_back(idx[n.first]);
|
||||
g.remove_node(n.first);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string polygon_descriptor(Polygon const &p, InstrumentMap const &instruments) {
|
||||
std::string polygon_descriptor(Polygon const &p,
|
||||
InstrumentMap const &instruments) {
|
||||
|
||||
auto get_sec = [&instruments](MDP3SecurityID id) {
|
||||
auto it = instruments.find(id);
|
||||
@@ -372,7 +343,7 @@ std::string polygon_descriptor(Polygon const &p, InstrumentMap const &instrument
|
||||
std::ostringstream str;
|
||||
for (auto const &part : p.parts) {
|
||||
auto const &sec = get_sec(part.sec_id);
|
||||
|
||||
|
||||
if (part.ratio > 0)
|
||||
str << "+";
|
||||
else if (part.ratio)
|
||||
@@ -390,10 +361,10 @@ std::string polygon_descriptor(Polygon const &p, InstrumentMap const &instrument
|
||||
return str.str();
|
||||
}
|
||||
|
||||
std::string polygon_product(Polygon const& polygon,
|
||||
InstrumentMap const& ins_map){
|
||||
for (const InstrumentPart& p : polygon.parts){
|
||||
const auto& ins = find_instrument(ins_map,p.sec_id);
|
||||
std::string polygon_product(Polygon const &polygon,
|
||||
InstrumentMap const &ins_map) {
|
||||
for (const InstrumentPart &p : polygon.parts) {
|
||||
const auto &ins = find_instrument(ins_map, p.sec_id);
|
||||
return std::string(ins.asset());
|
||||
}
|
||||
bts_assert(false);
|
||||
|
||||
@@ -60,4 +60,103 @@ std::vector<Polygon> find_polygons(std::vector<PolyInstrument> const &universe,
|
||||
// Testing helper
|
||||
bool nets_to_0(Polygon const &p, std::vector<PolyInstrument> const &universe);
|
||||
|
||||
template <class Label> class Graph {
|
||||
public:
|
||||
Graph() : adjacency() {}
|
||||
|
||||
void add_node(Label const &l1) { adjacency[l1]; }
|
||||
void add_directed_edge(Label const &l1, Label const &l2) {
|
||||
add_node(l2);
|
||||
adjacency[l1].push_back(l2);
|
||||
}
|
||||
|
||||
void add_edge(Label const &l1, Label const &l2) {
|
||||
add_directed_edge(l1, l2);
|
||||
add_directed_edge(l2, l1);
|
||||
}
|
||||
|
||||
size_t size() const { return adjacency.size(); }
|
||||
|
||||
void remove_node(Label const &l1) {
|
||||
adjacency.erase(l1);
|
||||
// Sometime I hate this shitty language
|
||||
for (auto &p : adjacency) {
|
||||
auto &edges = p.second;
|
||||
edges.erase(std::remove(edges.begin(), edges.end(), l1), edges.end());
|
||||
}
|
||||
}
|
||||
using Distance = size_t;
|
||||
static inline Distance infinity() {
|
||||
return std::numeric_limits<Distance>::max();
|
||||
}
|
||||
struct ShortPath {
|
||||
Label predecessor;
|
||||
Distance distance;
|
||||
};
|
||||
std::unordered_map<Label, ShortPath> shortest_path(Label const &src) {
|
||||
if (adjacency.empty())
|
||||
return {};
|
||||
else {
|
||||
// Calculated distances so far
|
||||
std::unordered_map<Label, ShortPath> distances;
|
||||
|
||||
// Nodes yet to be visited, ordered by distance
|
||||
using Unvisited = std::pair<Distance, Label>;
|
||||
std::set<Unvisited> unvisited;
|
||||
|
||||
// Initialize distances to infinity, mark all nodes as unvisited
|
||||
for (auto const &p : adjacency) {
|
||||
auto &node = p.first;
|
||||
distances[node] = {node, infinity()};
|
||||
unvisited.insert({infinity(), node});
|
||||
}
|
||||
distances[src] = {src, 0};
|
||||
unvisited.erase({infinity(), src});
|
||||
unvisited.insert({0, src});
|
||||
|
||||
while (unvisited.size()) {
|
||||
auto it = unvisited.begin();
|
||||
auto current_node = it->second;
|
||||
unvisited.erase(it);
|
||||
auto current_distance = distances[current_node].distance;
|
||||
if (current_distance == infinity()) {
|
||||
// There is no way to reach this node or any of the
|
||||
// ones that follow it so we are done.
|
||||
break;
|
||||
}
|
||||
for (auto &neighbour : adjacency[current_node]) {
|
||||
auto previous_distance = distances[neighbour].distance;
|
||||
if (previous_distance == infinity() ||
|
||||
previous_distance > current_distance + 1) {
|
||||
auto best_distance = current_distance + 1;
|
||||
// Update distance with this shorter path
|
||||
distances[neighbour].distance = best_distance;
|
||||
distances[neighbour].predecessor = current_node;
|
||||
// Update priority queue
|
||||
unvisited.erase({previous_distance, neighbour});
|
||||
unvisited.insert({best_distance, neighbour});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_map<Label, ShortPath> result;
|
||||
for (auto const &[l, sp] : distances)
|
||||
if (sp.distance != infinity())
|
||||
result[l] = sp;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
typename std::unordered_map<Label, std::vector<Label>>::iterator begin() {
|
||||
return adjacency.begin();
|
||||
}
|
||||
|
||||
typename std::unordered_map<Label, std::vector<Label>>::iterator end() {
|
||||
return adjacency.end();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<Label, std::vector<Label>> adjacency;
|
||||
};
|
||||
|
||||
} // namespace bts::spartan::mdp3
|
||||
@@ -16,6 +16,12 @@ struct FillCount {
|
||||
|
||||
struct FillCounter : IChannelObserver {
|
||||
std::vector<FillCount> result;
|
||||
std::unordered_map<MDP3SecurityID,size_t> result_map() {
|
||||
std::unordered_map<MDP3SecurityID,size_t> result_idx;
|
||||
for (auto const &f : result)
|
||||
result_idx[f.sec_id] = f.fills;
|
||||
return result_idx;
|
||||
}
|
||||
FillCounter();
|
||||
~FillCounter();
|
||||
|
||||
|
||||
@@ -39,13 +39,19 @@ void PolygonFinder::on_real_time_event(MDP3Event const &ev) {
|
||||
void PolygonFinder::finit() {
|
||||
me->fill_counter.finit();
|
||||
me->def_collector.finit();
|
||||
|
||||
|
||||
// Get securities with enough active fills
|
||||
std::unordered_map<MDP3SecurityID, PolyInstrument> poly_universe;
|
||||
for (auto &f : me->fill_counter.result)
|
||||
if (f.fills >= me->options.active_threshold_fills &&
|
||||
poly_universe.size() <= me->options.max_securities)
|
||||
poly_universe[f.sec_id] = PolyInstrument{f.sec_id};
|
||||
for (auto &f : me->fill_counter.result) {
|
||||
if (poly_universe.size() == me->options.max_securities)
|
||||
break;
|
||||
if (f.fills < me->options.active_threshold_fills)
|
||||
continue;
|
||||
if (std::abs(me->def_collector.ptr_result[f.sec_id]->cash_per_point()) <
|
||||
securities::market_epsilon())
|
||||
continue;
|
||||
poly_universe[f.sec_id] = PolyInstrument{f.sec_id};
|
||||
}
|
||||
|
||||
// Now fill in the security legs
|
||||
std::unordered_set<MDP3SecurityID> all_legs;
|
||||
@@ -83,7 +89,7 @@ void PolygonFinder::finit() {
|
||||
|
||||
auto polys = find_polygons(poly_universe_vec, me->options.max_vertices, 2);
|
||||
for (auto &p : polys) {
|
||||
auto id = result.size()+1;
|
||||
auto id = result.size() + 1;
|
||||
result[id] = p;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ struct PolygonFinder : IChannelObserver {
|
||||
|
||||
struct Options {
|
||||
Options () :
|
||||
active_threshold_fills(100),
|
||||
active_threshold_fills(1),
|
||||
max_vertices(5),
|
||||
max_securities(100)
|
||||
max_securities(70)
|
||||
{}
|
||||
|
||||
size_t active_threshold_fills ;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
std::ostream& operator<<(std::ostream&out,PolygonValuation const&v) {
|
||||
std::ostream &operator<<(std::ostream &out, PolygonValuation const &v) {
|
||||
out << v.vid << " ";
|
||||
out << v.polygon_id << " ";
|
||||
out << v.event_id << " ";
|
||||
@@ -16,23 +16,23 @@ std::ostream& operator<<(std::ostream&out,PolygonValuation const&v) {
|
||||
out << v.direct_value << " ";
|
||||
out << v.direct_quantity.value() << " ";
|
||||
out << v.merged_value << " ";
|
||||
out << v.merged_quantity.value () << std::endl;
|
||||
out << v.merged_quantity.value() << std::endl;
|
||||
return out;
|
||||
}
|
||||
|
||||
std::istream& operator>>(std::istream&in,PolygonValuation &v) {
|
||||
in >> v.vid ;
|
||||
in >> v.polygon_id ;
|
||||
in >> v.event_id ;
|
||||
in >> v.transact_ts.value() ;
|
||||
in >> v.send_ts.value() ;
|
||||
std::istream &operator>>(std::istream &in, PolygonValuation &v) {
|
||||
in >> v.vid;
|
||||
in >> v.polygon_id;
|
||||
in >> v.event_id;
|
||||
in >> v.transact_ts.value();
|
||||
in >> v.send_ts.value();
|
||||
int side;
|
||||
in >> side;
|
||||
v.side = (api::Side) side;
|
||||
in >> v.direct_value ;
|
||||
in >> v.direct_quantity.value() ;
|
||||
in >> v.merged_value ;
|
||||
in >> v.merged_quantity.value ();
|
||||
v.side = (api::Side)side;
|
||||
in >> v.direct_value;
|
||||
in >> v.direct_quantity.value();
|
||||
in >> v.merged_value;
|
||||
in >> v.merged_quantity.value();
|
||||
return in;
|
||||
}
|
||||
void poly_values_to_csv(std::vector<PolygonValuation> const &vals,
|
||||
@@ -153,7 +153,7 @@ aggregate(std::vector<PolygonValuation> const &vals) {
|
||||
|
||||
auto [next_idx, group] = get_next_group(vals, 0);
|
||||
opportunities.emplace_back() = start_new_opportunity(
|
||||
opportunities.size()+1, get_best_valuation(group));
|
||||
opportunities.size() + 1, get_best_valuation(group));
|
||||
|
||||
std::tie(next_idx, group) = get_next_group(vals, next_idx);
|
||||
while (group.size()) {
|
||||
@@ -181,14 +181,13 @@ aggregate(std::vector<PolygonValuation> const &vals) {
|
||||
else
|
||||
// No, start a new opportunity using best valuation available
|
||||
opportunities.emplace_back() = start_new_opportunity(
|
||||
opportunities.size()+1, get_best_valuation(group));
|
||||
opportunities.size() + 1, get_best_valuation(group));
|
||||
} else
|
||||
// There is a discontuinity so the last opportunity is
|
||||
// over and its time to start a new one.
|
||||
opportunities.emplace_back() = start_new_opportunity(
|
||||
opportunities.size()+1, get_best_valuation(group));
|
||||
opportunities.size() + 1, get_best_valuation(group));
|
||||
std::tie(next_idx, group) = get_next_group(vals, next_idx);
|
||||
|
||||
}
|
||||
}
|
||||
return opportunities;
|
||||
@@ -304,7 +303,10 @@ void PolygonOpportunityFinder::on_real_time_event(MDP3Event const &ev) {
|
||||
}
|
||||
}
|
||||
if (all_secs_are_trading &&
|
||||
(direct_available_qty > 0 || merged_available_qty > 0)) {
|
||||
((direct_available_qty > 0 &&
|
||||
val.direct_value > securities::market_epsilon()) ||
|
||||
(merged_available_qty > 0 &&
|
||||
val.merged_value > securities::market_epsilon()))) {
|
||||
val.vid = result.size() + 1;
|
||||
val.direct_quantity.value() = direct_available_qty;
|
||||
val.merged_quantity.value() = merged_available_qty;
|
||||
|
||||
Reference in new issue
Block a user