610 lines
19 KiB
C++
610 lines
19 KiB
C++
#include "CMECert.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/orderman/ilink3/IL3Dispatcher.h"
|
|
#include "lib/orderman/ilink3/IL3GW.h"
|
|
#include "lib/orderman/ilink3/IL3Reactor.h"
|
|
#include "lib/orderman/ilink3/ILink3Utils.h"
|
|
#include "lib/os/os.h"
|
|
#include "lib/utils/Dispatcher.h"
|
|
#include "lib/utils/SecureLogon.h"
|
|
#include "lib/utils/TimeStamp.h"
|
|
#include <chrono>
|
|
#include <fstream>
|
|
#include <future>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
namespace bts::cmds {
|
|
|
|
using namespace orderman::ilink3;
|
|
using namespace api::orderman;
|
|
using namespace bts::orderman;
|
|
|
|
void beginning_of_week_logon(IL3GW &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{TimeKeeper().now(), 1, 0}, 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;
|
|
Price fsettle1;
|
|
api::Security future2;
|
|
Price fsettle2;
|
|
api::Security spread1;
|
|
Price spread_settle;
|
|
};
|
|
|
|
Securities &securities() {
|
|
api::Security future1;
|
|
future1.set_id(100);
|
|
future1.mutable_glbx()->set_security_desc("0WSH1");
|
|
future1.mutable_glbx()->set_exchange_id(997820);
|
|
api::Security future2;
|
|
future2.set_id(101);
|
|
future2.mutable_glbx()->set_security_desc("0WSV0");
|
|
future2.mutable_glbx()->set_exchange_id(998140);
|
|
api::Security spread;
|
|
spread.set_id(200);
|
|
spread.mutable_glbx()->set_security_desc("0WSV0-0WSH1");
|
|
spread.mutable_glbx()->set_exchange_id(997800);
|
|
static Securities stable = {future1, Price{3674}, future2,
|
|
Price{0.5}, spread, Price{-159}};
|
|
return stable;
|
|
}
|
|
|
|
struct TestRunner {
|
|
IL3Reactor &reactor;
|
|
IL3GW &gw;
|
|
TestRunner(IL3Reactor &reactor, IL3GW &gw) : reactor(reactor), gw(gw) {}
|
|
|
|
private:
|
|
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 interactive_delay() {
|
|
// run until a key is pressed
|
|
std::future<void> res = std::async([]() {
|
|
std::string x;
|
|
std::cin >> x;
|
|
});
|
|
LOG(OrderMan, Error, "enter a character and hit enter: \n");
|
|
do {
|
|
reactor.do_work();
|
|
} while (res.wait_for(std::chrono::milliseconds(1)) !=
|
|
std::future_status::ready);
|
|
}
|
|
|
|
void sequence_keepalive_lapse() {
|
|
run(32);
|
|
gw.send_sequence(true);
|
|
gw.flush();
|
|
run(29);
|
|
gw.send_terminate(20);
|
|
gw.flush();
|
|
}
|
|
|
|
const RoutingRecord &get_rr() {
|
|
static api::orderman::RoutingRecord rr;
|
|
rr.set_account("CustAccount");
|
|
// rr.set_clearing_firm("004");
|
|
// rr.set_operator_id("NGG"); // SenderID : 48
|
|
rr.set_sender_compid("NGG004");
|
|
rr.set_customer_or_firm(api::orderman::CUSTOMER_OR_FIRM_CUSTOMER);
|
|
// rr.set_smp_id("12345678");
|
|
rr.clear_smp_id();
|
|
rr.set_smp_instruction(
|
|
api::orderman::SELF_MATCH_PREVENTION_INSTRUCTION_CANCEL_AGRESSING);
|
|
rr.set_location("US");
|
|
rr.set_cti_code(api::orderman::CTICode::CTI_CODE_CTI_CODE2);
|
|
rr.set_cust_order_handing_inst("H");
|
|
return rr;
|
|
}
|
|
|
|
CreateOrderMsg createOrderDefault(const api::Security *security = nullptr) {
|
|
if (security == nullptr) {
|
|
security = &securities().future2;
|
|
}
|
|
const auto &rr = get_rr();
|
|
return CreateOrderMsg{
|
|
OrderKey{99},
|
|
// Price { 81560 },
|
|
securities().fsettle2, Quantity{120}, security, api::SIDE_BUY,
|
|
// Price { 81560 },
|
|
securities().fsettle2, Quantity{120},
|
|
api::orderman::NativeOrderType::NATIVE_ORDER_TYPE_LIMIT,
|
|
api::orderman::TIME_IN_FORCE_DAY,
|
|
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO, &rr};
|
|
}
|
|
|
|
void incrementKey(CreateOrderMsg &msg) {
|
|
msg.key = OrderKey{msg.key.value() + 1};
|
|
}
|
|
|
|
void send_order(CreateOrderMsg &order) {
|
|
bts_assert(gw.state() == State::READY);
|
|
incrementKey(order);
|
|
gw.send_order(order, {});
|
|
gw.flush();
|
|
}
|
|
|
|
void send_modify(CreateOrderMsg &create, const ExecutionReport *report) {
|
|
bts_assert(gw.state() == State::READY);
|
|
CancelReplaceOrderMsg msg{
|
|
create, std::get<ExchangeOrderID64>(report->exchange_order_id)};
|
|
gw.send_cancel_replace(msg, {});
|
|
gw.flush();
|
|
}
|
|
|
|
void send_cancel(const ExecutionReport *last_report) {
|
|
LOG(OrderMan, Error, "sending cancel");
|
|
auto rr = get_rr();
|
|
gw.send_cancel(
|
|
CancelOrderMsg{
|
|
last_report->order_key,
|
|
std::get<ExchangeOrderID64>(last_report->exchange_order_id),
|
|
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO,
|
|
last_report->security, &rr, last_report->side},
|
|
{});
|
|
}
|
|
|
|
void bootup() {
|
|
run_until([&]() { return gw.state() == State::READY; });
|
|
LOG(OrderMan, Info, "HIT KEY");
|
|
interactive_delay();
|
|
LOG(OrderMan, Info, "Starting test");
|
|
}
|
|
|
|
public:
|
|
void scratch_space() { bootup(); }
|
|
|
|
void msgw_failover(int step) {
|
|
LOG(OrderMan, Info, "WAIT FOR SEQUENCE MSG");
|
|
bootup();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
|
|
// Instructions for running this test
|
|
// Set the connection function to connect to the primary gw
|
|
// ssh -nNT -L 64530:69.50.112.199:64530 bts@10.3.10.16
|
|
// run msg_failover(1)
|
|
// After disconnect
|
|
// 1) Go to ILinkGW.cpp:500 and change line 500 to replay from message 4
|
|
// instead of 1
|
|
// enqueue_retransmit(4, cme_prev_seq, cme_prev_uuid);
|
|
// 2) change this file to run msg_failover(2)
|
|
// 3) change the tunnel to use the backup segment 99 ip (I just switch the
|
|
// tunnel)
|
|
// ssh -nNT -L 64530:69.50.112.205:64530 bts@10.3.10.16
|
|
|
|
if (step == 1) {
|
|
auto order = createOrderDefault();
|
|
order.time_in_force = TimeInForce::TIME_IN_FORCE_DAY;
|
|
send_order(order);
|
|
if (!run_until([&]() { return reports.size() >= 1; }))
|
|
return;
|
|
order.key = OrderKey{order.key.value() + 1};
|
|
send_order(order);
|
|
LOG(OrderMan, Info, "SWITCH TO BACKUP. DELETE SESSION");
|
|
// expect a terminate
|
|
run();
|
|
} else {
|
|
LOG(OrderMan, Info, "WAIT FOR RETRANSMIT");
|
|
interactive_delay();
|
|
auto order = createOrderDefault();
|
|
order.key = OrderKey{1024};
|
|
send_order(order);
|
|
if (!run_until([&]() { return reports.size() >= 1; }))
|
|
return;
|
|
LOG(OrderMan, Info, "WAIT FOR SEQUENCE MSG");
|
|
interactive_delay();
|
|
gw.send_terminate();
|
|
gw.flush();
|
|
run();
|
|
}
|
|
}
|
|
|
|
void large_message_gaps() {
|
|
// Handles:
|
|
// Mid week Connection (Binding without Initialization) for On-Demand
|
|
// Format Processing Message Gaps of More than 2500 Messages for On-Demand
|
|
// Format Receiving and Processing Multiple Gaps for On-Demand Format
|
|
// Bi-Directional Gap Scenario (Sequence Gap Fill) - (Day or FAK/FOK
|
|
// orders) for On-Demand Format
|
|
// - Bi-Directional has an issue where it fails if you send the orders
|
|
// "too fast"
|
|
bootup();
|
|
// Send 3 orders qty 425
|
|
auto order = createOrderDefault();
|
|
order.display_quantity = order.quantity = Quantity{1};
|
|
// order.time_in_force = TimeInForce::TIME_IN_FORCE_IOC;
|
|
send_order(order);
|
|
// interactive_delay();
|
|
send_order(order);
|
|
// interactive_delay();
|
|
send_order(order);
|
|
// interactive_delay();
|
|
send_order(order);
|
|
run();
|
|
}
|
|
|
|
void connection_with_gt_orders() {
|
|
bootup();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
|
|
// Test retransmit
|
|
auto order = createOrderDefault();
|
|
order.time_in_force = api::orderman::TimeInForce::TIME_IN_FORCE_GTC;
|
|
send_order(order);
|
|
|
|
interactive_delay();
|
|
}
|
|
|
|
void manage_maximum_quantity_display2() {
|
|
LOG(OrderMan, Info,
|
|
" Manage Maximum Quantity Display with Cancel-Replace for On-Demand "
|
|
"Format ");
|
|
bootup();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
auto order = createOrderDefault();
|
|
order.quantity = Quantity{6};
|
|
order.display_quantity = Quantity{3};
|
|
send_order(order);
|
|
interactive_delay();
|
|
// increase quantity and display quantity by 1
|
|
order.quantity = Quantity{order.quantity.value() + 1};
|
|
order.display_quantity = Quantity{order.display_quantity.value() + 1};
|
|
send_modify(order, reports.back());
|
|
interactive_delay();
|
|
order.display_quantity = Quantity{order.display_quantity.value() - 1};
|
|
send_modify(order, reports.back());
|
|
run();
|
|
}
|
|
|
|
void manage_maximum_quantity_display() {
|
|
LOG(OrderMan, Info, " Cancel Replace with IFM for On-Demand Format ");
|
|
bootup();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
auto order = createOrderDefault();
|
|
order.quantity = Quantity{6};
|
|
order.display_quantity = Quantity{3};
|
|
send_order(order);
|
|
run();
|
|
}
|
|
|
|
void cancel_replace_with_ifm() {
|
|
LOG(OrderMan, Info, " Cancel Replace with IFM for On-Demand Format ");
|
|
bootup();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
auto order = createOrderDefault();
|
|
send_order(order);
|
|
// get partial fill
|
|
interactive_delay();
|
|
// Increase the qty
|
|
order.quantity = order.display_quantity =
|
|
Quantity{order.quantity.value() + 1};
|
|
send_modify(order, reports.back());
|
|
interactive_delay();
|
|
order.quantity = order.display_quantity =
|
|
Quantity{order.quantity.value() - 2};
|
|
send_modify(order, reports.back());
|
|
run();
|
|
}
|
|
|
|
void fak_order_test() {
|
|
bootup();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
auto order = createOrderDefault();
|
|
order.time_in_force = TimeInForce::TIME_IN_FORCE_IOC;
|
|
send_order(order);
|
|
if (!run_until([&]() { return reports.size() >= 2; }))
|
|
return;
|
|
send_order(order);
|
|
run();
|
|
}
|
|
|
|
void spread_comlete_order_test() {
|
|
bootup();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
|
|
// send limit order & get ExecutionReportElimination
|
|
auto order = createOrderDefault(&securities().spread1);
|
|
order.price = securities().spread_settle;
|
|
send_order(order);
|
|
if (!run_until([&]() { return reports.size() >= 1; }))
|
|
return;
|
|
interactive_delay();
|
|
// limit order qty 1
|
|
order.quantity = order.display_quantity = Quantity{1};
|
|
send_order(order);
|
|
// exe report new, trade spread, trade leg x2
|
|
// trade addendum
|
|
LOG(OrderMan, Info, "Wait for trade addendum...");
|
|
interactive_delay();
|
|
// send 4 limit orders qty 1
|
|
for (int x = 0; x < 4; ++x)
|
|
gw.send_order(order, {});
|
|
gw.flush();
|
|
|
|
// receive a ton of shit
|
|
run();
|
|
}
|
|
|
|
// Processing rejects test for on-demand format
|
|
void process_rejects() {
|
|
interactive_delay();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
|
|
// send a limit day order
|
|
auto sec = securities();
|
|
auto order = createOrderDefault(&sec.future2);
|
|
send_order(order);
|
|
// Get an execution report reject
|
|
if (!run_until([&]() { return reports.size() >= 1; }))
|
|
return;
|
|
// send another limit day order
|
|
send_order(order);
|
|
// get a business reject
|
|
LOG(OrderMan, Info, "Press a key after receiving a business reject");
|
|
interactive_delay();
|
|
gw.send_sequence();
|
|
send_order(order);
|
|
LOG(OrderMan, Info,
|
|
"Press a key after receiving a execution report *or* not applied");
|
|
interactive_delay();
|
|
send_order(order);
|
|
reports.clear();
|
|
if (!run_until([&]() { return reports.size() >= 1; }))
|
|
return;
|
|
send_cancel(reports.back());
|
|
// get a cancel reject
|
|
LOG(OrderMan, Info, "Press a key after receiving a cancel reject");
|
|
interactive_delay();
|
|
send_order(order);
|
|
if (!run_until([&]() { return reports.size() >= 2; }))
|
|
return;
|
|
order.quantity = Quantity{order.quantity.value() + 1};
|
|
send_modify(order, reports.back());
|
|
run();
|
|
}
|
|
|
|
void multiple_cancel_replace_for_on_demand_format() {
|
|
bootup();
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
// send limit day order
|
|
auto sec = securities();
|
|
auto security = sec.future2;
|
|
auto order = createOrderDefault(&security);
|
|
order.time_in_force = TimeInForce::TIME_IN_FORCE_DAY;
|
|
send_order(order);
|
|
// get exe report new
|
|
if (!run_until([&]() { return reports.size() >= 1; }))
|
|
return;
|
|
// send cancel/replace - change quantity
|
|
order.quantity = Quantity{order.quantity.value() + 1};
|
|
send_modify(order, reports.back());
|
|
if (!run_until([&]() { return reports.size() >= 2; }))
|
|
return;
|
|
// send cancel/replace - change price
|
|
order.price = Price{order.price.value() + 10};
|
|
send_modify(order, reports.back());
|
|
if (!run_until([&]() { return reports.size() >= 3; }))
|
|
return;
|
|
// exe report
|
|
// send cancel/replace - change price&quantity
|
|
order.price = Price{order.price.value() + 10};
|
|
order.quantity = Quantity{order.quantity.value() + 1};
|
|
send_modify(order, reports.back());
|
|
// exe report
|
|
// fill
|
|
run();
|
|
}
|
|
|
|
// Managed GTC/GTD order lifetime for on-demand format
|
|
void send_order_test() {
|
|
bootup();
|
|
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
run_until([&]() { return gw.state() == State::READY; });
|
|
|
|
// send an order
|
|
auto sec = securities();
|
|
auto security = sec.future2;
|
|
auto order = createOrderDefault(&security);
|
|
order.time_in_force = TimeInForce::TIME_IN_FORCE_GTC;
|
|
send_order(order);
|
|
|
|
// Get a confirm and trade
|
|
auto ok = run_until([&]() { return reports.size() >= 2; });
|
|
if (!ok) {
|
|
return;
|
|
}
|
|
|
|
// Wait 5 seconds so the cme can breathe
|
|
run(5);
|
|
|
|
order.time_in_force = TimeInForce::TIME_IN_FORCE_DATE;
|
|
send_order(order);
|
|
|
|
run();
|
|
}
|
|
|
|
// Market-Limit Order test fr On-Demand format
|
|
// Market Order test fr On-Demand format
|
|
void order_and_cancel_test() {
|
|
interactive_delay();
|
|
LOG(OrderMan, Info, "Starting test");
|
|
|
|
using namespace orderman;
|
|
std::vector<const ExecutionReport *> reports;
|
|
gw.subscribe_execution_reports(
|
|
[&](const ExecutionReport *r) { reports.push_back(r); });
|
|
|
|
// send an order
|
|
auto order = createOrderDefault();
|
|
order.order_type = NATIVE_ORDER_TYPE_MARKET;
|
|
send_order(order);
|
|
|
|
// Get a confirm and trade
|
|
auto ok = run_until([&]() { return reports.size() >= 2; });
|
|
if (!ok) {
|
|
LOG(OrderMan, Error, "Error encountered waiting for reports.");
|
|
return;
|
|
}
|
|
|
|
// Wait 10 seconds so the cme can breathe
|
|
run(10);
|
|
|
|
LOG(OrderMan, Error, "sending cancel");
|
|
auto rr = get_rr();
|
|
if (!reports.empty()) {
|
|
auto last_report = reports.back();
|
|
gw.send_cancel(
|
|
CancelOrderMsg{
|
|
last_report->order_key,
|
|
std::get<ExchangeOrderID64>(last_report->exchange_order_id),
|
|
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO,
|
|
last_report->security, &rr, last_report->side},
|
|
{});
|
|
}
|
|
// run_until([&]() { return reports.size() >= 3; });
|
|
run();
|
|
}
|
|
|
|
void semi_automated() {
|
|
interactive_delay();
|
|
auto order = createOrderDefault();
|
|
order.manual_order_indicator =
|
|
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_YES;
|
|
send_order(order);
|
|
interactive_delay();
|
|
order.manual_order_indicator =
|
|
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO;
|
|
send_order(order);
|
|
run();
|
|
}
|
|
|
|
void primary_gateway_enforcement() {
|
|
// success!
|
|
run(30);
|
|
run(30);
|
|
gw.send_terminate();
|
|
gw.flush();
|
|
}
|
|
};
|
|
|
|
CMECert::CMECert() : CLI::App("Tool to drive CME Cert", "cme-cert") {
|
|
callback([] {
|
|
// Typically a timestamp
|
|
SessionSaver ss;
|
|
ss.load();
|
|
Config config{"NGG", "004", false, [](auto, auto, auto) {}};
|
|
IL3Reactor reactor;
|
|
IL3MessageFramer framer;
|
|
auto &connection = reactor.register_connection(
|
|
{// local dev tunnel
|
|
TCPEndpoint{"127.0.0.1", 64530}, TCPEndpoint{"127.0.0.1", 64531},
|
|
// Direct connection
|
|
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
|
|
|
|
IL3GW gw(connection, framer, nullptr, ss.current, config, {});
|
|
gw.subscribe_session([&](auto &s) { ss.session_changed(s); });
|
|
gw.connect();
|
|
|
|
TestRunner run(reactor, gw);
|
|
|
|
///////////////////////////
|
|
//
|
|
// The current test
|
|
//
|
|
///////////////////////////
|
|
|
|
run.scratch_space();
|
|
|
|
///////////////////////////
|
|
|
|
for (auto x = 0; x < 5; ++x) {
|
|
reactor.do_work();
|
|
sleep(1);
|
|
}
|
|
gw.send_terminate();
|
|
gw.flush();
|
|
});
|
|
}
|
|
|
|
} // namespace bts::cmds
|