Cleaned up polygon generation.

This commit is contained in:
Pedro Pinto committed 2020-09-30 09:51:40 -04:00
1 parent 110af7795e
commit 271e14ae5b
16 files changed
+97 -97

No files matched your search

@@ -16,6 +16,7 @@ namespace orderman {
class TestSimServices : public bts::spartan::mdp3::ISimServices{
private:
InstrumentMap _instruments;
std::vector<Polygon> _polygons;
public:
OrderChange last_change;
size_t n_updates = 0;
@@ -25,10 +26,15 @@ public:
:
key(0){
}
std::unordered_map<MDP3SecurityID,InstrumentDefinition const*> const &instruments() const override{
InstrumentMap const &instruments() const override{
bts_assert(false);
return _instruments;
}
std::vector<Polygon> const&polygons() const override {
return _polygons;
}
MDP3OrderKey generate_order_key() override{
return MDP3OrderKey(key++);
}
+1 -1
View File
@@ -1 +1 @@
set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CMECertCmd.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/FIXSim.cpp;cmds/Help.cpp;cmds/KeyCode.cpp;cmds/Md.cpp;cmds/ParseHistoricalFIX.cpp;cmds/PrintHedgeOrders.cpp;cmds/QuickTest.cpp;cmds/Run.cpp;cmds/Span.cpp;cmds/Spartan.cpp;cmds/Test.cpp;cmds/Web.cpp;cmds/spartan/algo/polygon/polygon_utils.cpp;cmds/spartan/algo/polygon/tests/test_polygon_finder.cpp;cmds/spartan/algo/tas/RunTAS.cpp;cmds/spartan/algo/tas/TASOrderReactor.cpp;cmds/spartan/algo/tas/TASTriggerDef.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp;cmds/spartan/md/mdp3/back_testing/simulator/ExchangeSim.cpp;cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.cpp;cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.cpp;cmds/spartan/md/mdp3/back_testing/simulator/SimInput.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/AlgoAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/GatewayAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/TimerAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/algos/SimOrderGateway.cpp;cmds/spartan/md/mdp3/back_testing/simulator/algos/SimSpreader.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/AgentBase.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/Simulator.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/tests/test_simulator_core.cpp;cmds/spartan/md/mdp3/back_testing/simulator/events/SimEvents.cpp;cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp;cmds/spartan/md/mdp3/feed_control/IFeedController.cpp;cmds/spartan/md/mdp3/handler/ChannelHandler.cpp;cmds/spartan/md/mdp3/parsing/MDP3MsgBox.cpp;cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp;cmds/spartan/md/mdp3/recovery/BookSnapshot.cpp;cmds/spartan/md/mdp3/recovery/DefinitionSnapshot.cpp;cmds/spartan/md/mdp3/recovery/IncrementalQueue.cpp;cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp;cmds/spartan/md/mdp3/tests/matcher_tests.cpp;cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp;cmds/spartan/md/mdp3/utils/mdp3_utils.cpp;cmds/spartan/md/mdp3/utils/queries/DefinitionCollector.cpp;cmds/spartan/md/mdp3/utils/queries/FillCounter.cpp;cmds/spartan/md/mdp3/utils/queries/IcebergFinder.cpp;cmds/spartan/md/mdp3/utils/queries/PolygonFinder.cpp;cmds/spartan/md/mdp3/utils/queries/TransactionDeducer.cpp;cmds/spartan/util/MCastReceiver.cpp;cmds/spartan/util/PCapReader.cpp;cmds/spartan/util/SecurityDB.cpp;dspark/mirage/quick_queue/quick_queue.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/book/Book.cpp;exchange_sim/book/ExchangeSim.proto;exchange_sim/book/Order.cpp;exchange_sim/book/OrderMatcher.cpp;exchange_sim/book/book_transform.cpp;exchange_sim/tests/test_book_transform.cpp;http/ApiGateway.cpp;http/MultipartFormDecoder.cpp;http/WebServer.cpp;http/tests/MultiPartFormTests.cpp;main.cpp;md/ChannelVolume.cpp;md/MarketDataAuxiliaryServiceServer.cpp;md/VolumeAtPriceState.cpp;md/platforms/IMDPlatform.cpp;md/platforms/ice_onixs/ICEListener.cpp;md/platforms/ice_onixs/ICEMain.cpp;md/platforms/ice_onixs/tests/ice_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/MarginServiceServer.cpp;position/PositionAggregator.cpp;position/PositionMap.cpp;position/PositionServicePNLHelper.cpp;position/PositionServiceServer.cpp;position/import/BasePositionServiceServer.cpp;position/import/DownloadCommonStateMachine.cpp;position/import/DownloadWindow.cpp;position/import/MoneyFileServiceServer.cpp;position/import/parsing/gmi_parser.cpp;position/import/parsing/money_parser.cpp;position/import/parsing/parser_common.cpp;position/tests/PositionTests.cpp;position/tests/position_tests_internal.cpp;subprocess/SubProcessRunner.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceDB.cpp;user/AdminServiceServer.cpp;user/AuthServiceServer.cpp;user/tests/admin_service_tests.cpp)
set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CMECertCmd.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/FIXSim.cpp;cmds/Help.cpp;cmds/KeyCode.cpp;cmds/Md.cpp;cmds/ParseHistoricalFIX.cpp;cmds/PrintHedgeOrders.cpp;cmds/QuickTest.cpp;cmds/Run.cpp;cmds/Span.cpp;cmds/Spartan.cpp;cmds/Test.cpp;cmds/Web.cpp;cmds/spartan/algo/tas/RunTAS.cpp;cmds/spartan/algo/tas/TASOrderReactor.cpp;cmds/spartan/algo/tas/TASTriggerDef.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.cpp;cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp;cmds/spartan/md/mdp3/back_testing/simulator/ExchangeSim.cpp;cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.cpp;cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.cpp;cmds/spartan/md/mdp3/back_testing/simulator/SimInput.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/AlgoAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/GatewayAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/agents/TimerAgent.cpp;cmds/spartan/md/mdp3/back_testing/simulator/algos/SimOrderGateway.cpp;cmds/spartan/md/mdp3/back_testing/simulator/algos/SimSpreader.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/AgentBase.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/Simulator.cpp;cmds/spartan/md/mdp3/back_testing/simulator/core/tests/test_simulator_core.cpp;cmds/spartan/md/mdp3/back_testing/simulator/events/SimEvents.cpp;cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp;cmds/spartan/md/mdp3/feed_control/IFeedController.cpp;cmds/spartan/md/mdp3/handler/ChannelHandler.cpp;cmds/spartan/md/mdp3/parsing/MDP3MsgBox.cpp;cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp;cmds/spartan/md/mdp3/recovery/BookSnapshot.cpp;cmds/spartan/md/mdp3/recovery/DefinitionSnapshot.cpp;cmds/spartan/md/mdp3/recovery/IncrementalQueue.cpp;cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp;cmds/spartan/md/mdp3/tests/matcher_tests.cpp;cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp;cmds/spartan/md/mdp3/utils/mdp3_utils.cpp;cmds/spartan/md/mdp3/utils/polygon_utils.cpp;cmds/spartan/md/mdp3/utils/queries/DefinitionCollector.cpp;cmds/spartan/md/mdp3/utils/queries/FillCounter.cpp;cmds/spartan/md/mdp3/utils/queries/IcebergFinder.cpp;cmds/spartan/md/mdp3/utils/queries/PolygonFinder.cpp;cmds/spartan/md/mdp3/utils/queries/TransactionDeducer.cpp;cmds/spartan/md/mdp3/utils/tests/test_polygon_finder.cpp;cmds/spartan/util/MCastReceiver.cpp;cmds/spartan/util/PCapReader.cpp;cmds/spartan/util/SecurityDB.cpp;dspark/mirage/quick_queue/quick_queue.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/book/Book.cpp;exchange_sim/book/ExchangeSim.proto;exchange_sim/book/Order.cpp;exchange_sim/book/OrderMatcher.cpp;exchange_sim/book/book_transform.cpp;exchange_sim/tests/test_book_transform.cpp;http/ApiGateway.cpp;http/MultipartFormDecoder.cpp;http/WebServer.cpp;http/tests/MultiPartFormTests.cpp;main.cpp;md/ChannelVolume.cpp;md/MarketDataAuxiliaryServiceServer.cpp;md/VolumeAtPriceState.cpp;md/platforms/IMDPlatform.cpp;md/platforms/ice_onixs/ICEListener.cpp;md/platforms/ice_onixs/ICEMain.cpp;md/platforms/ice_onixs/tests/ice_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/MarginServiceServer.cpp;position/PositionAggregator.cpp;position/PositionMap.cpp;position/PositionServicePNLHelper.cpp;position/PositionServiceServer.cpp;position/import/BasePositionServiceServer.cpp;position/import/DownloadCommonStateMachine.cpp;position/import/DownloadWindow.cpp;position/import/MoneyFileServiceServer.cpp;position/import/parsing/gmi_parser.cpp;position/import/parsing/money_parser.cpp;position/import/parsing/parser_common.cpp;position/tests/PositionTests.cpp;position/tests/position_tests_internal.cpp;subprocess/SubProcessRunner.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceDB.cpp;user/AdminServiceServer.cpp;user/AuthServiceServer.cpp;user/tests/admin_service_tests.cpp)
+4 -8
View File
@@ -18,7 +18,7 @@
#include "server/sparkd/cmds/spartan/md/mdp3/handler/IChannelObserver.h"
#include "server/sparkd/cmds/spartan/md/mdp3/parsing/MDP3Parser.h"
#include "server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h"
#include "server/sparkd/cmds/spartan/algo/polygon/polygon_utils.h"
#include "server/sparkd/cmds/spartan/md/mdp3/utils/polygon_utils.h"
#include "server/sparkd/cmds/spartan/util/PCapReader.h"
#include <boost/algorithm/string.hpp>
@@ -208,15 +208,14 @@ std::vector<InputLeg> legs{
};
int QuickTest::execute(std::vector<std::string> const &) {
using namespace bts::spartan::algo::poly;
std::vector<Instrument> universe;
using namespace mdp3;
std::vector<PolyInstrument> universe;
auto get_instrument = [&universe](mdp3::MDP3SecurityID key) {
for (auto &i : universe)
if (i.sec_id == key)
return &i;
return &universe.emplace_back(Instrument{key});
return &universe.emplace_back(PolyInstrument{key});
};
for (auto l : legs) {
auto i = get_instrument(l.sec_id);
if (l.ratio == 0)
@@ -227,9 +226,6 @@ int QuickTest::execute(std::vector<std::string> const &) {
auto polygons_3 = find_polygons(universe,3);
auto polygons_4 = find_polygons(universe,4);
std::cout << polygons_3.size() + polygons_4.size() ;
// bts_assert(polygons.size());
// for (auto p : polygons)
// bts_assert(nets_to_0(p,universe));
return 0 ;
}
+1 -1
View File
@@ -56,7 +56,7 @@ void find_polys(std::vector<std::string> const&args) {
size_t i = 0;
for (auto &p : poly_finder.result) {
std::cout << ++i << ":" << std::endl;
for (auto &l : p.legs) {
for (auto &l : p.parts) {
if (l.ratio == -1)
std::cout << "\t-";
else if (l.ratio == -2)
@@ -31,6 +31,8 @@ struct ExchangeSim::PImpl {
std::max(i.max_order_id.value(), max_order_id.value())};
for (auto &[k, i] : i.instruments)
all_definitions[k] = &i;
all_polygons.insert(all_polygons.end(), i.polygons.begin(),
i.polygons.end());
}
bts_assert(all_definitions.size());
}
@@ -38,6 +40,7 @@ struct ExchangeSim::PImpl {
MDP3OrderKey max_order_id;
std::unordered_map<MDP3SecurityID, InstrumentDefinition const *>
all_definitions;
std::vector<Polygon> all_polygons;
std::optional<RunState> run_state;
};
@@ -68,8 +71,8 @@ void ExchangeSim::prepare_run(std::vector<IAlgoModel *> const &algos,
// Init algos, add them to simulator
for (auto algo : algos) {
me->run_state->algos.emplace_back(me->all_definitions, &me->max_order_id,
options, *algo);
me->run_state->algos.emplace_back(me->all_definitions, me->all_polygons,
&me->max_order_id, options, *algo);
me->run_state->simulator.add_agent(me->run_state->algos.back());
}
@@ -1,6 +1,7 @@
#pragma once
#include "../../common/mdp3_common.h"
#include "../../utils/polygon_utils.h"
#include "../../common/InstrumentMap.h"
#include "AlgoAction.h"
@@ -10,6 +11,7 @@ class InstrumentDefinition;
class ISimServices {
public:
virtual ~ISimServices();
virtual std::vector<Polygon> const &polygons() const = 0;
virtual InstrumentMap const &instruments() const = 0;
virtual MDP3OrderKey generate_order_key() = 0;
virtual MDP3TimeStamp now() const = 0;
@@ -4,17 +4,20 @@
#include "../../utils/mdp3_utils.h"
#include "../../utils/queries/TransactionDeducer.h"
#include "../../utils/queries/DefinitionCollector.h"
#include "../../utils/queries/PolygonFinder.h"
namespace bts::spartan::mdp3 {
SimInput make_sim_input_from_pcap(std::string const &pcap) {
TransactionDeducer td;
DefinitionCollector dc;
observe_channel(pcap,{&td,&dc});
PolygonFinder pf;
observe_channel(pcap,{&td,&dc,&pf});
SimInput result;
result.instruments = dc.result;
result.polygons = pf.result;
result.transactions = td.result;
MDP3OrderKey::Value max_ok = 0;
for (auto & t : result.transactions)
@@ -1,12 +1,15 @@
#pragma once
#include "../../common/InstrumentDefinition.h"
#include "../../utils/polygon_utils.h"
#include "ExchangeTransaction.h"
namespace bts::spartan::mdp3 {
struct SimInput {
std::unordered_map<MDP3SecurityID, InstrumentDefinition> instruments;
std::vector<Polygon> polygons;
std::vector<ExchangeTransaction> transactions;
MDP3OrderKey max_order_id;
};
@@ -6,6 +6,7 @@ namespace bts::spartan::mdp3 {
struct AlgoAgent::PImpl {
InstrumentMap const &instruments;
std::vector<Polygon> const &polygons;
MDP3OrderKey *last_order_key;
SimOptions options;
IAlgoModel &algo;
@@ -14,9 +15,10 @@ struct AlgoAgent::PImpl {
};
AlgoAgent::AlgoAgent(InstrumentMap const &instruments,
std::vector<Polygon> const &polygons,
MDP3OrderKey *next_order_key, SimOptions const &options,
IAlgoModel &algo)
: me(new PImpl{instruments, next_order_key, options, algo}) {}
: me(new PImpl{instruments, polygons, next_order_key, options, algo}) {}
AlgoAgent::AlgoAgent(AlgoAgent &&src) : me() { operator=(std::move(src)); }
@@ -59,6 +61,8 @@ void AlgoAgent::handle(sim::Event const &ev) {
AlgoAgent::~AlgoAgent() {}
std::vector<Polygon> const &AlgoAgent::polygons() const { return me->polygons; }
InstrumentMap const &AlgoAgent::instruments() const { return me->instruments; }
MDP3OrderKey AlgoAgent::generate_order_key() {
@@ -14,6 +14,7 @@ class InstrumentDefinition;
class AlgoAgent : ISimServices, public sim::AgentBase {
public:
AlgoAgent(InstrumentMap const &,
std::vector<Polygon> const&,
MDP3OrderKey *next_order,
SimOptions const&,
IAlgoModel &);
@@ -24,6 +25,7 @@ class AlgoAgent : ISimServices, public sim::AgentBase {
void init() override;
void handle(sim::Event const &) override;
private:
std::vector<Polygon> const &polygons() const override;
InstrumentMap const &instruments() const override;
MDP3OrderKey generate_order_key() override;
MDP3TimeStamp now() const override ;
@@ -685,7 +685,7 @@ int to_csv(std::string const &file, std::string const &out_dir) {
size_t pid = 0;
for (auto &p : poly_finder.result) {
pid++;
for (auto &pl : p.legs)
for (auto &pl : p.parts)
polys << pid << "," << pl.sec_id.value() << ","
<< (pl.ratio > 0 ? api::Side_Name(api::SIDE_BUY)
: api::Side_Name(api::SIDE_SELL))
@@ -1,23 +1,21 @@
#include "polygon_utils.h"
#include <iostream>
namespace bts::spartan::algo::poly {
namespace bts::spartan::mdp3 {
bool is_polygon(Instrument const &candidate,
std::vector<Instrument> const &universe);
size_t gcd(InstrumentParts const &);
Instrument make_spread(mdp3::MDP3SecurityID id,
std::initializer_list<InstrumentPart> legs) {
Instrument result;
PolyInstrument make_spread(mdp3::MDP3SecurityID id,
std::initializer_list<InstrumentPart> legs) {
PolyInstrument result;
result.sec_id = id;
for (auto l : legs)
result.parts.push_back(l);
return result;
}
Instrument make_outright(mdp3::MDP3SecurityID id) {
Instrument result;
PolyInstrument make_outright(mdp3::MDP3SecurityID id) {
PolyInstrument result;
result.sec_id = id;
result.parts.push_back(InstrumentPart{id, 1});
return result;
@@ -27,7 +25,7 @@ using RatioModifiers =
boost::container::small_vector<int, InstrumentParts::static_capacity>;
bool is_canonical_ratio(RatioModifiers const &parts) {
auto gcd= [] (RatioModifiers const &in) ->size_t {
auto gcd = [](RatioModifiers const &in) -> size_t {
size_t result = 0;
for (auto r : in)
result = std::gcd(std::abs(r), result);
@@ -51,11 +49,10 @@ bool is_canonical_ratio(RatioModifiers const &parts) {
void gen_ratio_modifiers(size_t max_ratio, RatioModifiers *working,
size_t working_idx, std::vector<RatioModifiers> *out) {
if (working_idx == working->size()) {
if (is_canonical_ratio(*working)) {
if (is_canonical_ratio(*working)) {
out->push_back(*working);
}
}
else {
} else {
for (int i = -max_ratio; i <= (int)max_ratio; i++) {
if (i == 0)
continue;
@@ -81,11 +78,10 @@ template <class C> void for_each_combination(size_t r, size_t n, C c) {
}
bool is_polygon(RatioModifiers const &ratio_modifier,
std::vector<Instrument const *> const &candidate) {
std::vector<PolyInstrument const *> const &candidate) {
bts_assert(candidate.size() == ratio_modifier.size());
InstrumentParts reduced_parts;
auto get_or_make_part =
[&](mdp3::MDP3SecurityID id) -> InstrumentPart & {
auto get_or_make_part = [&](mdp3::MDP3SecurityID id) -> InstrumentPart & {
for (auto &l : reduced_parts)
if (l.sec_id == id)
return l;
@@ -117,8 +113,8 @@ bool is_polygon(RatioModifiers const &ratio_modifier,
}
void eval_and_accumulate(RatioModifiers const &ratio_modifier,
std::vector<Instrument const *> const &candidate,
std::vector<Polygon> *out) {
std::vector<PolyInstrument const *> const &candidate,
std::vector<Polygon> *out) {
bts_assert(ratio_modifier.size() == candidate.size());
if (is_polygon(ratio_modifier, candidate)) {
@@ -144,7 +140,7 @@ bool operator<(Polygon const &i1, Polygon const &i2) {
if (l1.sec_id != l2.sec_id)
return l1.sec_id < l2.sec_id;
if (l1.ratio != l2.ratio)
return l1.ratio < l2.ratio;
return l1.ratio < l2.ratio;
}
return true;
}
@@ -164,31 +160,31 @@ bool operator==(Polygon const &i1, Polygon const &i2) {
}
std::vector<Polygon>
find_polygons(std::vector<Instrument> const &tradeable_instruments,
find_polygons(std::vector<PolyInstrument> const &tradeable_instruments,
size_t n_vertices, size_t max_ratio) {
std::vector<Polygon> result;
result.reserve(1024);
std::vector<Instrument const *> polygon_candidate;
std::vector<PolyInstrument const *> polygon_candidate;
polygon_candidate.reserve(100);
std::vector<RatioModifiers> modifiers;
gen_ratio_modifiers(n_vertices, max_ratio, &modifiers);
for_each_combination(n_vertices, tradeable_instruments.size(),
[&](auto const &idx_vec) {
polygon_candidate.clear();
for (size_t i = 0; i < idx_vec.size(); i++)
if (idx_vec[i])
polygon_candidate.push_back(&tradeable_instruments[i]);
for (auto &current_modifier : modifiers)
eval_and_accumulate(current_modifier, polygon_candidate, &result);
});
for_each_combination(
n_vertices, tradeable_instruments.size(), [&](auto const &idx_vec) {
polygon_candidate.clear();
for (size_t i = 0; i < idx_vec.size(); i++)
if (idx_vec[i])
polygon_candidate.push_back(&tradeable_instruments[i]);
for (auto &current_modifier : modifiers)
eval_and_accumulate(current_modifier, polygon_candidate, &result);
});
return result;
}
bool nets_to_0(Polygon const &p, std::vector<Instrument> const &universe) {
std::unordered_map<mdp3::MDP3SecurityID, Instrument const *> universe_idx;
bool nets_to_0(Polygon const &p, std::vector<PolyInstrument> const &universe) {
std::unordered_map<mdp3::MDP3SecurityID, PolyInstrument const *> universe_idx;
for (auto &i : universe)
universe_idx[i.sec_id] = &i;
@@ -205,7 +201,7 @@ bool nets_to_0(Polygon const &p, std::vector<Instrument> const &universe) {
return true;
}
std::ostream &operator<<(std::ostream &s, Instrument const &i) {
std::ostream &operator<<(std::ostream &s, PolyInstrument const &i) {
for (auto &l : i.parts)
s << l.ratio << ":" << l.sec_id.value() << " ";
return s;
@@ -217,4 +213,4 @@ std::ostream &operator<<(std::ostream &s, Polygon const &p) {
return s;
}
} // namespace bts::spartan::algo::poly
} // namespace bts::spartan::mdp3
@@ -1,10 +1,10 @@
#pragma once
#include "../../md/mdp3/common/mdp3_common.h"
#include "../common/mdp3_common.h"
#include <algorithm>
#include <boost/container/small_vector.hpp>
namespace bts::spartan::algo::poly {
namespace bts::spartan::mdp3 {
struct InstrumentPart {
mdp3::MDP3SecurityID sec_id = mdp3::MDP3SecurityID{0};
@@ -12,27 +12,26 @@ struct InstrumentPart {
};
using InstrumentParts = boost::container::small_vector<InstrumentPart, 6>;
struct Instrument {
struct PolyInstrument {
mdp3::MDP3SecurityID sec_id = mdp3::MDP3SecurityID{0};
InstrumentParts parts = {};
};
Instrument make_spread(mdp3::MDP3SecurityID,
PolyInstrument make_spread(mdp3::MDP3SecurityID,
std::initializer_list<InstrumentPart> legs);
Instrument make_outright(mdp3::MDP3SecurityID);
PolyInstrument make_outright(mdp3::MDP3SecurityID);
struct Polygon {
InstrumentParts parts;
};
bool operator==(Polygon const &, Polygon const &);
std::ostream &operator<<(std::ostream &s, Polygon const&);
std::ostream &operator<<(std::ostream &s, Polygon const &);
std::vector<Polygon>
find_polygons(std::vector<Instrument> const &tradeable_instruments,
find_polygons(std::vector<PolyInstrument> const &tradeable_instruments,
size_t n_vertices = 4, size_t max_ratio = 2);
// Testing helper
bool nets_to_0(Polygon const&p,
std::vector<Instrument> const &universe);
bool nets_to_0(Polygon const &p, std::vector<PolyInstrument> const &universe);
} // namespace bts::spartan::algo::poly
} // namespace bts::spartan::mdp3
@@ -1,6 +1,6 @@
#include "PolygonFinder.h"
#include "../../../../algo/polygon/polygon_utils.h"
#include "../polygon_utils.h"
#include "DefinitionCollector.h"
#include "FillCounter.h"
@@ -40,14 +40,12 @@ void PolygonFinder::finit() {
me->fill_counter.finit();
me->def_collector.finit();
using namespace algo;
// Get securities with enough active fills
std::unordered_map<MDP3SecurityID, poly::Instrument> poly_universe;
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] = poly::Instrument{f.sec_id};
poly_universe[f.sec_id] = PolyInstrument{f.sec_id};
// Now fill in the security legs
std::unordered_set<MDP3SecurityID> all_legs;
@@ -59,12 +57,12 @@ void PolygonFinder::finit() {
if (legs.empty())
poly_instrument.parts.push_back(
poly::InstrumentPart{poly_instrument.sec_id, 1});
InstrumentPart{poly_instrument.sec_id, 1});
else
for (auto &l : legs) {
poly_instrument.parts.push_back(
poly::InstrumentPart{l.security_id,
(l.side == api::SIDE_BUY? 1 : -1) * l.ratio.value()});
poly_instrument.parts.push_back(InstrumentPart{
l.security_id,
(l.side == api::SIDE_BUY ? 1 : -1) * l.ratio.value()});
all_legs.insert(l.security_id);
}
}
@@ -72,26 +70,22 @@ void PolygonFinder::finit() {
// Now close over the legs
for (auto lid : all_legs)
if (poly_universe.count(lid) == 0) {
poly::Instrument poly_instrument{lid};
PolyInstrument poly_instrument{lid};
poly_instrument.parts.push_back(
poly::InstrumentPart{poly_instrument.sec_id, 1});
InstrumentPart{poly_instrument.sec_id, 1});
poly_universe.insert({poly_instrument.sec_id, poly_instrument});
}
// Flatten this out in a vector
std::vector<poly::Instrument> poly_universe_vec;
std::vector<PolyInstrument> poly_universe_vec;
for (auto &p : poly_universe)
poly_universe_vec.push_back(p.second);
// Calc the polygon points, first for 3 vertices polygons, then 4, until max_vertices
for (size_t i = 3; i <= me->options.max_vertices;i++) {
auto polys = poly::find_polygons(poly_universe_vec,i,2);
// Massage the result
for (auto &p : polys) {
auto &polygon = result.emplace_back();
for (auto &l : p.parts)
polygon.legs.emplace_back(PolygonLeg{l.sec_id, l.ratio});
}
// Calc the polygon points, first for 3 vertices polygons, then 4,
// until max_vertices, accumulating all the results
for (size_t i = 3; i <= me->options.max_vertices; i++) {
auto polys = find_polygons(poly_universe_vec, i, 2);
result.insert(result.end(), polys.begin(), polys.end());
}
}
@@ -2,17 +2,10 @@
#include "../../common/mdp3_common.h"
#include "../../handler/IChannelObserver.h"
#include "../../utils/polygon_utils.h"
namespace bts::spartan::mdp3 {
struct PolygonLeg {
mdp3::MDP3SecurityID sec_id = mdp3::MDP3SecurityID{0};
int ratio = 0;
};
struct Polygon {
std::vector<PolygonLeg> legs = {};
};
struct PolygonFinder : IChannelObserver {
std::vector<Polygon> result;
@@ -1,21 +1,20 @@
#include "server/sparkd/cmds/spartan/algo/polygon/polygon_utils.h"
#include "../polygon_utils.h"
#include <catch/catch.hpp>
namespace bts::spartan::algo::poly {
namespace bts::spartan::mdp3 {
TEST_CASE("PolygonTests", "[gen_poly]") {
SECTION("a-b,-a,b") {
InstrumentPart la{mdp3::MDP3SecurityID{1}, 1};
InstrumentPart lb{mdp3::MDP3SecurityID{2}, -1};
Instrument a{la.sec_id};
PolyInstrument a{la.sec_id};
a.parts.push_back(la);
Instrument b{lb.sec_id};
PolyInstrument b{lb.sec_id};
b.parts.push_back(InstrumentPart{lb.sec_id, 1});
Instrument a_b{mdp3::MDP3SecurityID{3}};
PolyInstrument a_b{mdp3::MDP3SecurityID{3}};
a_b.parts.push_back(la);
a_b.parts.push_back(lb);
auto universe = {a, b, a_b};
@@ -225,12 +224,12 @@ TEST_CASE("PolygonTests", "[gen_poly]") {
{mdp3::MDP3SecurityID{4353}, mdp3::MDP3SecurityID{262483}, -1},
{mdp3::MDP3SecurityID{5029}, mdp3::MDP3SecurityID{262483}, 1},
};
std::vector<Instrument> universe;
std::vector<PolyInstrument> universe;
auto get_instrument = [&universe](mdp3::MDP3SecurityID key) {
for (auto &i : universe)
if (i.sec_id == key)
return &i;
return &universe.emplace_back(Instrument{key});
return &universe.emplace_back(PolyInstrument{key});
};
for (auto l : legs) {