122 lines
5.0 KiB
C++
122 lines
5.0 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>
|
|
// 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
|
|
class SecurityLimitNode{
|
|
private:
|
|
const uint32_t _account_id;
|
|
const uint32_t _clip_size;
|
|
ProductLimitNode& _product_node;
|
|
// 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;
|
|
uint32_t _short_pos;
|
|
public:
|
|
SecurityLimitNode(uint32_t account_id,
|
|
uint32_t parent_account_id,
|
|
const bts::api::admin::AccountSecurityPositionLimitUnit& limit,
|
|
bool apply_sec_position_limits,
|
|
ProductLimitNode& product_limit,
|
|
uint32_t clip_size);
|
|
// 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;
|
|
// 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){
|
|
_product_node.add_position(side, qty);
|
|
}
|
|
void remove_working_order(api::Side side, uint32_t qty){
|
|
_product_node.remove_position(side, qty);
|
|
}
|
|
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;
|
|
}
|
|
// risk limit checks ///////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////
|
|
Limiter::LimiterQueryResult can_send_new_order(
|
|
api::Side side,
|
|
Quantity quantity,
|
|
bool enforce_clip = true) const{
|
|
Limiter::LimiterQueryResult result;result.result= Limiter::OK;
|
|
if (enforce_clip && quantity.value() > _clip_size){
|
|
result.result = Limiter::CLIP_SIZE_BREACHED;
|
|
result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})",
|
|
_clip_size, quantity.value());
|
|
return result;
|
|
}
|
|
if (result.ok())
|
|
result = _product_node.can_send_new_order(side, quantity);
|
|
return result;
|
|
}
|
|
Limiter::LimiterQueryResult can_update_order(api::Side side,
|
|
Quantity from,
|
|
Quantity to,
|
|
bool enforce_clip = true) const{
|
|
Limiter::LimiterQueryResult result;result.result=Limiter::OK;
|
|
if (enforce_clip && to.value() > _clip_size){
|
|
result.result = Limiter::CLIP_SIZE_BREACHED;
|
|
result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})",
|
|
_clip_size, to.value());
|
|
return result;
|
|
}
|
|
if (result.ok())
|
|
result = _product_node.can_update_order(side, from, to);
|
|
return result;
|
|
}
|
|
};
|
|
}} |