181 lines
5.7 KiB
C++
181 lines
5.7 KiB
C++
#include "ExchangeSimApplication.h"
|
|
|
|
#include <quickfix/FileStore.h>
|
|
#include <quickfix/SocketAcceptor.h>
|
|
#include <quickfix/SessionSettings.h>
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
#include "lib/os/os.h"
|
|
#include "lib/config/protos/Ports.pb.h"
|
|
#include "lib/config/protos/ExchangeSimCfg.pb.h"
|
|
#include "lib/config/config.h"
|
|
|
|
#include <quickfix/FileLog.h>
|
|
#include "lib/utils/bts_chrono.h"
|
|
#include "lib/md/relay/RelayClient.h"
|
|
#include "lib/domain_model/Security.pb.h"
|
|
#include "lib/md/relay/RelayRecorder.h"
|
|
#include "lib/md/IntPrice.h"
|
|
|
|
#include <set>
|
|
|
|
namespace bts {
|
|
namespace sim {
|
|
auto cfg =
|
|
"[DEFAULT]\n"
|
|
"ConnectionType=acceptor\n"
|
|
"SocketAcceptPort=$EXCHANGE_SIM_FIX_PORT\n"
|
|
"SocketReuseAddress=Y\n"
|
|
"StartTime=07:50:00\n"
|
|
"EndTime=07:49:00\n"
|
|
"FileLogPath=$BTS_DATA_PATH/exchange_sim/log\n"
|
|
"FileStorePath=$BTS_DATA_PATH/exchange_sim/store\n"
|
|
"HeartBtInt=30\n"
|
|
"[SESSION]\n"
|
|
"BeginString=FIX.4.2\n"
|
|
"SenderCompID=BTS\n"
|
|
"TargetCompID=SPARK\n"
|
|
"UseDataDictionary=N\n"
|
|
"ValidateUserDefinedFields=N\n"
|
|
"ValidateFieldsOutOfOrder=N\n";
|
|
|
|
md::PriceSize coerce_precision(const co2::PriceSizeMessage& p) {
|
|
md::IntPrice raw = { p.mantissa(), (int8_t)p.exponent() };
|
|
auto price = md::IntPrice_from_double<ex_sim::precision>(raw.price());
|
|
return { price, p.size() };
|
|
}
|
|
|
|
struct MDPlayer : md::IRelayWriter
|
|
{
|
|
MDPlayer (IMDPlayerObserver &obs,std::map<uint32_t,md::Security> &known_securities) :
|
|
obs (obs),
|
|
known_securities(known_securities),
|
|
forward_implied(config::get<config::ExchangeSim>().forward_implied())
|
|
{}
|
|
|
|
|
|
void write(const co2::MDReply& data, bool* ) override {
|
|
for (auto const &ch : data.channels())
|
|
for (auto const &cr : ch.updates()) {
|
|
auto it = known_securities.find(cr.key());
|
|
|
|
api::Security const *sec_ptr (nullptr);
|
|
if (it == known_securities.end()) {
|
|
bts_assert(cr.has_security());
|
|
sec_ptr = &cr.security();
|
|
known_securities[cr.key()] = *sec_ptr;
|
|
obs.on_new_security(*sec_ptr);
|
|
}
|
|
else
|
|
sec_ptr = &it->second;
|
|
|
|
auto const &book = cr.direct();
|
|
md::PriceSize bids[md::COCONUT_MAX_DEPTH], asks[md::COCONUT_MAX_DEPTH];
|
|
|
|
size_t i = 0;
|
|
for (auto &b : book.bids())
|
|
bids[i++] = md::PriceSize({b.mantissa(),(int8_t)b.exponent()},b.size());
|
|
|
|
size_t n = 0;
|
|
for (auto &a : book.asks())
|
|
asks[n++] = md::PriceSize({a.mantissa(),(int8_t)a.exponent()},a.size());
|
|
|
|
|
|
std::array<md::PriceSize, md::COCONUT_MAX_IMPLIED_DEPTH> implied_bids, implied_asks;
|
|
bool has_implied = false;
|
|
{size_t x = 0;
|
|
for (const auto& b : cr.implied().bids()) {
|
|
implied_bids[x] = coerce_precision(b);
|
|
x += 1;
|
|
has_implied = true;
|
|
}}
|
|
{size_t x = 0;
|
|
for (const auto& b : cr.implied().asks()) {
|
|
implied_asks[x] = coerce_precision(b);
|
|
x += 1;
|
|
has_implied = true;
|
|
}}
|
|
|
|
if (has_implied && forward_implied) {
|
|
obs.on_depth(*sec_ptr, bids, asks, &implied_bids, &implied_asks);
|
|
}
|
|
else {
|
|
obs.on_depth(*sec_ptr, bids, asks, nullptr, nullptr);
|
|
}
|
|
|
|
if (cr.has_misc()) {
|
|
const auto& s = cr.misc();
|
|
obs.on_high(*sec_ptr, s.session_high());
|
|
obs.on_low(*sec_ptr, s.session_low());
|
|
obs.on_open(*sec_ptr, s.open_price());
|
|
obs.on_close(*sec_ptr, s.close_price());
|
|
if (cr.misc().has_settlement())
|
|
obs.on_settlement(*sec_ptr, { s.settlement().mantissa(), (int8_t)s.settlement().exponent() });
|
|
}
|
|
}
|
|
}
|
|
IMDPlayerObserver &obs;
|
|
std::map<uint32_t, md::Security> &known_securities;
|
|
bool forward_implied;
|
|
};
|
|
|
|
|
|
int start_exchange_sim()
|
|
{
|
|
try
|
|
{
|
|
auto str_settings = std::string(cfg);
|
|
boost::replace_all(str_settings,"$BTS_DATA_PATH", os::bts_data_path());
|
|
boost::replace_all(str_settings,"$EXCHANGE_SIM_FIX_PORT",
|
|
std::to_string(
|
|
config::get<config::Ports>().exchange_sim_fix_port()));
|
|
|
|
std::istringstream str{str_settings};
|
|
FIX::SessionSettings settings( str );
|
|
|
|
ex_sim::ExchangeSimApplication application;
|
|
FIX::FileStoreFactory storeFactory( settings );
|
|
FIX::FileLogFactory logFactory( settings );
|
|
FIX::SocketAcceptor acceptor( application, storeFactory, settings, logFactory );
|
|
|
|
acceptor.start();
|
|
|
|
std::map<uint32_t,md::Security> known_securities;
|
|
const auto& conf = config::get<config::ExchangeSim>();
|
|
while ( true ) {
|
|
MDPlayer player(application,known_securities);
|
|
switch(conf.source_case()) {
|
|
case config::ExchangeSim::SOURCE_NOT_SET:
|
|
case config::ExchangeSim::kReplayUrl:
|
|
md::replay_url_with_fallback(conf.replay_url(),
|
|
player,
|
|
config::get<md::RelayOptions>().naggle_ms());
|
|
break;
|
|
case config::ExchangeSim::kReplayFile:
|
|
md::replay_file(conf.replay_file(),
|
|
player,
|
|
config::get<md::RelayOptions>().naggle_ms(), true);
|
|
break;
|
|
case config::ExchangeSim::kLocalRelay: {
|
|
md::local_relay(player);
|
|
}
|
|
break;
|
|
|
|
}
|
|
}
|
|
acceptor.stop();
|
|
return 0;
|
|
}
|
|
catch ( std::exception & e )
|
|
{
|
|
std::cout << e.what() << std::endl;
|
|
return 1;
|
|
}
|
|
}
|
|
}}
|
|
|