span works again
This commit is contained in:
1 parent
159e8fba23
commit
be5d037ca9
16 files changed
+342
-381
No files matched your search
@@ -1,13 +1,11 @@
|
||||
#include "MarginServiceCore.h"
|
||||
|
||||
#include "PositionServiceCore.h"
|
||||
#include "Position.h"
|
||||
#include "MarginResult.h"
|
||||
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/api/services/AuthService.pb.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/Uuid.h"
|
||||
#include "lib/utils/Dispatcher.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
@@ -22,13 +20,7 @@
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <utility>
|
||||
|
||||
#define CONSOLE_LOG(...) std::cout , __VA_ARGS__ , std::endl
|
||||
|
||||
template <typename T> std::ostream& operator,(std::ostream& out, const T& t) { out << t; return out; }
|
||||
std::ostream& operator,(std::ostream& out, std::ostream&(*f)(std::ostream&)) { out << f; return out; }
|
||||
#define MARGIN_LOG(subsystem,level,...) \
|
||||
bts::log::_thread_logger.logger->enqueue(log::log_ts(),bts::log::subsystem,level,__FILE__,__LINE__,__VA_ARGS__); \
|
||||
CONSOLE_LOG(__VA_ARGS__)
|
||||
using bts::api::position::MarginResult;
|
||||
|
||||
namespace bts
|
||||
{
|
||||
@@ -38,19 +30,18 @@ namespace position
|
||||
using MarginMap = boost::container::flat_map<orderman::AccountName, MarginResult>;
|
||||
struct MarginServiceCore::PImpl
|
||||
{
|
||||
MarginMap last_results, last_valid_results;
|
||||
|
||||
utils::Dispatcher& dispatcher;
|
||||
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_call;
|
||||
PImpl() :
|
||||
PImpl(utils::Dispatcher& disp) :
|
||||
dispatcher(disp),
|
||||
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_call()
|
||||
@@ -58,7 +49,7 @@ struct MarginServiceCore::PImpl
|
||||
auto res = curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
if(res != CURLE_OK)
|
||||
{
|
||||
MARGIN_LOG(Gen,Error,"curl_global_init() failed: ", std::string(curl_easy_strerror(res)));
|
||||
LOG(Gen,Error,"curl_global_init() failed: {}", std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
}
|
||||
~PImpl()
|
||||
@@ -147,7 +138,7 @@ struct MarginServiceCore::PImpl
|
||||
struct curl_slist* setup_url_and_headers(CURL* curl, const std::string& xpath)
|
||||
{
|
||||
std::string full_url = url_base + xpath;
|
||||
MARGIN_LOG(Gen,Info,"CORE Margin URL: ",full_url);
|
||||
LOG(Gen,Info,"CORE Margin URL: {}",full_url);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, full_url.c_str());
|
||||
//curl_easy_setopt(curl, CURLOPT_URL, "https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/margins/");
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debug_callback);
|
||||
@@ -178,19 +169,19 @@ struct MarginServiceCore::PImpl
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&reply);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str());
|
||||
|
||||
MARGIN_LOG(Gen,Info,"CORE Margin Payload: ", payload);
|
||||
LOG(Gen,Info,"CORE Margin Payload: {}", payload);
|
||||
auto res = curl_easy_perform(curl);
|
||||
if(res == CURLE_OK)
|
||||
{
|
||||
std::string resp(reply.data.begin(), reply.data.end());
|
||||
MARGIN_LOG(Gen,Info,"CORE Margin Post Response: ", resp);
|
||||
LOG(Gen,Info,"CORE Margin Post Response: {}", resp);
|
||||
succ = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string error(curl_easy_strerror(res));
|
||||
MARGIN_LOG(Gen,Error,"POST curl_easy_perform() failed: ", error);
|
||||
std::cout << fmt::format("CORE Margin CURL Error: ", error);
|
||||
LOG(Gen,Error,"POST curl_easy_perform() failed: {}", error);
|
||||
//std::cout << fmt::format("CORE Margin CURL Error: ", error);
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
if(header)
|
||||
@@ -198,7 +189,7 @@ struct MarginServiceCore::PImpl
|
||||
}
|
||||
else
|
||||
{
|
||||
MARGIN_LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
}
|
||||
return succ;
|
||||
}
|
||||
@@ -217,12 +208,12 @@ struct MarginServiceCore::PImpl
|
||||
if(res == CURLE_OK)
|
||||
{
|
||||
std::string resp(reply.data.begin(), reply.data.end());
|
||||
MARGIN_LOG(Gen,Info,"CORE Margin GET Response: ", resp);
|
||||
LOG(Gen,Info,"CORE Margin GET Response: {}", resp);
|
||||
succ = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MARGIN_LOG(Gen,Error,"GET curl_easy_perform() failed: ", std::string(curl_easy_strerror(res)));
|
||||
LOG(Gen,Error,"GET curl_easy_perform() failed: {}", std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
if(header)
|
||||
@@ -230,7 +221,7 @@ struct MarginServiceCore::PImpl
|
||||
}
|
||||
else
|
||||
{
|
||||
MARGIN_LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
}
|
||||
return succ;
|
||||
}
|
||||
@@ -280,7 +271,7 @@ struct MarginServiceCore::PImpl
|
||||
|
||||
void parse_margin_calculation(const CurlMsg& msg, MarginResult& result)
|
||||
{
|
||||
result.valid = false;
|
||||
result.set_status(MarginResult::Error);
|
||||
std::string str(msg.data.begin(), msg.data.end());
|
||||
pugi::xml_document doc;
|
||||
doc.load_string(str.c_str());
|
||||
@@ -289,19 +280,19 @@ struct MarginServiceCore::PImpl
|
||||
child = child.child("margin");
|
||||
if(child)
|
||||
{
|
||||
result.settle_indicator = child.attribute("settleInd").as_string();
|
||||
result.settle_qualifier = child.attribute("settleQual").as_string();
|
||||
result.portfolio_id = child.attribute("portfolioId").as_string();
|
||||
result.as_of_time = child.attribute("asOfTime").as_string();
|
||||
result.calculation_id = child.attribute("id").as_string();
|
||||
result.set_settle_indicator(child.attribute("settleInd").as_string());
|
||||
result.set_settle_qualifier(child.attribute("settleQual").as_string());
|
||||
result.set_portfolio_id(child.attribute("portfolioId").as_string());
|
||||
result.set_as_of_time(child.attribute("asOfTime").as_string());
|
||||
result.set_calculation_id(child.attribute("id").as_string());
|
||||
auto val = child.child("amounts");
|
||||
if(val && result.portfolio_id != "")
|
||||
if(val && result.portfolio_id() != "")
|
||||
{
|
||||
result.opt_value = child.attribute("optVal").as_double();
|
||||
result.nonopt_value = child.attribute("nonOptVal").as_double();
|
||||
result.maint_margin = child.attribute("maint").as_double();
|
||||
result.initial_margin = child.attribute("init").as_double();
|
||||
result.valid = true;
|
||||
result.set_opt_value(val.attribute("optVal").as_double());
|
||||
result.set_nonopt_value(val.attribute("nonOptVal").as_double());
|
||||
result.set_maint_margin(val.attribute("maint").as_double());
|
||||
result.set_initial_margin(val.attribute("init").as_double());
|
||||
result.set_status(MarginResult::NewValid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -317,18 +308,17 @@ struct MarginServiceCore::PImpl
|
||||
};
|
||||
|
||||
bool compute_margin(const std::string& account_name, const std::string& portfolid_id,
|
||||
const std::vector<Position>& pos, std::function<void(const MarginResult&, bool)> cb)
|
||||
{
|
||||
margin.valid = false;
|
||||
const std::vector<Position>& pos, std::function<void(const MarginResult&)> cb)
|
||||
{
|
||||
pugi::xml_document doc;
|
||||
pugi::xml_node trades_node;
|
||||
create_margin_request(portfolid_id, doc, trades_node);
|
||||
std::string request = "\nFirm Id, Acct Id, Ticker Symbol, Net Positions\n";
|
||||
std::string request = "\nFirm Id, Acct Id, Exchange, Ticker Symbol, Net Positions\n";
|
||||
size_t count = 0;
|
||||
for(const auto& i : pos)
|
||||
{
|
||||
int p = i.position.value() * (i.side == api::SIDE_SELL ? -1 : 1);
|
||||
request += fmt::format("BTS, {}, {}, {} {}", account_name, i.security_desc, p, "\n");
|
||||
request += fmt::format("BTS, {}, {}, {}, {}, {}", account_name, "CME", i.security_desc, p, "\n");
|
||||
count++;
|
||||
}
|
||||
if(count > 0)
|
||||
@@ -338,54 +328,48 @@ struct MarginServiceCore::PImpl
|
||||
xml_string_writer writer(xml);
|
||||
doc.save(writer);
|
||||
|
||||
CurlMsg reply;
|
||||
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(
|
||||
{
|
||||
std::function<void()> do_web;
|
||||
do_web =
|
||||
[=]()
|
||||
{
|
||||
MarginResult margin;
|
||||
margin.set_status(MarginResult::Error);
|
||||
margin.set_account(account_name);
|
||||
CurlMsg reply2; bool done = false;
|
||||
try
|
||||
{
|
||||
if(get(get_margin_path + calcid_resp, reply2))
|
||||
{
|
||||
parse_margin_calculation(reply2, margin);
|
||||
return true;
|
||||
cb(margin);
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
catch(const std::exception& ex)
|
||||
{
|
||||
LOG(Gen, Error, "Exception in parse_margin_calculation {}", std::string(ex.what()));
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
}, 5000);
|
||||
LOG(Gen, Error, "Unknown Exception in parse_margin_calculation");
|
||||
}
|
||||
if(!done)
|
||||
dispatcher.post_copy(do_web, 5000);
|
||||
};
|
||||
dispatcher.post_copy(do_web, 5000);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
size_t iters = 0;
|
||||
while(iters++ < max_iterations)
|
||||
{
|
||||
CurlMsg reply;
|
||||
sleep_for(5s);
|
||||
post(post_margin_path, xml, reply);
|
||||
std::string portid_resp, calcid_resp;
|
||||
if(parse_margin_response(reply, portid_resp, calcid_resp))
|
||||
{
|
||||
CurlMsg reply2;
|
||||
sleep_for(2s);
|
||||
if(get(get_margin_path + calcid_resp, reply2))
|
||||
{
|
||||
parse_margin_calculation(reply2, margin);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
MarginServiceCore::MarginServiceCore() : me(new PImpl())
|
||||
MarginServiceCore::MarginServiceCore(utils::Dispatcher& disp) : me(new PImpl(disp))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -393,7 +377,7 @@ MarginServiceCore::~MarginServiceCore() {}
|
||||
|
||||
|
||||
bool MarginServiceCore::compute_margin(const orderman::AccountName& acct, const std::vector<Position>& pos,
|
||||
std::function<void(const MarginResult&, bool)> cb)
|
||||
std::function<void(const MarginResult&)> cb)
|
||||
{
|
||||
auto now = bts::datetime::now();
|
||||
auto diff = bts::datetime::ptime_diff_in_ms(me->last_call, now);
|
||||
@@ -407,10 +391,10 @@ bool MarginServiceCore::compute_margin(const orderman::AccountName& acct, const
|
||||
MarginResult result;
|
||||
if(get_last_valid_result(acct, result))
|
||||
{
|
||||
result.status = MarginResult::ResultStatus::CachedValid;
|
||||
result.set_status(MarginResult::CachedValid);
|
||||
cb(result);
|
||||
}
|
||||
return false; //no error
|
||||
return true; //no error, just returning cached
|
||||
}
|
||||
}
|
||||
|
||||
@@ -418,14 +402,27 @@ bool MarginServiceCore::query_margin_webapi(const orderman::AccountName& acct, c
|
||||
std::function<void(const MarginResult&)> cb)
|
||||
{
|
||||
utils::Uuid guid;
|
||||
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);
|
||||
});
|
||||
try
|
||||
{
|
||||
return me->compute_margin(orderman::to_str(acct), guid.to_text_string(), pos,
|
||||
[=] (const MarginResult& m)
|
||||
{
|
||||
me->last_results.insert_or_assign(acct, m);
|
||||
if(m.status() == MarginResult::NewValid)
|
||||
me->last_valid_results.insert_or_assign(acct, m);
|
||||
cb(m);
|
||||
});
|
||||
}
|
||||
catch(const std::exception& ex)
|
||||
{
|
||||
LOG(Gen, Error, "Exception in query_margin_webapi {}", std::string(ex.what()));
|
||||
return false;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
LOG(Gen, Error, "Unknown Exception in query_margin_webapi");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user