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
@@ -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)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user