Merge branch 'spartan_tas' of bitbucket.org:bluetradesys/spark into spartan_tas
This commit is contained in:
commit
dc8fd8baef
15 files changed
+131
-89
No files matched your search
Vendored
+2
-1
@@ -7,7 +7,8 @@
|
||||
"/usr/local/include",
|
||||
"${workspaceRoot}",
|
||||
"${workspaceFolder}/src",
|
||||
"${workspaceRoot}/externals/linux/include"
|
||||
"${workspaceRoot}/externals/linux/include",
|
||||
"${workspaceRoot}/build/ninja_debug/src"
|
||||
],
|
||||
"defines": [],
|
||||
"intelliSenseMode": "clang-x64",
|
||||
|
||||
Vendored
+14
-2
@@ -48,9 +48,9 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "linux build (Debug)",
|
||||
"label": "linux_build",
|
||||
"type": "shell",
|
||||
"command": "cd build/ninja_debug; time ninja",
|
||||
"command": "docker exec -i ${USER}s_docker bash -c \"cd build/ninja_${input:build_configuration} && ninja\"",
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
@@ -59,5 +59,17 @@
|
||||
"$gcc"
|
||||
]
|
||||
}
|
||||
],
|
||||
"inputs": [
|
||||
{
|
||||
"id": "build_configuration",
|
||||
"type": "pickString",
|
||||
"default": "debug",
|
||||
"description": "Configuration:",
|
||||
"options": [
|
||||
"debug",
|
||||
"release"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -87,6 +87,9 @@ class IntPrice {
|
||||
_exponent = temp._exponent;
|
||||
return *this;
|
||||
}
|
||||
IntPrice operator -() {
|
||||
return IntPrice(-_mantissa, _exponent);
|
||||
}
|
||||
IntPrice operator * (const IntPrice& other) const {
|
||||
return op(other, [](int64_t m1, int64_t m2) { return m1 * m2; });
|
||||
}
|
||||
|
||||
@@ -421,10 +421,9 @@ void AlgoWrapperOrder::subscribe_book(SecurityKey k) {
|
||||
me->services->subscribe_to_trade_signal(k, [this, k](auto const &ev) {
|
||||
ReportStateGuard _(*this);
|
||||
me->algo->on_trade_summary_signal(k,
|
||||
ev.signal.ag_side == co2::AggressorSide::AGGRESSOR_SIDE_BUY ?
|
||||
api::SIDE_BUY : api::SIDE_SELL,
|
||||
Price{ev.signal.min_price.price()},
|
||||
Price{ev.signal.max_price.price()});
|
||||
ev.signal.aggressor,
|
||||
Price{ev.signal.last_price.price()},
|
||||
Quantity{ev.signal.last_qty});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -56,7 +56,8 @@ class IAlgoOrder {
|
||||
// more trades between the two price levels specified for a buy or
|
||||
// sell aggressor.
|
||||
virtual void on_trade_summary_signal(SecurityKey, api::Side /* aggressor */,
|
||||
Price /* min_p */, Price /* max_p */) {}
|
||||
Price /* last_price */,
|
||||
Quantity /* last_quantity */) {}
|
||||
|
||||
// A periodic timer has fired
|
||||
virtual void on_timer() {}
|
||||
|
||||
@@ -482,8 +482,9 @@ void SniperAlgo::on_book(SecurityKey key, const Book& bk) {
|
||||
}
|
||||
|
||||
void SniperAlgo::on_trade_summary_signal(SecurityKey, api::Side /* aggressor */,
|
||||
Price /* min_p */, Price /* max_p */){
|
||||
}
|
||||
Price /* last_price */,
|
||||
Quantity /* last_quantity */)
|
||||
{}
|
||||
|
||||
void SniperAlgo::on_child_change(OrderKey key, const ChildState& st){
|
||||
LOG(OrderMan, Info,
|
||||
|
||||
@@ -2,7 +2,13 @@
|
||||
|
||||
#include "lib/orderman/orders/algo/IAlgoOrder.h"
|
||||
|
||||
namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}}
|
||||
namespace bts {
|
||||
namespace api {
|
||||
namespace orderman {
|
||||
class CustomOrderDefinition;
|
||||
}
|
||||
} // namespace api
|
||||
} // namespace bts
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -22,7 +28,8 @@ class SniperAlgo : public IAlgoOrder {
|
||||
void do_update(api::orderman::Update const &) override;
|
||||
void on_book(SecurityKey, Book const &) override;
|
||||
void on_trade_summary_signal(SecurityKey, api::Side /* aggressor */,
|
||||
Price /* min_p */, Price /* max_p */) override;
|
||||
Price /* last_price */,
|
||||
Quantity /* last_quantity */) override;
|
||||
void on_child_change(OrderKey, ChildState const &) override;
|
||||
void on_status(SecurityKey, co2::TradingStatus) override;
|
||||
void on_timer() override;
|
||||
@@ -31,14 +38,15 @@ class SniperAlgo : public IAlgoOrder {
|
||||
enum class State;
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
private:
|
||||
IAlgoServices& _algo() const ;
|
||||
bool _onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
const bts::api::orderman::CustomOrderDefinition& cod);
|
||||
void _compute_polygon_cross_value(SniperPolygon&);
|
||||
void _fire_orders(SecData& sec_data);
|
||||
|
||||
private:
|
||||
IAlgoServices &_algo() const;
|
||||
bool _onetime_setup(IBeforeLaunchAlgoServices &serv,
|
||||
const bts::api::orderman::CustomOrderDefinition &cod);
|
||||
void _compute_polygon_cross_value(SniperPolygon &);
|
||||
void _fire_orders(SecData &sec_data);
|
||||
// acts on md, cognizant of the state
|
||||
void _cxl_and_end (bool is_error, const std::string& err_msg);
|
||||
void _cxl_and_end(bool is_error, const std::string &err_msg);
|
||||
};
|
||||
|
||||
} // namespace orderman
|
||||
|
||||
@@ -57,7 +57,7 @@ struct FifoMatcherContext {
|
||||
// to initialize/reset the books (i.e., in response to the first callback,
|
||||
// or after reset events).
|
||||
template<class TIter>
|
||||
inline void reset(MDP3SecurityID sec_id, TIter first, TIter last) {
|
||||
inline void reset(MDP3SecurityID sec_id, TIter first, TIter last, std::vector<MatchEffect>* effects = nullptr) {
|
||||
_ensure_sync_implied_candidates();
|
||||
auto* matcher = _get_or_add_matcher(sec_id);
|
||||
// The 'order' in which orders are added to the matcher matters.
|
||||
@@ -80,6 +80,15 @@ struct FifoMatcherContext {
|
||||
first++;
|
||||
}
|
||||
matcher->matcher()->reset(sorted_orders.begin(), sorted_orders.end());
|
||||
if (effects != nullptr) {
|
||||
effects->emplace_back(MBPUpdate());
|
||||
MBPUpdate& mbp = std::get<MBPUpdate>(effects->back());
|
||||
mbp.security_id = sec_id;
|
||||
mbp.mbp_buy = matcher->book(api::Side::SIDE_BUY);
|
||||
mbp.mbp_sell = matcher->book(api::Side::SIDE_SELL);
|
||||
mbp.implied_mbp_buy = matcher->implied_book(api::Side::SIDE_BUY);
|
||||
mbp.implied_mbp_sell = matcher->implied_book(api::Side::SIDE_SELL);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace bts::spartan::mdp3
|
||||
@@ -86,10 +86,14 @@ struct MBPSnapshot {
|
||||
MBPData _initial_mbp;
|
||||
};
|
||||
|
||||
FifoMatcherContext& MergedMatcher::_asset_matcher(MDP3SecurityID sec) {
|
||||
FifoMatcherContext& MergedMatcher::_asset_matcher(MDP3SecurityID sec, bool add_if_missing) {
|
||||
auto ass_it = _asset_map.find(sec);
|
||||
if (add_if_missing && ass_it == _asset_map.end())
|
||||
ass_it = _asset_map.emplace(sec, std::string()).first;
|
||||
bts_assert(ass_it != _asset_map.end());
|
||||
auto mat_it = _matcher_map.find(ass_it->second);
|
||||
if (add_if_missing && mat_it == _matcher_map.end())
|
||||
mat_it = _matcher_map.emplace(ass_it->second, FifoMatcherContext({true})).first;
|
||||
bts_assert(mat_it != _matcher_map.end());
|
||||
return mat_it->second;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ private:
|
||||
size_t _match_count_this_per;
|
||||
google::protobuf::Timestamp _per_start_time;
|
||||
std::string _get_asset(MDP3SecurityID sec) const;
|
||||
FifoMatcherContext& _asset_matcher(MDP3SecurityID);
|
||||
FifoMatcherContext& _asset_matcher(MDP3SecurityID, bool add_if_missing = true);
|
||||
|
||||
public:
|
||||
MergedMatcher ();
|
||||
@@ -54,9 +54,11 @@ public:
|
||||
first++;
|
||||
}
|
||||
}
|
||||
template<class Iter>
|
||||
void reset(MDP3SecurityID sec, Iter first, Iter last) {
|
||||
_asset_matcher(sec).reset(sec, first, last);
|
||||
|
||||
void reset(MDP3SecurityID sec, std::vector<MDP3Order> orders, std::vector<MatchEffect>* effects = nullptr) {
|
||||
for (const auto& o : orders)
|
||||
_used_order_keys.emplace(o.key);
|
||||
_asset_matcher(sec).reset(sec, orders.begin(), orders.end(), effects);
|
||||
}
|
||||
void clear();
|
||||
};
|
||||
|
||||
@@ -32,7 +32,6 @@ void add_iceberg_annotation(
|
||||
auto show = std::get<1>(ib.show_qty_at(event_id));
|
||||
o.iceberg_quantity.value().total = ib.total_qty();
|
||||
o.iceberg_quantity.value().show = show;
|
||||
o.quantity = show;
|
||||
bts_assert(o.iceberg_quantity.value().show.value() > 0);
|
||||
//bts_assert(o.iceberg_quantity.value().total >= o.iceberg_quantity.value().show);
|
||||
if (set_exact_state) {
|
||||
|
||||
@@ -347,13 +347,12 @@ void Sim::_start_simulation() {
|
||||
matcher.add_def(snap.definition());
|
||||
if (snap.ready_to_trade()) {
|
||||
simulation.ready_secs.insert(sec_id);
|
||||
SmallOrderChangeVec matcher_input;
|
||||
for (auto &p : snap.orders())
|
||||
matcher_input.push_back(correct_pcap_order(
|
||||
OrderChange{p.second, p.second, OrderChange::added}, true));
|
||||
std::vector<MDP3Order> resting_orders;
|
||||
for (const auto& p : snap.orders())
|
||||
resting_orders.emplace_back(
|
||||
correct_pcap_order({p.second, p.second, OrderChange::added}, true).order);
|
||||
std::vector<MergedMatcher::MatchEffect> effects;
|
||||
simulation.matcher.match(MergedMatcher::SIMULATOR_INPUT, matcher_input,
|
||||
&effects);
|
||||
matcher.reset(sec_id, resting_orders, &effects);
|
||||
for (auto &ef : effects)
|
||||
std::visit(
|
||||
overloaded{[&](MBPUpdate &mbp) {
|
||||
@@ -473,7 +472,7 @@ void Sim::clear_simulated_order_book(MDP3TimeStamp transact_time,
|
||||
match_and_send_effects(transact_time, matcher_input);
|
||||
}
|
||||
|
||||
SmallOrderChange Sim::correct_pcap_order(OrderChange const &o, bool) {
|
||||
SmallOrderChange Sim::correct_pcap_order(OrderChange const &o, bool set_exact_ib_state) {
|
||||
bts_assert(me->state == State::SIM);
|
||||
bts_assert(me->simulation);
|
||||
auto &key_map = me->simulation->cme_key_map;
|
||||
@@ -502,14 +501,13 @@ SmallOrderChange Sim::correct_pcap_order(OrderChange const &o, bool) {
|
||||
result.order.key = our_key;
|
||||
}
|
||||
}
|
||||
// if (o.change != OrderChange::ChangeType::removed) {
|
||||
// // IcebergMap is keyed by exchange keys
|
||||
// auto ib_it = me->ib_map.find(result.original_key);
|
||||
// if (ib_it != me->ib_map.end())
|
||||
// iceberg_helper::add_iceberg_annotation(
|
||||
// result.order, result.change, ib_it->second,
|
||||
// me->event_id.first, set_exact_ib_state);
|
||||
// }
|
||||
if (o.change != OrderChange::ChangeType::removed) {
|
||||
// IcebergMap is keyed by exchange keys
|
||||
auto ib_it = me->ib_map.find(result.original_key);
|
||||
if (ib_it != me->ib_map.end())
|
||||
iceberg_helper::add_iceberg_annotation(
|
||||
result.order, result.change, ib_it->second, me->event_id.first, set_exact_ib_state);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,27 +52,27 @@ void compute_synthetic_book(
|
||||
Leg() : book(), curr_level(0), curr_level_qty_used(0), leg_qty(0) {}
|
||||
|
||||
md::IntPrice curr_price() const {
|
||||
// Assert that the book isn't 'exhausted' yet.
|
||||
// Assert that the book isn't empty yet.
|
||||
// Note: one could argue that nan be returned in this event,
|
||||
// but since the loop is designed to return "greedily,"
|
||||
// 'curr_price' should not be accessed after exhaustion.
|
||||
// 'curr_price' should not be accessed after it has been emptied.
|
||||
// This assertion enforces that the behavior is only changed
|
||||
// as a result of deliberate action, rather than an inadvertant
|
||||
// side-effect.
|
||||
bts_assert(!is_exhausted());
|
||||
bts_assert(!is_empty());
|
||||
return book[curr_level].price;
|
||||
}
|
||||
MDP3Quantity curr_level_qty_rem() const {
|
||||
return book[curr_level].quantity - curr_level_qty_used;
|
||||
}
|
||||
// The implied market's volume is limited by its thinnest market.
|
||||
// When one leg 'is_exhausted,' the full volume of the implied market
|
||||
// When one leg 'is_empty,' the full volume of the implied market
|
||||
// has been realized.
|
||||
bool is_exhausted() const { return curr_level >= book.size(); }
|
||||
bool is_empty() const { return curr_level >= book.size(); }
|
||||
};
|
||||
|
||||
std::vector<Leg> legs;
|
||||
for (auto [qty, price_mult, book] : leg_mkts) {
|
||||
for (const auto& [qty, price_mult, book] : leg_mkts) {
|
||||
// todo: implement price mult logic (for now just assert nan or 1)
|
||||
bts_assert(price_mult.is_nan() || price_mult.price() == 1);
|
||||
auto& l = legs.emplace_back();
|
||||
@@ -86,21 +86,21 @@ void compute_synthetic_book(
|
||||
return;
|
||||
}
|
||||
|
||||
auto any_legs_exhausted = [&]() {
|
||||
auto any_legs_empty = [&]() {
|
||||
for (auto& l : legs)
|
||||
if (l.is_exhausted())
|
||||
if (l.is_empty())
|
||||
return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
while (!any_legs_exhausted() && depth-- > 0) {
|
||||
while (!any_legs_empty() && depth-- > 0) {
|
||||
// legs can't be empty, because it is a 1:1 mapping of leg_mkts,
|
||||
// and it has already been established that the latter isn't empty.
|
||||
int limiting_factor = legs[0].curr_level_qty_rem().value();
|
||||
for (uint32_t i = 0; i < legs.size(); i++) {
|
||||
if (legs[i].curr_level_qty_rem().value() == 0) {
|
||||
legs[i].curr_level++;
|
||||
if (legs[i].is_exhausted()) {
|
||||
if (legs[i].is_empty()) {
|
||||
// book is complete
|
||||
return;
|
||||
}
|
||||
@@ -116,11 +116,16 @@ void compute_synthetic_book(
|
||||
legs[i].curr_level_qty_used += MDP3Quantity(limiting_factor);
|
||||
auto marginal_cost = md::INT_NAN;
|
||||
for (auto& l : legs) {
|
||||
// The book's prices have already been multiplied by abs(leg_qty);
|
||||
// Subtract the leg's cost if qty is negative, otherwise add it
|
||||
// to the synthetic's marginal cost.
|
||||
auto lcost = l.curr_price();
|
||||
if (l.leg_qty.value() < 0)
|
||||
lcost = -lcost;
|
||||
if (marginal_cost.is_nan())
|
||||
marginal_cost = l.curr_price() * l.leg_qty.value();
|
||||
marginal_cost = lcost;
|
||||
else
|
||||
marginal_cost =
|
||||
marginal_cost + l.curr_price() * l.leg_qty.value();
|
||||
marginal_cost += lcost;
|
||||
}
|
||||
dest.emplace_back(marginal_cost, MDP3Quantity(limiting_factor));
|
||||
}
|
||||
|
||||
@@ -230,47 +230,47 @@ class MDP3Listener : public onixs::HandlerListener,
|
||||
onMessage(onixs::Handler &,
|
||||
const onixs::IncrementalRefreshTradeSummary48Args &args) override {
|
||||
if (_trade_summary_dispatcher) {
|
||||
std::array<TradeSummarySignal, 2> levels;
|
||||
|
||||
security_key sk = invalid_security_key;
|
||||
TradeSummarySignal signal;
|
||||
|
||||
for (const auto &entry : args.message().entries()) {
|
||||
auto data = _index.get_security(entry.securityId());
|
||||
|
||||
if (!data)
|
||||
continue;
|
||||
return;
|
||||
|
||||
if (sk == invalid_security_key)
|
||||
sk = data->handle.key();
|
||||
// Get Onix Aggressor Side, bail if its weird
|
||||
onixs::AggressorSide::Enum onixs_ag_side;
|
||||
bts_assert(entry.aggressorSide(onixs_ag_side));
|
||||
api::Side ag_side;
|
||||
if (onixs_ag_side == onixs::AggressorSide::Buy)
|
||||
ag_side = api::SIDE_BUY;
|
||||
else if (onixs_ag_side == onixs::AggressorSide::Sell)
|
||||
ag_side = api::SIDE_SELL;
|
||||
else
|
||||
bts_assert(sk == data->handle.key());
|
||||
return;
|
||||
|
||||
onixs::AggressorSide::Enum onix_ag_side;
|
||||
bts_assert(entry.aggressorSide(onix_ag_side));
|
||||
|
||||
auto agg = map_aggressor(onix_ag_side);
|
||||
auto idx = (uint)agg;
|
||||
bts_assert(idx == 1 || idx == 2);
|
||||
auto &this_level = levels[idx - 1];
|
||||
this_level.ts = _packet_arrived;
|
||||
this_level.sk = sk;
|
||||
|
||||
auto price = convert_price(entry.entryPx(),
|
||||
data->handle.security().display_factor);
|
||||
auto &max_price = this_level.max_price;
|
||||
auto &min_price = this_level.min_price;
|
||||
if (price > max_price)
|
||||
max_price = price;
|
||||
if (price < min_price)
|
||||
min_price = price;
|
||||
}
|
||||
|
||||
if (levels[0].ts != levels[1].ts) {
|
||||
if (levels[0].ts != utils::TimeStamp()) {
|
||||
_trade_summary_dispatcher->emit(levels[0]);
|
||||
}
|
||||
else {
|
||||
_trade_summary_dispatcher->emit(levels[1]);
|
||||
// We only process vanilla trade summaries where the
|
||||
// aggressor is always the same order. The first summary
|
||||
// we find defines the entire signal. If we see anything
|
||||
// not vanilla we bail
|
||||
if (signal.sk == invalid_security_key) {
|
||||
// First entry defines the side and security
|
||||
signal.sk = data->handle.key();
|
||||
signal.ts = _packet_arrived;
|
||||
signal.aggressor = ag_side;
|
||||
}
|
||||
else
|
||||
if (signal.sk != data->handle.key() ||
|
||||
signal.aggressor != ag_side)
|
||||
return;
|
||||
|
||||
|
||||
signal.last_price = convert_price(entry.entryPx(),
|
||||
data->handle.security().display_factor);
|
||||
signal.last_qty = entry.entrySize();
|
||||
}
|
||||
_trade_summary_dispatcher->emit(signal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@ namespace md {
|
||||
struct TradeSummarySignal {
|
||||
utils::TimeStamp ts = {};
|
||||
security_key sk = invalid_security_key;
|
||||
co2::AggressorSide ag_side = co2::AggressorSide::AGGRESSOR_SIDE_NONE;
|
||||
IntPrice max_price = IntPrice {std::numeric_limits<int64_t>::max(), -7};
|
||||
IntPrice min_price = IntPrice {std::numeric_limits<int64_t>::max(), -7};
|
||||
api::Side aggressor = api::SIDE_UNDEFINED;
|
||||
IntPrice last_price = {} ;
|
||||
uint last_qty = 0;
|
||||
};
|
||||
|
||||
class ITradeSummarySignalListener {
|
||||
|
||||
Reference in new issue
Block a user