Struct PyBook
pub struct PyBook {
pub(crate) inner: Book,
}Expand description
A collection of trades valued together.
Fields§
§inner: BookImplementations§
§impl PyBook
impl PyBook
pub(crate) fn new() -> Self
pub(crate) fn new() -> Self
An empty book.
pub(crate) fn add(
&self,
id: &str,
instrument: &PyInstrument,
quantity: f64,
) -> PyResult<Self>
pub(crate) fn add( &self, id: &str, instrument: &PyInstrument, quantity: f64, ) -> PyResult<Self>
Books quantity of instrument under id. The new book carries this
book’s market.
pub(crate) fn extend(
&self,
trades: Vec<(String, PyInstrument, f64)>,
) -> PyResult<Self>
pub(crate) fn extend( &self, trades: Vec<(String, PyInstrument, f64)>, ) -> PyResult<Self>
Books every (id, instrument, quantity) of trades at once. The new
book carries this book’s market.
The form for a book of many trades: add copies the book — every
trade already booked and the attached market — per call, so booking
thousands of trades one add at a time is quadratic. This copies it
once. A non-finite quantity is refused naming its trade, and nothing
is booked.
pub(crate) fn from_fpml(
_cls: &Bound<'_, PyType>,
documents: Vec<Bound<'_, PyAny>>,
party: Option<String>,
counterparties: Option<Vec<PyRef<'_, PyCounterparty>>>,
credit: Option<HashMap<String, String>>,
reporting: Option<&str>,
strict: bool,
) -> PyResult<(Self, PyRecords)>
pub(crate) fn from_fpml( _cls: &Bound<'_, PyType>, documents: Vec<Bound<'_, PyAny>>, party: Option<String>, counterparties: Option<Vec<PyRef<'_, PyCounterparty>>>, credit: Option<HashMap<String, String>>, reporting: Option<&str>, strict: bool, ) -> PyResult<(Self, PyRecords)>
A book of every trade documents hold, with what was not booked.
Each document is its text, labelled by its one-based position, or a
(label, text) pair. Every trade is read as party, its CSA from
counterparties and its credit identities from credit, as
fpml.read_all reads them; each component is booked under the
trade’s identifier, or label#position where it states none. The
book reports in reporting where given.
Returns (book, refusals), the refusals a Records with the
columns source, position, trade, element, reason. With strict,
the first refusal raises ValueError and nothing is booked.
pub(crate) fn reporting_in(&self, code: &str) -> PyResult<Self>
pub(crate) fn reporting_in(&self, code: &str) -> PyResult<Self>
The book reporting in code: every trade’s value converted to that
currency at the market’s spot. The new book carries this book’s market.
pub(crate) fn attach_market(
&mut self,
py: Python<'_>,
market: Option<&PyMarket>,
) -> PyResult<()>
pub(crate) fn attach_market( &mut self, py: Python<'_>, market: Option<&PyMarket>, ) -> PyResult<()>
Attaches market to this book in place — it returns None — and
fetches what the book needs for market’s date (today where it has
none) from the market’s data source.
Unlike every with_* on Book, it changes its receiver and may do IO:
the market is the book’s from here on, and every verb values against
it. A market holding everything the book reads attaches without
calling any source; an offline one (with_data_source(None)) never
fetches. With no market, an undated MarketContext() carrying the
default source is attached.
pub(crate) fn market_context(&self) -> Option<PyMarket>
pub(crate) fn market_context(&self) -> Option<PyMarket>
The market this book carries, or None.
pub(crate) fn fetch_market_data(
&mut self,
py: Python<'_>,
as_of: NaiveDate,
) -> PyResult<()>
pub(crate) fn fetch_market_data( &mut self, py: Python<'_>, as_of: NaiveDate, ) -> PyResult<()>
Fetches what this book reads at as_of from its market’s data source
and keeps it — the explicit form of the fetch every verb makes for a
date it lacks, as the Rust book’s fetch_market_data. An undated
market is attached first where none is. On a refusal it raises the
verbs’ SnapshotError (or the source’s own exception) and leaves the
market as it was; fetch_report() says what was asked either way.
pub(crate) fn fetch_report(&self) -> PyRecords
pub(crate) fn fetch_report(&self) -> PyRecords
What the book’s last fetch asked for and got: family (strip,
fixing, fx, unresolved), key and status (answered,
missing, absent). Empty before any fetch.
pub(crate) fn recipe_ask(
&self,
py: Python<'_>,
as_of: Option<NaiveDate>,
) -> PyResult<Option<PyRecipeAsk>>
pub(crate) fn recipe_ask( &self, py: Python<'_>, as_of: Option<NaiveDate>, ) -> PyResult<Option<PyRecipeAsk>>
The first half of a fetch: the RecipeAsk a source must answer before
the demand at as_of can be stated, or None where nothing is to be
asked. With demand_for and install, the halves an async caller
drives: recipe_ask → await recipes → demand_for → await fetch
→ install.
pub(crate) fn demand_for(
&mut self,
py: Python<'_>,
as_of: NaiveDate,
recipes: Vec<PyRef<'_, PyRecipeSpec>>,
) -> PyResult<PyDemand>
pub(crate) fn demand_for( &mut self, py: Python<'_>, as_of: NaiveDate, recipes: Vec<PyRef<'_, PyRecipeSpec>>, ) -> PyResult<PyDemand>
The second half’s start: recipes (a source’s answer to
recipe_ask, as curves.RecipeSpec values) kept as the source’s
recipes for as_of, this book’s market moved to as_of, and that
date’s Demand — empty where nothing is missing.
pub(crate) fn install(
&mut self,
py: Python<'_>,
demand: &PyDemand,
data: &PyMarketData,
) -> PyResult<()>
pub(crate) fn install( &mut self, py: Python<'_>, demand: &PyDemand, data: &PyMarketData, ) -> PyResult<()>
The last half: data (a source’s answer to demand) built into this
book’s market at the demand’s date and kept, and fetch_report()
written — on a refusal as well, which raises SnapshotError naming
every gap and leaves the market as it was.
pub(crate) fn implied_vol(
&mut self,
py: Python<'_>,
premium: Option<f64>,
as_of: Option<NaiveDate>,
) -> PyResult<f64>
pub(crate) fn implied_vol( &mut self, py: Python<'_>, premium: Option<f64>, as_of: Option<NaiveDate>, ) -> PyResult<f64>
The single flat volatility under which every option in the book,
priced at it, sums to premium — or, with none given, to the book’s
own value: the quote a straddle or strangle trades at. The book must
be one strategy: FX options on one pair, or surface-engine European
swaptions in one currency.
pub(crate) fn under(
&mut self,
py: Python<'_>,
scenario: &PyScenario,
as_of: Option<NaiveDate>,
) -> PyResult<Self>
pub(crate) fn under( &mut self, py: Python<'_>, scenario: &PyScenario, as_of: Option<NaiveDate>, ) -> PyResult<Self>
This book on its market under scenario, at as_of: a new book whose
market is the shocked one. What the book lacks at as_of is fetched
first, so the shock always reaches the fetched market.
pub(crate) fn credit_bumps(
&mut self,
py: Python<'_>,
kind: &str,
bump: f64,
as_of: Option<NaiveDate>,
) -> PyResult<PyRecords>
pub(crate) fn credit_bumps( &mut self, py: Python<'_>, kind: &str, bump: f64, as_of: Option<NaiveDate>, ) -> PyResult<PyRecords>
A bumped sensitivity, one (key, value) row per credit index or
entity: kind is correlation (each index’s base correlation, bumped
in parallel), recovery (each entity’s market recovery, survival curve
held) or recovery-quote-held (CDS quotes held), each the
central-difference value change for a bump.
pub(crate) fn explain_pnl(
&mut self,
py: Python<'_>,
as_of1: NaiveDate,
as_of2: NaiveDate,
order: Option<Vec<Named<RiskAxis>>>,
) -> PyResult<PyExplainReport>
pub(crate) fn explain_pnl( &mut self, py: Python<'_>, as_of1: NaiveDate, as_of2: NaiveDate, order: Option<Vec<Named<RiskAxis>>>, ) -> PyResult<PyExplainReport>
The step-revaluation explain of this book from its market at as_of1
to as_of2, moving the axes in order (the core’s default where
None). Within one day (as_of1 == as_of2) the snapshot held is the
start and a fresh fetch of the same date — kept as the book’s — is the
end. Across days the end is the book’s fetch at as_of2, and the legs
open with cash, paid after as_of1 and up to as_of2, and carry,
the book on as_of1’s quotes rebuilt at as_of2 less its value at
as_of1.
pub(crate) fn predicted_pnl(
&mut self,
py: Python<'_>,
as_of1: NaiveDate,
as_of2: NaiveDate,
) -> PyResult<PyPredictedPnl>
pub(crate) fn predicted_pnl( &mut self, py: Python<'_>, as_of1: NaiveDate, as_of2: NaiveDate, ) -> PyResult<PyPredictedPnl>
The predicted explain within one day, on the terms of explain_pnl;
across days it is refused.
pub(crate) fn reset_pnl(
&mut self,
py: Python<'_>,
as_of1: NaiveDate,
as_of2: NaiveDate,
) -> PyResult<PyResetPnl>
pub(crate) fn reset_pnl( &mut self, py: Python<'_>, as_of1: NaiveDate, as_of2: NaiveDate, ) -> PyResult<PyResetPnl>
The reset P&L within one day, on the terms of explain_pnl; across
days it is refused.
pub(crate) fn through(&self, lens: &PyLens) -> Self
pub(crate) fn through(&self, lens: &PyLens) -> Self
The book restricted to the trades lens admits, carrying this book’s
market.
pub(crate) fn trades(&self) -> Vec<(String, PyInstrument, f64)>
pub(crate) fn trades(&self) -> Vec<(String, PyInstrument, f64)>
The book’s trades as (id, instrument, quantity), in booking order.
pub(crate) fn report(
&mut self,
py: Python<'_>,
measures: Vec<Bound<'_, PyAny>>,
as_of: Option<NaiveDate>,
) -> PyResult<PyRecords>
pub(crate) fn report( &mut self, py: Python<'_>, measures: Vec<Bound<'_, PyAny>>, as_of: Option<NaiveDate>, ) -> PyResult<PyRecords>
measures from one risk pass at as_of, as (measure, label, value, scale, curve, date) rows in the order asked.
Each measure is a preset (hlx.risk.rates.PAR_LADDER), a
risk.Measure value, or a spelling — "npv",
"delta:<axis>[:<basis>[:<aggregation>]][:<scale>]", or a preset’s
name. A preset asked for by name reports in its default scale
unless the measure or the book states another; a spelling reports per
unit unless the book states one.
measure is the preset’s name where the measure is one, and its
spelling otherwise; scale is the unit value is in. The label
carries which row of a measure this is: empty for a measure that
produces one number, and the curve, the bucket or the ladder point
otherwise.
pub(crate) fn scaled(&self, scales: &PyScales) -> Self
pub(crate) fn scaled(&self, scales: &PyScales) -> Self
The book reporting each quantity kind in scales wherever a measure
states no scale of its own. The new book carries this book’s market.
pub(crate) fn available_measures(
&self,
py: Python<'_>,
as_of: Option<NaiveDate>,
) -> PyResult<PyRecords>
pub(crate) fn available_measures( &self, py: Python<'_>, as_of: Option<NaiveDate>, ) -> PyResult<PyRecords>
The catalogued measures this book’s trades can answer — where its
market holds a snapshot at as_of, only those that snapshot can
supply — as the catalogue’s records. Never fetches.
pub(crate) fn market_vega(
&mut self,
py: Python<'_>,
fit: &PyHullWhiteFit,
as_of: Option<NaiveDate>,
) -> PyResult<PyRecords>
pub(crate) fn market_vega( &mut self, py: Python<'_>, fit: &PyHullWhiteFit, as_of: Option<NaiveDate>, ) -> PyResult<PyRecords>
MarketContext vega per quoted swaption: ((expiry, end), vega) rows, from
one risk pass and the fit’s quote sensitivities.
The book’s market must hold the fit pinned — the pass needs the model’s σ as an exogenous input to have a σ row at all.
pub(crate) fn npv(
&mut self,
py: Python<'_>,
as_of: Option<NaiveDate>,
) -> PyResult<f64>
pub(crate) fn npv( &mut self, py: Python<'_>, as_of: Option<NaiveDate>, ) -> PyResult<f64>
The book’s present value at as_of — the attached market’s date where
it has one, today otherwise — fetching first whatever the book lacks
there from the market’s data source.
Instantiates the passive mode: nothing is recorded, and no tape exists for a caller to know about.
The interpreter lock is released for the valuation: every value the
core reads is owned or reference-counted on the Rust side, so another
Python thread may value its own book at the same time. One book is not
shared between threads — a second thread calling it meanwhile raises
RuntimeError; give each thread its own copy with
book.through(Lens.all()). The error, if any, is mapped after the lock
is back.
pub(crate) fn npv_by_trade(
&mut self,
py: Python<'_>,
as_of: Option<NaiveDate>,
) -> PyResult<PyRecords>
pub(crate) fn npv_by_trade( &mut self, py: Python<'_>, as_of: Option<NaiveDate>, ) -> PyResult<PyRecords>
Each trade’s value at as_of (the market’s date, else today):
trade, npv_ccy, npv_local, pnl_ccy, npv_pnl — in the trade’s own
currency, quantity applied, and converted at spot into the P&L currency,
the book’s reporting one. npv_pnl sums to npv().
pub(crate) fn risk(
&mut self,
py: Python<'_>,
as_of: Option<NaiveDate>,
) -> PyResult<PyRiskFrame>
pub(crate) fn risk( &mut self, py: Python<'_>, as_of: Option<NaiveDate>, ) -> PyResult<PyRiskFrame>
One risk pass at as_of, held as a frame: its npv(), its raw
risk() rows, and read on any axis, in the zero or par basis, at
any aggregation — without another pass per reading.
Selects the reverse mode internally. The caller opens nothing, closes nothing, and never names a tape.
§impl PyBook
impl PyBook
pub(crate) fn default_date(
&self,
py: Python<'_>,
as_of: Option<NaiveDate>,
) -> PyResult<NaiveDate>
pub(crate) fn default_date( &self, py: Python<'_>, as_of: Option<NaiveDate>, ) -> PyResult<NaiveDate>
The date a verb values at: as_of where given, else the attached
market’s date, else today.
pub(crate) fn ensure(
&mut self,
py: Python<'_>,
as_of: Option<NaiveDate>,
) -> PyResult<NaiveDate>
pub(crate) fn ensure( &mut self, py: Python<'_>, as_of: Option<NaiveDate>, ) -> PyResult<NaiveDate>
Makes sure the book’s market covers what the book reads at as_of:
attaches an undated market where none is, and — where the market has a
data source — fetches what is missing there, once, in place. A market
with no source is left as it is, so a gap surfaces as the valuation’s
own refusal naming the trade. Returns the date.
Trait Implementations§
impl DerefToPyAny for PyBook
§impl PyClassImpl for PyBook
impl PyClassImpl for PyBook
§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
§type ThreadChecker = SendablePyClass<PyBook>
type ThreadChecker = SendablePyClass<PyBook>
§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
§impl PyClassNewTextSignature<PyBook> for PyClassImplCollector<PyBook>
impl PyClassNewTextSignature<PyBook> for PyClassImplCollector<PyBook>
fn new_text_signature(self) -> Option<&'static str>
§impl PyMethods<PyBook> for PyClassImplCollector<PyBook>
impl PyMethods<PyBook> for PyClassImplCollector<PyBook>
fn py_methods(self) -> &'static PyClassItems
§impl PyTypeInfo for PyBook
impl PyTypeInfo for PyBook
§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType>
fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of_bound(object: &Bound<'_, PyAny>) -> bool
fn is_type_of_bound(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.