added spreadsheet to help test span
This commit is contained in:
1 parent
d7d319f1e3
commit
f329470165
8 files changed
+186
-46
No files matched your search
Binary file not shown.
@@ -13,6 +13,8 @@ namespace bts.excel.rtd
|
||||
class JsonFormatter : IExcelFormatter
|
||||
{
|
||||
private object _data;
|
||||
static Google.Protobuf.JsonFormatter _protoformatter =
|
||||
new Google.Protobuf.JsonFormatter(Google.Protobuf.JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
|
||||
public JsonFormatter(object data)
|
||||
{
|
||||
_data = data;
|
||||
@@ -81,7 +83,7 @@ namespace bts.excel.rtd
|
||||
{
|
||||
string json = null;
|
||||
if(data is IMessage imsg)
|
||||
json = Google.Protobuf.JsonFormatter.Default.Format(imsg);
|
||||
json = _protoformatter.Format(imsg);
|
||||
else
|
||||
json = JsonConvert.SerializeObject(data);
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ message VoidMessage {}
|
||||
message BytesMessage {
|
||||
bytes buffer = 1;
|
||||
}
|
||||
message BoolResultMessage {
|
||||
bool success = 1;
|
||||
message ResultMessage {
|
||||
bool success = 1;
|
||||
string error_message = 2;
|
||||
}
|
||||
@@ -21,14 +21,37 @@ message MarginResult {
|
||||
double initial_margin = 130;
|
||||
}
|
||||
|
||||
message ExcessLiquidityReport {
|
||||
string account = 10 [(bts.api.key)=true];
|
||||
double leverage_factor = 20;
|
||||
double cash_balance = 30;
|
||||
MarginResult margin_result = 40;
|
||||
|
||||
double excess_liquidity = 100;
|
||||
}
|
||||
|
||||
|
||||
message MarginRequest {
|
||||
string account = 10;
|
||||
}
|
||||
|
||||
message CashFactorUpdate {
|
||||
string account = 10;
|
||||
double leverage_factor = 20;
|
||||
}
|
||||
|
||||
message CashBalanceUpdate {
|
||||
string account = 10;
|
||||
double cash_balance = 20;
|
||||
}
|
||||
|
||||
service MarginService {
|
||||
option (bts.api.service_description) = "Provides CME Margin Information per account";
|
||||
|
||||
// subscription
|
||||
rpc margin_data(bts.api.VoidMessage) returns (stream MarginResult);
|
||||
rpc get_margin(MarginRequest) returns (MarginResult);
|
||||
rpc get_margin(MarginRequest) returns (MarginResult);
|
||||
rpc set_cash_factor(CashFactorUpdate) returns (bts.api.ResultMessage);
|
||||
rpc set_cash_balance(CashBalanceUpdate) returns (bts.api.ResultMessage);
|
||||
|
||||
rpc margin_data(bts.api.VoidMessage) returns (stream MarginResult);
|
||||
rpc excess_liquidity_report(bts.api.VoidMessage) returns (stream ExcessLiquidityReport);
|
||||
}
|
||||
@@ -3,12 +3,14 @@
|
||||
#include "PositionAggregator.h"
|
||||
#include "Position.h"
|
||||
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/toggles.pb.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/Dispatcher.h"
|
||||
|
||||
@@ -17,6 +19,7 @@
|
||||
|
||||
using namespace bts::api::orderman;
|
||||
using namespace bts::api::position;
|
||||
using namespace bts::admin;
|
||||
using namespace std;
|
||||
using bts::api::position::MarginResult;
|
||||
|
||||
@@ -25,15 +28,30 @@ namespace position {
|
||||
|
||||
MarginServiceServer::MarginServiceServer(bts::api::ServiceContext& context, md::MDClient& mdclient)
|
||||
: bts::api::position::MarginServiceBase (context),
|
||||
_admin_service(context.locate<bts::api::admin::AdminService>()),
|
||||
_publisher(make_margin_data_stateful_publisher()),
|
||||
_report_publisher(make_excess_liquidity_report_stateful_publisher()),
|
||||
_dispatcher(utils::Dispatcher::get_named("MarginServiceServer")),
|
||||
_timer(_dispatcher, 60000, [=]() { publish_all();} ),
|
||||
_position(new PositionAggregator(context.locate<bts::api::position::PositionService>(), mdclient)),
|
||||
_margin(new MarginServiceCore(_dispatcher)),
|
||||
//state below
|
||||
_lock(),
|
||||
_last_update(),
|
||||
_lock()
|
||||
_admin_data()
|
||||
{
|
||||
LOG(Gen, Info, "MarginServiceServer: service started");
|
||||
|
||||
_admin_data = *_admin_service.get_admin_data(api::VoidMessage()).value();
|
||||
_admin_service.subscribe_admin_data() |
|
||||
rxcpp::operators::subscribe<api::admin::Admin_dataUpdate>(
|
||||
[this](auto const &update)
|
||||
{
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
bts_assert(update.all().size() == 1);
|
||||
_admin_data = *update.all()[0];
|
||||
});
|
||||
|
||||
publish_all();
|
||||
_timer.start();
|
||||
}
|
||||
@@ -42,25 +60,24 @@ void MarginServiceServer::publish_all()
|
||||
{
|
||||
if(config::get<bts::config::toggles::SpanMargin>().enabled())
|
||||
{
|
||||
auto accts = _position->get_accounts();
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
auto accts = AdminServiceUtils::accounts(_admin_data, false);
|
||||
for(const auto& i : accts)
|
||||
{
|
||||
datetime::LLDateTime since = 0;
|
||||
{
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
auto it = _last_update.find(i);
|
||||
if(it != _last_update.end())
|
||||
since = it->second;
|
||||
}
|
||||
std::vector<Position> pos;
|
||||
orderman::AccountName acct = i;
|
||||
std::string acct = i->name();
|
||||
auto it = _last_update.find(acct);
|
||||
if(it != _last_update.end())
|
||||
since = it->second;
|
||||
std::vector<Position> pos;
|
||||
if(_position->query_positions(acct, pos, &since))
|
||||
{
|
||||
LOG(Gen, Info, "MarginServiceServer: computing margin for {} ", orderman::to_str(i) );
|
||||
_margin->compute_margin(i, pos, [this, since, acct](const MarginResult& m)
|
||||
LOG(Gen, Info, "MarginServiceServer: computing margin for {} ", acct);
|
||||
auto aname = orderman::to<orderman::AccountName>(acct);
|
||||
_margin->compute_margin(aname, pos, [this, since, acct](const MarginResult& m)
|
||||
{
|
||||
publish_margin(m);
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
publish_margin(m);
|
||||
_last_update[acct] = since;
|
||||
});
|
||||
}
|
||||
@@ -84,8 +101,7 @@ void MarginServiceServer::get_margin(const api::position::MarginRequest& request
|
||||
orderman::AccountName acct;
|
||||
orderman::str_to(request.account(), &acct);
|
||||
std::vector<Position> pos;
|
||||
_position->query_positions(acct, pos);
|
||||
|
||||
_position->query_positions(request.account(), pos);
|
||||
_margin->compute_margin(acct, pos, [=](const MarginResult& m)
|
||||
{
|
||||
reply(m);
|
||||
@@ -94,12 +110,104 @@ void MarginServiceServer::get_margin(const api::position::MarginRequest& request
|
||||
}
|
||||
|
||||
|
||||
void MarginServiceServer::publish_margin(const api::position::MarginResult& account_result)
|
||||
void MarginServiceServer::publish_margin(const api::position::MarginResult& result)
|
||||
{
|
||||
//account_result here is keyed on account_id, which is included for streaming margin computations but not
|
||||
//on-demand computations
|
||||
_publisher.add_or_update(account_result);
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
_publisher.add_or_update(result);
|
||||
_publisher.flush();
|
||||
|
||||
publish_excess_liquidity_report(result);
|
||||
}
|
||||
|
||||
void MarginServiceServer::publish_excess_liquidity_report(const api::position::MarginResult& result)
|
||||
{
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
ExcessLiquidityReport report;
|
||||
report.set_account(result.account());
|
||||
report.mutable_margin_result()->CopyFrom(result);
|
||||
auto acct = AdminServiceUtils::find_account(_admin_data, result.account(), false);
|
||||
double netliq = 0.0;
|
||||
if(acct && acct->has_margin_limit())
|
||||
{
|
||||
report.set_leverage_factor( acct->margin_limit().leverage_factor_percentage() );
|
||||
report.set_cash_balance(acct->margin_limit().cash_balance());
|
||||
netliq = report.leverage_factor() * report.cash_balance();
|
||||
}
|
||||
netliq -= result.maint_margin();
|
||||
report.set_excess_liquidity(netliq);
|
||||
_report_publisher.add_or_update(report);
|
||||
_report_publisher.flush();
|
||||
}
|
||||
|
||||
void MarginServiceServer::update_report(const std::string& account)
|
||||
{
|
||||
MarginResult res;
|
||||
auto acct = orderman::to<orderman::AccountName>(account);
|
||||
if(_margin->get_last_valid_result(acct, res))
|
||||
publish_excess_liquidity_report(res);
|
||||
}
|
||||
|
||||
void MarginServiceServer::set_cash_factor(const ::bts::api::position::CashFactorUpdate& request,
|
||||
const api::Response<::bts::api::ResultMessage>& reply)
|
||||
{
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
auto acct = AdminServiceUtils::find_account(_admin_data, request.account(), false);
|
||||
if(!acct)
|
||||
{
|
||||
auto msg = "Account " + request.account() +"not found";
|
||||
api::ResultMessage resp;
|
||||
resp.set_success(false);
|
||||
resp.set_error_message(msg);
|
||||
reply(resp);
|
||||
}
|
||||
else
|
||||
{
|
||||
acct->mutable_margin_limit()->set_leverage_factor_percentage(request.leverage_factor());
|
||||
AccountUpdateAPIArgument update;
|
||||
update.mutable_complete_data()->CopyFrom(*acct);
|
||||
|
||||
_admin_service.update_account(update,
|
||||
[this, request, reply](const api::Result<bts::api::admin::AdminServiceOperationResult>& admin_result)
|
||||
{
|
||||
api::ResultMessage resp;
|
||||
resp.set_success(admin_result.success());
|
||||
resp.set_error_message(admin_result.error_message());
|
||||
reply(resp);
|
||||
update_report(request.account());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void MarginServiceServer::set_cash_balance(const ::bts::api::position::CashBalanceUpdate& request,
|
||||
const api::Response<::bts::api::ResultMessage>& reply)
|
||||
{
|
||||
std::scoped_lock<std::recursive_mutex> l(_lock);
|
||||
auto acct = AdminServiceUtils::find_account(_admin_data, request.account(), false);
|
||||
if(!acct)
|
||||
{
|
||||
auto msg = "Account " + request.account() +"not found";
|
||||
api::ResultMessage resp;
|
||||
resp.set_success(false);
|
||||
resp.set_error_message(msg);
|
||||
reply(resp);
|
||||
}
|
||||
else
|
||||
{
|
||||
acct->mutable_margin_limit()->set_cash_balance(request.cash_balance());
|
||||
AccountUpdateAPIArgument update;
|
||||
update.mutable_complete_data()->CopyFrom(*acct);
|
||||
|
||||
_admin_service.update_account(update,
|
||||
[this, request, reply](const api::Result<bts::api::admin::AdminServiceOperationResult>& admin_result)
|
||||
{
|
||||
api::ResultMessage resp;
|
||||
resp.set_success(admin_result.success());
|
||||
resp.set_error_message(admin_result.error_message());
|
||||
reply(resp);
|
||||
update_report(request.account());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/api/services/MarginService.server.pb.h"
|
||||
#include "lib/md/SecurityOracle.h"
|
||||
#include "lib/persistence/IStore.h"
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/api/services/PositionService.pb.h"
|
||||
#include "lib/api/services/PositionService.client.pb.h"
|
||||
#include "lib/api/services/MarginService.server.pb.h"
|
||||
#include "lib/api/services/AdminService.pb.h"
|
||||
#include "lib/api/services/AdminService.client.pb.h"
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include <map>
|
||||
@@ -28,18 +29,33 @@ public:
|
||||
|
||||
void get_margin(const api::position::MarginRequest& request,
|
||||
const api::Response<api::position::MarginResult>& reply) override;
|
||||
|
||||
void set_cash_factor(const ::bts::api::position::CashFactorUpdate& request,
|
||||
const api::Response<::bts::api::ResultMessage>& reply) override;
|
||||
|
||||
void set_cash_balance(const ::bts::api::position::CashBalanceUpdate& request,
|
||||
const api::Response<::bts::api::ResultMessage>& reply) override;
|
||||
|
||||
private:
|
||||
void publish_margin(const api::position::MarginResult&);
|
||||
void publish_excess_liquidity_report(const api::position::MarginResult&);
|
||||
void update_report(const std::string&);
|
||||
void publish_all();
|
||||
|
||||
private:
|
||||
api::admin::AdminService _admin_service;
|
||||
MarginServiceBase::Margin_dataStatefulPublisher _publisher;
|
||||
MarginServiceBase::Excess_liquidity_reportStatefulPublisher _report_publisher;
|
||||
utils::Dispatcher& _dispatcher;
|
||||
utils::DispatcherTimer _timer;
|
||||
std::unique_ptr<PositionAggregator> _position;
|
||||
std::unique_ptr<MarginServiceCore> _margin;
|
||||
std::map<orderman::AccountName, datetime::LLDateTime> _last_update;
|
||||
|
||||
private:
|
||||
//state
|
||||
std::recursive_mutex _lock;
|
||||
std::map<std::string, datetime::LLDateTime> _last_update;
|
||||
bts::api::admin::AdminData _admin_data;
|
||||
|
||||
};
|
||||
}}
|
||||
@@ -53,13 +53,6 @@ PositionAggregator::PositionAggregator(api::position::PositionService&& pos, md:
|
||||
|
||||
PositionAggregator::~PositionAggregator() {}
|
||||
|
||||
std::vector<orderman::AccountName> PositionAggregator::get_accounts()
|
||||
{
|
||||
std::vector<orderman::AccountName> vec;
|
||||
for(const auto& i : _accounts)
|
||||
vec.push_back(orderman::to<orderman::AccountName>(i.first));
|
||||
return vec;
|
||||
}
|
||||
|
||||
void eraseSubStr(std::string & mainStr, const std::string & toErase)
|
||||
{
|
||||
@@ -68,25 +61,23 @@ void eraseSubStr(std::string & mainStr, const std::string & toErase)
|
||||
mainStr.erase(pos, toErase.length());
|
||||
}
|
||||
|
||||
bool PositionAggregator::query_positions(const orderman::AccountName& account, std::vector<Position>& result,
|
||||
bool PositionAggregator::query_positions(const std::string& account, std::vector<Position>& result,
|
||||
datetime::LLDateTime* since)
|
||||
{
|
||||
std::string acc = orderman::to_trim_str(account);
|
||||
auto last_update = _accounts[acc];
|
||||
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);
|
||||
auto the_account = orderman::to_trim_str(account);
|
||||
for(auto& entry : _position_map)
|
||||
{
|
||||
const PositionUnit& unit = entry.second;
|
||||
if(unit.account() != the_account)
|
||||
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.account = orderman::to<orderman::AccountName>(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();
|
||||
|
||||
@@ -21,8 +21,7 @@ public:
|
||||
PositionAggregator(api::position::PositionService&&, md::MDClient&);
|
||||
~PositionAggregator();
|
||||
|
||||
std::vector<orderman::AccountName> get_accounts();
|
||||
bool query_positions(const orderman::AccountName& account, std::vector<Position>& result,
|
||||
bool query_positions(const std::string& account, std::vector<Position>& result,
|
||||
datetime::LLDateTime* since = nullptr);
|
||||
|
||||
private:
|
||||
|
||||
Reference in new issue
Block a user