Compare commits

...
Author SHA1 Message Date
John Colvin ad23a2aebe checkpoint b4 trip 2020-03-17 13:04:06 -05:00
John Colvin fdbf4aea46 incremental (FillBuilderPriceNode) 2020-03-17 13:04:06 -05:00
John Colvin 3c46f771d0 checkpoint (b4 fillbuilder template) 2020-03-17 13:04:06 -05:00
John Colvin 1abfb20678 FillOp (optimized) 2020-03-17 13:04:06 -05:00
John Colvin 3f70ababda checkpoint 2020-03-17 13:04:06 -05:00
14 changed files with 1045 additions and 0 deletions

No files matched your search

+23
View File
@@ -21,6 +21,7 @@ struct SnapshotOrder {
IntPrice price;
int32_t size;
};
struct MBOSnapshot {
MBOSnapshot() = default;
MBOSnapshot(const MBOSnapshot&) = delete;
@@ -81,6 +82,20 @@ namespace inc {
}
struct MBOUpdate {
inc::Update update;
utils::TimeStamp timestamp;
//google::protobuf::Timestamp pb_timestamp() {
// todo
// google::protobuf::Timestamp result;
// return result;
// }
//SnapshotOrder state;
ExchangeOrderKey id;
std::vector<SnapshotOrder> bids;
std::vector<SnapshotOrder> asks;
};
struct MBOWorkingState {
typedef std::tuple<IntPrice,inc::Priority> key_t;
struct Data {
@@ -168,6 +183,14 @@ public:
MBOReader& reader();
};
//struct MBOUpdateWrapper {
//MBOUpdateWrapper(const MBOReader& reader)
//: _reader(reader) {}
//private:
//bool _is_initialized;
//const MBOReader& _reader;
//};
//
}}
+92
View File
@@ -0,0 +1,92 @@
#pragma once
#include "lib/utils/TimeStamp.h"
#include "server/sparkd/fill_models/ExchangeOrderSnapshot.h"
#include "server/sparkd/fill_models/OrderStatus.h"
#include "server/sparkd/fill_models/PriceTimePriority.h"
namespace bts {
namespace md {
struct PriceCmp {
bool operator()(const IntPrice &lhs, const IntPrice &rhs) {
return lhs.price() > rhs.price();
}
};
struct BidPriceCmp {
bool operator()(const IntPrice &lhs, const IntPrice &rhs) {
return lhs.price() > rhs.price();
}
};
struct AskPriceCmp {
bool operator()(const IntPrice &lhs, const IntPrice &rhs) {
return lhs.price() < rhs.price();
}
};
struct BidOrderPriorityCmp {
bool operator()(const OrderStatus &lhs, const OrderStatus &rhs) {
return lhs.price.price() > rhs.price.price() ||
lhs.transaction_time.to_uint64() <
rhs.transaction_time.to_uint64();
}
bool operator()(const PriceTimePriorty &lhs, const PriceTimePriority &rhs) {
return lhs.price.price() > rhs.price.price() ||
lhs.time.to_uint64() < rhs.time.to_uint64();
}
};
struct AskOrderPriorityCmp {
bool operator()(const OrderStatus &lhs, const OrderStatus &rhs) {
return lhs.price.price() < rhs.price.price() ||
lhs.transaction_time.to_uint64() <
rhs.transaction_time.to_uint64();
}
bool operator()(const PriceTimePriorty &lhs, const PriceTimePriority &rhs) {
return lhs.price.price() < rhs.price.price() ||
lhs.time.to_uint64() < rhs.time.to_uint64();
}
};
struct TransactionTimeCmp {
// bool operator()(const OrderStatus& lhs, const OrderStatus& rhs) {
// return lhs.transaction_time > rhs.transaction_time;
// }
// bool cmp(const std::pair<const bts::utils::TimeStamp, bts::md::MBOUpdate>&
// , const bts::utils::TimeStamp& )
//{
// return true;
//}
bool comp_member(bts::utils::TimeStamp, bts::utils::TimeStamp) {
return true;
}
//bool operator()(const
bool operator()(const bts::utils::TimeStamp &lhs,
const bts::utils::TimeStamp &rhs) {
return lhs.to_uint64() > rhs.to_uint64();
}
bool operator()(const ExchangeOrderSnapshot &lhs,
const ExchangeOrderSnapshot &rhs) {
return lhs.transaction_time.to_uint64() >
rhs.transaction_time.to_uint64();
}
bool operator()(const OrderStatus &lhs, const OrderStatus &rhs) {
return lhs.transaction_time > rhs.transaction_time;
}
bool operator()(const google::protobuf::Timestamp &lhs,
const google::protobuf::Timestamp &rhs) {
if (lhs.seconds() > rhs.seconds()) {
return true;
} else if (lhs.seconds() < rhs.seconds()) {
return false;
} else {
return lhs.nanos() > rhs.nanos();
}
}
};
} // namespace md
} // namespace bts
@@ -0,0 +1,20 @@
#pragma once
#include "lib/md/MBOProcessor.h"
#include "lib/utils/TimeStamp.h"
namespace bts {
namespace md {
struct ExchangeOrderSnapshot {
SnapshotOrder *state;
bts::utils::TimeStamp transaction_time;
uint64_t priority;
bool is_bid;
ExchangeOrderSnapshot() {}
ExchangeOrderSnapshot(SnapshotOrder *state, TimeStamp transaction_time)
: state(state), transaction_time(transaction_time) {}
};
} // namespace md
} // namespace bts
@@ -0,0 +1,125 @@
#include "lib/config/config.h"
//#include "lib/config/protos/toggles.pb.h"
//#include "lib/utils/ConcurrentPool.h"
//#include "lib/utils/WaitFreeQueue.h"
//#include "lib/utils/thread_manager.h"
#include <atomic>
#include <functional>
#include <map>
#include <sstream>
#include <thread>
#include <vector>
#ifndef _WIN32
//#include <google/btree/btree_set.h>
//#include <google/btree/btree_map.h>
#endif
//#include <tbb/concurrent_hash_map.h>
//#include <tbb/concurrent_queue.h>
#include "FIFOFillModel.h"
namespace bts {
namespace md {
// btree::btree_map;
// btree::btree_map<
void FIFOFillModel::on_mbo(const MBOUpdate &order) {
auto result = _exchange_orders.find(order.id);
if (result == _exchange_orders.end()) {
std::map<TimeStamp, MBOUpdate, TransactionTimeCmp> history;
history.insert(std::pair<TimeStamp, MBOUpdate>(order.timestamp, order));
_exchange_orders[order.id] = history;
} else {
result->second.insert(
std::pair<TimeStamp, MBOUpdate>(order.timestamp, order));
}
}
void FIFOFillModel::on_order(const OrderStatus &order) {
auto result = _orders.find(order.id);
if (result != _orders.end()) {
_remove_order(result->second);
}
if (!order.is_active)
return;
auto orders_by_price = _get_orders_by_price(order.is_bid);
auto price_node = orders_by_price.find(order.price);
if (price_node != orders_by_price.end()) {
price_node->second.orders.insert(order);
} else {
OrdersOfPrice op(order.price);
op.orders.insert(order);
orders_by_price.insert(
std::pair<IntPrice, OrdersOfPrice>(order.price, op));
}
}
void FIFOFillModel::on_trade(TimeStamp transaction_time, IntPrice price,
co2::AggressorSide aggressor_side,
std::vector<TradeDetail> trades) {
switch (aggressor_side) {
case co2::AggressorSide::AGGRESSOR_SIDE_BUY:
break;
default:
break;
}
if (aggressor_side == co2::AggressorSide::AGGRESSOR_SIDE_BUY) {
std::vector<TradeDetail>::iterator tit = trades.begin(); // lol
if (tit == trades.end())
return;
auto bts_it = _bts_asks.begin();
if (bts_it == _bts_asks.end() || bts_it->first.price() > price.price())
return;
//auto orders_at_price_it = bts_it->second.orders.begin();
for (; tit != trades.end(); ++tit) {
auto exch_order_hist = _exchange_orders.find(tit->order_id);
if (exch_order_hist == _exchange_orders.end()) {
continue;
}
auto match = exch_order_hist->second.lower_bound(transaction_time);
while (match->second.timestamp >= transaction_time &&
match != exch_order_hist->second.begin())
match--;
if (match->second.timestamp > transaction_time)
continue;
//auto exch_order_time = match->second.timestamp;
// for (; orders_at_price_it != orders_at_price_it-> && orders_at_price_it->transaction_time < exch_order_time; orders_at_price_it++) {
// }
//if (bts_it->second.tim
}
}
}
void FIFOFillModel::_order_data_for_side(
bool, std::multiset<OrderStatus, IntPrice> &,
std::multiset<OrderStatus, IntPrice> &) {
// if (is_bid) {
//}
// IntPrice p;
// orders.find(p);
// opposite_side_orders.find(p);
}
void FIFOFillModel::_flush_crosses() {}
void FIFOFillModel::_remove_order(const OrderStatus &order) {
auto orders_by_price = _get_orders_by_price(order.is_bid);
auto price_node = orders_by_price.find(order.price);
if (price_node != orders_by_price.end()) {
price_node->second.orders.erase(order);
if (price_node->second.orders.empty())
orders_by_price.erase(order.price);
}
}
std::map<bts::md::IntPrice, OrdersOfPrice, bts::md::PriceCmp>
FIFOFillModel::_get_orders_by_price(bool is_bid) {
if (is_bid)
return _bts_bids;
return _bts_asks;
}
// void on_orders_filled(const std::vector<Order>& orders);
} // namespace md
} // namespace bts
@@ -0,0 +1,103 @@
#pragma once
#include <stdlib.h>
#include "server/sparkd/fill_models/IFillModel.h"
#include "server/sparkd/fill_models/Comparers.h"
#include "lib/md/ExchangeOrderKey.h"
#include "lib/md/MBOProcessor.h"
#include "lib/md/IntPrice.h"
#include <unordered_map>
#include <map>
#include <set>
#include "server/sparkd/exchange_sim/book/OrderMatcher.h"
#ifndef _WIN32
#include <google/btree/btree_set.h>
#endif
//typedef std::tuple<bts::md::IntPrice, bts::md::inc::Priority> OrderPricePriority;
//typedef std::tuple<ExchangeOrderKey,inc::Priority> OrderPricePriority;
//using OrdersByPrice std::map<bts::md::IntPrice, std::multiset<bts::md::OrderStatus, bts::md::TransactionTimeCmp>, bts::md::PriceCmp>;
namespace bts {
namespace md {
struct MBOUpdateCmp {
MBOUpdateCmp() {}
MBOUpdateCmp(bool ascending) : ascending(ascending) {}
bool operator()(MBOUpdate const &, MBOUpdate const &) const {
if (ascending) {
return true;
}
return false;
}
bool ascending;
};
struct OrdersOfPrice {
OrdersOfPrice() {}
OrdersOfPrice(IntPrice price) : price(price), orders() { }
OrdersOfPrice(const OrdersOfPrice& other) : price(other.price), orders(other.orders) {
}
IntPrice price;
std::multiset<OrderStatus, TransactionTimeCmp> orders;
};
struct PriceCmp {
PriceCmp() {}
bool operator()(const OrderStatus &lhs, const OrderStatus &rhs) {
double diff = lhs.price.price() - rhs.price.price();
if (diff > std::numeric_limits<double>::epsilon())
return true;
if (diff < -std::numeric_limits<double>::epsilon())
return false;
return lhs.id.value() > rhs.id.value();
}
bool operator()(const IntPrice& lhs, const IntPrice& rhs) {
double diff = lhs.price() - rhs.price();
if (diff > std::numeric_limits<double>::epsilon())
return true;
if (diff < -std::numeric_limits<double>::epsilon())
return false;
return false;
}
bool operator()(const OrdersOfPrice& lhs, const OrdersOfPrice& rhs) {
double diff = lhs.price.price() - rhs.price.price();
if (diff > std::numeric_limits<double>::epsilon())
return true;
if (diff < -std::numeric_limits<double>::epsilon())
return false;
return false;
}
};
struct OrderSideData {
std::map<IntPrice, std::multiset<OrderStatus, TransactionTimeCmp>, PriceCmp> bts_orders;
};
struct FIFOFillModel : public IFillModel {
void on_order(const OrderStatus &order) override;
void on_mbo(const MBOUpdate &update) override;
void on_trade(bts::utils::TimeStamp transaction_time, IntPrice price, co2::AggressorSide aggressor_side, std::vector<TradeDetail> real_trades) override;
private:
//btree::btree_set<MBOWorkingState*> dirty;
//btree::btree_map<OrderPricePriority, Order> working_orders;
//std::multiset<OrderStatus, IntPrice> _bids, _asks;
std::map<bts::md::IntPrice, OrdersOfPrice, PriceCmp> _bts_bids, _bts_asks;
std::unordered_map<ExchangeOrderKey, std::map<TimeStamp, MBOUpdate, TransactionTimeCmp>> _exchange_orders;
//std::unordered_map<ExchangeOrderKey, std::multiset<MBOUpdate, MBOUpdateCmp>> _exchange_orders;
std::unordered_map<ExchangeOrderKey, OrderStatus> _orders;
//void check_crosses();
//void on_orders_filled(const std::vector<Order>& orders);
void _order_data_for_side(bool is_bid, std::multiset<OrderStatus, IntPrice>& orders, std::multiset<OrderStatus, IntPrice>& opposite_side_orders);
void _remove_order(const OrderStatus& order);
void _flush_crosses();
std::map<bts::md::IntPrice, OrdersOfPrice, PriceCmp> _get_orders_by_price(bool is_bid);
};
} // namespace md
} // namespace bts
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include <stdlib.h>
namespace bts {
namespace md {
struct FillData {
private:
std::vector<std::pair<IntPrice, uint32_t>> fills;
public:
uint32_t qty;
uint32_t filled_qty;
ExchangeOrderKey id;
double avg_price;
FillData() {}
FillData(uint64_t qty, uint64_t filled_qty)
: qty(qty), filled_qty(filled_qty) {}
uint32_t working_qty() { return qty > filled_qty ? qty - filled_qty : 0; }
bool is_filled() { return filled_qty >= working_qty; }
uint32_t add_partial_fill(IntPrice price, uint32_t qty) {
if (working_qty() < qty) {
qty = working_qty();
}
if (qty > 0) {
filled_qty += qty;
fills.push_back(std::make_pair(price, qty));
}
return qty;
}
};
} // namespace md
} // namespace bts
Whitespace-only changes.
+67
View File
@@ -0,0 +1,67 @@
//#pragma once
//
//#include "lib/md/ExchangeOrderKey.h"
//#include "lib/md/IntPrice.h"
//#include "lib/utils/TimeStamp.h"
//#include <stdlib.h>
//
//namespace bts {
//namespace md {
//
//struct WorkingOrdersOfPrice {
// IntPrice price;
// std::map<TimeStamp, OrderStatus, TransactionTimeCmp> orders;
//};
//using WorkingBook = std::map<IntPrice, WorkingOrdersOfPrice>;
//using WorkingBookIter = std::map<IntPrice, WorkingOrdersOfPrice>::iterator;
//using WorkingOrderIter =
// std::map<TimeStamp, OrderStatus, TransactionTimeCmp>::iterator;
//struct FillOp {
// bool is_done() { return _price_level == _book_end; }
//
// void fill_against(const ExchangeOrderSnapshot &exch_order,
// uint32_t fill_qty) {
// if (_curr_fill.is_filled()) {
// // todo: assert (curr_fill should be immediately reset when it is
// // 'filled')
// }
// if (!_ensure_working())
// return;
// // requirements to 'steal' fill from the exchange order:
// // 1) transaction time 'at or before' that of the exch order.
// // 2) price 'better than or equal to' that of the exchange order
// // (i.e., >= for bids, <= for asks).
// while (_ensure_working()) {
// uint32_t remaining_qty = _curr_order().working_qty();
// }
// }
//
// private:
// WorkingBookIter _price_level, _book_end;
// WorkingOrderIter _order, _order_end;
// FillData _curr_fill;
// // std::vector<std::tuple<ExchangeOrderKey, uint32_t, IntPrice>> _fills;
// std::unordered_map<ExchangeOrderKey, FillData> _fills;
// bool _done;
//
// const OrderStatus &_curr_order() { return _order->second; }
//
// // advance enumeration until _order
// // points to a working order.
// bool _ensure_working() {
// while (true) {
// while (_order != _order_end) {
// if (_order->second.is_working())
// return true;
// _order++;
// }
// if (++_price_level == _book_end) {
// break;
// }
// _order = _price_level->second.orders.begin();
// }
// return false;
// }
//};
//} // namespace md
//} // namespace bts
@@ -0,0 +1,35 @@
#pragma once
#include <stdlib.h>
#include "server/sparkd/fill_models/OrderStatus.h"
#include "TradeDetail.h"
#include "lib/md/IntPrice.h"
#include "lib/md/protos/Co2.pb.h"
//#include "lib/domain_model/Security.pb.h"
#include "lib/md/MBOProcessor.h"
#include <google/protobuf/timestamp.pb.h>
#include "lib/utils/PBUtils.h"
#include "lib/utils/TimeStamp.h"
//#include "lib/api/services/OrderService.pb.h"
//#include "lib/orderman/orders/Order.h"
//#include "server/sparkd/exchange_sim/book/Order.h"
//using bts::ex_sim::Order = OrderStatus;
//using Side = bts::api::Side;
namespace bts {
namespace md {
using Timestamp = bts::utils::TimeStamp;
using TimeStamp = bts::utils::TimeStamp;
struct PriceCmp;
struct TransactionTimeCmp;
class IFillModel {
public:
virtual void on_order(const OrderStatus& ) { }
virtual void on_mbo(const MBOUpdate& ){ }
virtual void on_trade(bts::utils::TimeStamp , IntPrice , co2::AggressorSide, std::vector<TradeDetail>) {}
};
} // namespace md
} // namespace bts
+295
View File
@@ -0,0 +1,295 @@
#include "server/sparkd/fill_models/MBOBook.h"
namespace bts {
namespace md {
bool FillSimulatorUtils::order_state_for_trade(
const std::map<ExchangeOrderKey, std::map<TimeStamp, ExchangeOrderSnapshot>>
&cache,
ExchangeOrderKey order_id, TimeStamp transaction_time,
ExchangeOrderSnapshot &result) {
auto hist = cache.find(order_id);
if (hist == cache.end()) {
return false;
}
auto state_it = hist->second.lower_bound(transaction_time);
if (state_it == hist->second.begin())
return false;
auto priority = --state_it->second.priority;
while (state_it != hist->second.begin() &&
--state_it->second.priority == priority)
;
result = state_it->second;
return true;
}
void FillBuilderPriceNode::add_or_update(OrderStatus order) {
auto existing = _cache.find(order.id);
if (existing != _cache.end()) {
auto old_state = _orders.find(existing->second);
}
}
void FillBuilderPriceNode::remove(OrderStatus order) {}
void FillBuilderPriceNode::assign_fills(
TimeStamp transaction_time, std::vector<TradeDetail>::iterator tdit,
std::vector<TradeDetail>::iterator end) {
auto order_it = _orders.begin();
if (order_it == _orders.end())
return;
for (; tdit != end; tdit++) {
uint32_t = remaining_qty = tdit->last_qty;
while (remaining_qty > 0) {
if (curr_fill.is_filled()) {
// fills.insert(std::make_pair(curr_fill.id, curr_fill));
if (++order_it == _orders.end() ||
order_it->transaction_time.to_uint64() >= transaction_time)
return;
curr_fill =
&(fills
.insert(std::make_pair(
order_it->id,
FillData(order_it->qty, order_it->filled_quantity)))
.second);
}
remaining_qty -= curr_fill.add_partial_fill(price, remaining_qty);
}
}
}
// FillBuilder
bool FillBuilder::has_existing_fills(ExchangeOrderKey order_id) {
FillData &exfills;
return existing_fill_data(order_id, ex_fills);
}
bool FillBuilder::existing_fill_data(ExchangeOrderKey order_id,
FillData &result) {
auto match = _fills.find(order_id);
if (match == _fills.end())
return false;
result = match->second;
return true;
}
void FillBuilder<TComp>::assign_fills(
TimeStamp transaction_time,
std::map<IntPrice,
std::map<TimeStamp, std::vector<TradeDetail>, TransactionTimeCmp>>
trades_by_price) {
// auto price_nodes = FillBuilderPriceNode()
auto pnit = _price_nodes.begin();
for (auto tbpit = trades_by_price.begin(); tit != trades_by_price.end();
tit++) {
if (pnit == _price_nodes.end())
break;
if (pnit == _price_nodes.end()) {
continue;
}
while (pnit->second.is_done()) {
if (++pnit == _price_nodes.end())
return;
}
auto price_level = pnit->second;
auto price = tbpit->first;
for (auto oit = tbpit->second.begin(); oit != tbpit->second.end();
oit++) {
auto price_trades = oit->second;
}
if (trade.last_qty == 0) {
// assert?
return;
}
if (_market.size() == 0)
return;
auto exch_order_hist = _market.find(trade.order_id);
if (exch_order_hist == _market.end())
return;
ExchangeOrderSnapshot &exch_order;
if (FillSimulatorUtils::order_state_for_trade(
_market, trade.order_id, transaction_time, exch_order)) {
if (_curr_fill.is_filled()) {
// todo (prob): assert against this. Here's why:
// when curr bid fill is "filled"
// it should be added to _fills (a map of order_id -> FillData),
// the _bid_pos iterator should be incremented,
// and _curr_bid_fill should be initialized with the working
// qty/fills of the order now pointed by the iterator.
}
uint32_t remaining_qty = trade.last_qty;
auto price_it = _bid_its.begin();
while (remaining_qty > 0) {
while (price_it->first.price() <= price.price()) {
}
}
}
}
}
// FillOp
bool FillOp::is_done() { return _price_level == _book_end; }
void FillOp::fill_against(const ExchangeOrderSnapshot &exch_order,
uint32_t fill_qty) {
while (fill_qty > 0 && _ensure_working(exch_order)) {
if (_curr_fill == nullptr) {
// todo: assert (the purpose of _ensure_working is to prepare
// curr_fill for manipulation)
}
if (_curr_fill->is_filled()) {
// todo: assert (curr_fill should be immediately reset when it is
// 'filled')
}
if (_curr_fill->filled_qty == 0) {
}
fill_qty -= _curr_fill.add_partial_fill(_curr_order().price, fill_qty);
}
}
const OrderStatus &FillOp::_curr_order() { return _order->second; }
// requirements to 'steal' fill from the exchange order:
// 1) transaction time 'at or before' that of the exch order.
// 2) price 'better than or equal to' that of the exchange order
// (i.e., >= for bids, <= for asks).
bool FillOp::_can_steal_fill(const ExchangeOrderSnapshot &exch_order,
const OrderStatus &order) {
return order.transaction_time.to_uint64() <=
exch_order.transaction_time.to_uint64() &&
(exch_order.is_bid
? order.price.price() >= exch_order.state->price.price()
: order.price.price() <= exch_order.state->price.price());
}
bool FillOp::_ensure_working(const ExchangeOrderSnapshot &exch_order) {
if (_curr_fill != nullptr && _curr_fill->working_qty() > 0)
return true;
// God forbid, something terrible happens and ++_price_level never reaches
// _book_end limit the num of loops to an arbitrary large number
int num_loops = 0;
while (true) {
if (++num_loops > 16384) {
// todo: log a warning/error (something bad must have happened)
break;
}
while (_order != _order_end) {
if (_order->second.is_working() &&
_can_steal_fill(exch_order, _order->second)) {
auto p = _fills.insert_or_assign(
_order->second.id,
FillData(_order->second.size, _order->second.filled_quantity));
if (p.second) {
// todo: maybe assert (order should not already have a
// corresponding fill item)
}
_curr_fill = &(p.first->second);
return true;
}
_order++;
}
if (++_price_level == _book_end) {
break;
}
_order = _price_level->second.orders.begin();
}
return false;
}
// uint32_t MBOBook::_assign_fills(const OrderStatus& target_order, uint32_t
// filled_qty, OrderAtPriceIter order_it, OrderAtPriceIter order_end,
// PriceNodeIter price_node_it, PriceNodeIter price_node_end) { if (order_it ==
// order_end) { if (price_node_it == price_node_end) return filled_qty;
//
//}
// int32_t working_qty = target_order.working_quantity();
// while (filled_qty < working_qty) {
// if (order_it == order_end) {
// if (++price_node_it == price_node_end)
// break;
// price_node_it->second.orders.begin();
//}
//}
// return filled_qty;
//}
void MBOBook::assign_fills(
TimeStamp transaction_time,
std::map<TimeStamp, OrderStatus>::iterator working_orders_it,
std::map<TimeStamp, OrderStatus>::iterator working_orders_end,
std::vector<std::pair<ExchangeOrderKey, FillData>> &fills_out,
std::vector<TradeDetail> trades) {
if (working_orders_it == working_orders_it)
return;
FillData curr_fill;
for (auto tit = trades.begin(); tit != trades.end(); tit++) {
auto exch_order_it = _cache.find(tit->order_id);
if (exch_order_it != _cache.end()) {
SnapshotOrder state;
TimeStamp exch_order_transaction_time;
if (exch_order_it->second.try_match_order(
transaction_time, *state, *exch_order_transaction_time)) {
if (exch_order_transaction_time.to_uint64() >
working_orders_it->first) {
uint32_t wkg_qty = working_orders_it->second.working_quantity();
}
}
}
}
}
void MBOBook::_assign_fills(const OrderStatus &target_order,
const std::vector<TradeDetail> &trades,
std::vector<std::pair<IntPrice, uint32_t>> &fills) {
}
void MBOBook::_assign_fills(
TimeStamp transaction_time, WorkingBookIter worders_it,
WorkingBookIter worders_end,
std::vector<std::pair<ExchangeOrderKey, FillData>> &fills_out,
std::vector<TradeDetail>::iterator trades_it,
std::vector<TradeDetail>::iterator trades_end) {
if (trades_it == trades_end || worders_it == worders_end)
return;
FillData fill_data;
worders_it->second.orders.begin();
do {
FillData curr_fill;
ExchangeOrderSnapshot exch_order;
if (_find_exchange_order(transaction_time, trades_it->order_id,
exch_order)) {
}
trades_it++;
} while (trades_it != trades_end);
}
bool MBOBook::_find_exchange_order(TimeStamp transaction_time,
ExchangeOrderKey order_id,
ExchangeOrderSnapshot &result) {
auto order_hist = _cache.find(order_id);
if (order_hist == _cache.end()) {
return false;
}
auto state_it = order_hist->second.lower_bound(transaction_time);
if (state_it == order_hist->second.begin())
return false;
auto priority = --state_it->second.priority;
while (state_it != order_hist->second.begin() &&
--state_it->second.priority == priority)
;
result = state_it->second;
return true;
}
bool MBOBook::_find_working_order(
TimeStamp transaction_time, IntPrice price, bool is_bid,
std::map<TimeStamp, OrderStatus, TransactionTimeCmp>::iterator &it) {
auto working_orders = is_bid ? _working_bids : _working_asks;
auto price_level_it = working_orders.begin();
if (price_level_it == working_orders.end())
return false;
it = working_orders.lower_bound(transaction_time);
if (it == working_orders.begin())
return false;
it--;
return true;
}
// void MBOBook::_assign_fills(const OrderStatus& target_order,
} // namespace md
} // namespace bts
} // namespace bts
+183
View File
@@ -0,0 +1,183 @@
#pragma once
#include "lib/md/ExchangeOrderKey.h"
#include "lib/md/IntPrice.h"
#include "lib/md/MBOProcessor.h"
#include "server/sparkd/fill_models/Comparers.h"
#include "server/sparkd/fill_models/ExchangeOrderSnapshot.h"
#include "server/sparkd/fill_models/FillData.h"
#include "server/sparkd/fill_models/OrderStatus.h"
#include "server/sparkd/fill_models/PriceTimePriority.h"
#include "server/sparkd/fill_models/TradeDetail.h"
#include <map>
#include <set>
#include <stdlib.h>
#include <unordered_map>
namespace bts {
namespace md {
using TimeStamp = bts::utils::TimeStamp;
using OrderAtPriceIter =
std::map<TimeStamp, SnapshotOrder, TransactionTimeCmp>::iterator;
// std::multiset<ExchangeOrderSnapshot, TransactionTimeCmp>::iterator;
using MarketOrderHistory =
std::map<TimeStamp, ExchangeOrderSnapshot, TransactionTimeCmp>;
using WorkingOrderIter =
std::map<TimeStamp, OrderStatus, TransactionTimeCmp>::iterator;
struct MarketOrdersOfPrice {
IntPrice price;
std::map<TimeStamp, SnapshotOrder, TransactionTimeCmp> orders;
MarketOrdersOfPrice() {}
MarketOrdersOfPrice(IntPrice price) : price(price), orders() {}
MarketOrdersOfPrice(const OrdersOfPrice &other)
: price(other.price), orders(other.orders) {}
bool try_match_order(TimeStamp transaction_time, SnapshotOrder &result,
TimeStamp &result_transaction_time) {
auto it = orders.lower_bound(transaction_time);
if (it == orders.begin()) {
return false;
}
do {
it--;
} while (it->first.to_uint64() > transaction_time);
result_transaction_time = it->first;
result = it->second;
return true;
}
};
struct WorkingOrdersOfPrice {
IntPrice price;
std::map<TimeStamp, OrderStatus, TransactionTimeCmp> orders;
};
using PriceNodes = std::map<IntPrice, MarketOrdersOfPrice>;
using PriceNodeIter = std::map<IntPrice, MarketOrdersOfPrice>::iterator;
using WorkingBook = std::map<IntPrice, WorkingOrdersOfPrice, PriceCmp>;
using WorkingBookIter =
std::map<IntPrice, WorkingOrdersOfPrice, PriceCmp>::iterator;
using WorkingOrderIter =
std::map<TimeStamp, OrderStatus, TransactionTimeCmp>::iterator;
struct FillSimulatorUtils {
/*static bool assign_fills(const std::unordered_map<ExchangeOrderKey,
FillData> fills) {
}*/
static bool order_state_for_trade(
const std::map<ExchangeOrderKey,
std::map<TimeStamp, ExchangeOrderSnapshot>> &cache,
ExchangeOrderKey order_id, TimeStamp transaction_time,
ExchangeOrderSnapshot &result);
};
// FillOp is optimized to fill orders incrementally against exchange orders,
// by assuming that the exchange orders are sorted by price first, then by
// transaction time. However, it will still work if they are not sorted (just
// may not perform as well).
struct FillOp {
bool is_done();
void fill_against(const ExchangeOrderSnapshot &exch_order,
uint32_t fill_qty);
FillOp(WorkingBookIter price_level, WorkingBookIter price_level_end,
WorkingOrderIter order, WorkingOrderIter order_end)
: price_level(price_level), price_level_end(price_level_end),
order(order), order_end(order_end) {}
private:
std::map<IntPrice, WorkingOrdersOfPrice, PriceCmp> _orders;
WorkingBookIter _price_level, _book_end;
WorkingOrderIter _order, _order_end;
FillData *_curr_fill;
// std::vector<std::tuple<ExchangeOrderKey, uint32_t, IntPrice>> _fills;
std::unordered_map<ExchangeOrderKey, FillData> _fills;
const OrderStatus &_curr_order();
// advance enumeration until _order
// points to a working order.
bool _ensure_working(const ExchangeOrderSnapshot &exch_order);
bool _can_steal_fill(const ExchangeOrderSnapshot &exch_order,
const OrderStatus &order);
};
struct OrderFillData {
OrderStatus* order;
FillData *fill;
OrderFillData(OrderStatus *order, FillData *fill)
: order(order), fill(fill) {}
};
struct FillBuilderPriceNode {
// when a fill is completed, the orderfilldata
// should be removed from _orders
std::multiset<OrderFillData, TransactionTimeCmp> _orders;
std::unordered_map<ExchangeOrderKey, OrderFillData>& _cache;
IntPrice price;
FillData& curr_fill;
FillBuilderPriceNode(IntPrice price, std::multiset<OrderFillData, TransactionTimeCmp> orders,
std::unordered_map<ExchangeOrderKey, OrderFillData>& cache)
: price(price), order_it(orders.begin()), order_it(orders.end()), cache(cache) {
}
void assign_fills(TimeStamp transaction_time,
std::vector<TradeDetail>::iterator trade_it,
std::vector<TradeDetail>::iterator end);
void add_or_update(OrderStatus order);
void remove(OrderStatus order);
bool is_done() {
return order_it == order_end;
}
};
template <class TComp>
struct FillBuilder {
std::map<IntPrice, std::multiset<OrderStatus, TransactionTimeCmp>, TComp> _orders;
std::map<IntPrice, FillBuilderPriceNode, TComp> _price_nodes;
std::unordered_map<ExchangeOrderKey, std::map<TimeStamp, ExchangeOrderSnapshot>> _market;
std::unordered_map<ExchangeOrderKey, FillData> _fills;
void assign_fills(TimeStamp transaction_time, std::map<IntPrice, std::map<TimeStamp, std::vector<TradeDetail>, TransactionTimeCmp>, TComp> trades);
//void assign_fills(std::map<IntPrice, std::multimap<TimeStamp, TradeDetail>>)
bool has_existing_fills(ExchangeOrderKey order_id);
bool existing_fill_data(ExchangeOrderKey order_id, FillData &result);
};
struct MBOBook {
void on_mbo_update(MBOUpdate);
void
assign_fills(TimeStamp transaction_time,
std::vector<std::pair<ExchangeOrderKey, FillData>> &fills_out,
std::vector<TradeDetail> trades);
bool try_get_history();
private:
std::unordered_map<ExchangeOrderKey, MarketOrderHistory> _cache;
std::map<IntPrice, WorkingOrdersOfPrice> _working_bids, _working_asks;
// std::map<IntPrice, MarketOrdersOfPrice>
void
_assign_fills(TimeStamp transaction_time, WorkingBookIter working_orders_it,
WorkingBookIter working_orders_end,
std::vector<std::pair<ExchangeOrderKey, FillData>> &fills_out,
std::vector<TradeDetail>::iterator trades_it,
std::vector<TradeDetail>::iterator trades_end);
bool _find_exchange_order(TimeStamp transaction_time,
ExchangeOrderKey order_id,
ExchangeOrderSnapshot &result);
// bool _find_working_orders(IntPrice price, std::map<TimeStamp, )
bool _find_working_order(
TimeStamp transaction_time, IntPrice price, bool is_bid,
std::map<TimeStamp, OrderStatus, TransactionTimeCmp>::iterator &it);
// std::vector<std::pair<IntPrice, uint32_t>>& fills); uint32_t
// _assign_fills(const OrderStatus &target_order, uint32_t filled_qty);
};
} // namespace md
} // namespace bts
@@ -0,0 +1,30 @@
#pragma once
#include "lib/md/IntPrice.h"
#include "lib/md/ExchangeOrderKey.h"
#include <google/protobuf/timestamp.pb.h>
#include "lib/utils/PBUtils.h"
#include "lib/utils/TimeStamp.h"
namespace bts {
namespace md {
struct OrderStatus {
ExchangeOrderKey id;
bts::utils::TimeStamp transaction_time;
IntPrice price;
int32_t size;
int32_t filled_quantity;
bool is_active, is_bid;
OrderStatus()
: id(0), price(IntPrice(0, 0)), size(0),
filled_quantity(0), is_active(false), is_bid(false) {}
int32_t working_quantity() { return size - filled_quantity; }
bool is_filled() { return filled_quantity >= size; }
bool is_working() { return size >= filled_quantity; }
};
} // namespace md
} // namespace bts
@@ -0,0 +1,17 @@
#pragma once
#include "lib/md/IntPrice.h"
#include "lib/utils/TimeStamp.h"
namespace bts {
namespace md {
struct PriceTimePriority {
const IntPrice& price;
const bts::utils::TimeStamp& time;
PriceTimePriority() {}
PriceTimePriority(IntPrice price, bts::utils::TimeStamp time)
: price(price), time(time) {}
};
} // namespace md
} // namespace bts
@@ -0,0 +1,19 @@
#pragma once
#include "lib/md/ExchangeOrderKey.h"
namespace bts {
namespace md {
enum class AggressorSide {
Buy, Sell, None
};
struct TradeDetail {
const ExchangeOrderKey& order_id;
const uint32_t& last_qty;
TradeDetail(ExchangeOrderKey order_id, uint32_t last_qty)
: order_id(order_id), last_qty(last_qty) { }
};
} // namespace md
} // namespace bts