55 lines
1.8 KiB
C++
55 lines
1.8 KiB
C++
#include "ChannelVolume.h"
|
|
#include "spark/api/services/MarketDataRelay.pb.h"
|
|
#include "spark/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::unordered_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());
|
|
IntPrice p = md::IntPrice(m.mantissa(), (int8_t)m.exponent());
|
|
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());
|
|
if (m.aggressor_side() == AggressorSide::AGGRESSOR_SIDE_BUY)
|
|
vol.set_buy_volume(vol.buy_volume() + m.size());
|
|
else if (m.aggressor_side() == AggressorSide::AGGRESSOR_SIDE_SELL)
|
|
vol.set_sell_volume(vol.sell_volume() + m.size());
|
|
else if (m.aggressor_side() == AggressorSide::AGGRESSOR_SIDE_NONE)
|
|
vol.set_passive_volume(vol.passive_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;
|
|
}
|
|
|
|
} // namespace auxiliary
|
|
} // namespace md
|
|
} // namespace bts
|