FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
fastmm::PositionTracker Class Reference

#include <fastmm/core/position.hpp>

Classes

struct  Totals
 Totals of one currency, in that currency (converting() only). More...
 

Public Member Functions

const Position & get (InstrumentId id) const noexcept
 
const Position & operator[] (InstrumentId id) const noexcept
 
void on_fill (InstrumentId id, Side side, Price px, Qty qty, Notional fee, const Instrument &inst) noexcept
 Applies a fill.
 
void correct_fill (InstrumentId id, Side side, Price est_px, Price px, Qty qty, Notional fee, const Instrument &inst) noexcept
 A fill booked from a venue's cumulative quantity, at the order's own price and with no fee, turned out to be an execution at px costing fee.
 
void on_funding (InstrumentId id, Notional amount) noexcept
 A funding payment on the instrument's position, in its settlement currency (negative paid, positive received).
 
Notional funding (InstrumentId id) const noexcept
 Funding booked on one instrument, in its settlement currency (part of its realized PnL).
 
void mark (InstrumentId id, Price mid, const Instrument &inst) noexcept
 Mark-to-market the open position, in the instrument's settlement currency.
 
void set (InstrumentId id, Qty qty, Price avg_px, const Instrument &inst) noexcept
 Reconciliation: overwrite qty/avg with the venue's view.
 
void reset (InstrumentId id) noexcept
 
void set_accounting (const FxPlan &plan) noexcept
 Accounting across settlement currencies.
 
void set_rate (std::size_t c, FxRate r) noexcept
 The rate of currency c (an index of the plan): the totals in it are converted again.
 
bool converting () const noexcept
 
std::size_t currency_count () const noexcept
 
FxRate rate (std::size_t c) const noexcept
 
const Totals & native (std::size_t c) const noexcept
 
Notional gross_exposure () const noexcept
 Totals over every instrument, kept up to date by the updates above (the engine checks net_pnl() on every market-data event; summing kMaxInstruments positions there cost more than the rest of the event).
 
Notional net_exposure () const noexcept
 
Notional total_realized () const noexcept
 
Notional total_unrealized () const noexcept
 
Notional total_fees () const noexcept
 
Notional total_funding () const noexcept
 Funding over every instrument; part of total_realized().
 
Notional native_funding (std::size_t c) const noexcept
 Funding of one currency, in that currency (converting() only).
 
Notional net_pnl () const noexcept
 
Portfolio portfolio () const noexcept
 

Member Function Documentation

◆ get()

const Position & fastmm::PositionTracker::get ( InstrumentId  id) const
inlinenoexcept

◆ operator[]()

const Position & fastmm::PositionTracker::operator[] ( InstrumentId  id) const
inlinenoexcept

◆ on_fill()

void fastmm::PositionTracker::on_fill ( InstrumentId  id,
Side  side,
Price  px,
Qty  qty,
Notional  fee,
const Instrument &  inst 
)
inlinenoexcept

Applies a fill.

PnL is scaled by the instrument's contract multiplier and, for an inverse (coin-margined) contract, computed in the base coin: qty * multiplier * (1/avg - 1/px). The average entry price of an inverse position is the size-weighted harmonic mean, which is what makes that exact.

Opening / adding: a new average over the old position and this fill.

Reducing / flipping: realise on the closed part in the position's direction.

< flipped through zero: the remainder opens at px

◆ correct_fill()

void fastmm::PositionTracker::correct_fill ( InstrumentId  id,
Side  side,
Price  est_px,
Price  px,
Qty  qty,
Notional  fee,
const Instrument &  inst 
)
inlinenoexcept

A fill booked from a venue's cumulative quantity, at the order's own price and with no fee, turned out to be an execution at px costing fee.

Its quantity is already in the position, so on_fill() would count it twice; what was never booked is what it cost. Paying px instead of est_px for qty is worth qty * (px - est_px), given up on a buy and received on a sell, and that difference is exactly the error the estimate left in this position's PnL. The average entry price keeps the estimate: moving it would need the position the fill was booked into, which is history, and the error it carries is the one this correction has already taken out.

◆ on_funding()

void fastmm::PositionTracker::on_funding ( InstrumentId  id,
Notional  amount 
)
inlinenoexcept

A funding payment on the instrument's position, in its settlement currency (negative paid, positive received).

It is realized PnL, not a fee: it is not a cost of trading but the carry of holding the position, and a position that is never traded pays it all the same. funding() and total_funding() report the part of realized that it is.

◆ funding()

Notional fastmm::PositionTracker::funding ( InstrumentId  id) const
inlinenoexcept

Funding booked on one instrument, in its settlement currency (part of its realized PnL).

◆ mark()

void fastmm::PositionTracker::mark ( InstrumentId  id,
Price  mid,
const Instrument &  inst 
)
inlinenoexcept

Mark-to-market the open position, in the instrument's settlement currency.

◆ set()

void fastmm::PositionTracker::set ( InstrumentId  id,
Qty  qty,
Price  avg_px,
const Instrument &  inst 
)
inlinenoexcept

Reconciliation: overwrite qty/avg with the venue's view.

Realized PnL and fees are history and are kept; the unrealized PnL of the new position is remeasured at the last mark, so the totals the max-loss budget reads stay consistent with the positions. A record without an average price (zero: the venue gave none, or none that could be read) keeps the tracked one when the position stays on the same side, else takes the last mark: valued at an average of 0, a short of 0.0014 BTC is a loss of its whole notional, which trips max_loss.

◆ reset()

void fastmm::PositionTracker::reset ( InstrumentId  id)
inlinenoexcept

◆ set_accounting()

void fastmm::PositionTracker::set_accounting ( const FxPlan &  plan)
inlinenoexcept

Accounting across settlement currencies.

With a plan of two or more currencies the tracker also keeps the totals per currency and converts them at the rate last set for each (set_rate), and the totals below are in the reporting currency. A currency whose rate was never set counts as zero. Without one (the default) nothing is converted and every Notional is added as it is.

What is booked already (a tracker configured after fills) moves into its currencies.

◆ set_rate()

void fastmm::PositionTracker::set_rate ( std::size_t  c,
FxRate  r 
)
inlinenoexcept

The rate of currency c (an index of the plan): the totals in it are converted again.

◆ converting()

bool fastmm::PositionTracker::converting ( ) const
inlinenoexcept

◆ currency_count()

std::size_t fastmm::PositionTracker::currency_count ( ) const
inlinenoexcept

◆ rate()

FxRate fastmm::PositionTracker::rate ( std::size_t  c) const
inlinenoexcept

◆ native()

const Totals & fastmm::PositionTracker::native ( std::size_t  c) const
inlinenoexcept

◆ gross_exposure()

Notional fastmm::PositionTracker::gross_exposure ( ) const
inlinenoexcept

Totals over every instrument, kept up to date by the updates above (the engine checks net_pnl() on every market-data event; summing kMaxInstruments positions there cost more than the rest of the event).

Without conversion Notional carries no currency, so they are only meaningful when every instrument settles in the same one: InstrumentTable::settlement_mix() finds a table that mixes them, and fastmm-live refuses to start on one while [risk] max_loss is set and [accounting] does not cover it. Portfolio exposure at the last marks: the sum of |position| and the signed sum.

◆ net_exposure()

Notional fastmm::PositionTracker::net_exposure ( ) const
inlinenoexcept

◆ total_realized()

Notional fastmm::PositionTracker::total_realized ( ) const
inlinenoexcept

◆ total_unrealized()

Notional fastmm::PositionTracker::total_unrealized ( ) const
inlinenoexcept

◆ total_fees()

Notional fastmm::PositionTracker::total_fees ( ) const
inlinenoexcept

◆ total_funding()

Notional fastmm::PositionTracker::total_funding ( ) const
inlinenoexcept

Funding over every instrument; part of total_realized().

◆ native_funding()

Notional fastmm::PositionTracker::native_funding ( std::size_t  c) const
inlinenoexcept

Funding of one currency, in that currency (converting() only).

◆ net_pnl()

Notional fastmm::PositionTracker::net_pnl ( ) const
inlinenoexcept

◆ portfolio()

Portfolio fastmm::PositionTracker::portfolio ( ) const
inlinenoexcept