This commit is contained in:
Taha Afzal authored and John Colvin committed 2019-11-06 22:40:53 -06:00
1 parent 23a07c444b
commit 4c3aefc98a
2 files changed
+53 -77

No files matched your search

@@ -11,7 +11,6 @@
#include "lib/utils/PBUtils.h"
#include "lib/utils/datetime.h"
#include <stdint.h>
#include <stdio.h>
namespace bts {
namespace orderman {
@@ -21,33 +20,24 @@ using namespace api::orderman;
enum class TASAlgo::Status { WAITING_FOR_SIGNAL, WORKING, DONE };
struct TASLegData{
size_t index;
SecurityKey sec_key;
double tick_size;
api::Side side; //trading side
Price price;
Quantity quantity;
ChildTemplate* order_template = nullptr;
OrderKey order_key;
ChildState order_state;
TASLegData (size_t index,
SecurityKey sec_key,
double tick_size,
api::Side side,
Price price,
Quantity quantity,
ChildTemplate* order_template)
TASLegData (api::Side side,
Price price,
Quantity quantity,
ChildTemplate* order_template)
:
index (index),
sec_key (sec_key),
tick_size(tick_size),
side (side),
price(price),
quantity(quantity),
order_template (order_template),
order_key(null_order_key()),
order_state()
{
order_state(){
order_state.status = ChildStatus::WORKING;
}
};
@@ -57,9 +47,6 @@ struct TASAlgo::PImpl {
SecurityKey sec_key = SecurityKey(0);
TASAlgo::TASAlgoParams algo_params;
std::vector<TASLegData> legs;
api::Side side = api::Side::SIDE_BUY;
Price out_price = Price(0);
Quantity out_qty = Quantity(0);
bts::datetime::PTime start_time = bts::datetime::local_now();
PImpl(){
}
@@ -73,55 +60,51 @@ struct TASAlgo::PImpl {
TASAlgo::TASAlgo() : me(new PImpl()) {}
TASAlgo::~TASAlgo() {}
IAlgoServices& TASAlgo::_algo() const { return *me->algo_services; }
// initialization /////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
OrderOpResult TASAlgo::do_before_launch(IBeforeLaunchAlgoServices& serv) {
auto& def = serv.definition();
if (def.has_custom_def() && def.custom_def().has_dynamic()){
const auto& sec = serv.security();
me->sec_key = SecurityKey(sec.id());
me->side = def.side();
me->sec_key = SecurityKey(sec.id());
me->algo_params = _convert_params(def.custom_def());
double tick_size = sec.contract_spec().low_tick();
size_t i = 0;
for (const auto& t : me->algo_params.child_orders){
Price price = std::get<0>(t);
Quantity quantity = std::get<1>(t);
api::Side side = std::get<2>(t);
auto* ordt = _create_order_template(
serv,
SecurityKey(sec.id()),
side);
me->legs.emplace_back(i++,
me->sec_key,
tick_size,
side,
price,
quantity,
ordt);
if (me->algo_params.child_orders.size() > 0){
for (const auto& t : me->algo_params.child_orders){
Price price = std::get<0>(t);
Quantity quantity = std::get<1>(t);
api::Side side = std::get<2>(t);
auto* ordt = _create_order_template(
serv,
SecurityKey(sec.id()),
side);
me->legs.emplace_back(side,
price,
quantity,
ordt);
}
return {};
}
}
else
return {OrderError::ORDER_ERROR_INVALID_PARAMETER,
std::string("Missing parameters")};
return {};
return {OrderError::ORDER_ERROR_INVALID_PARAMETER,
std::string("Parameters are not well formed")};
}
void TASAlgo::do_launch(IAlgoServices& algo_services) {
me->algo_services = &algo_services;
_algo().subscribe_status(me->sec_key);
_algo().subscribe_timer();
}
void TASAlgo::do_launch_attached(IAlgoServices&,
OrderKey,
ChildState) {
_cxl_and_end(true, "Algo can not be attached to working orders");
void TASAlgo::do_launch_attached(IAlgoServices& algo_services,
OrderKey,
ChildState) {
me->algo_services = &algo_services;
_cxl_and_end(true, "TAS Algo can not be attached to working orders");
}
// updates ////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void TASAlgo::do_update(const api::orderman::Update&) {
// TODO
LOG(OrderMan, Warn, "TASAlgo::do_update order:{} state:{} update not allowed ",
_algo().order_key().value(),
static_cast<int>(me->state));
}
// callbacks //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
@@ -146,7 +129,7 @@ void TASAlgo::on_status(SecurityKey key, co2::TradingStatus status){
l.order_key = _algo().launch_child(
*l.order_template,
l.price,
l.quantity);
l.quantity);
_switch_state(TASAlgo::Status::WORKING);
break;
case TASAlgo::Status::WORKING:
@@ -159,9 +142,7 @@ void TASAlgo::on_timer(){
if (bts::datetime::msecs_since(me->start_time) > 10000)
on_status(me->sec_key,co2::TradingStatus::TRADINGSTATUS_PRE_OPEN);
}
IAlgoOrder::OnCancelResponse TASAlgo::on_cancel() {
return IAlgoOrder::OnCancelResponse::HONOR;
}
IAlgoServices& TASAlgo::_algo() const { return *me->algo_services; }
///////////////////////////////////////////////////////////////////////////////
// stateless conversion ///////////////////////////////////////////////////////
TASAlgo::TASAlgoParams TASAlgo::_convert_params (
@@ -169,21 +150,31 @@ TASAlgo::TASAlgoParams TASAlgo::_convert_params (
const DynamicOrderDefinition& def = cod.dynamic();
const auto& params = def.order_params();
TASAlgo::TASAlgoParams rv;
// top contract
for (size_t i = 0; i < 1024; i++){
auto idx = std::to_string(i+1);
auto price = get_if<double>(params, "Order " + idx + " Price");
auto qty = get_if<uint32_t>(params, "Order " + idx + " Quantity");
auto is_buy = get_if<bool>(params, "Order " + idx + " Buy?");
if(is_buy.has_value()){
api::Side side = is_buy.value() ? api::Side::SIDE_BUY : api::Side::SIDE_SELL;
// TODO, get it from the parent
auto is_buy = get_if<bool>(params, "Order " + idx + " Buy?");
if(price.has_value() && qty.has_value() && qty.value() != 0){
api::Side side = is_buy.has_value() ?
(is_buy.value() ? api::Side::SIDE_BUY : api::Side::SIDE_SELL) :
api::Side::SIDE_SELL;
rv.child_orders.emplace_back(Price(price.value()), Quantity(qty.value()), side);
}
}
return rv;
}
ChildTemplate* TASAlgo::_create_order_template (
IBeforeLaunchAlgoServices& serv,
SecurityKey key,
api::Side side){
OrderDefinition order_def = serv.definition();
order_def.set_security_key (key.value());
order_def.set_side(side);
order_def.mutable_custom_def()->mutable_limit()->MergeFrom (
api::orderman::LimitOrderDefinition());
return &(serv.make_child_template(order_def));
}
void TASAlgo::_switch_state(TASAlgo::Status new_state){
switch (new_state){
case (TASAlgo::Status::WAITING_FOR_SIGNAL):
@@ -207,17 +198,5 @@ void TASAlgo::_cxl_and_end (bool is_error, const std::string& err_msg){
_switch_state(TASAlgo::Status::DONE);
}
ChildTemplate* TASAlgo::_create_order_template (
IBeforeLaunchAlgoServices& serv,
SecurityKey key,
api::Side side) const{
OrderDefinition order_def = serv.definition();
order_def.set_security_key (key.value());
order_def.set_side(side);
order_def.mutable_custom_def()->mutable_limit()->MergeFrom (
api::orderman::LimitOrderDefinition());
return &(serv.make_child_template(order_def));
}
} // namespace orderman
} // namespace bts
@@ -13,8 +13,6 @@ class TASAlgo : public IAlgoOrder {
TASAlgo();
~TASAlgo();
struct TASAlgoParams{
// first one is the top
bool cxl_child_on_cxl;
std::vector<std::tuple<Price, Quantity, api::Side>> child_orders;
};
public:
@@ -26,7 +24,6 @@ class TASAlgo : public IAlgoOrder {
void on_child_change(OrderKey, ChildState const &) override;
void on_status(SecurityKey, co2::TradingStatus) override;
void on_timer() override;
IAlgoOrder::OnCancelResponse on_cancel() override;
private:
enum class Status;
@@ -36,9 +33,9 @@ private:
IAlgoServices& _algo() const ;
static TASAlgoParams _convert_params (
const bts::api::orderman::CustomOrderDefinition&);
ChildTemplate* _create_order_template (IBeforeLaunchAlgoServices&,
SecurityKey,
api::Side) const;
static ChildTemplate* _create_order_template (IBeforeLaunchAlgoServices&,
SecurityKey,
api::Side);
// acts on md, cognizant of the state
void _switch_state (Status new_status);
void _cxl_and_end (bool is_error, const std::string& err_msg);