1#include <slick/logger.hpp>
11 for (std::size_t broker = 0; broker < journaled_.size(); ++broker) {
14 store.
for_each([
this](uint32_t slot, std::string_view strategy_id,
const Position& position,
15 std::span<const PositionLot> lots) {
16 auto& pos = positions_[std::string(strategy_id)][position.
instrument];
17 pos.position = position;
18 pos.lots.assign(lots.begin(), lots.end());
19 pos.store_slot = slot;
23 LOG_INFO(
"PositionTracker: restored {} {} pos: {} avg: {} realized_pnl: {} ({} open lot(s))",
29void PositionTracker::persist(
const std::string& strategy_id, PositionWithLots& pos,
const OrderFillEvent* fill) {
31 pos.store_slot = store_->
save(pos.store_slot, strategy_id, pos.position, pos.lots,
37 const auto broker =
static_cast<std::size_t
>(fill.
broker_id);
56 auto& strategy_positions = positions_[strategy_id];
57 auto& pos_with_lots = strategy_positions[instrument];
59 if (pos_with_lots.position.instrument.empty()) {
60 pos_with_lots.position.instrument = instrument;
72 :
compute_fee(fill_price, fill_qty, fee_structure_);
74 apply_fill(pos_with_lots, is_buy, fill_qty, fill_price, timestamp, fee);
75 pos_with_lots.position.last_update_time = timestamp;
76 persist(strategy_id, pos_with_lots, &fill);
78 LOG_INFO(
"PositionTracker: {} {} fill: {} {}@{} fee:{} | pos: {} avg: {} realized_pnl: {} realized_fees: {} open_fees: {}",
79 strategy_id, instrument,
80 is_buy ?
"BUY" :
"SELL",
89void PositionTracker::apply_fill(PositionWithLots& pos,
bool is_buy,
qty_t fill_qty,
94 LOG_ERROR(
"PositionTracker: fill has no fill_price — skipping P&L update");
97 Position& p = pos.position;
98 std::vector<Lot>& lots = pos.lots;
100 const bool position_is_long = p.is_long();
101 const bool position_is_short = p.is_short();
102 const bool position_is_flat = p.is_flat();
106 if (position_is_flat || (is_buy && position_is_long) || (!is_buy && position_is_short)) {
107 const qty_t signed_fill = is_buy ? fill_qty : -fill_qty;
109 if (!position_is_flat) {
110 p.avg_price = calculate_new_avg_price(p.avg_price, p.quantity, fill_price, signed_fill);
112 p.avg_price = fill_price;
115 p.quantity += signed_fill;
116 p.open_fees += fee_amount;
117 lots.push_back(Lot{fill_qty, fill_price, timestamp});
123 qty_t remaining_qty = fill_qty;
125 while (remaining_qty > 0 && !lots.empty()) {
126 Lot& oldest_lot = lots.front();
127 const qty_t close_qty = std::min(remaining_qty, oldest_lot.quantity);
131 const qty_t current_qty = std::abs(p.quantity);
132 const price_t entry_fee_transfer = (current_qty > 0)
137 const price_t exit_fee_portion = (fill_qty > 0)
141 p.realized_fees += entry_fee_transfer + exit_fee_portion;
142 p.open_fees -= entry_fee_transfer;
145 const double pnl_per_unit = is_buy
152 const qty_t signed_close = is_buy ? close_qty : -close_qty;
153 p.quantity += signed_close;
156 oldest_lot.quantity -= close_qty;
157 if (oldest_lot.quantity == 0) {
158 lots.erase(lots.begin());
161 remaining_qty -= close_qty;
166 if (remaining_qty > 0) {
167 const qty_t signed_remaining = is_buy ? remaining_qty : -remaining_qty;
168 p.quantity += signed_remaining;
169 p.avg_price = fill_price;
170 lots.push_back(Lot{remaining_qty, fill_price, timestamp});
173 p.open_fees = (fill_qty > 0)
193 if (!std::isfinite(multiplier) || multiplier <= 0.0) {
194 LOG_ERROR(
"PositionTracker: rejecting contract multiplier {} for {}", multiplier, instrument);
198 const std::string key(instrument);
199 contract_multipliers_[key] = multiplier;
202 for (
auto& [strategy_id, strategy_positions] : positions_) {
203 auto it = strategy_positions.find(key);
204 if (it != strategy_positions.end() && it->second.position.multiplier != multiplier) {
205 it->second.position.multiplier = multiplier;
206 persist(strategy_id, it->second);
213 auto it = contract_multipliers_.find(std::string(instrument));
214 return it == contract_multipliers_.end() ? 1.0 : it->second;
220 auto strategy_it = positions_.find(strategy_id);
221 if (strategy_it == positions_.end()) {
225 auto pos_it = strategy_it->second.find(std::string(instrument));
226 if (pos_it == strategy_it->second.end()) {
230 return pos_it->second.position;
235 std::unordered_map<std::string, Position> result;
237 auto strategy_it = positions_.find(strategy_id);
238 if (strategy_it == positions_.end()) {
242 for (
const auto& [instrument, pos_with_lots] : strategy_it->second) {
243 result.emplace(instrument, pos_with_lots.position);
249std::unordered_map<std::string, std::unordered_map<std::string, Position>>
252 std::unordered_map<std::string, std::unordered_map<std::string, Position>> result;
254 for (
const auto& [strategy_id, strategy_positions] : positions_) {
255 auto& strategy_result = result[strategy_id];
256 for (
const auto& [instrument, pos_with_lots] : strategy_positions) {
257 strategy_result.emplace(instrument, pos_with_lots.position);
265 positions_.erase(strategy_id);
267 store_->erase_strategy(strategy_id);
269 LOG_INFO(
"PositionTracker: Cleared positions for strategy {}", strategy_id);
277 LOG_INFO(
"PositionTracker: Cleared all positions");
void for_each(const Visitor &visit) const
uint32_t save(uint32_t slot, std::string_view strategy_id, const Position &position, std::span< const PositionLot > lots, BrokerId broker=BrokerId::UNKNOWN, uint64_t journal_seq=0)
uint64_t journaled(BrokerId broker) const noexcept
void clear_strategy_positions(const std::string &strategy_id)
bool set_contract_multiplier(std::string_view instrument, double multiplier)
std::unordered_map< std::string, std::unordered_map< std::string, Position > > get_all_positions() const
void attach_store(PositionStore &store)
double get_contract_multiplier(std::string_view instrument) const
void fill_dispatched(const OrderFillEvent &fill) noexcept
void on_fill(const std::string &strategy_id, const OrderFillEvent &fill)
Position get_position(const std::string &strategy_id, std::string_view instrument) const
double from_price_t(price_t value)
price_t to_price_t(double value)
price_t compute_fee(price_t fill_price, qty_t fill_qty, const FeeStructure &fee)
constexpr price_t NULL_PRICE
double from_qty_t(qty_t value)
char instrument[MAX_INSTRUMENT_LENGTH]
double contract_multiplier