304 lines
8.4 KiB
C++
304 lines
8.4 KiB
C++
#include "CMECertCmd.h"
|
|
|
|
#include "lib/orderman/actors/ilink3/IL3Reactor.h"
|
|
#include "lib/orderman/actors/ilink3/ILinkGW.h"
|
|
#include "lib/orderman/actors/ilink3/IL3Dispatcher.h"
|
|
#include "lib/utils/TimeStamp.h"
|
|
#include "lib/utils/Dispatcher.h"
|
|
#include "lib/utils/SecureLogon.h"
|
|
#include "lib/os/os.h"
|
|
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
namespace bts {
|
|
namespace cmd {
|
|
|
|
using namespace orderman::ilink3;
|
|
|
|
void beginning_of_week_logon(ILinkGW& gw) {
|
|
// Over an iLink connection, submit a Negotiate message .
|
|
// Receive and process a Negotiate Response message
|
|
// Submit an Establish message to bind connection
|
|
// Receive and process a Establishment Acknowledgment message.
|
|
// Submit a sequence message
|
|
// Receive and process a sequence message
|
|
gw.connect();
|
|
}
|
|
|
|
struct SessionSaver {
|
|
Session current;
|
|
std::string path;
|
|
SessionSaver() :
|
|
current { Session::none() },
|
|
path { os::sparkdata_path("ilink3") + "session.txt" }
|
|
{}
|
|
|
|
void save() {
|
|
std::ofstream writer(path, std::ios::binary);
|
|
writer
|
|
<< current.uuid << " "
|
|
<< current.to_cme << " "
|
|
<< current.from_cme;
|
|
writer.close();
|
|
}
|
|
bool load() {
|
|
std::ifstream reader(path, std::ios::binary);
|
|
if (reader) {
|
|
reader >> current.uuid
|
|
>> current.to_cme
|
|
>> current.from_cme;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
void session_changed(const Session& s) {
|
|
current = s;
|
|
save();
|
|
}
|
|
};
|
|
|
|
struct Securities {
|
|
api::Security future1;
|
|
api::Security future2;
|
|
api::Security spread1;
|
|
};
|
|
|
|
Securities securities() {
|
|
api::Security future1;
|
|
future1.set_id(100);
|
|
future1.mutable_glbx()->set_security_desc("0DVEZ0");
|
|
future1.mutable_glbx()->set_exchange_id(996820);
|
|
api::Security future2;
|
|
future2.mutable_glbx()->set_security_desc("0DVEH1");
|
|
future2.mutable_glbx()->set_exchange_id(996830);
|
|
future2.set_id(101);
|
|
api::Security spread;
|
|
spread.set_id(200);
|
|
spread.mutable_glbx()->set_security_desc("0DVEZ0-0DVEH1");
|
|
spread.mutable_glbx()->set_exchange_id(996810);
|
|
return { future1, future2, spread };
|
|
}
|
|
|
|
struct TestRunner {
|
|
Reactor& reactor;
|
|
ILinkGW& gw;
|
|
TestRunner(Reactor& reactor, ILinkGW& gw):
|
|
reactor(reactor), gw(gw)
|
|
{}
|
|
|
|
void run(double seconds=0) {
|
|
auto start = utils::TimeStamp::now();
|
|
while(gw.state() != State::ERROR) {
|
|
reactor.do_work();
|
|
if (seconds > 0 && (utils::TimeStamp::now() - start).seconds() >= seconds) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool run_until(const std::function<bool()>& test) {
|
|
while(gw.state() != State::ERROR) {
|
|
reactor.do_work();
|
|
if (test()) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void sequence_keepalive_lapse() {
|
|
run(32);
|
|
gw.send_sequence(true);
|
|
gw.flush();
|
|
run(29);
|
|
gw.send_terminate(20);
|
|
gw.flush();
|
|
}
|
|
|
|
void send_order(api::Security& security) {
|
|
bts_assert(gw.state() == State::READY);
|
|
using namespace orderman;
|
|
api::orderman::RoutingRecord r;
|
|
r.set_account("CustAccount");
|
|
r.set_clearing_firm("001");
|
|
r.set_operator_id("NGG"); // SenderID : 48
|
|
r.set_customer_or_firm(api::orderman::CUSTOMER_OR_FIRM_CUSTOMER);
|
|
r.set_smp_id("12345678");
|
|
r.set_smp_instruction(api::orderman::SELF_MATCH_PREVENTION_INSTRUCTION_CANCEL_AGRESSING);
|
|
r.set_location("US");
|
|
|
|
CreateOrderMsg order {
|
|
OrderKey { 99 },
|
|
Price { 1.5 },
|
|
Quantity { 2 },
|
|
&security,
|
|
api::SIDE_SELL,
|
|
Price { 0.0 },
|
|
Quantity { 2 },
|
|
api::orderman::NativeOrderType::NATIVE_ORDER_TYPE_MARKET,
|
|
api::orderman::TIME_IN_FORCE_DAY,
|
|
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO,
|
|
&r
|
|
};
|
|
gw.send_order(order);
|
|
gw.flush();
|
|
}
|
|
|
|
void send_market_limit_order() {
|
|
using namespace orderman;
|
|
const ExecutionReport* last_report;
|
|
gw.subscribe_execution_reports([&](const ExecutionReport* r) { last_report = r; });
|
|
run_until([&]() { return gw.state() == State::READY; });
|
|
if (gw.state() != State::READY) {
|
|
std::cout << "Error?";
|
|
return;
|
|
}
|
|
|
|
// send a market order
|
|
auto sec = securities();
|
|
send_order(sec.future1);
|
|
run(300);
|
|
// get a confirm
|
|
auto ok = run_until([&]() { return last_report != nullptr; });
|
|
if (!ok) {
|
|
std::cout << "Error waiting for ack" << std::endl;
|
|
}
|
|
last_report = nullptr;
|
|
// get a fill
|
|
ok = run_until([&]() { return last_report != nullptr; });
|
|
if (!ok) {
|
|
std::cout << "Error waiting for fill" << std::endl;
|
|
}
|
|
|
|
// send a cancel
|
|
if (last_report) {
|
|
// gw.send_cancel(
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void primary_gateway_enforcement() {
|
|
// success!
|
|
run(30);
|
|
run(30);
|
|
gw.send_terminate();
|
|
gw.flush();
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
int CMECert::execute(const std::vector<std::string>&) {
|
|
// Typically a timestamp
|
|
SessionSaver ss;
|
|
ss.load();
|
|
Config config { Session::none(), "NGG", "004", false, {} };
|
|
Reactor reactor;
|
|
auto& connection = reactor.register_connection({
|
|
// local dev
|
|
TCPEndpoint { "127.0.0.1", 64530 },
|
|
TCPEndpoint { "127.0.0.1", 64531 },
|
|
// General NRCERT not AutoCert+
|
|
// {"69.50.112.184", 64530},
|
|
// {"69.50.112.185", 64530},
|
|
// NGF (AutoCert)
|
|
// TCPEndpoint { "69.50.112.199", 64529 },
|
|
// TCPEndpoint { "69.50.12.205", 64529 },
|
|
// NGG (AutoCert)
|
|
TCPEndpoint { "69.50.112.199", 64530 },
|
|
TCPEndpoint { "69.50.12.205", 64530 }
|
|
});
|
|
|
|
// NGG primary - 69.50.112.199 64530
|
|
// NGG backup - 69.50.112.205 64530
|
|
|
|
ILinkGW gw(connection, ss.current, config);
|
|
gw.subscribe_session([&](auto& s) { ss.session_changed(s); });
|
|
gw.connect();
|
|
|
|
TestRunner run(reactor,gw);
|
|
run.send_market_limit_order();
|
|
// run.run();
|
|
|
|
std::cout << std::endl << "We're done here.";
|
|
// sleep(100);
|
|
return 0;
|
|
}
|
|
|
|
std::string CMECert::help() {
|
|
return "Tool to drive CME Cert";
|
|
}
|
|
|
|
//////////////////////////////////////////
|
|
// This code worked once...
|
|
//////////////////////////////////////////
|
|
|
|
// uint64_t determine_session() {
|
|
// // this is fairly complex
|
|
// // see session_name.h
|
|
// return utils::TimeStamp::now().to_uint64();
|
|
// }
|
|
|
|
// template <class Msg, size_t Data=0, size_t Sz=4+iLinkBinary::MessageHeader::encodedLength()+Msg::sbeBlockLength()>
|
|
// std::array<char,Sz+Data> mkbuffer() {
|
|
// static_assert(sizeof(FramingHeader) == 4);
|
|
// return std::array<char,Sz+Data> {};
|
|
// }
|
|
|
|
// template <class Msg, size_t Sz>
|
|
// Msg frame_message(std::array<char,Sz>& buffer) {
|
|
// static_assert(Sz >= 4+iLinkBinary::MessageHeader::encodedLength()+Msg::sbeBlockLength());
|
|
// static_assert(sizeof(FramingHeader) == 4);
|
|
// FramingHeader* fhead = reinterpret_cast<FramingHeader*>(&buffer[0]);
|
|
// fhead->message_size = buffer.size();
|
|
// fhead->encoding_type = 0xCAFE;
|
|
// Msg msg;
|
|
// msg.wrapAndApplyHeader((char*)&buffer, sizeof(FramingHeader), buffer.size());
|
|
// return msg;
|
|
// }
|
|
|
|
// void do_negotiate() {
|
|
// // add 2 bytes for empty 'credentials' (39008) length
|
|
// auto buffer = mkbuffer<iLinkBinary::Negotiate500,2>();
|
|
// auto msg = frame_message<iLinkBinary::Negotiate500>(buffer);
|
|
|
|
// // msg.customerFlow(); // constant
|
|
// // msg.hMACVersion(); // constant
|
|
// msg.uUID(1111);
|
|
// msg.requestTimestamp(9999);
|
|
// msg.putSession("NGG");
|
|
// // sets the length of the credential block to ushort 00
|
|
// msg.putCredentials("");
|
|
// // pad with 0 at the front *and* null at the back
|
|
// const std::string_view firm = "004";
|
|
// // this must use the std::string_view overload
|
|
// // the char* overload does not pad correctly
|
|
// msg.putFirm(firm);
|
|
// const auto ld = glbx::calculate_hmac(
|
|
// msg.getSessionAsString(),
|
|
// std::to_string(msg.requestTimestamp()),
|
|
// std::to_string(msg.uUID()),
|
|
// msg.getSessionAsString(),
|
|
// firm.data()
|
|
// );
|
|
// msg.putAccessKeyID(ld.access_key);
|
|
// msg.putHMACSignature(ld.hmac);
|
|
|
|
// // connection->send({ buffer.data(), buffer.size() });
|
|
|
|
// auto fd = fopen("/home/eric/old.bin", "wb");
|
|
// if (fd) {
|
|
// fwrite(buffer.data(), 1, buffer.size(), fd);
|
|
// fclose(fd);
|
|
// }
|
|
// }
|
|
|
|
//////////////////////////////////////////
|
|
// This code worked once
|
|
//////////////////////////////////////////
|
|
}}//namespace
|