SPARK-1672(Volume at price)

This commit is contained in:
Taha Afzal committed 2018-09-13 15:24:49 -04:00
1 parent bb19f737d5
commit 5dbacfddd5
6 files changed
+174 -25

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
#include "ChannelVolume.h"
#include "lib/api/services/MarketDataRelay.pb.h"
#include "lib/md/PriceSize.h"
namespace bts{
namespace md{
namespace auxiliary{
using namespace bts::co2;
using namespace bts::api::md::auxiliary;
ChannelVolume::ChannelVolume(SecurityChannel* channel)
:
_channel(channel){
}
const VolumeAtPriceLevel& ChannelVolume::add_trade(const TradeMessage& m,
int& record_id){
auto price_map_it=_sec_map.find(m.key());
if(price_map_it==_sec_map.end()){
_sec_map[m.key()]=std::map<uint64_t,VolumeAtPriceLevel>();
price_map_it=_sec_map.find(m.key());
}
auto& price_map=price_map_it->second;
auto price_it=price_map.find(m.mantissa());
if(price_it==price_map.end()){
VolumeAtPriceLevel vol;
vol.set_record_id(record_id++);
vol.set_security_id(m.key());
auto p = md::PriceSize(m.mantissa(),m.exponent(),0);
vol.set_price(p.price());
price_map[m.mantissa()] = vol;
price_it=price_map.find(m.mantissa());
}
VolumeAtPriceLevel& vol=price_it->second;
vol.set_volume(vol.volume()+m.size());
return vol;
}
bool ChannelVolume::match_update(const SecurityChannel& channel){
if(_channel->type()==channel.type() &&
_channel->exchange()==channel.exchange() &&
_channel->symbol()==channel.symbol()){
_channel->CopyFrom(channel);
return true;
}
return false;
}
}}}
+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include "lib/api/services/MarketDataAuxiliaryService.pb.h"
namespace bts{
namespace co2 {class TradeMessage;}
namespace md{
namespace auxiliary{
// volume at the various price levels for all
// securities belonging to a single channel.
class ChannelVolume{
private:
bts::co2::SecurityChannel* const _channel;
std::map<uint32_t,std::map<uint64_t,bts::api::md::auxiliary::VolumeAtPriceLevel>> _sec_map;
public:
ChannelVolume(bts::co2::SecurityChannel* channel);
// finds the VolumeAtPriceLevel for the <sec, price_level>
// in the trade and updates the volume. if <sec,price_level>
// is not found, it increases the record_id before assigning
// it to VolumeAtPriceLevel's record_id
const bts::api::md::auxiliary::VolumeAtPriceLevel&
add_trade(const bts::co2::TradeMessage& m, int& record_id);
// if the channel is matched, it updated with it with
// the data in the arg, and returns true
bool match_update(const bts::co2::SecurityChannel& channel);
};
}}}
@@ -6,6 +6,7 @@
#include "lib/utils/PBUtils.h"
#include "lib/utils/Dispatcher.h"
#include "lib/md/get_symbols.h"
#include "VolumeAtPriceState.h"
namespace bts{
namespace md{
@@ -15,33 +16,19 @@ using namespace bts::co2;
using namespace bts::api::md::auxiliary;
struct MarketDataAuxiliaryServiceServer::PImpl{
MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher publisher;
MarketDataRelay relay_service;
MDRequest md_req;
MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher publisher;
std::unique_ptr<utils::DispatcherTimer> trade_timer;
int64_t poll_period_ms;
int id;
VolumeAtPriceState state;
PImpl(bts::api::ServiceContext& context,
const MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher& publisher)
:
publisher(publisher),
:
relay_service(context.locate<MarketDataRelay>()),
md_req(),
publisher(publisher),
trade_timer(),
poll_period_ms(100),
id(1)
{
md_req.set_host("MarketDataAuxiliaryServiceServer");
std::set<bts::md::UserSymbol> syms;
bts::md::get_system_md_symbols(syms);
for(const auto& s : syms){
SecurityChannel* p = md_req.add_channels();
p->set_flags(MDRequest::TRADE_LOG);
p->set_type(s.type());
p->set_exchange(s.exchange());
p->set_symbol(s.symbol());
p->set_trade_cursor(0);
}
state(){
}
};
@@ -56,7 +43,7 @@ me(new PImpl(context,
std::make_unique<utils::DispatcherTimer>(threadpool,
me->poll_period_ms,
[this]() {
me->relay_service.requestmd(me->md_req,
me->relay_service.requestmd(me->state.md_req(),
[this](const bts::api::Result<MDReply>& md_reply)
{
if(md_reply.result()!=nullptr)
@@ -66,9 +53,9 @@ me(new PImpl(context,
me->trade_timer->start();
}
void MarketDataAuxiliaryServiceServer::_scan_trades(const MDReply&){
// go through the md_replay, update
// the state incrementally, and publish
void MarketDataAuxiliaryServiceServer::_scan_trades(const MDReply& md_reply){
for(const auto& cr : md_reply.channels())
me->state.on_new_trades(cr,me->publisher);
}
MarketDataAuxiliaryServiceServer::~MarketDataAuxiliaryServiceServer(){
@@ -0,0 +1,56 @@
#include "VolumeAtPriceState.h"
#include "lib/md/get_symbols.h"
#include "lib/api/services/MarketDataRelay.pb.h"
//#include "lib/api/services/MarketDataRelay.client.pb.h"
namespace bts{
namespace md{
namespace auxiliary{
using namespace bts::api::md::auxiliary;
using namespace bts::co2;
using namespace std;
VolumeAtPriceState::VolumeAtPriceState()
:
_channels(),
_md_req(),
_record_id(0){
std::set<bts::md::UserSymbol> syms;
bts::md::get_system_md_symbols(syms);
for(const auto& s : syms){
SecurityChannel c;
c.set_flags(MDRequest::TRADE_LOG);
c.set_type(s.type());
c.set_exchange(s.exchange());
c.set_symbol(s.symbol());
c.set_trade_cursor(0);
c.set_segment_id(0);
_update_or_add(c);
}
}
ChannelVolume& VolumeAtPriceState::_update_or_add(const SecurityChannel& channel){
for(auto& v : _channels)
if(v.match_update(channel))
return v;
SecurityChannel* new_channel_ptr = _md_req.add_channels();
new_channel_ptr->CopyFrom(channel);
_channels.emplace_back(new_channel_ptr);
return _channels.back();
}
void VolumeAtPriceState::on_new_trades(const ChannelReply& reply,
MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher& publisher){
ChannelVolume& channel_vol=_update_or_add(reply.channel());
for(const TradeMessage& t : reply.trade_log()){
const VolumeAtPriceLevel& sec_level_vol=channel_vol.add_trade(t,
_record_id);
publisher.add_or_update(sec_level_vol);
}
}
const bts::co2::MDRequest& VolumeAtPriceState::md_req() const{
return _md_req;
}
}}}
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include "lib/api/services/MarketDataAuxiliaryService.server.pb.h"
//#include "lib/api/services/MarketDataRelay.pb.h"
//#include "lib/api/services/MarketDataRelay.client.pb.h"
#include "ChannelVolume.h"
namespace bts{
namespace md{
namespace auxiliary{
// this is the entire state for the volume at
// price levels for all channels.
class VolumeAtPriceState{
private:
std::vector<ChannelVolume> _channels;
bts::co2::MDRequest _md_req;
int _record_id;
private:
// if the channel is present, its updated (trade_cursor etc)
// else its created from the channel
ChannelVolume& _update_or_add(const bts::co2::SecurityChannel& channel);
public:
VolumeAtPriceState ();
//encorporates the reply, and publishes it
void on_new_trades(const bts::co2::ChannelReply& channel,
bts::api::md::auxiliary::MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher& publisher);
const bts::co2::MDRequest& md_req() const;
};
}}}
@@ -223,11 +223,10 @@ void AdminServiceServer::_cancel_orders(const vector<string>& accounts,
user_map.find(ord.def().user()) != user_map.end())
keys.push_back(ord.key());
}
auto cb = [&](const bts::api::Result<::bts::api::orderman::Response>&) -> void {};
for(uint32_t k : keys){
Cancel c;
c.set_order_key(k);
_order_service.cancel_order(c, cb);
_order_service.cancel_order(c, [this](const auto&){});
}
}
void AdminServiceServer::_publish_md_entitlements(){