299 lines
12 KiB
C++
299 lines
12 KiB
C++
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
#include "lib/api/services/OrderService.pb.h"
|
|
#include "lib/api/services/PositionService.pb.h"
|
|
#include "lib/api/services/PositionService.server.pb.h"
|
|
#include "lib/orderman/orderman_utils/orderman_common.h"
|
|
#include "lib/domain_model/Security.pb.h"
|
|
#include "lib/api/services/AdminService.server.pb.h"
|
|
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
|
#include "lib/api/service_helpers/PositionUtilities.h"
|
|
#include "Limiter.h"
|
|
#include "LimiterUtils.h"
|
|
#include "ProductLimitTree.h"
|
|
|
|
namespace bts{
|
|
namespace orderman{
|
|
|
|
// Scope : <security, account>
|
|
// Created by : SecurityLimitTree
|
|
// Keeps the running position and clip limits
|
|
// for a single <security, account>.
|
|
// Has a reference to the ProductLimitNode for the product
|
|
// that this security belongs to.
|
|
// Position limits are enforced through the ProductLimitNode
|
|
// and clip limits are enforced here
|
|
class SecurityLimitNode{
|
|
private:
|
|
const uint32_t _account_id;
|
|
const uint32_t _prod_clip_size;
|
|
ProductLimitNode& _product_node;
|
|
const bts::api::admin::SecurityRiskLimitUnit& _sec_limit;
|
|
// this is the committed position. working order
|
|
// adds and removes are passed straight to the
|
|
// product node. Position updates are changed to
|
|
// increments and then passed
|
|
uint32_t _long_pos, _long_working_pos;
|
|
uint32_t _short_pos, _short_working_pos;
|
|
bool _is_child;
|
|
SecurityLimitNode* _relation;
|
|
int _net_pos() const {
|
|
return static_cast<int>(_long_pos)-static_cast<int>(_short_pos);
|
|
}
|
|
private:
|
|
bool _is_parent() const {return !_is_child && _relation != nullptr;}
|
|
// helper functions
|
|
uint32_t _eff_long_pos() const{
|
|
return (_long_pos + _long_working_pos) +
|
|
(_is_parent() ? _relation->_eff_long_pos() : 0);
|
|
}
|
|
uint32_t _eff_short_pos() const{
|
|
return (_short_pos + _short_working_pos) +
|
|
(_is_parent() ? _relation->_eff_short_pos() : 0);
|
|
}
|
|
uint32_t _subtract_unsigned(uint32_t n1, uint32_t n2){
|
|
if(n1 >= n2)
|
|
return n1-n2;
|
|
else{
|
|
LOG(OrderMan, Error, "SecurityLimitNode._subtract_unsigned {0}-{1} ",
|
|
n1, n2);
|
|
return 0;
|
|
}
|
|
}
|
|
void _check_sec_clip (Quantity q,
|
|
Limiter::LimiterQueryResult& out_res) const{
|
|
if(_sec_limit.enforce_clip_limits() && q.value()>_sec_limit.clip_size()){
|
|
out_res.result = Limiter::CLIP_SIZE_BREACHED;
|
|
out_res.error_msg =
|
|
fmt::format("Security Clip Size Max:{} Violation:{})",
|
|
_sec_limit.clip_size(), q.value());
|
|
}
|
|
}
|
|
void _check_prod_clip (Quantity q,
|
|
Limiter::LimiterQueryResult& out_res) const{
|
|
if (q.value() > _prod_clip_size){
|
|
out_res.result = Limiter::CLIP_SIZE_BREACHED;
|
|
out_res.error_msg =
|
|
fmt::format("Product Clip Size Limit:{} Violation:{})",
|
|
_prod_clip_size, q.value());
|
|
}
|
|
}
|
|
void _check_sec_price_band(api::Side s, Price p,
|
|
Limiter::LimiterQueryResult& out_res) const{
|
|
if(_sec_limit.enforce_price_bands()){
|
|
if(s == api::Side::SIDE_BUY){
|
|
if(p.value() > _sec_limit.max_buy_price()){
|
|
out_res.result = Limiter::SEC_PRICE_BAND_BREACHED;
|
|
out_res.error_msg =
|
|
fmt::format("Price Band Violation Limit:{} Violation:{})",
|
|
_sec_limit.max_buy_price(), p.value());
|
|
}
|
|
}
|
|
else{
|
|
if(p.value() < _sec_limit.min_sell_price()){
|
|
out_res.result = Limiter::SEC_PRICE_BAND_BREACHED;
|
|
out_res.error_msg =
|
|
fmt::format("Price Band Violation Limit:{} Violation:{})",
|
|
_sec_limit.min_sell_price(), p.value());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
void _check_pos_on_new_order(api::Side side,
|
|
Quantity quantity,
|
|
Limiter::LimiterQueryResult& out_res) const{
|
|
if(_sec_limit.enforce_net_pos_limits()){
|
|
// sec
|
|
LimiterUtils::can_send_new_order_net(
|
|
side,
|
|
quantity,
|
|
true,
|
|
_sec_limit.net_long_position_limit(),
|
|
_sec_limit.net_short_position_limit(),
|
|
_eff_long_pos(),
|
|
_eff_short_pos(),
|
|
_product_node.product_scope(),
|
|
out_res);
|
|
if(!out_res.ok())
|
|
return;
|
|
}
|
|
// prod
|
|
out_res = _product_node.can_send_new_order(side, quantity);
|
|
if(!out_res.ok())
|
|
return;
|
|
// if its a slave, check with master
|
|
if(_is_child){
|
|
// if not master symbol found return with that
|
|
if(_relation == nullptr){
|
|
out_res.result = Limiter::LimiterErrorCode::NO_PARENT_SECURITY;
|
|
out_res.error_msg = "Could not find parent security"; // TODO
|
|
return;
|
|
}
|
|
else
|
|
out_res = _relation->can_send_new_order(
|
|
side,quantity,Price(0),
|
|
Limiter::EnforcementParams{true,false,false});
|
|
}
|
|
}
|
|
void _check_pos_on_order_update(api::Side side,
|
|
Quantity from,
|
|
Quantity to,
|
|
Limiter::LimiterQueryResult& out_res) const{
|
|
// sec
|
|
if(_sec_limit.enforce_net_pos_limits()){
|
|
LimiterUtils::can_update_order_net(side,
|
|
from,
|
|
to,
|
|
true,
|
|
_sec_limit.net_long_position_limit(),
|
|
_sec_limit.net_short_position_limit(),
|
|
_eff_long_pos(),
|
|
_eff_short_pos(),
|
|
_product_node.product_scope(),
|
|
out_res);
|
|
if(!out_res.ok())
|
|
return;
|
|
}
|
|
// prod
|
|
out_res = _product_node.can_update_order(side, from, to);
|
|
if(!out_res.ok())
|
|
return;
|
|
// if its a slave, check with master
|
|
if(_is_child){
|
|
// if not master symbol found return with that
|
|
if(_relation == nullptr){
|
|
out_res.result = Limiter::LimiterErrorCode::NO_PARENT_SECURITY;
|
|
out_res.error_msg = "Could not find parent security";
|
|
return;
|
|
}
|
|
else
|
|
out_res = _relation->can_update_order(
|
|
side, from, to, Price(0),
|
|
Limiter::EnforcementParams{true,false,false});
|
|
}
|
|
}
|
|
public:
|
|
SecurityLimitNode(uint32_t account_id,
|
|
ProductLimitNode& product_limit,
|
|
uint32_t prod_clip_size,
|
|
const bts::api::admin::SecurityRiskLimitUnit& sec_limit);
|
|
// accessors ///////////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////
|
|
uint32_t account_id() const {
|
|
return _account_id;
|
|
}
|
|
// position updates ////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////
|
|
void copy_positions_from(const SecurityLimitNode& source){
|
|
_long_pos = source._long_pos;
|
|
_short_pos = source._short_pos;
|
|
_long_working_pos = source._long_working_pos;
|
|
_short_working_pos = source._short_working_pos;
|
|
// this operation will be repeated since multiple
|
|
// security nodes share the same product node, but
|
|
// since its a copy (and not as addition) for
|
|
// simplicity's sake we do this
|
|
_product_node.copy_positions_from(source._product_node);
|
|
}
|
|
void add_working_order(api::Side side, uint32_t qty, bool real_op=true){
|
|
if(side == api::Side::SIDE_BUY)
|
|
_long_working_pos += qty;
|
|
else
|
|
_short_working_pos += qty;
|
|
_product_node.add_position(side, qty, real_op);
|
|
}
|
|
void remove_working_order(api::Side side, uint32_t qty, bool real_op=true){
|
|
if(side == api::Side::SIDE_BUY)
|
|
_long_working_pos = _subtract_unsigned(_long_working_pos,qty);
|
|
else
|
|
_short_working_pos = _subtract_unsigned(_short_working_pos,qty);
|
|
_product_node.remove_position(side, qty, real_op);
|
|
}
|
|
void update_position (AccountID account_id,
|
|
Quantity long_pos,
|
|
Quantity short_pos){
|
|
bts_assert(_account_id == account_id.value());
|
|
uint32_t new_long = long_pos.value(),
|
|
new_short = short_pos.value();
|
|
// long
|
|
if(new_long > _long_pos)
|
|
_product_node.add_position(api::Side::SIDE_BUY,
|
|
new_long-_long_pos);
|
|
else if(new_long < _long_pos)
|
|
_product_node.remove_position(api::Side::SIDE_BUY,
|
|
_long_pos-new_long);
|
|
_long_pos = new_long;
|
|
// short
|
|
if(new_short > _short_pos)
|
|
_product_node.add_position(api::Side::SIDE_SELL,
|
|
new_short-_short_pos);
|
|
else if(new_short < _short_pos)
|
|
_product_node.remove_position(api::Side::SIDE_SELL,
|
|
_short_pos-new_short);
|
|
_short_pos = new_short;
|
|
}
|
|
// parent child hierarchy //////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////
|
|
void set_parent_child (bool is_child, SecurityLimitNode* relation);
|
|
// risk limit checks ///////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////
|
|
Limiter::LimiterQueryResult can_send_new_order(
|
|
api::Side side,
|
|
Quantity quantity,
|
|
Price price,
|
|
Limiter::EnforcementParams enforcement) const{
|
|
Limiter::LimiterQueryResult result;result.result= Limiter::OK;
|
|
// clip
|
|
if(enforcement.clip){
|
|
// sec
|
|
_check_sec_clip(quantity, result);
|
|
if(!result.ok())
|
|
return result;
|
|
// prod
|
|
_check_prod_clip(quantity, result);
|
|
if(!result.ok())
|
|
return result;
|
|
}
|
|
//price band
|
|
if(enforcement.price_limits){
|
|
// only sec level price band limits
|
|
_check_sec_price_band(side, price, result);
|
|
if(!result.ok())
|
|
return result;
|
|
}
|
|
// pos limit
|
|
if(enforcement.pos)
|
|
_check_pos_on_new_order(side,quantity,result);
|
|
return result;
|
|
}
|
|
Limiter::LimiterQueryResult can_update_order(
|
|
api::Side side,
|
|
Quantity from,
|
|
Quantity to,
|
|
Price price,
|
|
Limiter::EnforcementParams enforcement) const{
|
|
Limiter::LimiterQueryResult result;result.result=Limiter::OK;
|
|
// clip lim
|
|
if(enforcement.clip){
|
|
// sec
|
|
_check_sec_clip(to, result);
|
|
if(!result.ok())
|
|
return result;
|
|
// prod
|
|
_check_prod_clip(to, result);
|
|
if(!result.ok())
|
|
return result;
|
|
}
|
|
// sec price band
|
|
_check_sec_price_band(side, price, result);
|
|
if(!result.ok())
|
|
return result;
|
|
// pos limit
|
|
if(enforcement.pos)
|
|
_check_pos_on_order_update(side,from,to,result);
|
|
return result;
|
|
}
|
|
};
|
|
}} |