beginnings of span, should't be long
This commit is contained in:
1 parent
e452413b92
commit
159e8fba23
9 files changed
+236
-94
No files matched your search
@@ -0,0 +1,28 @@
|
||||
syntax="proto3";
|
||||
package bts.api.position;
|
||||
option csharp_namespace="bts.api.position";
|
||||
import "lib/api/apioptions.proto";
|
||||
import "lib/api/apicore.proto";
|
||||
import "lib/domain_model/Security.proto";
|
||||
|
||||
message MarginResult {
|
||||
int32 id = 1 [(bts.api.key)=true];
|
||||
string account = 10;
|
||||
string portfolio_id = 15;
|
||||
string settle_indicator = 20;
|
||||
string settle_qualifier = 30;
|
||||
string as_of_time = 40;
|
||||
string calculation_id = 50;
|
||||
|
||||
double opt_value = 100;
|
||||
double nonopt_value = 110;
|
||||
double maint_margin = 120;
|
||||
double initial_margin = 130;
|
||||
}
|
||||
|
||||
service MarginService {
|
||||
option (bts.api.service_description) = "Provides CME Margin Information per account";
|
||||
|
||||
// subscription
|
||||
rpc margin_data(bts.api.VoidMessage) returns (stream MarginResult);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
#include "IMarginObserver.h"
|
||||
|
||||
namespace bts
|
||||
{
|
||||
namespace position
|
||||
{
|
||||
IMarginObserver::~IMarginObserver() {}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
|
||||
namespace bts
|
||||
{
|
||||
namespace position
|
||||
{
|
||||
struct MarginResult;
|
||||
class
|
||||
IMarginObserver
|
||||
{
|
||||
public:
|
||||
virtual ~IMarginObserver();
|
||||
virtual void on_margin_change(const orderman::AccountName&, const MarginResult&) = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,9 @@ namespace position
|
||||
{
|
||||
struct MarginResult
|
||||
{
|
||||
bool valid;
|
||||
enum class ResultStatus { NewValid, CachedValid, Error };
|
||||
ResultStatus result_status;
|
||||
|
||||
std::string settle_indicator;
|
||||
std::string settle_qualifier;
|
||||
std::string portfolio_id;
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "PositionServiceCore.h"
|
||||
#include "Position.h"
|
||||
#include "MarginResult.h"
|
||||
#include "IMarginObserver.h"
|
||||
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/api/services/AuthService.pb.h"
|
||||
@@ -39,25 +38,22 @@ namespace position
|
||||
using MarginMap = boost::container::flat_map<orderman::AccountName, MarginResult>;
|
||||
struct MarginServiceCore::PImpl
|
||||
{
|
||||
securities::ISecurityDB& db;
|
||||
PositionServiceCore position_service;
|
||||
IMarginObserver* observer;
|
||||
MarginMap last_results, last_valid_results;
|
||||
|
||||
std::string url_base, core_user, core_password, post_margin_path, get_margin_path;
|
||||
size_t max_iterations;
|
||||
bool verbose;
|
||||
int throttle;
|
||||
bts::datetime::PTime last_update;
|
||||
PImpl(securities::ISecurityDB& d, IMarginObserver* obs) :
|
||||
db(d), position_service(d), observer(obs), last_results(), last_valid_results(),
|
||||
url_base("https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/"), core_user("api_bts1"), core_password("BT5u53r!"),
|
||||
bts::datetime::PTime last_call;
|
||||
PImpl() :
|
||||
last_results(), last_valid_results(), //api_bts_span BT5Margin!
|
||||
url_base("https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/"), core_user("api_bts1"), core_password("Summer123"),
|
||||
post_margin_path("margins/?complete=true"),
|
||||
get_margin_path("margins/"),
|
||||
max_iterations(20),
|
||||
verbose(false),
|
||||
throttle(30000),
|
||||
last_update()
|
||||
last_call()
|
||||
{
|
||||
auto res = curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
if(res != CURLE_OK)
|
||||
@@ -321,7 +317,7 @@ struct MarginServiceCore::PImpl
|
||||
};
|
||||
|
||||
bool compute_margin(const std::string& account_name, const std::string& portfolid_id,
|
||||
const std::vector<Position>& pos, MarginResult& margin)
|
||||
const std::vector<Position>& pos, std::function<void(const MarginResult&, bool)> cb)
|
||||
{
|
||||
margin.valid = false;
|
||||
pugi::xml_document doc;
|
||||
@@ -331,12 +327,9 @@ struct MarginServiceCore::PImpl
|
||||
size_t count = 0;
|
||||
for(const auto& i : pos)
|
||||
{
|
||||
if(i.security && i.security->has_glbx())
|
||||
{
|
||||
int p = i.position.value() * (i.side == api::SIDE_SELL ? -1 : 1);
|
||||
request += fmt::format("BTS, {}, {}, {} {}", account_name, i.security->glbx().security_desc(), p, "\n");
|
||||
count++;
|
||||
}
|
||||
int p = i.position.value() * (i.side == api::SIDE_SELL ? -1 : 1);
|
||||
request += fmt::format("BTS, {}, {}, {} {}", account_name, i.security_desc, p, "\n");
|
||||
count++;
|
||||
}
|
||||
if(count > 0)
|
||||
{
|
||||
@@ -345,6 +338,30 @@ struct MarginServiceCore::PImpl
|
||||
xml_string_writer writer(xml);
|
||||
doc.save(writer);
|
||||
|
||||
post(post_margin_path, xml, reply);
|
||||
std::string portid_resp, calcid_resp;
|
||||
if(parse_margin_response(reply, portid_resp, calcid_resp))
|
||||
{
|
||||
utils::Dispatcher::get_named("MarginServiceServer").post(
|
||||
[=]()
|
||||
{
|
||||
try
|
||||
{
|
||||
if(get(get_margin_path + calcid_resp, reply2))
|
||||
{
|
||||
parse_margin_calculation(reply2, margin);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
}, 5000);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
size_t iters = 0;
|
||||
while(iters++ < max_iterations)
|
||||
{
|
||||
@@ -368,56 +385,49 @@ struct MarginServiceCore::PImpl
|
||||
}
|
||||
};
|
||||
|
||||
MarginServiceCore::MarginServiceCore(securities::ISecurityDB& db, IMarginObserver* obs)
|
||||
: me(new PImpl(db, obs))
|
||||
MarginServiceCore::MarginServiceCore() : me(new PImpl())
|
||||
{
|
||||
}
|
||||
|
||||
MarginServiceCore::~MarginServiceCore() {}
|
||||
|
||||
bool MarginServiceCore::pump(const orderman::AccountName& acct)
|
||||
{
|
||||
MarginResult result;
|
||||
return compute_margin(acct, result);
|
||||
}
|
||||
|
||||
bool MarginServiceCore::compute_margin(const orderman::AccountName& acct, MarginResult& result)
|
||||
bool MarginServiceCore::compute_margin(const orderman::AccountName& acct, const std::vector<Position>& pos,
|
||||
std::function<void(const MarginResult&, bool)> cb)
|
||||
{
|
||||
auto now = bts::datetime::now();
|
||||
auto diff = bts::datetime::ptime_diff_in_ms(now, me->last_update);
|
||||
if(me->last_update.is_not_a_date_time() || abs(diff) > me->throttle)
|
||||
auto diff = bts::datetime::ptime_diff_in_ms(me->last_call, now);
|
||||
if(me->last_call.is_not_a_date_time() || diff > me->throttle)
|
||||
{
|
||||
query_margin_webapi(acct, result);
|
||||
me->last_update = now;
|
||||
return true;
|
||||
me->last_call = now;
|
||||
return query_margin_webapi(acct, pos, cb);
|
||||
}
|
||||
else
|
||||
{
|
||||
get_last_valid_result(acct, result);
|
||||
return false; //false, we did not get new data
|
||||
MarginResult result;
|
||||
if(get_last_valid_result(acct, result))
|
||||
{
|
||||
result.status = MarginResult::ResultStatus::CachedValid;
|
||||
cb(result);
|
||||
}
|
||||
return false; //no error
|
||||
}
|
||||
}
|
||||
|
||||
bool MarginServiceCore::query_margin_webapi(const orderman::AccountName& acct, MarginResult& margin)
|
||||
bool MarginServiceCore::query_margin_webapi(const orderman::AccountName& acct, const std::vector<Position>& pos,
|
||||
std::function<void(const MarginResult&)> cb)
|
||||
{
|
||||
std::vector<Position> pos;
|
||||
me->position_service.query_all_positions(acct, pos);
|
||||
utils::Uuid guid;
|
||||
bool succ = me->compute_margin(orderman::to_str(acct), guid.to_text_string(), pos, margin);
|
||||
if(me->observer)
|
||||
me->observer->on_margin_change(acct, margin);
|
||||
me->last_results.insert_or_assign(acct, margin);
|
||||
if(succ && margin.valid)
|
||||
me->last_valid_results.insert_or_assign(acct, margin);
|
||||
return succ;
|
||||
return me->compute_margin(orderman::to_str(acct), guid.to_text_string(), pos,
|
||||
[this, &] (const MarginResult& m)
|
||||
{
|
||||
me->last_results.insert_or_assign(acct, m);
|
||||
if(m.status == MarginResult::ResultStatus::NewValid)
|
||||
me->last_valid_results.insert_or_assign(acct, m);
|
||||
cb(m);
|
||||
});
|
||||
}
|
||||
|
||||
bool MarginServiceCore::query_margin_webapi(const std::vector<Position>& pos, MarginResult& margin)
|
||||
{
|
||||
utils::Uuid guid;
|
||||
std::string str = guid.to_text_string();
|
||||
return me->compute_margin(str.substr(0, 3), str, pos, margin);
|
||||
}
|
||||
|
||||
void MarginServiceCore::set_verbose_debugging(bool verb)
|
||||
{
|
||||
|
||||
@@ -17,22 +17,18 @@ class MarginServiceCore
|
||||
{
|
||||
BTS_NO_COPY(MarginServiceCore);
|
||||
public:
|
||||
MarginServiceCore(securities::ISecurityDB&, IMarginObserver* obs = nullptr);
|
||||
|
||||
MarginServiceCore();
|
||||
~MarginServiceCore();
|
||||
|
||||
//Call as much as you want; it will update IMarginObserver at most once every 30s
|
||||
bool pump(const orderman::AccountName&);
|
||||
//Call as much as you want; it will query the webapi at most once every 30s
|
||||
bool compute_margin(const orderman::AccountName&, MarginResult&);
|
||||
void compute_margin(const orderman::AccountName&, const std::vector<Position>&, std::function<void(const MarginResult&)>);
|
||||
|
||||
private:
|
||||
friend class PositionTests;
|
||||
bool get_last_valid_result(const orderman::AccountName&, MarginResult&);
|
||||
bool get_last_result(const orderman::AccountName&, MarginResult&);
|
||||
//This is a blocking call to a WebAPI. Very Slow.
|
||||
bool query_margin_webapi(const orderman::AccountName&, MarginResult&);
|
||||
//This is a blocking call to a WebAPI. Very Slow.
|
||||
bool query_margin_webapi(const std::vector<Position>&, MarginResult&);
|
||||
|
||||
//This is a blocking call to a WebAPI. Very Slow.
|
||||
bool query_margin_webapi(const orderman::AccountName&, const std::vector<Position>&, std::function<void(const MarginResult&)>);
|
||||
void set_verbose_debugging(bool);
|
||||
|
||||
private:
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
#include "MarginServiceServer.h"
|
||||
#include "Position.h"
|
||||
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/AuthToken.h"
|
||||
#include "lib/utils/Dispatcher.h"
|
||||
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <fmt/format.h>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
using namespace bts::api::orderman;
|
||||
using namespace bts::api::position;
|
||||
using namespace std;
|
||||
|
||||
namespace bts {
|
||||
namespace position {
|
||||
|
||||
MarginServiceServer::MarginServiceServer(bts::api::ServiceContext& context, md::MDClient& mdclient)
|
||||
: bts::api::position::MarginServiceBase (context),
|
||||
_publisher(make_margin_data_stateful_publisher()),
|
||||
_position_service(context.locate<bts::api::position::PositionService>()),
|
||||
_position_map(),
|
||||
_oracle(mdclient)
|
||||
{
|
||||
LOG(Gen, Info, "MarginServiceServer: service started");
|
||||
|
||||
_position_service.subscribe_position_data() |
|
||||
rxcpp::operators::subscribe<api::position::Position_dataUpdate>(
|
||||
[this](auto const& update)
|
||||
{
|
||||
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;
|
||||
}
|
||||
for (const auto& o : update.added())
|
||||
{
|
||||
_position_map[o->record_id()] = *o;
|
||||
}
|
||||
});
|
||||
|
||||
auto& disp = utils::Dispatcher::get_named("MarginServiceServer");
|
||||
utils::DispatcherTimer web_api_timer(disp, 250, [&]()
|
||||
{
|
||||
for (auto& p: platforms)
|
||||
p->send_heartbeats();
|
||||
});
|
||||
web_api_timer.start();
|
||||
|
||||
disp.run();
|
||||
}
|
||||
|
||||
MarginServiceServer::~MarginServiceServer() {}
|
||||
|
||||
void MarginServiceServer::query_accounts_and_positions(std::map<AccountName>, std::vector<Position>& result)
|
||||
|
||||
void MarginServiceServer::query_all_positions(const orderman::AccountName& account, std::vector<Position>& result)
|
||||
{
|
||||
result.reserve(_position_map.size());
|
||||
for(auto& entry : _position_map)
|
||||
{
|
||||
if(entry.second.account() != account)
|
||||
continue;
|
||||
const PositionUnit& unit = entry.second;
|
||||
auto sec = _oracle.get_security(unit.security_id());
|
||||
if(sec && sec->has_glbx())
|
||||
{
|
||||
Position pos;
|
||||
pos.account = unit.account();
|
||||
pos.security_desc = securities::descriptor(*sec);
|
||||
int32_t pos = unit.sod_long_pos() + unit.day_long_pos() - unit.sod_short_pos() - unit.day_short_pos();
|
||||
pos.position = Quantity(abs(pos));
|
||||
pos.side = (pos < 0.0 ? api::SIDE_SELL : api::SIDE_BUY);
|
||||
result.push_back(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MarginServiceServer::_publish_margin()
|
||||
{
|
||||
// // TODO, need to delete all state
|
||||
// std::vector<MDEntitlementReportUnit> entitlements;
|
||||
// AdminServiceUtils::get_md_entitlement_report(_state,entitlements);
|
||||
// for (const auto& e : entitlements)
|
||||
// _md_entit_publisher.add_or_update(e);
|
||||
// _md_entit_publisher.flush();
|
||||
}
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/MarginService.server.pb.h"
|
||||
#include "lib/md/SecurityOracle.h"
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/api/services/PositionService.pb.h"
|
||||
#include "lib/api/services/PositionService.client.pb.h"
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace securities { class ISecurityDB; }
|
||||
namespace position {
|
||||
struct Position;
|
||||
|
||||
class MarginServiceServer : public api::position::MarginServiceBase
|
||||
{
|
||||
public:
|
||||
MarginServiceServer(bts::api::ServiceContext&, md::MDClient&);
|
||||
~MarginServiceServer();
|
||||
|
||||
private:
|
||||
void query_all_positions(const orderman::AccountName&, std::vector<Position>&);
|
||||
void _publish_margin();
|
||||
|
||||
private:
|
||||
MarginServiceBase::Margin_dataStatefulPublisher _publisher;
|
||||
api::position::PositionService _position_service;
|
||||
std::map<int32_t,api::position::PositionUnit> _position_map;
|
||||
securities::SecurityOracle _oracle;
|
||||
};
|
||||
}}
|
||||
@@ -1,19 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
|
||||
namespace bts
|
||||
{
|
||||
namespace position
|
||||
{
|
||||
namespace bts {
|
||||
namespace position {
|
||||
struct Position
|
||||
{
|
||||
orderman::AccountName account;
|
||||
const api::Security* security;
|
||||
std::string security_desc;
|
||||
Quantity position;
|
||||
api::Side side;
|
||||
Price avg_price;
|
||||
};
|
||||
}
|
||||
}
|
||||
}}
|
||||
Reference in new issue
Block a user