80 return pos_[
id.value];
96 const std::int64_t f = side == Side::Buy ? qty.raw : -qty.raw;
97 const std::int64_t cur = p.
qty.
raw;
98 const bool inv = inst.inverse();
101 book(
id, &Totals::fees, fee);
104 if (cur == 0 || (cur > 0) == (f > 0)) {
106 const std::int64_t denom = cur + f;
107 p.
avg_px = inv ? harmonic_avg(cur, p.
avg_px, f, px, denom)
108 : arithmetic_avg(cur, p.
avg_px, f, px, denom);
109 p.
qty = Qty::from_raw(denom);
112 const std::int64_t abs_cur = cur < 0 ? -cur : cur;
113 const std::int64_t abs_f = f < 0 ? -f : f;
114 const std::int64_t closed = abs_f < abs_cur ? abs_f : abs_cur;
115 const std::int64_t dir = cur > 0 ? 1 : -1;
118 r = inst.inverse_pnl(p.
avg_px, px, Qty::from_raw(closed * dir));
121 r = scale(Notional::from_raw(
static_cast<std::int64_t
>(pnl)), inst);
124 realized_total_ += r;
125 book(
id, &Totals::realized, r);
126 const std::int64_t remaining = abs_f - closed;
128 p.
qty = Qty::from_raw(f > 0 ? remaining : -remaining);
131 p.
qty = Qty::from_raw(cur + f);
138 set_exposure(
id, signed_notional(p, px, inst));
160 ? inst.inverse_pnl(est_px, px, qty)
161 : scale(Notional::from_raw(detail::mul_div<kFixedScale>(px.raw - est_px.raw, qty.raw)),
165 realized_total_ += adj;
166 book(
id, &Totals::realized, adj);
169 book(
id, &Totals::fees, fee);
177 if (delta.is_zero())
return;
180 const std::uint32_t fills = p.
fills;
181 on_fill(
id, delta.is_negative() ? Side::Sell :
Side::
Buy, px, delta.abs(),
Notional{}, inst);
194 realized_total_ += amount;
195 book(
id, &Totals::realized, amount);
196 fund_->per[
id.value] += amount;
197 fund_->total += amount;
198 if (FASTMM_UNLIKELY(converting())) fund_->native[ccy_[
id.value]] += amount;
202 FASTMM_ASSERT(
id.value < kMaxInstruments);
203 return fund_->per[
id.value];
208 [[nodiscard]] std::uint64_t changes() const noexcept {
return changes_; }
213 return revalue(pos_[
id.value],
mid, inst);
219 set_unrealized(
id, p, revalue(p, mid, inst));
220 set_exposure(
id, signed_notional(p, mid, inst));
232 if (!qty.is_zero() && !avg_px.is_positive()) {
238 set_unrealized(
id, p, revalue(p, p.
last_mark, inst));
239 set_exposure(
id, signed_notional(p, p.
last_mark, inst));
241 void reset(InstrumentId
id)
noexcept {
246 fees_total_ -= p.
fees;
248 fund_->total -= fund_->per[
id.value];
249 if (converting()) fund_->native[ccy_[
id.value]] -= fund_->per[
id.value];
250 fund_->per[
id.value] = Notional{};
251 set_unrealized(
id, p, Notional{});
252 set_exposure(
id, Notional{});
260 void set_accounting(
const FxPlan& plan)
noexcept {
262 ccy_count_ = plan.count;
263 for (std::size_t i = 0; i <
kMaxInstruments; ++i) ccy_[i] = plan.ccy[i];
264 for (
auto& r : rate_) r = FxRate{};
265 rate_[0] = FxRate::identity();
266 for (
auto& t : native_) t =
Totals{};
267 for (
auto& t : conv_) t =
Totals{};
270 if (!converting())
return;
271 for (std::size_t i = 0; i < kMaxInstruments; ++i) fund_->native[ccy_[i]] += fund_->per[i];
273 for (std::size_t i = 0; i < kMaxInstruments; ++i) {
274 const Position& p = pos_[i];
275 Totals& t = native_[ccy_[i]];
277 t.unrealized += p.unrealized;
279 t.gross += exposure_[i].
abs();
280 t.net += exposure_[i];
285 void set_rate(std::size_t c, FxRate r)
noexcept {
286 if (c == 0 || c >= kMaxCurrencies)
return;
291 [[nodiscard]]
bool converting() const noexcept {
return ccy_count_ > 1; }
292 [[nodiscard]] std::size_t currency_count() const noexcept {
return ccy_count_; }
293 [[nodiscard]] FxRate rate(std::size_t c)
const noexcept {
294 return c < kMaxCurrencies ? rate_[c] : FxRate{};
303 [[nodiscard]]
Notional net_pnl() const noexcept {
return realized + unrealized - fees; }
305 [[nodiscard]]
const Totals& native(std::size_t c)
const noexcept {
return native_[c]; }
314 [[nodiscard]]
Notional gross_exposure() const noexcept {
315 return FASTMM_UNLIKELY(converting()) ? conv_total_.gross : gross_exposure_;
317 [[nodiscard]]
Notional net_exposure() const noexcept {
318 return FASTMM_UNLIKELY(converting()) ? conv_total_.net : net_exposure_;
320 [[nodiscard]]
Notional total_realized() const noexcept {
321 return FASTMM_UNLIKELY(converting()) ? conv_total_.realized : realized_total_;
323 [[nodiscard]]
Notional total_unrealized() const noexcept {
324 return FASTMM_UNLIKELY(converting()) ? conv_total_.unrealized : unrealized_total_;
326 [[nodiscard]]
Notional total_fees() const noexcept {
327 return FASTMM_UNLIKELY(converting()) ? conv_total_.fees : fees_total_;
330 [[nodiscard]]
Notional total_funding() const noexcept {
331 if (!converting())
return fund_->total;
333 for (std::size_t c = 0; c < ccy_count_; ++c) t += convert(fund_->native[c], rate_[c]);
337 [[nodiscard]] Notional native_funding(std::size_t c)
const noexcept {
338 return c < kMaxCurrencies ? fund_->native[c] : Notional{};
340 [[nodiscard]]
Notional net_pnl() const noexcept {
341 return total_realized() + total_unrealized() - total_fees();
343 [[nodiscard]]
Portfolio portfolio() const noexcept {
345 t.realized = total_realized();
346 t.unrealized = total_unrealized();
347 t.fees = total_fees();
348 t.net = t.realized + t.unrealized - t.fees;
354 if (inst.contract_multiplier == Qty::from_int(1))
return n;
355 return Notional::from_raw(
mul_raw(n, inst.contract_multiplier));
358 static Price arithmetic_avg(
359 std::int64_t cur, Price avg, std::int64_t f, Price px, std::int64_t denom)
noexcept {
360 const Int128 num =
static_cast<Int128
>(cur) * avg.
raw +
static_cast<Int128
>(f) * px.raw;
361 return Price::from_raw(
static_cast<std::int64_t
>(num / denom));
365 static Price harmonic_avg(
366 std::int64_t cur, Price avg, std::int64_t f, Price px, std::int64_t denom)
noexcept {
367 if (!px.is_positive())
return avg;
368 Int128 inv =
static_cast<Int128
>(f) * kFixedScale / px.
raw;
369 if (cur != 0 && avg.is_positive()) inv +=
static_cast<Int128
>(cur) * kFixedScale / avg.raw;
370 if (inv == 0)
return px;
371 return Price::from_raw(
372 static_cast<std::int64_t
>(
static_cast<Int128
>(denom) * kFixedScale / inv));
376 static Notional signed_notional(
const Position& p, Price mark,
const Instrument& inst)
noexcept {
381 static Notional revalue(
const Position& p, Price mark,
const Instrument& inst)
noexcept {
383 if (inst.inverse())
return inst.inverse_pnl(p.
avg_px, mark, p.
qty);
385 Notional::from_raw(detail::mul_div<kFixedScale>(mark.raw - p.
avg_px.
raw, p.
qty.
raw)), inst);
389 void set_exposure(InstrumentId
id, Notional e)
noexcept {
390 Notional& cur = exposure_[
id.value];
393 gross_exposure_ += dg;
395 book(
id, &Totals::gross, dg);
396 book(
id, &Totals::net, dn);
399 void set_unrealized(InstrumentId
id, Position& p, Notional u)
noexcept {
401 unrealized_total_ += d;
402 book(
id, &Totals::unrealized, d);
407 FASTMM_FORCE_INLINE
void book(InstrumentId
id, Notional Totals::*f,
Notional d) noexcept {
408 if (FASTMM_UNLIKELY(converting())) book_converted(id, f, d);
410 FASTMM_NOINLINE
void book_converted(InstrumentId
id, Notional Totals::*f,
Notional d) noexcept {
411 const std::uint8_t c = ccy_[id.value];
414 const Notional v = convert(n, rate_[c]);
415 conv_total_.*f += v - conv_[c].*f;
418 void reconvert(std::size_t c)
noexcept {
420 {&Totals::realized, &Totals::unrealized, &Totals::fees, &Totals::gross, &Totals::net}) {
421 const Notional v = convert(native_[c].*f, rate_[c]);
422 conv_total_.*f += v - conv_[c].*f;
433 std::uint8_t ccy_count_ = 0;
434 std::array<std::uint8_t, kMaxInstruments> ccy_{};
435 std::array<FxRate, kMaxCurrencies> rate_{FxRate::identity()};
436 std::array<Totals, kMaxCurrencies> native_{};
437 std::array<Totals, kMaxCurrencies> conv_{};
438 Totals conv_total_{};
439 std::uint64_t changes_ = 0;
444 std::array<Notional, kMaxInstruments> per{};
445 std::array<Notional, kMaxCurrencies> native{};
448 std::unique_ptr<Funding> p = std::make_unique<Funding>();
449 FundingBox() =
default;
450 FundingBox(
const FundingBox& o) : p(std::make_unique<
Funding>(*o.p)) {}
451 FundingBox(FundingBox&&) noexcept = default;
452 FundingBox& operator=(const FundingBox& o) {
453 if (
this != &o) *p = *o.p;
456 FundingBox& operator=(FundingBox&&) noexcept = default;
457 ~FundingBox() = default;
458 Funding* operator->() const noexcept {
return p.get(); }
constexpr std::int64_t opening_qty(std::int64_t q, Side side, std::int64_t ahead, std::int64_t qty) noexcept
The part of an order of side for qty that position q does not absorb once ahead (our open orders on t...
Definition position.hpp:40