143 lines
4.7 KiB
C++
143 lines
4.7 KiB
C++
#include "RiskLimiter.h"
|
|
|
|
#include "OperatorRiskLimiter.h"
|
|
#include "AccountRiskLimiter.h"
|
|
|
|
#include "lib/config/config.h"
|
|
#include "lib/api/services/AccountService.pb.h"
|
|
#include "lib/api/services/UserService.pb.h"
|
|
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/logging/logging.h"
|
|
|
|
#include <fmt/format.h>
|
|
|
|
using namespace bts::orderman2;
|
|
|
|
namespace bts
|
|
{
|
|
namespace risklimits
|
|
{
|
|
|
|
struct RiskLimiter::PImpl
|
|
{
|
|
OperatorRiskLimiter& _operator;
|
|
SharedLimiterData& shared;
|
|
|
|
orderman2::OrderKey key;
|
|
Quantity remaining_quantity;
|
|
Quantity filled_quantity;
|
|
|
|
PImpl(OperatorRiskLimiter& oplim, orderman2::OrderKey ok) :
|
|
_operator(oplim),
|
|
shared(oplim.account_limiter().shared_data()),
|
|
key(ok),
|
|
remaining_quantity(0),
|
|
filled_quantity(0)
|
|
{
|
|
}
|
|
bool check_can_trade(std::string& message) const
|
|
{
|
|
if(!shared.account_info.can_trade())
|
|
{
|
|
const std::string& acct = shared.account_info.tag1();
|
|
message = fmt::format("Account {} does not have permission to trade. {}", acct, shared.info_string);
|
|
return false;
|
|
}
|
|
if(!_operator.operator_can_trade())
|
|
{
|
|
std::string opid; to_str(_operator.operator_key().value(), &opid);
|
|
message = fmt::format("Operator {} does not have permission to trade. {}", opid, shared.info_string);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool check_clip_size(Quantity qty, std::string& message) const
|
|
{
|
|
if(qty.value() > shared.risk_limit.max_order_size())
|
|
{
|
|
message = fmt::format("Max Clip Size Violation: quantity {} violates limit {}. {}",
|
|
qty.value(), shared.risk_limit.max_order_size(), shared.info_string);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool check_position_limit(Quantity qty, api::Side side, std::string& message) const
|
|
{
|
|
uint32_t longpos = shared.position_info.long_position + shared.position_info.long_working + qty.value();
|
|
if(side == api::SIDE_BUY &&
|
|
longpos > shared.risk_limit.max_position_long())
|
|
{
|
|
message = fmt::format("Max Position Long Violation: quantity {} violates limit {}. {}",
|
|
qty.value(), shared.risk_limit.max_position_long(), shared.info_string);
|
|
return false;
|
|
}
|
|
uint32_t shortpos = shared.position_info.short_position + shared.position_info.short_working + qty.value();
|
|
if(side == api::SIDE_SELL &&
|
|
shortpos > shared.risk_limit.max_position_short())
|
|
{
|
|
message = fmt::format("Max Position Short Violation: quantity {} violates limit {}. {}",
|
|
qty.value(), shared.risk_limit.max_position_short(), shared.info_string);
|
|
return false;
|
|
}
|
|
|
|
int position_change = (side == api::SIDE_SELL ? -1 : 1) * qty.value();
|
|
int net_pos = (int)shared.position_info.long_position - (int)shared.position_info.short_position;
|
|
if(abs(net_pos + position_change) > (int)shared.risk_limit.max_position_net())
|
|
{
|
|
message = fmt::format("Max Position Net Violation: quantity {} violates limit {}. {}",
|
|
qty.value(), shared.risk_limit.max_position_net(), shared.info_string);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
RiskLimiter::RiskLimiter(OperatorRiskLimiter& sh, orderman2::OrderKey key)
|
|
: me(new PImpl(sh, key))
|
|
{
|
|
}
|
|
|
|
RiskLimiter::~RiskLimiter() {}
|
|
|
|
RiskLimiter::RiskLimitResult RiskLimiter::check(Quantity qty, api::Side side) const
|
|
{
|
|
RiskLimiter::RiskLimitResult res;
|
|
res.success = me->check_can_trade(res.message);
|
|
if(!res.success)
|
|
return res;
|
|
res.success = me->check_clip_size(qty, res.message);
|
|
if(!res.success)
|
|
return res;
|
|
res.success = me->check_position_limit(qty, side, res.message);
|
|
|
|
return res;
|
|
}
|
|
|
|
void RiskLimiter::on_order_state(api::Side side, Quantity quantity, Quantity filled_quantity)
|
|
{
|
|
auto remaining = Quantity(quantity.value() - filled_quantity.value());
|
|
int working_diff = remaining.value() - me->remaining_quantity.value();
|
|
int filled_diff = filled_quantity.value() - me->filled_quantity.value();
|
|
if(side == api::SIDE_BUY)
|
|
{
|
|
me->shared.position_info.long_working += working_diff;
|
|
me->shared.position_info.long_position += filled_diff;
|
|
}
|
|
else
|
|
{
|
|
me->shared.position_info.short_working += working_diff;
|
|
me->shared.position_info.short_position += filled_diff;
|
|
}
|
|
me->remaining_quantity = remaining;
|
|
me->filled_quantity = filled_quantity;
|
|
}
|
|
|
|
SharedLimiterData& RiskLimiter::shared_data()
|
|
{
|
|
return me->shared;
|
|
}
|
|
|
|
}
|
|
}
|