successful connection to CME span for futures
This commit is contained in:
1 parent
76fed35a76
commit
cbd2a58b5c
4 files changed
+122
-34
No files matched your search
@@ -90,6 +90,8 @@ if (WIN32)
|
||||
else ()
|
||||
target_link_libraries( spark_lib
|
||||
curl
|
||||
ssl
|
||||
crypto
|
||||
fmt
|
||||
tbb
|
||||
boost_system
|
||||
@@ -112,6 +114,3 @@ endif()
|
||||
if (WIN32)
|
||||
include(${CMAKE_SOURCE_DIR}/tools/vs_filters.cmake)
|
||||
endif()
|
||||
|
||||
|
||||
|
||||
@@ -22,6 +22,15 @@
|
||||
#include <boost/container/flat_map.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(bts::log::subsystem,level,__FILE__,__LINE__,__VA_ARGS__); \
|
||||
CONSOLE_LOG(__VA_ARGS__)
|
||||
|
||||
|
||||
namespace bts
|
||||
{
|
||||
namespace position
|
||||
@@ -36,17 +45,20 @@ struct MarginServiceCore::PImpl
|
||||
|
||||
std::string url_base, core_user, core_password, post_margin_path, get_margin_path;
|
||||
size_t max_iterations;
|
||||
bool verbose;
|
||||
PImpl(securities::SecurityDB& d, IMarginObserver* obs) :
|
||||
db(d), position_service(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/"),
|
||||
max_iterations(20)
|
||||
|
||||
max_iterations(20),
|
||||
verbose(false)
|
||||
{
|
||||
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)));
|
||||
{
|
||||
MARGIN_LOG(Gen,Error,"curl_global_init() failed: ", std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
}
|
||||
~PImpl()
|
||||
{
|
||||
@@ -69,15 +81,77 @@ struct MarginServiceCore::PImpl
|
||||
return realsize;
|
||||
}
|
||||
|
||||
static void dump(const char* text, FILE* stream, unsigned char* ptr, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
size_t c;
|
||||
unsigned int width=0x10;
|
||||
fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n", text, (long)size, (long)size);
|
||||
for(i=0; i<size; i+= width)
|
||||
{
|
||||
fprintf(stream, "%4.4lx: ", (long)i);
|
||||
/* show hex to the left */
|
||||
for(c = 0; c < width; c++)
|
||||
{
|
||||
if(i+c < size)
|
||||
fprintf(stream, "%02x ", ptr[i+c]);
|
||||
else
|
||||
fputs(" ", stream);
|
||||
}
|
||||
/* show data on the right */
|
||||
for(c = 0; (c < width) && (i+c < size); c++)
|
||||
{
|
||||
char x = (ptr[i+c] >= 0x20 && ptr[i+c] < 0x80) ? ptr[i+c] : '.';
|
||||
fputc(x, stream);
|
||||
}
|
||||
fputc('\n', stream); /* newline */
|
||||
}
|
||||
}
|
||||
|
||||
static int debug_callback(CURL*, curl_infotype type, char *data, size_t size, void *userp)
|
||||
{
|
||||
const char *text; UNUSED(userp);
|
||||
switch (type)
|
||||
{
|
||||
case CURLINFO_TEXT:
|
||||
//fprintf(stderr, "== Info: %s", data);
|
||||
text = "=> Info:";
|
||||
break;
|
||||
case CURLINFO_HEADER_OUT:
|
||||
text = "=> Send header";
|
||||
break;
|
||||
case CURLINFO_DATA_OUT:
|
||||
text = "=> Send data";
|
||||
break;
|
||||
case CURLINFO_SSL_DATA_OUT:
|
||||
text = "=> Send SSL data";
|
||||
break;
|
||||
case CURLINFO_HEADER_IN:
|
||||
text = "<= Recv header";
|
||||
break;
|
||||
case CURLINFO_DATA_IN:
|
||||
text = "<= Recv data";
|
||||
break;
|
||||
case CURLINFO_SSL_DATA_IN:
|
||||
text = "<= Recv SSL data";
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
dump(text, stderr, (unsigned char *)data, size);
|
||||
dump(text, stdout, (unsigned char *)data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct curl_slist* setup_url_and_headers(CURL* curl, const std::string& xpath)
|
||||
{
|
||||
//URL and HTTPS
|
||||
std::string full_url = url_base + xpath;
|
||||
LOG(Gen,Info,"CORE Margin URL: {}",full_url);
|
||||
std::cout << fmt::format("CORE Margin URL: {}\n",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_VERBOSE, 1L);
|
||||
MARGIN_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);
|
||||
if(verbose)
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
@@ -103,30 +177,29 @@ struct MarginServiceCore::PImpl
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&reply);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str());
|
||||
|
||||
LOG(Gen,Info,"CORE Margin Payload: {}", payload);
|
||||
std::cout << fmt::format("CORE Margin Payload: {}", payload);
|
||||
|
||||
auto res = curl_easy_perform(curl);
|
||||
MARGIN_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());
|
||||
LOG(Gen,Info,"CORE Margin Post Response: {}", resp);
|
||||
std::cout << fmt::format("CORE Margin Post Response: {}", resp);
|
||||
MARGIN_LOG(Gen,Info,"CORE Margin Post Response: ", resp);
|
||||
succ = true;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
std::string error(curl_easy_strerror(res));
|
||||
LOG(Gen,Error,"POST curl_easy_perform() failed: {}\n", error);
|
||||
std::cout << fmt::format("CORE Margin CURL Error: {}\n", error);
|
||||
MARGIN_LOG(Gen,Error,"POST curl_easy_perform() failed: ", error);
|
||||
std::cout << fmt::format("CORE Margin CURL Error: ", error);
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
if(header)
|
||||
curl_slist_free_all(header);
|
||||
}
|
||||
else
|
||||
LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
return succ;
|
||||
{
|
||||
MARGIN_LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
}
|
||||
return succ;
|
||||
}
|
||||
|
||||
bool get(const std::string& xpath, CurlMsg& reply)
|
||||
@@ -139,17 +212,25 @@ struct MarginServiceCore::PImpl
|
||||
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)));
|
||||
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 GET Response: ", resp);
|
||||
succ = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MARGIN_LOG(Gen,Error,"GET curl_easy_perform() failed: ", std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
if(header)
|
||||
curl_slist_free_all(header);
|
||||
succ = true;
|
||||
curl_slist_free_all(header);
|
||||
}
|
||||
else
|
||||
LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
{
|
||||
MARGIN_LOG(Gen,Error,"curl_easy_init() failed\n");
|
||||
}
|
||||
return succ;
|
||||
}
|
||||
|
||||
@@ -307,5 +388,10 @@ bool MarginServiceCore::query_margin(const std::vector<Position>& pos, MarginRes
|
||||
return me->compute_margin(str.substr(0, 3), str, pos, margin);
|
||||
}
|
||||
|
||||
void MarginServiceCore::set_verbose_debugging(bool verb)
|
||||
{
|
||||
me->verbose = verb;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ public:
|
||||
MarginServiceCore(securities::SecurityDB&, IMarginObserver* obs = nullptr);
|
||||
~MarginServiceCore();
|
||||
|
||||
void set_verbose_debugging(bool);
|
||||
//This is a blocking call to a WebAPI. Very Slow.
|
||||
bool query_margin(const orderman::AccountName& account, MarginResult&);
|
||||
//This is a blocking call to a WebAPI. Very Slow.
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace bts
|
||||
namespace position
|
||||
{
|
||||
|
||||
TEST_CASE ("Margin API", "[position]")
|
||||
TEST_CASE ("Margin API", "[margin]")
|
||||
{
|
||||
securities::SecurityDB _db;
|
||||
MarginServiceCore _margin(_db);
|
||||
@@ -48,16 +48,18 @@ TEST_CASE ("Margin API", "[position]")
|
||||
p.side = api::SIDE_BUY;
|
||||
std::vector<Position> pos;
|
||||
pos.push_back(p);
|
||||
p.position = Quantity(3);
|
||||
p.side = api::SIDE_SELL;
|
||||
pos.push_back(p);
|
||||
|
||||
MarginResult res;
|
||||
//_margin.set_verbose_debugging(true);
|
||||
_margin.query_margin(pos, res);
|
||||
REQUIRE(res.success);
|
||||
|
||||
REQUIRE(res.success);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
|
||||
|
||||
Reference in new issue
Block a user