This commit is contained in:
Pedro Pinto committed 2017-09-02 17:13:44 -04:00
1 parent af9d2543c3
commit 293968407a
13 files changed
+559 -321

No files matched your search

+24
View File
@@ -67,6 +67,7 @@ message OrderError
DUPLICATE_ORDER = 4;
UNKNOWN_ORDER = 5;
UNKNOWN_SECURITY = 6;
ORDER_NOT_ACK = 7;
}
}
@@ -80,3 +81,26 @@ message CTICode
CTT4 = 52 ;
}
}
message ExecType
{
enum Type {
UNDEFINED = 0 ;
NEW = 48 ;
PARTIAL_FILL = 49 ;
FILL = 50 ;
DONE = 51 ;
CANCELED = 52 ;
REPLACED = 53 ;
PENDING_CANCEL = 54 ;
STOPPED = 55 ;
REJECTED = 56 ;
SUSPENDED = 57 ;
PENDING_NEW = 65 ;
CALCULATED = 66 ;
EXPIRED = 67 ;
RESTATED = 68 ;
PENDING_REPLACE = 69 ;
CANCEL_ACK = 72 ;
}
}
+1
View File
@@ -58,6 +58,7 @@ set (Sparkd_Sources
# orderman
order_man/OrderMan.cpp
order_man/IOrderObserver.cpp
order_man/WorkingOrder.cpp
# md, mdp3 onixs platform
# md/platforms/mdp3_onixs/MDP3Listener.cpp
@@ -94,6 +94,7 @@ typedef CharField<150> ExecType;
typedef StringField<11> ClOrdID;
typedef CharField<54> Side;
typedef FloatField<31> LastPx;
typedef FloatField<6> AvgPx;
typedef IntField<32> LastShares;
typedef IntField<14> CumQty;
typedef StringField<527> SecondaryExecID;
@@ -23,17 +23,25 @@ typedef FIXMsg<MessageTypes::ExecutionReport,
ExecType,
ClOrdID,
Side,
OrderID,
TimeInForce,
LastPx,
AvgPx,
Price,
LastShares,
CumQty,
LeavesQty,
ExecID,
ManualOrderIndicator,
OrdType,
OrdStatus,
OrderQty,
SecurityID,
MultiLegReportingType,
SecurityDesc,
Text,
TargetSubID,
TransactTime>
TransactTimeStr>
ExecutionReportMsg;
typedef FIXMsg<MessageTypes::BusinessMessageReject,Text>
@@ -12,6 +12,7 @@ class IFIXGateway
{
public:
virtual ~IFIXGateway();
virtual jetfix::OutFIXPtr make_outfix_buffer();
virtual void send(jetfix::OutFIXPtr const&msg) = 0;
virtual std::string const &name() const = 0;
};
+7 -1
View File
@@ -8,7 +8,13 @@ namespace orderman {
class OrderCancel
{
public:
OrderKey order_key() const;
const api::Security *security;
OrderKey order_key;
CustomerOrFirm::Type customer_or_firm;
CTICode::Type cti_code;
UserLogin user_login;
ManualOrderIndicator manual_order_indicator;
OrderTimeStamp transact_time;
};
}}
+14 -28
View File
@@ -14,34 +14,20 @@ namespace orderman {
class OrderCreate
{
public:
api::Security const&security() const {return *_security;}
SecurityKey security_key() const {return get_sec_key(_order_key);};
AccountName const &account() const {return _account_name;};
OperatorID const&operator_id() const {return _operator_id;};
OrderKey order_key() const {return _order_key;};
api::BuySell side() const {return _side;};
TimeInForce::Type time_in_force() const {return _time_in_force;};
OrderType::Type order_type() const {return _order_type;};
CustomerOrFirm::Type costumer_or_firm() const {return _customer_or_firm;}
CTICode::Type cti_code() const {return _cti_code;}
UserLogin const &user_login() const {return _user_login;}
ManualOrderIndicator manual_order_indicator() const {return _manual_order_indicator;}
OrderPrice price() const {return _price;} // 0 for market orders
OrderQuantity quantity() const {return _quantity;}
private:
const api::Security *_security;
AccountName _account_name;
OperatorID _operator_id;
OrderKey _order_key;
api::BuySell _side;
TimeInForce::Type _time_in_force;
OrderType::Type _order_type;
CustomerOrFirm::Type _customer_or_firm;
CTICode::Type _cti_code;
UserLogin _user_login;
ManualOrderIndicator _manual_order_indicator;
OrderPrice _price;
OrderQuantity _quantity;
const api::Security *security;
AccountName account_name;
OperatorID operator_id;
OrderKey order_key;
api::BuySell side;
TimeInForce::Type time_in_force;
OrderType::Type order_type;
CustomerOrFirm::Type customer_or_firm;
CTICode::Type cti_code;
UserLogin user_login;
ManualOrderIndicator manual_order_indicator;
OrderPrice price;
OrderQuantity quantity;
OrderTimeStamp transact_time;
};
}}
+177 -147
View File
@@ -1,178 +1,208 @@
// #include "OrderMan.h"
#include "OrderMan.h"
// #include "WorkingOrder.h"
// #include "OrderCreate.h"
// #include "OrderUpdate.h"
// #include "OrderState.h"
// #include "OrderCancel.h"
// #include "IFIXGateway.h"
// #include "WorkingOrder.h"
// #include "IOrderObserver.h"
#include "WorkingOrder.h"
#include "OrderCreate.h"
#include "OrderUpdate.h"
#include "OrderState.h"
#include "OrderCancel.h"
#include "IFIXGateway.h"
#include "WorkingOrder.h"
#include "IOrderObserver.h"
// #include "lib/utils/bts_assert.h"
// #include "lib/logging/logging.h"
#include "lib/utils/bts_assert.h"
#include "lib/logging/logging.h"
#include "server/sparkd/jetfix/common/OutFIX.h"
// #include <boost/container/flat_map.hpp>
#include <boost/container/flat_map.hpp>
// namespace bts {
// namespace orderman {
namespace bts {
namespace orderman {
// enum class OrderMan::State { DISCONNECTED, REPLAYING, READY };
// using WorkingOrderPtr = std::unique_ptr<WorkingOrder>;
// using OrderMap = boost::container::flat_map<OrderKey,WorkingOrderPtr> ;
enum class OrderMan::State { DISCONNECTED, REPLAYING, READY };
using WorkingOrderPtr = std::unique_ptr<WorkingOrder>;
using OrderMap = boost::container::flat_map<OrderKey,WorkingOrderPtr> ;
// struct OrderMan::PImpl
// {
// PImpl(std::vector<OrderState> const &working_orders,
// IOrderObserver &order_observer,
// IFIXGateway &gw) :
// state (State::DISCONNECTED),
// order_observer(order_observer),
// gw(gw),
// order_map()
// {
// for (auto const&o : working_orders)
// order_map[o.order_key()] = std::make_unique<WorkingOrder> (o);
// }
struct OrderMan::PImpl
{
PImpl(std::vector<OrderState> const &working_orders,
IOrderObserver &order_observer,
IFIXGateway &gw) :
state (State::DISCONNECTED),
order_observer(order_observer),
gw(gw),
order_map()
{
for (auto const&o : working_orders)
order_map[o.order_key] = std::make_unique<WorkingOrder> (o);
}
// State state;
// IOrderObserver &order_observer;
// IFIXGateway &gw;
// OrderMap order_map;
// };
State state;
IOrderObserver &order_observer;
IFIXGateway &gw;
OrderMap order_map;
};
// OrderMan::OrderMan (std::vector<OrderState> const &working_orders,
// IOrderObserver &order_observer,
// IFIXGateway &gw) : me (new PImpl(working_orders,order_observer,gw))
// {
// }
OrderMan::OrderMan (std::vector<OrderState> const &working_orders,
IOrderObserver &order_observer,
IFIXGateway &gw) : me (new PImpl(working_orders,order_observer,gw))
{
}
// OrderMan::~OrderMan () {}
OrderMan::~OrderMan () {}
// OrderMan::CreateResult OrderMan::create_order(OrderCreate const &create)
// {
// if (create.order_type () != OrderType::LIMIT)
// return CreateResult { OrderError::UNSUPPORTED_ORDER_TYPE};
OrderMan::CreateResult OrderMan::create_order(OrderCreate const &create)
{
if (create.order_type != OrderType::LIMIT)
return CreateResult { OrderError::UNSUPPORTED_ORDER_TYPE};
// if (me->state != State::READY)
// return CreateResult { OrderError::VENUE_NOT_READY};
if (me->state != State::READY)
return CreateResult { OrderError::VENUE_NOT_READY};
// if (me->order_map.find(create.order_key()) != me->order_map.end())
// return CreateResult { OrderError::DUPLICATE_ORDER};
if (me->order_map.find(create.order_key) != me->order_map.end())
return CreateResult { OrderError::DUPLICATE_ORDER};
// auto ord = std::make_unique<WorkingOrder> (create);
// me->gw.send(ord->encode_create(create));
// me->order_observer.on_order_change(ord->state());
// me->order_map[create.order_key()] = std::move(ord);
// return CreateResult {OrderError::OK};
// }
auto ord = std::make_unique<WorkingOrder> (create);
auto fix_buffer = me->gw.make_outfix_buffer();
auto error = ord->encode_create(create,&fix_buffer->fix[0]);
if (error == OrderError::OK) {
me->gw.send(fix_buffer);
me->order_observer.on_order_change(ord->state());
me->order_map[create.order_key] = std::move(ord);
return CreateResult {OrderError::OK};
}
else
return CreateResult {error};
}
// OrderMan::UpdateResult OrderMan::update_order(OrderUpdate const&update)
// {
// if (me->state != State::READY)
// return UpdateResult {OrderError::VENUE_NOT_READY};
OrderMan::UpdateResult OrderMan::update_order(OrderUpdate const&update)
{
if (me->state != State::READY)
return UpdateResult {OrderError::VENUE_NOT_READY};
// auto it = me->order_map.find(update.order_key());
// if (it == me->order_map.end())
// return UpdateResult {OrderError::UNKNOWN_ORDER};
auto it = me->order_map.find(update.order_key);
if (it == me->order_map.end())
return UpdateResult {OrderError::UNKNOWN_ORDER};
// auto &o = it->second;
// me->gw.send(o->encode_update(update));
// return UpdateResult {OrderError::OK};
// }
auto &o = it->second;
auto fix_buffer = me->gw.make_outfix_buffer();
auto error = o->encode_update(update,&fix_buffer->fix[0]);
if (error == OrderError::OK) {
me->gw.send(fix_buffer);
return UpdateResult {OrderError::OK};
}
else
return UpdateResult {error};
}
// OrderMan::CancelResult OrderMan::cancel_order(OrderCancel const &cancel)
// {
// if (me->state != State::READY)
// return CancelResult {OrderError::VENUE_NOT_READY};
OrderMan::CancelResult OrderMan::cancel_order(OrderCancel const &cancel)
{
if (me->state != State::READY)
return CancelResult {OrderError::VENUE_NOT_READY};
// auto it = me->order_map.find(cancel.order_key());
// if (it == me->order_map.end())
// return CancelResult {OrderError::UNKNOWN_ORDER};
auto it = me->order_map.find(cancel.order_key);
if (it == me->order_map.end())
return CancelResult {OrderError::UNKNOWN_ORDER};
// auto &o = it->second;
// me->gw.send(o->encode_cancel(cancel));
// return CancelResult {OrderError::OK};
// }
auto &o = it->second;
auto fix_buffer = me->gw.make_outfix_buffer();
auto error = o->encode_cancel(cancel,&fix_buffer->fix[0]);
if (error == OrderError::OK) {
me->gw.send(fix_buffer);
return CancelResult {OrderError::OK};
}
else
return CancelResult {error};
}
// void OrderMan::on_venue_ready()
// {
// bts_assert(me->state == State::DISCONNECTED);
// me->state = State::READY;
// LOG(OrderMan,Warn, "Gateway {} is ready", me->gw.name());
// }
void OrderMan::on_venue_ready()
{
bts_assert(me->state == State::DISCONNECTED);
me->state = State::READY;
LOG(OrderMan,Warn, "Gateway {} is ready", me->gw.name());
}
// void OrderMan::on_venue_disconnect()
// {
// bts_assert(me->state == State::READY);
// me->state = State::DISCONNECTED;
// LOG(OrderMan,Warn, "Gateway {} disconnected", me->gw.name());
// }
void OrderMan::on_venue_disconnect()
{
bts_assert(me->state == State::READY);
me->state = State::DISCONNECTED;
LOG(OrderMan,Warn, "Gateway {} disconnected", me->gw.name());
}
// void OrderMan::on_begin_replay()
// {
// bts_assert(me->state == State::DISCONNECTED);
// me->state = State::REPLAYING;
// LOG(OrderMan,Warn, "Begin FIX message replay on {}", me->gw.name());
// }
void OrderMan::on_begin_replay()
{
bts_assert(me->state == State::DISCONNECTED);
me->state = State::REPLAYING;
LOG(OrderMan,Warn, "Begin FIX message replay on {}", me->gw.name());
}
// void OrderMan::on_end_replay()
// {
// bts_assert(me->state == State::DISCONNECTED);
// me->state = State::DISCONNECTED;
// LOG(OrderMan,Warn, "FIX message replay complete on {}", me->gw.name());
// std::vector<OrderState> all_states;
// for (auto &o : me->order_map)
// all_states.emplace_back(o.second->state());
// me->order_observer.on_order_change(all_states);
// }
void OrderMan::on_end_replay()
{
bts_assert(me->state == State::DISCONNECTED);
me->state = State::DISCONNECTED;
LOG(OrderMan,Warn, "FIX message replay complete on {}", me->gw.name());
std::vector<OrderState> all_states;
for (auto &o : me->order_map)
all_states.emplace_back(o.second->state());
me->order_observer.on_order_change(all_states);
}
// void OrderMan::on_exe_report(jetfix::fix::ExecutionReportMsg const&msg)
// {
// using namespace jetfix;
// auto order_key = OrderKey(std::stoi(msg.get<fix::ClOrdID>()));
// LOG(OrderMan,Info, "Received execution report {} for order {} on {}",
// msg.get<fix::ExecID>(),
// order_key.value(),me->gw.name());
void OrderMan::on_exe_report(jetfix::fix::ExecutionReportMsg const&msg)
{
using namespace jetfix;
auto order_key = OrderKey(std::stoi(msg.get<fix::ClOrdID>()));
LOG(OrderMan,Info, "Received execution report {} for order {} on {}",
msg.get<fix::ExecID>(),
order_key.value(),me->gw.name());
// bool order_changed = false;
bool order_changed = false;
// auto it = me->order_map.find(order_key);
// if (it == me->order_map.end()) {
// LOG(OrderMan,Error,
// "Received execution report for unknown order {} on {}. This should be investigated."
// " Will try to recreate the order from the execution report",
// order_key.value(),me->gw.name());
// it = me->order_map.emplace(order_key, std::make_unique<WorkingOrder>(msg)).first;
// order_changed = true;
// }
// else
// order_changed = it->second->on_exe_report(msg);
// if (order_changed)
// me->order_observer.on_order_change(it->second->state());
// }
auto it = me->order_map.find(order_key);
if (it == me->order_map.end()) {
if (msg.get<fix::ExecType>() == ExecType::NEW) {
LOG(OrderMan,Error,
"Received execution report for unknown order {} on {}. This should be investigated."
" Will try to recreate the order from the execution report",
order_key.value(),me->gw.name());
it = me->order_map.emplace(order_key, std::make_unique<WorkingOrder>(msg)).first;
order_changed = true;
}
else {
LOG(OrderMan,Error,
"Received execution report for unknown order {} on {}. "
" Will not be able to recover the order because the execution report is not an acknowledgment. "
" This should never happen. Please investigate",
order_key.value(),me->gw.name());
order_changed = false;
}
}
else
order_changed = it->second->on_exe_report(msg);
if (order_changed)
me->order_observer.on_order_change(it->second->state());
}
// void OrderMan::on_cancel_reject(jetfix::fix::CancelRejectMsg const&msg)
// {
// using namespace jetfix;
// auto order_key = OrderKey(std::stoi(msg.get<fix::ClOrdID>()));
// LOG(OrderMan,Error, "Received cancel reject {} for order {} on {}",
// msg.get<fix::ExecID>(),
// order_key.value(),me->gw.name());
void OrderMan::on_cancel_reject(jetfix::fix::CancelRejectMsg const&msg)
{
using namespace jetfix;
auto order_key = OrderKey(std::stoi(msg.get<fix::ClOrdID>()));
LOG(OrderMan,Error, "Received cancel reject {} for order {} on {}",
msg.get<fix::ExecID>(),
order_key.value(),me->gw.name());
// auto it = me->order_map.find(order_key);
// if (it == me->order_map.end()) {
// LOG(OrderMan,Error,
// "Received cancel rejection for unknown order {} on {}. This should be investigated.",
// order_key.value(),me->gw.name());
// }
// else {
// auto order_changed = it->second->on_cancel_reject(msg);
// if (order_changed)
// me->order_observer.on_order_change(it->second->state());
// }
// }
auto it = me->order_map.find(order_key);
if (it == me->order_map.end()) {
LOG(OrderMan,Error,
"Received cancel rejection for unknown order {} on {}. This should be investigated."
" This should never happen. Please investigate",
order_key.value(),me->gw.name());
}
else {
auto order_changed = it->second->on_cancel_reject(msg);
if (order_changed)
me->order_observer.on_order_change(it->second->state());
}
}
// }}
}}
+23 -42
View File
@@ -14,51 +14,32 @@ class OrderState
{
public:
// Immutable, always available
AccountName const &account() const {return _account_name;};
OperatorID const&operator_id() const {return _operator_id;};
OrderKey order_key() const {return _order_key;};
SecurityKey security_key() const {return get_sec_key(_order_key);};
api::BuySell side() const {return _side;};
TimeInForce::Type time_in_force() const {return _time_in_force;};
OrderType::Type order_type() const {return _order_type;};
AccountName account_name;
OperatorID operator_id;
OrderKey order_key;
api::BuySell side;
TimeInForce::Type time_in_force;
OrderType::Type order_type;
// Immutable once available, might not always be available
CustomerOrFirm::Type costumer_or_firm() const {return _customer_or_firm;}
CTICode::Type cti_code() const {return _cti_code;}
UserLogin const &user_login() const {return _user_login;}
ExchangeOrderID const& exchange_order_id() const {return _exchange_order_id;}
// Mutable, always available
ManualOrderIndicator manual_order_indicator() const {return _manual_order_indicator;}
OrderPrice price() const {return _price;} // 0 for market orders
OrderQuantity quantity() const {return _quantity;}
OrderStatus::Type status() const {return _status;}
OrderPrice avg_fill_price() const {return _avg_fill_price;}
OrderQuantity filled_quantity() const {return _filled_quantity;}
OrderQuantity remaining_quantity() const {return _remaining_quantity;}
std::string const &reject_msg() const {return _reject_msg;}
CustomerOrFirm::Type customer_or_firm;
CTICode::Type cti_code;
UserLogin user_login;
ExchangeOrderID exchange_order_id;
private:
friend class WorkingOrder;
AccountName _account_name;
OperatorID _operator_id;
OrderKey _order_key;
api::BuySell _side;
TimeInForce::Type _time_in_force;
OrderType::Type _order_type;
CustomerOrFirm::Type _customer_or_firm;
CTICode::Type _cti_code;
UserLogin _user_login;
ExchangeOrderID _exchange_order_id;
ManualOrderIndicator _manual_order_indicator;
OrderPrice _price;
OrderQuantity _quantity;
OrderStatus::Type _status;
OrderPrice _avg_fill_price;
OrderQuantity _filled_quantity;
OrderQuantity _remaining_quantity;
std::string _reject_msg;
// Mutable, always available
ManualOrderIndicator manual_order_indicator;
OrderPrice price;
OrderQuantity quantity;
OrderStatus::Type status;
OrderPrice avg_fill_price;
OrderQuantity filled_quantity;
OrderQuantity remaining_quantity;
std::string reject_msg;
OrderTimeStamp transact_time;
// helper
SecurityKey security_key() const {return get_sec_key(order_key);};
};
}}
+9 -1
View File
@@ -8,7 +8,15 @@ namespace orderman {
class OrderUpdate
{
public:
OrderKey order_key() const;
const api::Security *security;
OrderKey order_key;
CustomerOrFirm::Type customer_or_firm;
CTICode::Type cti_code;
UserLogin user_login;
ManualOrderIndicator manual_order_indicator;
OrderPrice price;
OrderQuantity quantity;
OrderTimeStamp transact_time;
};
}}
+274 -94
View File
@@ -1,120 +1,300 @@
#include "WorkingOrder.h"
#include "server/sparkd/jetfix/code/FIXFragment.h"
#include "server/sparkd/jetfix/codec/FIXFragment.h"
#include "server/sparkd/jetfix/codec/FIXEncodingStream.h"
#include "server/sparkd/jetfix/codec/MessageTypes.h"
#include "lib/order_man/protos/OrderMan.pb.h"
#include "lib/api/services/MarketDataService.pb.h"
#include "OrderState.h"
#include "OrderCreate.h"
#include "OrderUpdate.h"
#include "OrderCancel.h"
namespace bts {
namespace orderman {
using namespace jetfix;
enum class WorkingOrder::InternalState { UNACKNOWLEDGED, RECOVERED,READY };
static auto const PriceFieldSz=20;
static auto const SizeFieldSz=6;
struct WorkingOrder::PImpl
{
PImpl(AccountName const& account_name,
OperatorID const &operator_id,
OrderKey order_key,
api::BuySell side,
TimeInForce::Type time_in_force,
OrderType::Type order_type,
CustomerOrFirm::Type customer_or_firm,
CTICode::Type cti_code,
UserLogin const &user_login,
ExchangeOrderID const &exchange_order_id,
ManualOrderIndicator manual_order_indicator,
OrderPrice price,
OrderQuantity quantity,
OrderStatus::Type status,
OrderPrice avg_fill_price,
OrderQuantity filled_quantity,
OrderQuantity remaining_quantity,
std::string const &reject_msg)
:
state(account_name,
operator_id,
order_key,
side,
time_in_force,
order_type,
customer_or_firm,
cti_code,
user_login,
exchange_order_id,
manual_order_indicator,
price,
quantity,
status,
avg_fill_price,
filled_quantity,
remaining_quantity,
reject_msg)
PImpl()
: order_data(),
internal_state(InternalState::UNACKNOWLEDGED),
update_encoding(),
manual_order_indicator_bm(),
price_bm(),
quantity_bm(),
transact_time_bm()
{}
OrderState state;
OrderState order_data;
InternalState internal_state;
FIXFragment update_encoding;
FIXFragment::Bookmark<jetfix::fix::TransactTime> transact_time_bm;
FIXFragment::Bookmark<fix::ManualOrderIndicator> manual_order_indicator_bm;
FIXFragment::Bookmark<fix::Price> price_bm;
FIXFragment::Bookmark<fix::OrderQty> quantity_bm;
FIXFragment::Bookmark<fix::TransactTimeStr> transact_time_bm;
};
WorkingOrder::WorkingOrder(OrderCreate const&c)
: me (new PImpl(c.account(),
c.operator_id() ,
c.order_key() ,
c.side() ,
c.time_in_force() ,
c.order_type(),
c.costumer_or_firm() ,
c.cti_code() ,
c.user_login() ,
std::string(),
c.manual_order_indicator() ,
c.price() ,
c.quantity(),
OrderStatus::UNDEFINED, 0,0,0,std::string()))
: me (new PImpl())
{
me->order_data = OrderState {
c.account_name,
c.operator_id ,
c.order_key ,
c.side ,
c.time_in_force ,
c.order_type,
c.customer_or_firm ,
c.cti_code ,
c.user_login ,
ExchangeOrderID(),
c.manual_order_indicator ,
c.price ,
c.quantity,
OrderStatus::UNACKNOWLEDGED,
OrderPrice(0),
OrderQuantity(0),
OrderQuantity(c.quantity),
std::string(),
c.transact_time
};
me->internal_state = InternalState::UNACKNOWLEDGED;
}
WorkingOrder::~WorkingOrder ()
{}
void WorkingOrder::init_encoding()
WorkingOrder::WorkingOrder(fix::ExecutionReportMsg const&msg)
: me (new PImpl())
{
bts_assert(me->update_encoding.size() == 0);
auto &frag = me->update_encodinglj
auto const &state = me->state;
bts_assert(msg.get<fix::ExecType>() == ExecType::NEW);
auto const &ts_str = msg.get<fix::TransactTimeStr>();
OrderTimeStamp ts;
std::copy(ts_str.begin(),ts_str.end(),&ts[0]);
me->order_data = OrderState {
AccountName(msg.get<fix::Account>()),
OperatorID(msg.get<fix::TargetSubID>()),
OrderKey(std::stoi(msg.get<fix::ClOrdID>())),
api::BuySell(msg.get<fix::Side>()),
TimeInForce::Type(msg.get<fix::TimeInForce>()),
OrderType::Type(msg.get<fix::OrdType>()),
CustomerOrFirm::UNDEFINED,
CTICode::UNDEFINED,
UserLogin(),
ExchangeOrderID(msg.get<fix::OrderID>()),
ManualOrderIndicator(msg.get<fix::ManualOrderIndicator>()),
OrderPrice (msg.get<fix::Price>()),
OrderQuantity (msg.get<fix::OrderQty>()),
OrderStatus::Type(msg.get<fix::OrdStatus>()),
OrderPrice(0),
OrderQuantity(0),
OrderQuantity(msg.get<fix::OrderQty>()),
std::string(),
ts
};
me->internal_state = InternalState::RECOVERED;
}
WorkingOrder::WorkingOrder(OrderState const&st)
: me (new PImpl())
{
me->order_data = st;
me->internal_state = InternalState::READY;
}
OrderKey WorkingOrder::order_key() const
{return me->order_data.order_key;}
OrderState const &WorkingOrder::state() const
{return me->order_data;}
OrderError::Type WorkingOrder::encode_create(OrderCreate const&create,jetfix::FIXEncodingStream *stream)
{
bts_assert(me->internal_state == InternalState::UNACKNOWLEDGED);
auto &encoding = stream->new_msg(MessageTypes::NewOrderSingle);
encoding.add<fix::ManualOrderIndicator> (create.manual_order_indicator.value());
encoding.add<fix::Price> (scale_price(*create.security,create.price), PriceFieldSz);
encoding.add<fix::OrderQty> (0,SizeFieldSz);
encoding.add<fix::TransactTimeStr>(&create.transact_time[0],create.transact_time.size());
encoding.add<fix::Account> (create.account_name.value());
encoding.add<fix::SenderSubID> (create.operator_id.value());
OrderKeyStr kstr;
encode_clorid(create.order_key,&kstr);
encoding.add<fix::ClOrdID> (&kstr[0],20);
encoding.add<fix::Side> (create.side);
encoding.add<fix::TimeInForce> (create.time_in_force);
encoding.add<fix::OrdType> (create.order_type);
encoding.add<fix::CustomerOrFirm> (create.customer_or_firm);
encoding.add<fix::CtiCode> (create.cti_code);
encoding.add<fix::SecurityDesc> (create.security->glbx().security_desc());
return OrderError::OK;
}
OrderError::Type WorkingOrder::encode_update(OrderUpdate const&update,jetfix::FIXEncodingStream *stream)
{
switch (me->internal_state) {
case InternalState::UNACKNOWLEDGED:
return OrderError::ORDER_NOT_ACK;
case InternalState::RECOVERED:
me->internal_state = InternalState::READY;
me->order_data.customer_or_firm = update.customer_or_firm;
me->order_data.cti_code = update.cti_code;
me->order_data.user_login = update.user_login;
init_encoding(*update.security);
break;
case InternalState::READY:
break;
}
me->order_data.manual_order_indicator = update.manual_order_indicator;
me->order_data.price = update.price;
me->order_data.quantity = update.quantity;
update_encoding(*update.security);
auto &f = stream->new_msg(MessageTypes::CancelReplace);
f.append(me->update_encoding);
return OrderError::OK;
}
OrderError::Type WorkingOrder::encode_cancel(OrderCancel const&cancel,jetfix::FIXEncodingStream *stream)
{
switch (me->internal_state) {
case InternalState::UNACKNOWLEDGED:
return OrderError::ORDER_NOT_ACK;
case InternalState::RECOVERED:
me->internal_state = InternalState::READY;
me->order_data.customer_or_firm = cancel.customer_or_firm;
me->order_data.cti_code = cancel.cti_code;
me->order_data.user_login = cancel.user_login;
break;
case InternalState::READY:
break;
}
me->order_data.manual_order_indicator = cancel.manual_order_indicator;
me->order_data.transact_time = cancel.transact_time;
auto const &state = me->order_data;
auto &encoding = stream->new_msg(MessageTypes::NewOrderSingle);
OrderKeyStr kstr;
encode_clorid(state.order_key,&kstr);
encoding.add<fix::ClOrdID> (&kstr[0],20);
encoding.add<fix::OrderID> (state.exchange_order_id.value());
encoding.add<fix::OrigClOrdID> (&kstr[0],20);
encoding.add<fix::Side> (state.side);
encoding.add<fix::TransactTimeStr>(&state.transact_time[0],state.transact_time.size());
encoding.add<fix::ManualOrderIndicator> (state.manual_order_indicator.value());
encoding.add<fix::SecurityDesc> (cancel.security->glbx().security_desc());
encoding.add<fix::CorrelationClOrdID> (&kstr[0],20);
return OrderError::OK;
}
void WorkingOrder::init_encoding(api::Security const&sec)
{
bts_assert(me->order_data.exchange_order_id.value().size());
bts_assert(me->order_data.cti_code != CTICode::UNDEFINED);
bts_assert(me->order_data.customer_or_firm != CustomerOrFirm::UNDEFINED);
auto &encoding = me->update_encoding;
auto &state = me->order_data;
using namespace jetfix;
frag.add<fix::SenderSubID (state.operator_id());
frag.add<fix::Account> (state.account());
char ord_id_str[20];
u32_toa(state.order_key() >> 32,ord_id_str,10);
u32_toa(state.security_key(),ord_id_str+10,10);
frag.add<fix::ClOrdID> (ord_id_str,20);
frag.add<fix::OrderID> (state.exchange_order_id());
frag.add<fix::OrderQty> (state.quantity());
frag.add<fix::OrderType> ((char) state.order_type());
frag.add<fix::CustomerOrFirm> (me->cfg.customer_or_firm());
me->manual_order_indicator_bm =
encoding.add<fix::ManualOrderIndicator> (state.manual_order_indicator.value());
me->price_bm =
encoding.add<fix::Price> (0,PriceFieldSz);
me->quantity_bm =
encoding.add<fix::OrderQty> (0,SizeFieldSz);
me->transact_time_bm =
encoding.add<fix::TransactTimeStr> (datetime::FIX_timestamp(datetime::now()));
frag.add<fix::OrdType> ('1');
if(me->cfg.tag_order_with_price()) {
auto display_factor = sec->display_factor();
auto p = std::round (price/display_factor);
frag.add<fix::Price> (p);
}
frag.add<fix::TimeInForce>('3');
frag.add<fix::ManualOrderIndicator> (false);
bts_assert (con.cti_code().size());
frag.add<fix::CtiCode> (con.cti_code()[0]);
frag.add<fix::TransactTimeStr>(datetime::FIX_timestamp(datetime::now()));
frag.add<fix::ClOrdID> (std::to_string (sec->security_key ()));
if (side == Side::BUY)
frag.add<fix::Side> ('1');
encoding.add<fix::Account> (state.account_name.value());
encoding.add<fix::SenderSubID> (state.operator_id.value());
OrderKeyStr kstr;
encode_clorid(state.order_key,&kstr);
encoding.add<fix::ClOrdID> (&kstr[0],20);
encoding.add<fix::Side> (state.side);
encoding.add<fix::TimeInForce> (state.time_in_force);
encoding.add<fix::OrdType> (state.order_type);
encoding.add<fix::CustomerOrFirm> (state.customer_or_firm);
encoding.add<fix::CtiCode> (state.cti_code);
encoding.add<fix::OrderID> (state.exchange_order_id.value());
encoding.add<fix::SecurityDesc> (sec.glbx().security_desc());
}
void WorkingOrder::update_encoding(api::Security const&sec)
{
bts_assert(me->order_data.exchange_order_id.value().size());
bts_assert(me->order_data.cti_code != CTICode::UNDEFINED);
bts_assert(me->order_data.customer_or_firm != CustomerOrFirm::UNDEFINED);
auto &encoding = me->update_encoding;
auto &state = me->order_data;
using namespace jetfix;
// manual order indicator
encoding.overwrite(me->manual_order_indicator_bm,state.manual_order_indicator.value());
// price
encoding.overwrite(me->price_bm,scale_price(sec,state.price),PriceFieldSz);
// size
encoding.overwrite(me->quantity_bm,state.quantity.value());
// time
encoding.overwrite(me->transact_time_bm,&state.transact_time[0],state.transact_time.size());
}
WorkingOrder::~WorkingOrder () {}
OrderPrice::Value WorkingOrder::scale_price(api::Security const&sec,OrderPrice price)
{
auto display_factor = sec.contract_spec().price_multiplier();
auto p = price.value();
if (display_factor != 1.0)
p = std::round (p/display_factor);
return p;
}
void WorkingOrder::encode_clorid(OrderKey k, OrderKeyStr *out)
{
u32_toa(k.value() >> 31,&((*out)[0]),10);
u32_toa(k.value(),&((*out)[10]),10);
}
bool WorkingOrder::on_exe_report (jetfix::fix::ExecutionReportMsg const&msg)
{
me->order_data.order_type = OrderType::Type (msg.get<fix::OrdType>());
if (me->internal_state == InternalState::UNACKNOWLEDGED)
me->order_data.exchange_order_id = ExchangeOrderID(msg.get<fix::OrderID>());
me->order_data.status = OrderStatus::Type(msg.get<fix::OrdStatus>());
me->order_data.avg_fill_price = OrderPrice(msg.get<fix::AvgPx> ());
me->order_data.filled_quantity = OrderQuantity(msg.get<fix::CumQty> ());
me->order_data.remaining_quantity = OrderQuantity(msg.get<fix::LeavesQty> ());
if (msg.has<fix::Text>())
me->order_data.reject_msg = msg.get<fix::Text>();
else
frag.add<fix::Side> ('2');
frag.add<fix::Symbol> (sec->cme_symbol());
frag.add<fix::SecurityDesc> (sec->security_desc());
frag.add<jetfix::fix::SecurityType> ("FUT");
me->order_data.reject_msg.clear();
auto const &ts_str = msg.get<fix::TransactTimeStr>();
std::copy(ts_str.begin(),ts_str.end(),&(me->order_data.transact_time[0]));
return true;
}
bool WorkingOrder::on_cancel_reject (jetfix::fix::CancelRejectMsg const&)
{
return false;
}
}}
+17 -6
View File
@@ -2,13 +2,18 @@
#include <memory>
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
#include "server/sparkd/jetfix/codec/MessageDefinitions.h"
#include "lib/order_man/protos/OrderMan.pb.h"
namespace bts {
namespace jetfix {class FIXEncodingStream;}
namespace api {class Security;}
namespace orderman {
class OrderState;
class OrderCreate;
class OrderUpdate;
class OrderCancel;
class WorkingOrder
{
@@ -16,21 +21,27 @@ public:
WorkingOrder(OrderCreate const&);
WorkingOrder(jetfix::fix::ExecutionReportMsg const&);
WorkingOrder(OrderState const&);
~WorkingOrder();
OrderKey order_key() const;
OrderState state() const ;
OrderState const &state() const ;
void encode_create(OrderCreate const&,jetfix::FIXEncodingStream *out) const;
void encode_update(OrderUpdate const&,jetfix::FIXEncodingStream *out) const;
void encode_cancel(OrderCancel const&,jetfix::FIXEncodingStream *out) const;
OrderError::Type encode_create(OrderCreate const&,jetfix::FIXEncodingStream *) ;
OrderError::Type encode_update(OrderUpdate const&,jetfix::FIXEncodingStream *) ;
OrderError::Type encode_cancel(OrderCancel const&,jetfix::FIXEncodingStream *) ;
// return wheter the execution report caused the order to change
bool on_exe_report (jetfix::fix::ExecutionReportMsg const&);
bool on_cancel_reject (jetfix::fix::CancelRejectMsg const&);
private:
enum class InternalState;
struct PImpl;
std::unique_ptr<PImpl> me;
void init_encoding(api::Security const&sec);
void update_encoding(api::Security const&sec);
OrderPrice::Value scale_price(api::Security const&sec,OrderPrice);
void encode_clorid(OrderKey k, OrderKeyStr *out);
};
}}
@@ -15,7 +15,8 @@ using OperatorID = utils::ValueWrapper<struct OperatorIDTag,std::string>;
using UserLogin = utils::ValueWrapper<struct UserLoginTag,std::string>;
using ExchangeOrderID = utils::ValueWrapper<struct ExchangeOrderIDTag,std::string>;
using ManualOrderIndicator= utils::ValueWrapper<struct ManualOrderIndicatorTag,bool>;
using OrderTimeStamp = std::array<char, sizeof("20121204-14:44:02.000")> ;
using OrderKeyStr = std::array<char, 20> ;
inline SecurityKey get_sec_key(OrderKey ok)
{