Basic scaffolding plus price level match

This commit is contained in:
Taha Afzal committed 2020-08-11 20:09:09 -04:00
1 parent 52bc0f1a17
commit 780b74fc56
4 files changed
+177

No files matched your search

@@ -0,0 +1,19 @@
#include "FifoPriceLevelMatcher.h"
#include "../common/InstrumentSnapshot.h"
namespace bts::spartan::mdp3 {
FifoPriceLevelMatcher::FifoPriceLevelMatcher (
md::IntPrice price,
api::Side side)
:
_price(price),
_side(side),
_lowest_priority(0),
_orders()
{}
void FifoPriceLevelMatcher::_compress(){
}
}
@@ -0,0 +1,116 @@
#pragma once
// did not want to fwd decl everything specially
// the templated classes (will fix later)
#include "../common/InstrumentSnapshot.h"
namespace bts::spartan::mdp3 {
// Builds the order state for all orders correponding to one
// price level for one security
class FifoPriceLevelMatcher{
private:
struct Order{
MDP3OrderKey key;
MDP3OrderPriority priority;
MDP3Quantity quantity;
bool is_active=true;
Order (MDP3OrderKey key_arg,
MDP3OrderPriority priority_arg,
MDP3Quantity quantity_arg)
:
key(key_arg),
priority(priority_arg),
quantity(quantity_arg)
{}
};
const md::IntPrice _price;
const api::Side _side;
MDP3OrderPriority _lowest_priority;
std::vector<FifoPriceLevelMatcher::Order> _orders;
// removes the holes
void _compress();
Order* _find(MDP3OrderKey key){
for(auto& o : _orders)
if(o.key == key)
return &o;
return nullptr;
}
void _logically_delete(Order& o){
o.is_active = false;
}
void _add_order(MDP3OrderKey key,
MDP3OrderPriority priority,
MDP3Quantity quantity){
// assert for this priority to be higher than last element
bts_assert(priority.value() > _lowest_priority.value());
_orders.emplace_back(key, priority, quantity);
_lowest_priority = priority;
}
public:
// accessors
MDP3Quantity quantity() const{
// this can be kept as a state
uint32_t rv = 0;
for(const auto& o : _orders)
if(o.is_active)
rv += o.quantity.value();
return MDP3Quantity(rv);
}
// same side order
void on_new_order(const MDP3Order& new_ord){
bts_assert(new_ord.side == _side);
_add_order(new_ord.key, new_ord.priority, new_ord.quantity);
}
// returns true if removed from this price level
bool on_order_update(const MDP3Order& upd_ord){
bts_assert(upd_ord.side == _side);
FifoPriceLevelMatcher::Order* ord_p = _find(upd_ord.key);
bts_assert(ord_p != nullptr);
if(upd_ord.price != _price){
_logically_delete(*ord_p);
return true;
}
else if(upd_ord.quantity.value() > ord_p->quantity.value()){
// remove it from the current location and add it to
// the end
_logically_delete(*ord_p);
_add_order(upd_ord.key, upd_ord.priority, upd_ord.quantity);
}
return false;
}
void on_order_remove(MDP3OrderKey k){
FifoPriceLevelMatcher::Order* ord_p = _find(k);
bts_assert(ord_p != nullptr);
_logically_delete(*ord_p);
}
// opp side order
MDP3Quantity on_new_opp_order(const MDP3Order& opp_order){
bts_assert(opp_order.side != _side);
auto rem = opp_order.quantity.value();
// go through all the orders and see which ones
// get filled
for(auto& o : _orders){
if(o.is_active){
if(rem >= o.quantity.value()){
// whole order gets swallowed
o.is_active = false;
rem -= o.quantity.value();
}
else{
bts_assert(o.quantity.value() > rem);
o.quantity = MDP3Quantity(o.quantity.value() - rem);
rem = 0;
break;
}
}
}
bts_assert(rem <= opp_order.quantity.value());
return MDP3Quantity(opp_order.quantity.value() - rem);
}
// cons
FifoPriceLevelMatcher (md::IntPrice price,
api::Side side);
};
} // namespace bts::spartan::mdp3
@@ -0,0 +1,19 @@
#include "FifoSecurityMatcher.h"
#include "../common/InstrumentSnapshot.h"
namespace bts::spartan::mdp3 {
FifoSecurityMatcher::FifoSecurityMatcher(const InstrumentSnapshot&){
}
void FifoSecurityMatcher::on_new_order(const MDP3Order&){
}
void FifoSecurityMatcher::on_order_update(const MDP3Order&){
}
void FifoSecurityMatcher::on_order_remove(MDP3OrderKey){
}
}
@@ -0,0 +1,23 @@
#pragma once
// did not want to fwd decl everything specially
// the templated classes (will fix later)
#include "../common/InstrumentSnapshot.h"
#include "FifoPriceLevelMatcher.h"
namespace bts::spartan::mdp3 {
class FifoSecurityMatcher {
private:
std::map<md::IntPrice,FifoPriceLevelMatcher> _bid_price_level_map,
_ask_price_level_map;
FifoPriceLevelMatcher* _top_bid, *_top_ask;
public:
FifoSecurityMatcher(const InstrumentSnapshot&);
void on_new_order(const MDP3Order&);
void on_order_update(const MDP3Order&);
void on_order_remove(MDP3OrderKey);
// for validation
Book book(api::Side) const;
};
} // namespace bts::spartan::mdp3