Files
spark/src/server/sparkd/cmds/spartan/back_testing/algos/PolyObserver.cpp
T
2021-02-05 21:09:37 -05:00

184 lines
6.0 KiB
C++

#include "PolyObserver.h"
#include "back_testing/sim/ISim.h"
#include "back_testing/sim/sim_common.h"
#include "domain/SecurityDefinition.h"
#include "domain/polygons/polygon_utils.h"
namespace bts::spartan::mdp3 {
static auto constexpr Worthless = -1.0;
struct PolyObserver::Target {
size_t polygon_id;
Polygon const polygon;
std::string const descriptor;
api::Side side;
std::string const product;
double value = Worthless;
Quantity quantity = Quantity{0};
void mark_as_worthless() { value = Worthless; }
bool has_value() const { return value > securities::market_epsilon(); }
};
struct PolyObserver::Opportunity {
Target *target;
double value;
Quantity quantity;
EventHeader start;
};
struct PolyObserver::PImpl {
std::vector<std::unique_ptr<Target>> targets;
std::optional<Opportunity> current_opportunity;
std::unordered_map<SecurityID, SimBookUpdate> books;
EventHeader last_event;
ISim *sim = nullptr;
};
PolyObserver::PolyObserver() : me(new PImpl()) {}
PolyObserver::~PolyObserver() {}
void PolyObserver::init(ISim &s, protos::SimRunOptions const &) {
me->sim = &s;
for (auto &p : s.polygons()) {
auto descriptor = polygon_descriptor(p.second, s.securities());
auto buy_t = Target{
p.first, p.second, descriptor, api::SIDE_BUY,
polygon_product(p.second, s.securities())};
auto sell_t = Target{
p.first, flip(p.second), descriptor, api::SIDE_SELL,
polygon_product(p.second, s.securities())};
me->targets.emplace_back(std::make_unique<Target>(buy_t));
me->targets.emplace_back(std::make_unique<Target>(sell_t));
}
}
void PolyObserver::finit() {}
void PolyObserver::on_replay_start(
EventHeader const &current_event,
std::vector<SimBookUpdate> const &current_books) {
me->sim->log("replay_start", current_event.event_id.first);
me->books.clear();
for (auto const &b : current_books)
me->books[b.security_id] = b;
}
void PolyObserver::eval() {
for (auto &t : me->targets) {
t->value = 0;
int qty = std::numeric_limits<int>::max();
for (auto &l : t->polygon.parts) {
auto mkt_side = side_from_ratio(l.ratio);
auto &mkt = me->books[l.sec_id].mbp_side(mkt_side);
auto this_qty = mkt[0].quantity.value();
auto this_ratio = this_qty / std::abs(l.ratio);
if (this_ratio < qty)
qty = this_ratio;
if (qty == 0) {
t->mark_as_worthless();
break;
} else {
auto cash = me->sim->get_security(l.sec_id).to_cash(mkt[0].price);
t->quantity.value() = qty;
t->value += -l.ratio * cash;
}
}
}
}
void PolyObserver::rank() {
std::sort(
me->targets.begin(), me->targets.end(),
[](std::unique_ptr<Target> const &ptr1,
std::unique_ptr<Target> const &ptr2) {
auto val1 = ptr1->value; // * ptr1->quantity.value();
auto val2 = ptr2->value; // * ptr2->quantity.value();
if (std::abs(val1 - val2) > securities::market_epsilon())
return val1 > val2;
else if (ptr1->polygon.parts.size() != ptr2->polygon.parts.size())
return ptr1->polygon.parts.size() < ptr2->polygon.parts.size();
else
return ptr1->polygon_id < ptr2->polygon_id;
});
}
void PolyObserver::try_start_new_opportunity() {
bts_assert(!me->current_opportunity);
auto best = me->targets.begin()->get();
if (best->has_value()) {
// We have an opportunity lets record it
me->current_opportunity =
Opportunity{best, best->value, best->quantity, me->last_event};
me->sim->log(
"opportunity_begin", best->polygon_id, best->descriptor,
api::Side_Name(best->side), best->value, best->quantity.value(), 0, 0,
(me->last_event.observable_time - me->last_event.transact_time)
.micros(),
me->last_event.event_id.first, me->last_event.event_id.first);
}
}
void PolyObserver::analyze() {
if (me->current_opportunity) {
auto &opportunity = *me->current_opportunity;
auto &target = *opportunity.target;
if (target.has_value()) {
// Add value and still has value, opportunity is still ongoing
me->sim->log(
"opportunity_continue", target.polygon_id, target.descriptor,
api::Side_Name(target.side), target.value, target.quantity.value(),
(me->last_event.observable_time -
opportunity.start.observable_time)
.micros(),
(me->last_event.transact_time - opportunity.start.transact_time)
.micros(),
(opportunity.start.observable_time -
opportunity.start.transact_time)
.micros(),
opportunity.start.event_id.first);
} else {
// Had value but not anymore, opportunity is concluded
me->sim->log(
"opportunity_end", target.polygon_id, target.descriptor,
api::Side_Name(target.side), opportunity.value,
opportunity.quantity.value(),
(me->last_event.observable_time -
opportunity.start.observable_time)
.micros(),
(me->last_event.transact_time - opportunity.start.transact_time)
.micros(),
(opportunity.start.observable_time -
opportunity.start.transact_time)
.micros(),
opportunity.start.event_id.first,
me->last_event.event_id.first - 1);
me->current_opportunity.reset();
try_start_new_opportunity();
}
} else {
// We did not have an opportunity before. Maybe we do now?
try_start_new_opportunity();
}
}
void PolyObserver::on_order_update(
EventHeader const &, SimOrderUpdate const &) {
bts_assert(false);
}
void PolyObserver::on_mbp(EventHeader const &h, SimBookUpdate const &mbp) {
me->last_event = h;
me->books[mbp.security_id] = mbp;
eval();
rank();
analyze();
}
void PolyObserver::on_time() {}
} // namespace bts::spartan::mdp3