118 lines
4.2 KiB
C++
118 lines
4.2 KiB
C++
|
|
#include "PositionAggregator.h"
|
|
#include "Position.h"
|
|
|
|
#include "lib/domain_model/Security.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/md/MDClient.h"
|
|
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
|
#include "lib/utils/Dispatcher.h"
|
|
#include "lib/utils/TimeStamp.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
|
|
#include <set>
|
|
#include <utility>
|
|
|
|
using namespace bts::api::orderman;
|
|
using namespace bts::api::position;
|
|
using namespace std;
|
|
|
|
namespace bts {
|
|
namespace position {
|
|
|
|
PositionAggregator::PositionAggregator(
|
|
api::position::PositionService &&pos, md::MDClient &mdclient)
|
|
: _position_service(std::move(pos)), _position_map(), _pnl_map(),
|
|
_accounts(), _oracle(mdclient.oracle()), _lock() {
|
|
_position_service.subscribe_position_data() |
|
|
rxcpp::operators::subscribe<api::position::Position_dataUpdate>(
|
|
[this](auto const &update) {
|
|
std::scoped_lock<std::recursive_mutex> l(_lock);
|
|
for (const auto &o : update.removed()) {
|
|
const auto &it = _position_map.find(o->record_id());
|
|
if (it != _position_map.end())
|
|
_position_map.erase(it);
|
|
}
|
|
for (const auto &ch : update.changed()) {
|
|
const auto &o = ch.current();
|
|
_position_map[o->record_id()] = *o;
|
|
_accounts[o->account()] = bts::datetime::ll_now();
|
|
}
|
|
for (const auto &o : update.added()) {
|
|
_position_map[o->record_id()] = *o;
|
|
_accounts[o->account()] = bts::datetime::ll_now();
|
|
}
|
|
});
|
|
_position_service.subscribe_pnl_account_data() |
|
|
rxcpp::operators::subscribe<api::position::Pnl_account_dataUpdate>(
|
|
[this](auto const &update) {
|
|
std::scoped_lock<std::recursive_mutex> l(_lock);
|
|
for (const auto &o : update.removed()) {
|
|
const auto &it = _pnl_map.find(o->account_name());
|
|
if (it != _pnl_map.end())
|
|
_pnl_map.erase(it);
|
|
}
|
|
for (const auto &ch : update.changed()) {
|
|
const auto &o = ch.current();
|
|
_pnl_map[o->account_name()] = *o;
|
|
}
|
|
for (const auto &o : update.added()) {
|
|
_pnl_map[o->account_name()] = *o;
|
|
}
|
|
});
|
|
}
|
|
|
|
PositionAggregator::~PositionAggregator() {}
|
|
|
|
void eraseSubStr(std::string &mainStr, const std::string &toErase) {
|
|
size_t pos = mainStr.find(toErase);
|
|
if (pos != std::string::npos)
|
|
mainStr.erase(pos, toErase.length());
|
|
}
|
|
|
|
bool PositionAggregator::query_pnl(
|
|
const std::string &account, api::position::AccountPNL &pnl) {
|
|
auto pit = _pnl_map.find(account);
|
|
if (pit != _pnl_map.end()) {
|
|
pnl = pit->second;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool PositionAggregator::query_positions(
|
|
const std::string &account,
|
|
std::vector<Position> &result,
|
|
api::position::AccountPNL &pnl,
|
|
datetime::LLDateTime *since) {
|
|
auto last_update = _accounts[account];
|
|
if (!since || *since == 0 ||
|
|
bts::datetime::ll_diff_in_us(*since, last_update) > 0) {
|
|
std::scoped_lock<std::recursive_mutex> l(_lock);
|
|
for (auto &entry : _position_map) {
|
|
const PositionUnit &unit = entry.second;
|
|
if (unit.account() != account)
|
|
continue;
|
|
auto sec = _oracle.get_security(SecurityKey(unit.security_id()));
|
|
if (sec && sec->has_glbx() && sec->has_future()) {
|
|
Position pos;
|
|
pos.account = account;
|
|
pos.security_desc = securities::descriptor(*sec);
|
|
eraseSubStr(pos.security_desc, "SIM:");
|
|
int32_t totalpos = unit.sod_long_pos() + unit.day_long_pos() -
|
|
unit.sod_short_pos() - unit.day_short_pos();
|
|
pos.position = Quantity(abs(totalpos));
|
|
pos.side = (totalpos < 0.0 ? api::SIDE_SELL : api::SIDE_BUY);
|
|
result.push_back(pos);
|
|
}
|
|
}
|
|
if (since)
|
|
*since = last_update;
|
|
return query_pnl(account, pnl);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
} // namespace position
|
|
} // namespace bts
|