142 lines
4.3 KiB
C++
142 lines
4.3 KiB
C++
#include "PositionServiceCore.h"
|
|
|
|
#include "Position.h"
|
|
#include "IPositionObserver.h"
|
|
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
|
#include "server/sparkd/securities/SecurityDB.h"
|
|
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/logging/logging.h"
|
|
|
|
#include <boost/container/flat_map.hpp>
|
|
#include <utility>
|
|
|
|
namespace bts
|
|
{
|
|
namespace position
|
|
{
|
|
|
|
template <typename T> int sgn(T val) {
|
|
return (T(0) < val) - (val < T(0));
|
|
}
|
|
|
|
class BookEntry
|
|
{
|
|
public:
|
|
orderman2::AccountName account;
|
|
SecurityKey security_key;
|
|
double position;
|
|
double avg_price;
|
|
public:
|
|
BookEntry(const orderman2::AccountName& acc, SecurityKey k) :
|
|
account(acc), security_key(k), position(0.0), avg_price(0.0)
|
|
{
|
|
}
|
|
void add_fill(double last_size, double last_price, api::Side side)
|
|
{
|
|
if(side == api::SIDE_SELL)
|
|
last_size = -last_size;
|
|
if(position == 0.0 || sgn(position) == sgn(last_size))
|
|
{
|
|
double prev = position * avg_price;
|
|
position += last_size;
|
|
avg_price = prev + (last_size*last_price) / position;
|
|
}
|
|
else
|
|
position += last_size;
|
|
}
|
|
};
|
|
|
|
struct PositionServiceCore::PImpl
|
|
{
|
|
typedef std::pair<orderman2::AccountName, SecurityKey> KeyType;
|
|
boost::container::flat_map<KeyType, std::shared_ptr<BookEntry>> book;
|
|
IPositionObserver* observer;
|
|
PImpl(IPositionObserver* obs) : book(), observer(obs)
|
|
{
|
|
}
|
|
|
|
BookEntry& find_entry(const orderman2::AccountName& account, SecurityKey skey)
|
|
{
|
|
KeyType key(account, skey);
|
|
auto& entry = book[key];
|
|
if(!entry)
|
|
entry = std::make_shared<BookEntry>(account, skey);
|
|
return *entry;
|
|
}
|
|
|
|
void book_raw_fill(const orderman2::AccountName& account, SecurityKey sec,
|
|
double last_size, double last_price, api::Side side)
|
|
{
|
|
auto& entry = find_entry(account, sec);
|
|
entry.add_fill(last_size, last_price, side);
|
|
if(observer)
|
|
observer->on_position_change(account, sec, entry.position, entry.avg_price);
|
|
}
|
|
};
|
|
|
|
PositionServiceCore::PositionServiceCore(securities::SecurityDB& db, IPositionObserver* obs)
|
|
: me(new PImpl(obs)), _db(db)
|
|
{
|
|
}
|
|
|
|
PositionServiceCore::~PositionServiceCore() {}
|
|
|
|
bool PositionServiceCore::query_position(const orderman2::AccountName& account, SecurityKey seckey, Position& result)
|
|
{
|
|
auto& entry = me->find_entry(account, seckey);
|
|
auto sec = _db.resolve(entry.security_key);
|
|
if(sec)
|
|
{
|
|
result.account = entry.account;
|
|
result.avg_price = Price(entry.avg_price);
|
|
result.position = Quantity(abs(entry.position));
|
|
result.side = (entry.position < 0.0 ? api::SIDE_SELL : api::SIDE_BUY);
|
|
result.security = sec;
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
void PositionServiceCore::query_all_positions(const orderman2::AccountName& account, std::vector<Position>& result)
|
|
{
|
|
result.reserve(me->book.size());
|
|
for(auto& entry : me->book)
|
|
{
|
|
if(entry.first.first != account)
|
|
continue;
|
|
auto sec = _db.resolve(entry.second->security_key);
|
|
if(sec)
|
|
{
|
|
Position pos;
|
|
pos.account = entry.second->account;
|
|
pos.avg_price = Price(entry.second->avg_price);
|
|
pos.position = Quantity(abs(entry.second->position));
|
|
pos.side = (entry.second->position < 0.0 ? api::SIDE_SELL : api::SIDE_BUY);
|
|
pos.security = sec;
|
|
result.push_back(pos);
|
|
}
|
|
}
|
|
}
|
|
|
|
void PositionServiceCore::consume_raw_fill(const std::string& account, uint32_t security_key,
|
|
double last_size, double last_price, api::Side side)
|
|
{
|
|
orderman2::AccountName acc = orderman2::to<orderman2::AccountName>(account);
|
|
SecurityKey skey(security_key);
|
|
auto sec = _db.resolve(skey);
|
|
if(sec->Contract_case() == api::Security::ContractCase::kSpread)
|
|
{
|
|
//TODO this is wrong; need the leg fills
|
|
for(int i = 0; i < sec->spread().legs_size(); i++)
|
|
me->book_raw_fill(acc, SecurityKey(sec->spread().legs(i).security().id()),
|
|
0.0, 0.0, side);
|
|
}
|
|
else
|
|
me->book_raw_fill(acc, skey, last_size, last_price, side);
|
|
}
|
|
|
|
}
|
|
}
|