Sniper is sniping. We are going to be simulated millionaires.
This commit is contained in:
1 parent
1fb1af7132
commit
299bf7b4da
4 files changed
+165
-20
No files matched your search
@@ -1,15 +1,29 @@
|
||||
#include "Sniper.h"
|
||||
#include "../../utils/mdp3_utils.h"
|
||||
#include "../simulator/ISimServices.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/logging/Logger.h"
|
||||
#include "lib/os/os.h"
|
||||
|
||||
#include <queue>
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
enum class Sniper::State { INIT, WATCH, WAIT };
|
||||
enum class Sniper::State { WATCH, WAIT };
|
||||
|
||||
struct Sniper::PImpl {
|
||||
ISimServices *sim = nullptr;
|
||||
State state = State::INIT;
|
||||
State state = State::WATCH;
|
||||
std::vector<Polygon> const *polygons;
|
||||
std::unordered_map<MDP3SecurityID, MBPUpdate> polygon_parts_books;
|
||||
std::unordered_map<MDP3SecurityID, MBPUpdate> books;
|
||||
std::unordered_set<MDP3OrderKey> in_flight;
|
||||
size_t current_shot;
|
||||
};
|
||||
|
||||
struct Sniper::Target {
|
||||
Polygon polygon;
|
||||
double value;
|
||||
bool operator<(Target const &rhs) const { return value < rhs.value; }
|
||||
};
|
||||
|
||||
Sniper::Sniper() : me(new PImpl()) {}
|
||||
@@ -17,13 +31,15 @@ Sniper::~Sniper() {}
|
||||
|
||||
void Sniper::init(ISimServices &s) {
|
||||
me->sim = &s;
|
||||
me->state = State::INIT;
|
||||
me->state = State::WATCH;
|
||||
me->polygons = &s.polygons();
|
||||
me->polygon_parts_books.clear();
|
||||
me->books.clear();
|
||||
me->in_flight.clear();
|
||||
me->current_shot = 0;
|
||||
|
||||
for (auto &p : *me->polygons)
|
||||
for (auto &pp : p.parts) {
|
||||
me->polygon_parts_books[pp.sec_id] = {};
|
||||
}
|
||||
for (auto &pp : p.parts)
|
||||
me->books[pp.sec_id] = {};
|
||||
}
|
||||
|
||||
void Sniper::on_time(MDP3TimeStamp) {}
|
||||
@@ -32,21 +48,124 @@ void Sniper::on_notification(AlgoNotification const &n) {
|
||||
std::visit(*this, n.event);
|
||||
}
|
||||
|
||||
void Sniper::operator()(const MBPUpdate &mbp) {
|
||||
if (me->polygon_parts_books.count(mbp.security_id)) {
|
||||
me->polygon_parts_books[mbp.security_id] = mbp;
|
||||
api::Side side_from_ratio(int ratio) {
|
||||
if (ratio > 0)
|
||||
return api::Side::SIDE_BUY;
|
||||
else
|
||||
return api::Side::SIDE_SELL;
|
||||
}
|
||||
|
||||
if (me->state == State::INIT) {
|
||||
for (auto &pp : me->polygon_parts_books)
|
||||
if (pp.second.mbp_buy[0].quantity.value() == 0 ||
|
||||
pp.second.mbp_sell[0].quantity.value() == 0)
|
||||
return;
|
||||
}
|
||||
std::cout << "Got all books";
|
||||
me->state = State::WATCH;
|
||||
Sniper::Target Sniper::eval(Polygon const &p, api::Side s) {
|
||||
Polygon poly = p;
|
||||
if (s == api::Side::SIDE_SELL)
|
||||
poly = flip(poly);
|
||||
|
||||
Target result{poly, 0};
|
||||
for (auto &l : poly.parts) {
|
||||
auto mkt_side = securities::other_side(side_from_ratio(l.ratio));
|
||||
auto &mkt = me->books[l.sec_id].mbp_side(mkt_side)[0];
|
||||
if (mkt.quantity.value() == 0)
|
||||
return Target{poly, 0};
|
||||
else
|
||||
result.value += (-l.ratio * mkt.price.price());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Sniper::MaybeTarget Sniper::pick() {
|
||||
std::set<Target> valuation;
|
||||
for (auto &p : *me->polygons) {
|
||||
valuation.insert(eval(p, api::SIDE_BUY));
|
||||
valuation.insert(eval(p, api::SIDE_SELL));
|
||||
}
|
||||
LOG(Spartan, Diag, "Evaluating --------------------------");
|
||||
for (auto const &op : valuation)
|
||||
LOG(Spartan, Diag, "Found opportunity: {}", op.value);
|
||||
|
||||
auto const &best = *valuation.rbegin();
|
||||
if (best.value > 0)
|
||||
return {best};
|
||||
else
|
||||
return {};
|
||||
}
|
||||
|
||||
void Sniper::fire(Target const &t) {
|
||||
for (auto &p : t.polygon.parts) {
|
||||
auto part_side = p.ratio > 0 ? api::SIDE_BUY : api::SIDE_SELL;
|
||||
auto mkt_side = securities::other_side(part_side);
|
||||
auto mkt = me->books[p.sec_id].mbp_side(mkt_side)[0];
|
||||
auto mkt_price = mkt.price;
|
||||
|
||||
auto order_change = OrderChange{};
|
||||
order_change.order.key = me->sim->generate_order_key();
|
||||
order_change.order.security = p.sec_id;
|
||||
order_change.order.price = mkt_price;
|
||||
order_change.order.quantity = MDP3Quantity{1};
|
||||
order_change.order.side = part_side;
|
||||
order_change.change = OrderChange::added;
|
||||
if (os::is_nan(order_change.order.price.price()))
|
||||
debug_break();
|
||||
LOG(Spartan, Info, "Time: {} Shot: {} Firing order: {}",
|
||||
to_time_string(me->sim->now()), me->current_shot, order_change.order);
|
||||
me->sim->send(order_change);
|
||||
order_change.change = OrderChange::removed;
|
||||
me->sim->send(order_change);
|
||||
me->in_flight.insert(order_change.order.key);
|
||||
}
|
||||
me->current_shot++;
|
||||
}
|
||||
|
||||
void Sniper::eval_pick_fire() {
|
||||
bts_assert(me->state == State::WATCH);
|
||||
auto best_target = pick();
|
||||
if (best_target) {
|
||||
LOG(Spartan, Info,
|
||||
"Found mispricing. Value: {}. Firing ------------------",
|
||||
best_target.value().value);
|
||||
fire(best_target.value());
|
||||
transition(State::WAIT);
|
||||
}
|
||||
}
|
||||
|
||||
void Sniper::operator()(const OrderUpdate &) {}
|
||||
void Sniper::operator()(const MBPUpdate &mbp) {
|
||||
if (me->books.count(mbp.security_id)) {
|
||||
me->books[mbp.security_id] = mbp;
|
||||
switch (me->state) {
|
||||
case State::WATCH:
|
||||
eval_pick_fire();
|
||||
case State::WAIT:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Sniper::operator()(const OrderUpdate &update) {
|
||||
bts_assert(update.status == OrderUpdate::FILLED ||
|
||||
update.status == OrderUpdate::CANCELED);
|
||||
bts_assert(me->in_flight.count(update.key));
|
||||
me->in_flight.erase(update.key);
|
||||
LOG(Spartan, Info, "Order: {} Status: {}", update.key.value(),
|
||||
log::CharPtrWrapper{update.status == OrderUpdate::FILLED ? "filled"
|
||||
: "canceled"});
|
||||
bts_assert(me->state == State::WAIT);
|
||||
if (me->in_flight.empty()) {
|
||||
transition(State::WATCH);
|
||||
eval_pick_fire();
|
||||
}
|
||||
}
|
||||
|
||||
const char *Sniper::state_name(State s) {
|
||||
switch (s) {
|
||||
case State::WAIT:
|
||||
return "WAIT";
|
||||
case State::WATCH:
|
||||
return "WATCH";
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Sniper::transition(State new_state) {
|
||||
me->state = new_state;
|
||||
}
|
||||
|
||||
} // namespace bts::spartan::mdp3
|
||||
@@ -1,9 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "../simulator/IAlgoModel.h"
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
struct Polygon;
|
||||
|
||||
class Sniper : public IAlgoModel {
|
||||
public:
|
||||
@@ -19,6 +22,15 @@ public:
|
||||
private:
|
||||
enum class State;
|
||||
struct PImpl;
|
||||
struct Target;
|
||||
using MaybeTarget = std::optional<Target>;
|
||||
Target eval(Polygon const&,api::Side);
|
||||
MaybeTarget pick();
|
||||
void fire(Target const&);
|
||||
void eval_pick_fire();
|
||||
void transition(State);
|
||||
const char *state_name(State);
|
||||
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
|
||||
@@ -21,6 +21,17 @@ PolyInstrument make_outright(mdp3::MDP3SecurityID id) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Polygon flip(Polygon const&p) {
|
||||
return scale(p,-1);
|
||||
}
|
||||
|
||||
Polygon scale(Polygon const&p, int8_t by) {
|
||||
Polygon result = p;
|
||||
for (auto &parts : result.parts)
|
||||
parts.ratio *= by;
|
||||
return result;
|
||||
}
|
||||
|
||||
using RatioModifiers =
|
||||
boost::container::small_vector<int, InstrumentParts::static_capacity>;
|
||||
|
||||
|
||||
@@ -27,6 +27,9 @@ struct Polygon {
|
||||
bool operator==(Polygon const &, Polygon const &);
|
||||
std::ostream &operator<<(std::ostream &s, Polygon const &);
|
||||
|
||||
Polygon flip(Polygon const&);
|
||||
Polygon scale(Polygon const&,int8_t by);
|
||||
|
||||
std::vector<Polygon>
|
||||
find_polygons(std::vector<PolyInstrument> const &tradeable_instruments,
|
||||
size_t n_vertices = 4, size_t max_ratio = 2);
|
||||
|
||||
Reference in new issue
Block a user