Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da1c35bb85 |
No files matched your search
@@ -0,0 +1,70 @@
|
||||
#include "BookUtils.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
// todo: use existing price epsilon (if any), or verify this is appropriate
|
||||
const double price_epsilon = 1e-8;
|
||||
|
||||
Size BookUtils::volume_at_or_better(const Book &book, Side side, Price price) {
|
||||
Size volume = 0;
|
||||
for (const auto &plevel : BookSideImpl<side>::get_depth(book)) {
|
||||
if (BookSideImpl<side>::(price, plevel.price()))
|
||||
break;
|
||||
volume += plevel.size();
|
||||
}
|
||||
return volume;
|
||||
}
|
||||
|
||||
const BookSide &BookUtils::get_depth(const Book &book, Side side) {
|
||||
return BookSideImpl<side>::get_depth(side);
|
||||
}
|
||||
|
||||
const BookSide &BookUtils::get_opposite_depth(const Book &book, Side side) {
|
||||
return BookSideImpl<side>::get_opposite_depth(book);
|
||||
}
|
||||
|
||||
md::PriceSize BookUtils::get_top(const Book &book, Side side) {
|
||||
return *BookSideImpl<side>::get_depth(book).begin();
|
||||
}
|
||||
|
||||
md::PriceSize BookUtils::get_opposite_top(const Book &book, Side side) {
|
||||
return *BookSideImpl<side>::get_opposite_depth(book).begin();
|
||||
}
|
||||
|
||||
bool BookUtils::is_price_better(Side side, double price, double comparand) {
|
||||
return BookSideImpl<side>::(price, comparand);
|
||||
}
|
||||
|
||||
bool BookUtils::is_price_worse(Side side, double price, double comparand) {
|
||||
return BookSideImpl<side>::(comparand, price);
|
||||
}
|
||||
|
||||
bool BookUtils::is_top_better(Side side, double price) {
|
||||
auto top = BookUtils::get_top(side);
|
||||
return !top.is_nan() && BookUtils::is_price_better(side, price, top.price());
|
||||
}
|
||||
|
||||
bool BookUtils::is_top_better_or_equal(Side side, double price) {
|
||||
auto top = BookUtils::get_top(side);
|
||||
return !top.is_nan() &&
|
||||
(((top - price) < epsilon && (top - price) > -epsilon) ||
|
||||
BookUtils::is_price_better(side, price, top.price()));
|
||||
}
|
||||
|
||||
bool BookUtils::is_top_worse(Side side, double price) {
|
||||
auto top = BookUtils::get_top(side);
|
||||
return !top.is_nan() && BookUtils::is_price_worse(side, price, top.price());
|
||||
}
|
||||
|
||||
bool BookUtils::is_top_worse_or_equal(Side side, double price) {
|
||||
auto top = BookUtils::get_top(side);
|
||||
return !top.is_nan() &&
|
||||
(((top - price) < epsilon && (top - price) > -epsilon) ||
|
||||
BookUtils::is_price_worse(side, price, top.price()));
|
||||
}
|
||||
|
||||
Side BookUtils::to_opposite_side(Side side) {
|
||||
return BookSideImpl<side>::to_opposite_side();
|
||||
}
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include "Book.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/config/protos/MarketData.pb.h"
|
||||
#include "lib/domain_model/common.h"
|
||||
#include "lib/md/PriceSize.h"
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
using Size = bts::md::Size;
|
||||
using Side = bts::api::Side;
|
||||
|
||||
struct BookUtils {
|
||||
static Size volume_at_or_better(const Book &book, Side side, Price price);
|
||||
static const BookSide &get_depth(const Book &book, Side side);
|
||||
static const BookSide &get_opposite_depth(const Book &book, Side side);
|
||||
static md::PriceSize get_top(const Book &book, Side side);
|
||||
static md::PriceSize get_opposite_top(const Book &book, Side side);
|
||||
static bool is_price_better(Side side, double price, double comparand);
|
||||
static bool is_price_worse(Side side, double price, double comparand);
|
||||
static bool BookUtils::is_top_better(Side side, double price);
|
||||
static bool BookUtils::is_top_better_or_equal(Side side, double price);
|
||||
static bool BookUtils::is_top_worse(Side side, double price);
|
||||
static bool BookUtils::is_top_worse_or_equal(Side side, double price);
|
||||
static Side to_opposite_side(Side side);
|
||||
|
||||
private:
|
||||
// put side-dependent logic in the SIDE_BUY/SIDE_ASK impls
|
||||
template <bts::api::Side side> struct BookSideImpl {
|
||||
static inline BookSide get_depth(const Book &book) {
|
||||
// todo: format message
|
||||
bts_assert(false, "Unexpected side argument '{}' (not buy or sell)");
|
||||
}
|
||||
static inline const BookSide &get_opposite_depth(const Book &book) {
|
||||
// todo: format message
|
||||
bts_assert(false, "Unexpected side argument '{}' (not buy or sell)");
|
||||
}
|
||||
bool operator()(const Price &lhs, const Price &rhs) {
|
||||
// since this is just a comparison operator,
|
||||
// it's probably okay to use the default comparison
|
||||
// rather than assert
|
||||
return lhs.price() > rhs.price();
|
||||
}
|
||||
bool operator()(const double &lhs, const double &rhs) {
|
||||
// since this is just a comparison operator,
|
||||
// it's probably okay to use the default comparison
|
||||
// rather than assert
|
||||
return lhs > rhs;
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct BookSideImpl<bts::api::SIDE_BUY> {
|
||||
static inline const BookSide &get_depth(const Book &book) {
|
||||
return book.bid();
|
||||
}
|
||||
static inline const BookSide &get_opposite_depth(const Book &book) {
|
||||
return book.ask();
|
||||
}
|
||||
static inline std::string name() { return "Buy"; }
|
||||
static inline Side to_opposite_side() { return bts::api::SIDE_SELL; }
|
||||
bool operator()(const Price &lhs, const Price &rhs) {
|
||||
return lhs.price() > rhs.price();
|
||||
}
|
||||
bool operator()(const double &lhs, const double &rhs) {
|
||||
return lhs > rhs;
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct BookSideImpl<bts::api::SIDE_SELL> {
|
||||
static inline const BookSide &get_depth(const Book &book) {
|
||||
return book.ask();
|
||||
}
|
||||
static inline const BookSide &get_opposite_depth(const Book &book) {
|
||||
return book.bid();
|
||||
}
|
||||
static inline Side to_opposite_side() { return bts::api::SIDE_BUY; }
|
||||
static inline std::string name() { return "Sell"; }
|
||||
bool operator()(const Price &lhs, const Price &rhs) {
|
||||
return lhs.price() < rhs.price();
|
||||
}
|
||||
bool operator()(const double &lhs, const double &rhs) {
|
||||
return lhs < rhs;
|
||||
}
|
||||
};
|
||||
};
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "server/sparkd/fill_models/OrderStatus.h"
|
||||
#include "server/sparkd/fill_models/ExchangeOrderSnapshot.h"
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
template <bts::api::Side side> struct PriceComp {
|
||||
bool operator()(const double &lhs, const double &rhs) {
|
||||
bts_assert(false);
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct PriceComp<bts::api::SIDE_BUY> {
|
||||
bool operator()(const double &lhs, const double &rhs) {
|
||||
return lhs > rhs;
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct PriceComp<bts::api::SIDE_SELL> {
|
||||
bool operator()(const double &lhs, const double &rhs) {
|
||||
return lhs < rhs;
|
||||
}
|
||||
};
|
||||
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 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,18 @@
|
||||
#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;
|
||||
|
||||
ExchangeOrderSnapshot() {}
|
||||
ExchangeOrderSnapshot(SnapshotOrder *state, TimeStamp transaction_time)
|
||||
: state(state), transaction_time(transaction_time) {}
|
||||
};
|
||||
} // namespace md
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "FIFOFillModel.h"
|
||||
#include "lib/config/config.h"
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
void FIFOFillModel::BTSOrderCache::on_order(const OrderStatus& order) {
|
||||
auto id_it = by_id.find(order.id);
|
||||
if (id_it == by_id.end()) {
|
||||
auto pair = std::make_pair(order.id, BTSOrderHistory());
|
||||
pair.second.insert(order.transaction_time, order);
|
||||
by_id.insert(pair);
|
||||
_compute_fills(order);
|
||||
} else {
|
||||
auto pos_in_hist = id_it->second.lower_bound(order.transaction_time);
|
||||
if (pos_in_hist == id_it->second.end())
|
||||
_compute_fills(order);
|
||||
id_it->second.insert_or_assign(order.transaction_time, order);
|
||||
}
|
||||
by_time.insert(order.transaction_time, order);
|
||||
}
|
||||
|
||||
void FIFOFillModel::on_mbo(SecurityKey id, TimeStamp transaction_time, const MBOSnapshot &snapshot) {
|
||||
for (auto o : snapshot.bids) {
|
||||
auto it = _exch_orders.find(o.id);
|
||||
ExchOrderHistory &hist;
|
||||
if (it == _exch_orders.end()) {
|
||||
auto pair = std::make_pair(o.id, ExchOrderHistory());
|
||||
hist = pair.second;
|
||||
_exch_orders.insert(pair);
|
||||
} else {
|
||||
hist = it->second;
|
||||
}
|
||||
hist.insert_or_assign(transaction_time, ExchangeOrderSnapshot(&o, transaction_time));
|
||||
}
|
||||
// flush crosses
|
||||
_compute_fills();
|
||||
}
|
||||
|
||||
void FIFOFillModel::on_exch_order(const ExchangeOrderSnapshot &order) {}
|
||||
|
||||
void FIFOFillModel::on_order(const OrderStatus &order) {
|
||||
|
||||
_bts_orders.on_order(order);
|
||||
}
|
||||
|
||||
void FIFOFillModel::on_trade(TimeStamp transaction_time, IntPrice price,
|
||||
co2::AggressorSide aggressor_side,
|
||||
std::vector<TradeDetail> trades) {
|
||||
bts_assert(!price.is_nan());
|
||||
std::unordered_set<ExchangeOrderKey> processed;
|
||||
for (auto trade : trades) {
|
||||
bts_assert(trade.last_qty > 0);
|
||||
Size rem_trade_qty = trade.last_qty;
|
||||
ExchOrderHistory exch_order_hist;
|
||||
if (find_exch_order(trade.order_id, exch_order_hist)) {
|
||||
auto bts_order_it = _bts_orders.by_time.lower_bound(transaction_time);
|
||||
bts_order_it != _bts_orders.by_time.end();
|
||||
while (bts_order_it != _bts_orders.by_time.end()) {
|
||||
auto bts_order = bts_order_it->second;
|
||||
if (!bts_order.is_active) {
|
||||
_terminal_orders.insert(
|
||||
std::make_pair(bts_order.transaction_time, bts_order));
|
||||
} else if (bts_order.transaction_time > transaction_time ||
|
||||
!FillModelUtils::can_fill_at_price(bts_order,
|
||||
price.price()) ||
|
||||
is_terminal(bts_order) ||
|
||||
processed.find(bts_order.id) != processed.end()) {
|
||||
//
|
||||
bts_order_it++;
|
||||
} else if (FillModelUtils::can_fill_at_price(bts_order,
|
||||
price.price())) {
|
||||
processed.insert(bts_order.id);
|
||||
Size fill_qty =
|
||||
std::min(rem_trade_qty, bts_order.working_quantity());
|
||||
bts_assert(fill_qty > 0);
|
||||
auto fill = _realize_fill(transaction_time, bts_order,
|
||||
price.price(), fill_qty);
|
||||
fill.order_key = trade.order_id;
|
||||
if (FillModelUtils::normalize_aggressor_side(aggressor_side) ==
|
||||
bts_order.side) {
|
||||
fill.flags |= FillDataFlags::SameSideAggressor;
|
||||
}
|
||||
rem_trade_qty -= fill_qty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FIFOFillModel::on_book(SecurityKey sec, TimeStamp transaction_time,
|
||||
const Book &book) {
|
||||
_books.insert_or_assign(sec, book);
|
||||
_compute_fills();
|
||||
}
|
||||
|
||||
void FIFOFillModel::_compute_fills() {
|
||||
for (auto order : _bts_orders.by_time) {
|
||||
_compute_fills(order.second);
|
||||
}
|
||||
}
|
||||
|
||||
void FIFOFillModel::_compute_fills(OrderStatus &order) {
|
||||
if (!is_terminal(order))
|
||||
_assert_compute_fills(order);
|
||||
}
|
||||
|
||||
std::vector<FillData> FIFOFillModel::_assert_compute_fills(OrderStatus &order) {
|
||||
std::vector<FillData> fills;
|
||||
auto book = _get_book(order.security);
|
||||
auto depth = bts::orderman::BookUtils::get_opposite_depth(book);
|
||||
for (int level = 0;
|
||||
!order.is_filled() && level < depth.size() &&
|
||||
FillModelUtils::can_fill_at_price(order, depth[level].price.value());
|
||||
level++) {
|
||||
auto ps = depth[level];
|
||||
auto fill_qty =
|
||||
std::max(static_cast<int>(order.working_quantity()), ps.size());
|
||||
bts_assert(fill_qty > 0);
|
||||
fills.push_back(_realize_fill(order.transaction_time, order, ps.price(), fill_qty);
|
||||
}
|
||||
|
||||
return fills;
|
||||
}
|
||||
|
||||
FillData FIFOFillModel::_realize_fill(bts::utils::TimeStamp transaction_time,
|
||||
OrderStatus &order, double fill_price,
|
||||
Size fill_qty) {
|
||||
// todo
|
||||
return FillData();
|
||||
}
|
||||
|
||||
bool FIFOFillModel::find_exch_order(ExchangeOrderKey key,
|
||||
ExchOrderHistory &history) {
|
||||
// todo
|
||||
return true;
|
||||
}
|
||||
bool FIFOFillModel::is_terminal(const OrderStatus &order) {
|
||||
return !order.is_active ||
|
||||
_terminal_orders.find(order.id) != _terminal_orders.end();
|
||||
}
|
||||
} // namespace md
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,57 @@
|
||||
#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/domain_model/common.h"
|
||||
#include "lib/md/IntPrice.h"
|
||||
#include "server/sparkd/fill_models/FillData.h"
|
||||
#include "lib/orderman/orderman_utils/BookUtils.h"
|
||||
#include "server/sparkd/fill_models/FillModelUtils.h"
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_set>
|
||||
#include "server/sparkd/exchange_sim/book/OrderMatcher.h"
|
||||
|
||||
using Book = bts::orderman::Book;
|
||||
using TimeStamp = bts::utils::TimeStamp;
|
||||
using ExchOrderHistory = std::map<TimeStamp, bts::md::ExchangeOrderSnapshot>;
|
||||
using BTSOrderHistory = std::map<TimeStamp, bts::md::OrderStatus>;
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
struct FIFOFillModel : public IFillModel {
|
||||
void on_order(const OrderStatus &order) override;
|
||||
void on_book(SecurityKey, TimeStamp, const Book&) override;
|
||||
void on_mbo(SecurityKey, TimeStamp, const MBOSnapshot &) override;
|
||||
void on_exch_order(const ExchangeOrderSnapshot &update) override;
|
||||
void on_trade(TimeStamp transaction_time, IntPrice price, co2::AggressorSide aggressor_side, std::vector<TradeDetail> real_trades) override;
|
||||
// keeps a cache of order history per id
|
||||
// and a flat history of all orders
|
||||
struct BTSOrderCache {
|
||||
std::unordered_map<ExchangeOrderKey, BTSOrderHistory> by_id;
|
||||
std::multimap<TimeStamp, OrderStatus> by_time;
|
||||
void on_order(const OrderStatus &order);
|
||||
};
|
||||
private:
|
||||
std::map<SecurityKey, Book> _books;
|
||||
std::map<ExchangeOrderKey, ExchOrderHistory> _exch_orders;
|
||||
BTSOrderCache _bts_orders;
|
||||
std::unordered_set<ExchangeOrderKey> _terminal_orders;
|
||||
FillData _realize_fill(TimeStamp transaction_time,
|
||||
OrderStatus &order, double fill_price, Size fill_qty);
|
||||
// computes fills for all working/non-terminal orders
|
||||
void _compute_fills();
|
||||
// checks if the order is stil working/non terminal
|
||||
// before calling _compute_fills_for (this call should never assert)
|
||||
void _compute_fills(OrderStatus &order);
|
||||
// asserts that the order is still working/non-terminal
|
||||
std::vector<FillData> _assert_compute_fills(OrderStatus& order);
|
||||
bool find_exch_order(ExchangeOrderKey key, ExchOrderHistory &history);
|
||||
bool is_terminal(const OrderStatus &order);
|
||||
};
|
||||
} // namespace md
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
enum FillDataFlags {
|
||||
None = 0,
|
||||
SameSideAggressor = 1
|
||||
};
|
||||
struct FillData {
|
||||
uint32_t working_qty;
|
||||
uint32_t filled_qty;
|
||||
ExchangeOrderKey order_key;
|
||||
FillDataFlags flags;
|
||||
|
||||
FillData(){}
|
||||
FillData(uint64_t working_qty, uint64_t filled_qty)
|
||||
: working_qty(working_qty), filled_qty(filled_qty) {}
|
||||
FillData(uint64_t working_qty, uint64_t filled_qty, ExchangeOrderKey order_key, FillDataFlags flags)
|
||||
: working_qty(working_qty), filled_qty(filled_qty), order_key(order_key), flags(flags) {}
|
||||
};
|
||||
} // namespace md
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "FillModelUtils.h"
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
bool FillModelUtils::can_fill_at_price(const OrderStatus &order,
|
||||
double price) {
|
||||
return !os::is_nan(price) && (order.is_market_order ||
|
||||
!bts::orderman::BookUtils::is_price_better(order.side, price, order.price.price()));
|
||||
}
|
||||
Side FillModelUtils::normalize_aggressor_side(
|
||||
co2::AggressorSide aggressor_side) {
|
||||
// todo
|
||||
return bts::api::SIDE_SELL;
|
||||
}
|
||||
} // namespace md
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/md/protos/Co2.pb.h"
|
||||
#include "lib/orderman/orderman_utils/Book.h"
|
||||
#include "lib/orderman/orderman_utils/BookUtils.h"
|
||||
#include "server/sparkd/fill_models/OrderStatus.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/config/protos/MarketData.pb.h"
|
||||
#include "lib/domain_model/common.h"
|
||||
#include "lib/md/PriceSize.h"
|
||||
#include "server/sparkd/fill_models/Comparers.h"
|
||||
#include <memory>
|
||||
|
||||
using bts::orderman;
|
||||
|
||||
using Book = bts::orderman::Book;
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
using Size = bts::md::Size;
|
||||
using Side = bts::api::Side;
|
||||
|
||||
struct FillModelUtils {
|
||||
static bool can_fill_at_price(const OrderStatus& order, double price);
|
||||
static Side normalize_aggressor_side(co2::AggressorSide aggressor_side);
|
||||
//static bool can_fill_at_price(bts::api::Side order_side, double order_price, double fill_price);
|
||||
};
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "TradeDetail.h"
|
||||
#include "lib/md/IntPrice.h"
|
||||
#include "lib/md/protos/Co2.pb.h"
|
||||
#include "server/sparkd/fill_models/OrderStatus.h"
|
||||
#include <stdlib.h>
|
||||
//#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/md/MBOProcessor.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include <google/protobuf/timestamp.pb.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_mbo(SecurityKey, TimeStamp, const MBOSnapshot &) {}
|
||||
virtual void on_book(SecurityKey, TimeStamp, const Book &) {}
|
||||
virtual void on_exch_order(const ExchangeOrderSnapshot &update) {}
|
||||
virtual void on_order(const OrderStatus &) {}
|
||||
virtual void on_trade(TimeStamp transaction_time, IntPrice price,
|
||||
co2::AggressorSide aggressor_side,
|
||||
std::vector<TradeDetail> real_trades) {}
|
||||
};
|
||||
} // namespace md
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "server/sparkd/fill_models/MBOBook.h"
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
//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) {
|
||||
auto exch_order_match = _cache.find(target_order.id);
|
||||
exch_order_match->second.lower_bound(target_order)
|
||||
if (exch_order_match == _cache.end()) return;
|
||||
exch_order_match->first.value
|
||||
//while (exch_order_match != _cache.begin() && exch_order_match->second.transaction_time)
|
||||
}
|
||||
}
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/md/ExchangeOrderKey.h"
|
||||
#include "lib/md/IntPrice.h"
|
||||
#include "lib/md/MBOProcessor.h"
|
||||
#include "server/sparkd/fill_models/ExchangeOrderSnapshot.h"
|
||||
#include "server/sparkd/fill_models/Comparers.h"
|
||||
#include "server/sparkd/fill_models/FillData.h"
|
||||
#include "server/sparkd/fill_models/OrderStatus.h"
|
||||
#include <map>
|
||||
#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::multiset<ExchangeOrderSnapshot, TransactionTimeCmp>;
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
using PriceNodes = std::map<IntPrice, MarketOrdersOfPrice>;
|
||||
using PriceNodeIter = std::map<IntPrice, MarketOrdersOfPrice>::iterator;
|
||||
|
||||
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;
|
||||
//void MBOBook::_assign_fills(const OrderStatus& target_order, 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,31 @@
|
||||
#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;
|
||||
SecurityKey security;
|
||||
bts::utils::TimeStamp transaction_time;
|
||||
IntPrice price;
|
||||
int32_t quantity;
|
||||
int32_t filled_quantity;
|
||||
bool is_active, is_market_order;
|
||||
bts::api::Side side;
|
||||
|
||||
OrderStatus()
|
||||
: id(0), price(IntPrice(0, 0)), quantity(0),
|
||||
filled_quantity(0), is_active(false), is_bid(false) {}
|
||||
|
||||
int32_t working_quantity() { return quantity - filled_quantity; }
|
||||
bool is_filled() { return filled_quantity >= quantity; }
|
||||
};
|
||||
|
||||
} // 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 uint& last_qty;
|
||||
|
||||
TradeDetail(ExchangeOrderKey order_id, uint last_qty)
|
||||
: order_id(order_id), last_qty(last_qty) { }
|
||||
};
|
||||
} // namespace md
|
||||
} // namespace bts
|
||||
Reference in new issue
Block a user