a simple position service, wrong for exchange traded spreads until I can get at the leg fills

This commit is contained in:
Kevin Darby committed 2017-11-13 17:29:27 -05:00
1 parent d03574ccc7
commit a8050617c5
3 files changed
+99 -20

No files matched your search

@@ -1,35 +1,118 @@
#include "PositionServiceCore.h"
#include "IPositionObserver.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "server/sparkd/securities/SecurityDB.h"
#include "lib/config/config.h"
#include "lib/api/services/AccountService.pb.h"
#include "lib/api/services/UserService.pb.h"
#include "lib/utils/bts_assert.h"
#include "lib/logging/logging.h"
#include <fmt/format.h>
using namespace bts::orderman;
#include <boost/container/flat_map.hpp>
#include <utility>
namespace bts
{
namespace position
{
struct PositionServiceCore::PImpl
template <typename T> int sgn(T val) {
return (T(0) < val) - (val < T(0));
}
class BookEntry
{
PImpl()
orderman::AccountName _account;
SecurityKey _key;
double _position;
double _avg_price;
public:
BookEntry(const orderman::AccountName& acc, SecurityKey k) :
_account(acc), _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;
}
void position_and_price(double& position, double& avg_price)
{
position = _position;
avg_price = _avg_price;
}
};
PositionServiceCore::PositionServiceCore(const securities::SecurityDB&, IPositionObserver*)
: me(new PImpl())
struct PositionServiceCore::PImpl
{
typedef std::pair<orderman::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 orderman::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 orderman::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)
{
double position, avg_price;
entry.position_and_price(position, avg_price);
observer->on_position_change(account, sec, position, avg_price);
}
}
};
PositionServiceCore::PositionServiceCore(securities::SecurityDB& db, IPositionObserver* obs)
: me(new PImpl(obs)), _db(db)
{
}
PositionServiceCore::~PositionServiceCore() {}
void PositionServiceCore::query_position(const orderman::AccountName& account, SecurityKey sec,
double& position, double& avg_price)
{
auto& entry = me->find_entry(account, sec);
entry.position_and_price(position, avg_price);
}
void PositionServiceCore::consume_raw_fill(const std::string& account, uint32_t security_key,
double last_size, double last_price, api::Side side)
{
orderman::AccountName acc = orderman::to<orderman::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);
}
}
}