Module Algostream_strategy.Context

Read-only view of the world, handed to the strategy on every event.

Market and portfolio state are exposed as accessor closures rather than as handed-out hashtables. The engine owns the mutable state; a strategy that received the table itself could corrupt the engine's bookkeeping, and a strategy that received a copy would pay for a snapshot on every event. Closures give O(1) reads with no ownership transfer.

ts_ns is the strategy's only legitimate "now". lib/strategy is on the wall-clock lint list, so there is no other source available.

type t = {
  1. ts_ns : int64;
    (*

    current event time

    *)
  2. seq : int;
    (*

    monotone event counter within the run; useful for tie-breaking and diagnostics

    *)
  3. portfolio : Portfolio.portfolio;
  4. nav : float;
    (*

    cash + marked-to-market positions

    *)
  5. working_orders : Order.order list;
    (*

    live orders at the venue, in submission order

    *)
  6. position : string -> float;
    (*

    signed quantity; 0.0 when flat or unknown

    *)
  7. last_price : string -> float option;
  8. quote : string -> (float * float) option;
    (*

    (bid, ask)

    *)
  9. book : string -> Order_book.order_book option;
  10. risk : Algostream_risk_management.Risk_snapshot.t option;
    (*

    None when the engine was configured without a risk monitor

    *)
}
val mid : t -> string -> float option

Mid price from quote, falling back to last_price when only one side is known.

val half_spread_bps : t -> string -> float option

Half-spread in basis points of mid, when both sides are known.

val has_position : t -> string -> bool

Whether the strategy currently holds a non-flat position in symbol.

val working_for : t -> string -> Order.order list

Working orders filtered to one symbol.