115 lines
3.7 KiB
C++
115 lines
3.7 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include "lib/orderman/orders/Order.h"
|
|
#include "lib/api/services/OrderService.server.pb.h"
|
|
|
|
namespace bts {
|
|
namespace api {namespace orderman {class OrderDefinition;}}
|
|
namespace mercury {class TimerEvent;}
|
|
namespace md {class PriceSize;}
|
|
namespace orderman {
|
|
|
|
class OrderStateUpdateEvent;
|
|
class BookUpdateEvent;
|
|
class PreActivationOrderServices;
|
|
|
|
class TriggerOrder : public Order
|
|
{
|
|
private:
|
|
|
|
enum State{
|
|
// remains here till on_actiavte is called
|
|
START=0,
|
|
// waiting for trigger conditions, sends
|
|
// order and waits to steady
|
|
WAITING_FOR_TRIGGER=1,
|
|
// once the order is submitted it gets into
|
|
// this state until getting a callback
|
|
// from the child order
|
|
WAITING_FOR_FIRST_ACK=2,
|
|
// order is out
|
|
STEADY=3,
|
|
END=4
|
|
};
|
|
// this is the ways a child order
|
|
// can be cancelled
|
|
enum CancelMode{
|
|
// not cancelled, initial value
|
|
none=0,
|
|
// meaning the trigger order
|
|
// cancelled the child order because
|
|
// of time or conditional reasons
|
|
trigger,
|
|
// the trigger order is itself
|
|
// externally cancelled
|
|
// through the cancel call
|
|
external
|
|
};
|
|
public:
|
|
TriggerOrder();
|
|
~TriggerOrder();
|
|
api::orderman::OrderDefinition const& definition() const override;
|
|
void on_init_new(api::orderman::OrderDefinition const&,
|
|
const PreActivationOrderServices&) override;
|
|
void on_init_recovered(api::orderman::OrderRecord const&,
|
|
const PreActivationOrderServices &) override;
|
|
void on_activate(Price, Quantity,
|
|
OrderServices&,
|
|
trace::CorID const&) override ;
|
|
void on_update(Price, Quantity, trace::CorID const&) override;
|
|
void on_crack(trace::CorID const&) override;
|
|
void on_cancel(trace::CorID const&) override;
|
|
private:
|
|
// callbacks from book and child order udpates
|
|
void _on_book_update(const BookUpdateEvent&);
|
|
void _on_child_order_update(OrderStateUpdateEvent const&);
|
|
void _on_time (const bts::mercury::TimerEvent&);
|
|
private:
|
|
// called from init_new
|
|
bool _on_init_new(const api::orderman::OrderDefinition& order_def,
|
|
const PreActivationOrderServices& pre_os);
|
|
void _report_state();
|
|
void _switch_state(State new_state);
|
|
// this is called in the WAITING_FOR_TRIGGER state,
|
|
// to check for the trigger condition and shoot
|
|
// an order if the condition is met
|
|
void _analyze_trigger_and_shoot();
|
|
// this is called in the STEADY state when the
|
|
// child order is working. (Based on the
|
|
// parameters) if timed-out or the trigger
|
|
// condition being false, the
|
|
// child order is cancelled
|
|
void _analyze_trigger_and_cancel();
|
|
api::orderman::OrderStatus _status() const;
|
|
void _compute_state();
|
|
// for non trailing trigger prices, we need a
|
|
// notion if initially the trigger price
|
|
// was over the observed market or not
|
|
void _observe_market_once();
|
|
// based on the trigger market to watch returns
|
|
// a pointer to the market (bid, ask, ltp). If not
|
|
// present returns a nullptr
|
|
const md::PriceSize* _observed_market() const;
|
|
// this is the price of the parent order
|
|
// (the trigger price)
|
|
double _trigger_price() const;
|
|
// this is in the trigger action
|
|
double _child_order_price() const;
|
|
void _cancel_order(CancelMode cancel_mode);
|
|
bool _activated() const;
|
|
//returns true is the trigger condition is met
|
|
static bool _trigger_met(const md::PriceSize* obs_md,
|
|
double trigger_price,
|
|
double tick_size,
|
|
bool trigger_above_md);
|
|
//returns true is p1 >= p2
|
|
static bool _gr_than_eq(double p1, double p2, double tick_size);
|
|
private:
|
|
State _state;
|
|
struct PImpl;
|
|
std::unique_ptr<PImpl> me;
|
|
};
|
|
|
|
}}
|