Skip to main content

MesoLive Risk Graph Hub

Use MesoLiveRiskGraphHubClient for position, strategy, underlying, and ad hoc combo Risk Graph snapshots and streams.

Runnable examples

The mesolive-sdk-examples package includes risk_graph_example:

python -m mesolive_sdk_examples.risk_graph_example --help
python -m mesolive_sdk_examples.risk_graph_example position snapshot --position-id 789
python -m mesolive_sdk_examples.risk_graph_example position stream --position-id 789 --max-events 5
python -m mesolive_sdk_examples.risk_graph_example strategy snapshot --strategy-id 123 --underlying-id 456
python -m mesolive_sdk_examples.risk_graph_example underlying snapshot --underlying-id 456 --provider IBKR

Examples decode payloads by default and print JSON-safe output. Pass --raw-payload when you need the compressed envelope instead.

Add --plot-html <path> to write an interactive Plotly report from the decoded payload. Use --no-json-output when you only want the chart, --plot-open to open the generated report, and --plotlyjs embed when the report must be self-contained instead of loading Plotly from the CDN. Plotting still decodes the payload when --raw-payload is used; --raw-payload only changes terminal output.

python -m mesolive_sdk_examples.risk_graph_example position snapshot --position-id 789 --plot-html risk-graph.html --no-json-output
python -m mesolive_sdk_examples.risk_graph_example strategy snapshot --strategy-id 123 --underlying-id 456 --mode ThreeDimensionalWithGreeks --plot-greek Delta --plot-html strategy-risk.html
python -m mesolive_sdk_examples.risk_graph_example position stream --position-id 789 --max-events 5 --plot-html latest-risk.html

For stream commands, Plotly rendering requires a finite stream (--max-events or --seconds) and renders the last received event after the stream completes.

Snapshot vs stream

Each target shape has both forms:

TargetSnapshot methodStream method
Positionget_position_risk_graph_snapshotstream_position_risk_graph_data
Strategy + underlyingget_strategy_risk_graph_snapshotstream_strategy_risk_graph_data
Underlying exposureget_underlying_risk_graph_snapshotstream_underlying_risk_graph_data
Ad hoc combo contractsget_combo_contracts_risk_graph_snapshotstream_combo_contracts_risk_graph_data

Stream entries are recalculated snapshots after market-data or position changes. They are suitable for UI/monitoring loops, not for every-tick audit logs.

Plotly output

Risk Graph Plotly reports use RiskGraphData.AggregateSeries.PnL for the main chart. Normal mode renders a 2D line chart across the underlying price axis. ThreeDimensionalWithGreeks mode renders a PnL surface and, when present in the payload, a second surface for the Greek selected with --plot-greek (Delta, Gamma, Theta, Vega, WVega, or Rho).

Payload decoding

Risk Graph responses include a RiskGraphPayload envelope. Decode before reading axes, horizons, series, and diagnostics:

from mesolive_sdk import MesoLiveRiskGraphHubClient, models
from mesolive_sdk.risk_graph import decode_risk_graph_payload

risk = MesoLiveRiskGraphHubClient(
mesolive_instance=instance,
mesolive_api_key=api_key,
)

snapshot = await risk.get_position_risk_graph_snapshot(
models.GetPositionRiskGraphSnapshotArgs(PositionId=789)
)
decoded = decode_risk_graph_payload(snapshot)
print(decoded.compression, decoded.compressed_size_bytes, decoded.uncompressed_size_bytes)
print(decoded.data.UnderlyingPrices)

decode_risk_graph_data(snapshot) returns only the decoded RiskGraphData model.

Query options

RiskGraphQueryOptions controls pricing mode, provider override, projection mode, expiration horizon selection, underlying price axis, and compression. Defaults are server-compatible, so most callers only set fields they need to override.

options = models.RiskGraphQueryOptions(
Provider=models.DataProviderType.IBKR,
PricingMode=models.RiskGraphPricingMode.MarkPreferred,
Compression=models.RiskGraphCompression.Gzip,
)

Reconnect hooks

Risk Graph clients support the same lifecycle hooks as other hub clients:

risk.on_reconnecting(lambda: print("risk graph reconnecting"))
risk.on_reconnected(lambda: print("risk graph reconnected"))

After reconnecting a stream, treat the next item as the current state and refresh any local cache from a snapshot if you need a hard resync.