Fixed a bug with reporting

This commit is contained in:
Taha Afzal authored and John Colvin committed 2019-11-06 22:40:51 -06:00
1 parent 05aeb78026
commit 85590d8d0d
2 files changed
+54 -47

No files matched your search

@@ -17,7 +17,6 @@ using namespace api::orderman;
enum class PyramidAlgo::Status { WAITING_FOR_MD, WORKING, WAITING_FOR_CANCEL, DONE };
struct LegData{
const size_t index;
const SecurityKey sec_key;
@@ -25,13 +24,15 @@ struct LegData{
ChildTemplate* order_template = nullptr;
OrderKey order_key;
ChildState order_state;
bool recv_book;
Book book;
Quantity top_delete; // this will be DONTCARE for covers
Quantity cover_payup;// this will be DONTCARE for top
LegData (size_t index_a,
SecurityKey sec_key_a,
api::Side side_a,
ChildTemplate* order_template_a)
ChildTemplate* order_template_a,
Quantity qty)
:
index (index_a),
sec_key (sec_key_a),
@@ -39,9 +40,10 @@ struct LegData{
order_template (order_template_a),
order_key(null_order_key()),
order_state(),
recv_book(false),
book(),
top_delete(0),
cover_payup(0)
top_delete(index == 0 ? qty.value() : 0),
cover_payup(index == 0 ? 0 : qty.value())
{
}
const BookSide& opp_md() const{
@@ -50,6 +52,10 @@ struct LegData{
bool is_top_spread() const{
return index == 0;
}
void set_book (const Book& book_a){
recv_book = true;
book = book_a;
}
};
struct PyramidAlgo::PImpl {
@@ -92,15 +98,17 @@ OrderOpResult PyramidAlgo::do_before_launch(IBeforeLaunchAlgoServices& serv) {
auto* t = _create_order_template(serv,
me->params.top_spread,
def.side());
me->legs.emplace_back(0, me->params.top_spread, def.side(), t);
me->legs.emplace_back(0, me->params.top_spread, def.side(), t,
me->params.top_delete);
// covers
size_t i = 1;
size_t i = 0;
for (SecurityKey key : me->params.cover_spreads){
auto side = _opp(def.side());
auto* t = _create_order_template(serv,
key,
side);
me->legs.emplace_back(i++, key, side, t);
me->legs.emplace_back(i+1, key, side, t, me->params.cover_payups[i]);
i++;
}
}
else
@@ -181,8 +189,8 @@ void PyramidAlgo::do_update(const api::orderman::Update& update) {
void PyramidAlgo::on_book(SecurityKey key, const Book& book) {
LegData* leg_data = me->find_leg_from_sec(key);
if(leg_data != nullptr){
leg_data->book = book;
_analyze_and_act();
leg_data->set_book(book);
_analyze_md_and_act();
}
}
void PyramidAlgo::on_child_change(OrderKey key, const ChildState &st) {
@@ -194,20 +202,26 @@ void PyramidAlgo::on_child_change(OrderKey key, const ChildState &st) {
static_cast<int>(me->state),
static_cast<int>(st.status),
st.total_fills,
st.last_price);
switch(me->state){
st.last_price);
LegData* leg = me->find_leg_from_ord_key (key);
if (leg == nullptr){
LOG(OrderMan, Error,
"PyramidAlgo::on_child_change key:{} unknown order:{}",
_algo().order_key(), key);
return;
}
switch(me->state){
case PyramidAlgo::Status::WAITING_FOR_MD:
bts_assert_msg (false, "No child orders");
break;
case PyramidAlgo::Status::WORKING:
_handle_order_in_working (key, st);
_handle_order_in_working (*leg, st);
break;
case PyramidAlgo::Status::WAITING_FOR_CANCEL:
_handle_order_in_waiting_cancel(key, st);
_handle_order_in_waiting_cancel(*leg, st);
break;
case PyramidAlgo::Status::DONE:
break;
break;
}
}
@@ -278,11 +292,11 @@ ChildTemplate* PyramidAlgo::_create_order_template (
return &(serv.make_child_template(order_def));
}
void PyramidAlgo::_analyze_and_act(){
void PyramidAlgo::_analyze_md_and_act(){
switch(me->state){
case PyramidAlgo::Status::WAITING_FOR_MD:
for (const LegData& l : me->legs)
if(l.opp_md()[0].size() == 0)
if(!l.recv_book)
return;
// received data for the first time
if (_working_state_met()){
@@ -292,7 +306,7 @@ void PyramidAlgo::_analyze_and_act(){
*top_leg.order_template,
me->out_price,
me->out_qty);
LOG(OrderMan, Info,"PyramidAlgo::_analyze_and_act launching order key:{} "
LOG(OrderMan, Info,"PyramidAlgo::_analyze_md_and_act launching order key:{} "
"child_key:{} price:{} qty:{}",
_algo().order_key(),
top_leg.order_key,
@@ -331,7 +345,7 @@ bool PyramidAlgo::_working_state_met() const{
me->out_price.value() <= cover_sum) ||
(me->side == api::Side::SIDE_SELL &&
me->out_price.value() >= cover_sum));
if (!rv){
if (!rv && !os::is_nan(cover_sum)){
// price condition does not met, but if the
// cum qty at the top leg price is > threashold
// then the condition is met ()
@@ -379,26 +393,11 @@ void PyramidAlgo::_switch_state(Status new_state){
me->state = new_state;
}
void PyramidAlgo::_handle_order_in_waiting_cancel(OrderKey key,
const ChildState& st){
LegData* leg = me->find_leg_from_ord_key (key);
if (leg == nullptr)
LOG(OrderMan, Error,
"PyramidAlgo::on_child_change key:{} unknown order:{}",
_algo().order_key(), key);
else if(leg->is_top_spread())
_send_hedges(st.last_fills);
_check_for_completion();
}
void PyramidAlgo::_handle_order_in_working (OrderKey key,
void PyramidAlgo::_handle_order_in_working (LegData& leg,
const ChildState &st){
LegData* leg = me->find_leg_from_ord_key (key);
if (leg == nullptr)
LOG(OrderMan, Error,
"PyramidAlgo::on_child_change key:{} unknown order:{}",
_algo().order_key(), key);
else if(leg->is_top_spread()){
leg.order_state = st;
if(leg.is_top_spread()){
_algo().update_fill_state(st.total_fills, st.last_price);
_send_hedges(st.last_fills);
switch (st.status) {
case ChildStatus::WORKING:
@@ -408,9 +407,19 @@ void PyramidAlgo::_handle_order_in_working (OrderKey key,
_algo().cancel("Unexpected child cancel");
_switch_state(PyramidAlgo::Status::WAITING_FOR_CANCEL);
break;
}
_check_for_completion();
}
}
_check_for_completion();
}
void PyramidAlgo::_handle_order_in_waiting_cancel(LegData& leg,
const ChildState& st){
leg.order_state = st;
if(leg.is_top_spread()){
_algo().update_fill_state(st.total_fills, st.last_price);
_send_hedges(st.last_fills);
}
_check_for_completion();
}
void PyramidAlgo::_send_hedges (Quantity filled_qty){
@@ -429,6 +438,7 @@ void PyramidAlgo::_check_for_completion(){
if (l.order_state.status == ChildStatus::WORKING)
return;
// all orders are in end state -> go to end state
_algo().complete();
_switch_state(PyramidAlgo::Status::DONE);
}
@@ -1,15 +1,12 @@
#pragma once
#include "lib/domain_model/Security.pb.h"
#include "lib/md/PriceSize.h"
#include "lib/orderman/orders/algo/IAlgoOrder.h"
//namespace bts{namespace api{namespace orderman{class ChildTemplate;}}}
namespace bts {
namespace orderman {
class ChildTemplate;
class LegData;
class PyramidAlgo : public IAlgoOrder {
public:
@@ -49,12 +46,12 @@ private:
ChildTemplate* _create_order_template (IBeforeLaunchAlgoServices&,
SecurityKey,
api::Side) const;
void _analyze_and_act();
void _analyze_md_and_act();
bool _working_state_met() const;
static api::Side _opp (api::Side);
void _switch_state (Status new_status);
void _handle_order_in_working (OrderKey key, const ChildState &st);
void _handle_order_in_waiting_cancel (OrderKey key, const ChildState &st);
void _handle_order_in_working (LegData&, const ChildState &st);
void _handle_order_in_waiting_cancel (LegData&, const ChildState &st);
void _send_hedges (Quantity filled_qty);
void _check_for_completion();
};