Comments added and some cleanup for the position update related changes to SecurityLimitNode and ProductLimitNode

This commit is contained in:
Taha Afzal committed 2019-08-05 19:38:43 -04:00
1 parent c42bc45f24
commit 03e2433201
4 files changed
+100 -70

No files matched your search

+10 -8
View File
@@ -5,14 +5,16 @@ namespace orderman {
ProductLimitNode::ProductLimitNode(
uint32_t account_id,
uint32_t, // parent_account_id,
uint32_t,
const bts::api::admin::AccountProductPositionLimitUnit &limit,
bool enforce_net)
: _account_id(account_id),
//_parent_account_id(parent_account_id),
_limit(limit), _enforce_net(enforce_net),
_long_pos_limit(limit.long_position_limit()),
_short_pos_limit(limit.short_position_limit()), _long_pos(0),
_short_pos(0) {}
:
_account_id(account_id),
_limit(limit),
_enforce_net(enforce_net),
_long_pos_limit(limit.long_position_limit()),
_short_pos_limit(limit.short_position_limit()),
_long_pos(0),
_short_pos(0) {}
} // namespace orderman
} // namespace bts
} // namespace bts
+48 -35
View File
@@ -11,32 +11,37 @@
namespace bts{
namespace orderman{
// Keeps the running position and risk limits
// for a single <product, account>
// Its the equivelant to SecurityLimitNode
// however it does not need to check for
// clip size.
// Scope : <product, account>
// All the <product, account> level position limits are
// enforced here.
// Keeps the running long and short position (both working
// orders and positions - which is transparent to this class)
// and risk limits
class ProductLimitNode{
private:
const uint32_t _account_id; // , _parent_account_id;
const uint32_t _account_id;
// this is only kept to faciliate testing
const bts::api::admin::AccountProductPositionLimitUnit& _limit;
const bool _enforce_net;
const uint32_t _long_pos_limit, _short_pos_limit;
// this is the only state here
uint32_t _long_pos, _short_pos;
public:
ProductLimitNode(uint32_t account_id,
uint32_t parent_account_id,
const bts::api::admin::AccountProductPositionLimitUnit& limit,
bool enforce_net);
// accessors
////////////
uint32_t account_id() const {return _account_id;}
ProductLimitNode(
uint32_t account_id,
uint32_t parent_account_id,
const bts::api::admin::AccountProductPositionLimitUnit& limit,
bool enforce_net);
// accessors ///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
uint32_t account_id() const {
return _account_id;
}
int net_pos() const {
return static_cast<int>(_long_pos)-static_cast<int>(_short_pos);
}
// position updates
///////////////////
// position updates ////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void copy_positions_from(const ProductLimitNode& source){
_long_pos=source._long_pos;
_short_pos=source._short_pos;
@@ -55,37 +60,42 @@ public:
}
// risk limit checks
////////////////////
Limiter::LimiterQueryResult can_send_new_order(api::Side side, Quantity quantity) const{
Limiter::LimiterQueryResult can_send_new_order(api::Side side,
Quantity quantity) const{
Limiter::LimiterQueryResult result;
if (_enforce_net)
LimiterUtils::can_send_new_order_net (side,
quantity,
false,
static_cast<int>(_long_pos_limit),
-1 * static_cast<int>(_short_pos_limit),
net_pos(),
result);
LimiterUtils::can_send_new_order_net (
side,
quantity,
false,
static_cast<int>(_long_pos_limit),
-1 * static_cast<int>(_short_pos_limit),
net_pos(),
result);
else
LimiterUtils::can_send_new_order_abs (side,
LimiterUtils::can_send_new_order_abs (side,
quantity,
_long_pos_limit,
_long_pos_limit,
_short_pos_limit,
_long_pos,
_short_pos,
result);
return result;
}
Limiter::LimiterQueryResult can_update_order(api::Side side, Quantity from, Quantity to) const{
Limiter::LimiterQueryResult can_update_order(api::Side side,
Quantity from,
Quantity to) const{
Limiter::LimiterQueryResult result;
if(_enforce_net)
LimiterUtils::can_update_order_net (side,
from,
to,
false,
static_cast<int>(_long_pos_limit),
-1 * static_cast<int>(_short_pos_limit),
net_pos(),
result);
LimiterUtils::can_update_order_net (
side,
from,
to,
false,
static_cast<int>(_long_pos_limit),
-1 * static_cast<int>(_short_pos_limit),
net_pos(),
result);
else
LimiterUtils::can_update_order_abs (side,
from,
@@ -108,6 +118,9 @@ public:
}
// test only functions
//////////////////////
const bts::api::admin::AccountProductPositionLimitUnit& test_only_func_limit() const {return _limit;}
const bts::api::admin::AccountProductPositionLimitUnit&
test_only_func_limit() const {
return _limit;
}
};
}}
@@ -13,10 +13,10 @@ SecurityLimitNode::SecurityLimitNode(
:
_account_id(account_id),
_clip_size(clip_size),
_product_node(product_node),
_long_pos(0),
_short_pos(0)
{}
_product_node(product_node),
_long_pos(0),
_short_pos(0)
{}
} // namespace orderman
} // namespace bts
} // namespace bts
+37 -22
View File
@@ -16,16 +16,22 @@
namespace bts{
namespace orderman{
// Keeps the running position and risk limits
// (both position limits and clip sizes) for a
// single <security, account>
// It also keeps a reference to the product
// node, that is updated as fills come in.
// 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:
@@ -35,12 +41,13 @@ public:
bool apply_sec_position_limits,
ProductLimitNode& product_limit,
uint32_t clip_size);
//accessors
///////////
uint32_t account_id() const {return _account_id;}
//int net_pos() const {return 0;}
// position updates
///////////////////
// 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;
@@ -60,23 +67,29 @@ public:
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();
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);
_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);
_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);
_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);
_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,
// 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;
@@ -90,8 +103,10 @@ public:
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 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;
@@ -104,4 +119,4 @@ public:
return result;
}
};
}}
}}