margin / position feedback into limits; needs more work
This commit is contained in:
1 parent
c33473a5f0
commit
c48beff9da
19 files changed
+389
-97
No files matched your search
@@ -1,15 +1,23 @@
|
||||
#include "MarginServiceCore.h"
|
||||
|
||||
#include "PositionServiceCore.h"
|
||||
#include "MarginResult.h"
|
||||
#include "IMarginObserver.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 "lib/utils/helper_macros.h"
|
||||
|
||||
#include "curl/curl.h"
|
||||
#include "pugixml/pugixml.hpp"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
using namespace bts::orderman;
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <utility>
|
||||
|
||||
namespace bts
|
||||
{
|
||||
@@ -18,17 +26,194 @@ namespace position
|
||||
|
||||
struct MarginServiceCore::PImpl
|
||||
{
|
||||
PImpl()
|
||||
securities::SecurityDB& db;
|
||||
IMarginObserver* observer;
|
||||
boost::container::flat_map<orderman::AccountName, std::shared_ptr<MarginResult>> results;
|
||||
|
||||
std::string url_base, core_user, core_password, post_margin_path, get_margin_path;
|
||||
PImpl(securities::SecurityDB& d, IMarginObserver* obs) :
|
||||
db(d), observer(obs), results(),
|
||||
url_base("https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/"), core_user("api_bts1"), core_password("BT5u53r!"),
|
||||
post_margin_path("margins/?complete=true"),
|
||||
get_margin_path("margins/")
|
||||
|
||||
{
|
||||
auto res = curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
if(res != CURLE_OK)
|
||||
LOG(Gen,Error,"curl_global_init() failed: {}\n", std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
~PImpl()
|
||||
{
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
struct CurlMsg
|
||||
{
|
||||
std::vector<char> data;
|
||||
};
|
||||
|
||||
static size_t create_reply_callback(void* contents, size_t size, size_t nmemb, void* userp)
|
||||
{
|
||||
size_t realsize = size * nmemb;
|
||||
CurlMsg& mem = *(CurlMsg*)userp;
|
||||
mem.data.reserve(mem.data.size() + realsize + 1);
|
||||
for(size_t i = 0; i < realsize; i++)
|
||||
mem.data.push_back(static_cast<char*>(contents)[i]);
|
||||
mem.data.push_back(0);
|
||||
return realsize;
|
||||
}
|
||||
|
||||
struct curl_slist* setup_url_and_headers(CURL* curl, const std::string& xpath)
|
||||
{
|
||||
//URL and HTTPS
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url_base + xpath);
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
|
||||
//HEADERS
|
||||
struct curl_slist *chunk = nullptr;
|
||||
chunk = curl_slist_append(chunk, std::string("username:" + core_user).c_str());
|
||||
chunk = curl_slist_append(chunk, std::string("password:" + core_password).c_str());
|
||||
chunk = curl_slist_append(chunk, "Content-Type:application/xml");
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
||||
return chunk;
|
||||
}
|
||||
|
||||
bool post(const std::string& xpath, const std::string& payload, CurlMsg& reply)
|
||||
{
|
||||
CURL* curl = curl_easy_init();
|
||||
if (curl)
|
||||
{
|
||||
struct curl_slist* header = setup_url_and_headers(curl, xpath);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, create_reply_callback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&reply);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
|
||||
|
||||
auto res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
if(res != CURLE_OK)
|
||||
LOG(Gen,Error,"POST curl_easy_perform() failed: {}\n", std::string(curl_easy_strerror(res)));
|
||||
curl_easy_cleanup(curl);
|
||||
if(header)
|
||||
curl_slist_free_all(header);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool get(const std::string& xpath, CurlMsg& reply)
|
||||
{
|
||||
CURL* curl = curl_easy_init();
|
||||
if (curl)
|
||||
{
|
||||
struct curl_slist* header = setup_url_and_headers(curl, xpath);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, create_reply_callback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&reply);
|
||||
|
||||
auto res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
if(res != CURLE_OK)
|
||||
LOG(Gen,Error,"GET curl_easy_perform() failed: {}\n", std::string(curl_easy_strerror(res)));
|
||||
curl_easy_cleanup(curl);
|
||||
if(header)
|
||||
curl_slist_free_all(header);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
//For futures- it is only necessary to populate one of the following: Ticker, or Maturity Code and Clearing Code, or Maturity Code and Product Name.
|
||||
//so Ticker, Net Positions
|
||||
void create_margin_request(const std::string& account, pugi::xml_document& doc, pugi::xml_node& trades_node)
|
||||
{
|
||||
auto att = doc.append_attribute("standalone");
|
||||
att.set_value("yes");
|
||||
auto ns = doc.append_child("ns2:marginReq");
|
||||
auto ns2 = ns.append_attribute("xmlns:ns2");
|
||||
ns2.set_value("http://cmegroup.com/schema/core/1.4");
|
||||
auto user = ns.append_attribute("reqUserId");
|
||||
user.set_value(core_user.c_str());
|
||||
|
||||
auto child = ns.append_child("margin");
|
||||
child = child.append_child("transactions");
|
||||
child = child.append_child("transaction");
|
||||
child.append_attribute("type").set_value("TRADE");
|
||||
child.append_attribute("portfolioId").set_value(account.c_str());
|
||||
child = child.append_child("payload");
|
||||
child.append_attribute("format").set_value("CSV");
|
||||
child.append_attribute("encoding").set_value("STRING");
|
||||
trades_node = child.append_child("string");
|
||||
trades_node.set_value("Ticker, Net Positions\n");
|
||||
}
|
||||
|
||||
bool parse_margin_response(const CurlMsg& msg, std::string& account, std::string& calculation_id)
|
||||
{
|
||||
std::string str(msg.data.begin(), msg.data.end());
|
||||
pugi::xml_document doc;
|
||||
doc.load_string(str.c_str());
|
||||
|
||||
auto child = doc.child("ns2:marginRpt");
|
||||
child = child.child("margin");
|
||||
if(child)
|
||||
{
|
||||
account = child.attribute("portfolioId").as_string();
|
||||
calculation_id = child.attribute("id").as_string();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void parse_margin_calculation(const CurlMsg& msg, MarginResult& result)
|
||||
{
|
||||
result.success = false;
|
||||
std::string str(msg.data.begin(), msg.data.end());
|
||||
pugi::xml_document doc;
|
||||
doc.load_string(str.c_str());
|
||||
|
||||
auto child = doc.child("ns2:marginRpt");
|
||||
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();
|
||||
auto val = child.child("amounts");
|
||||
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.success = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
MarginServiceCore::MarginServiceCore()
|
||||
: me(new PImpl())
|
||||
MarginServiceCore::MarginServiceCore(securities::SecurityDB& db, IMarginObserver* obs)
|
||||
: me(new PImpl(db, obs))
|
||||
{
|
||||
}
|
||||
|
||||
MarginServiceCore::~MarginServiceCore() {}
|
||||
|
||||
void MarginServiceCore::query_margin(const orderman::AccountName&, MarginResult&)
|
||||
{
|
||||
}
|
||||
|
||||
void MarginServiceCore::test_margin()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user