Class AlphaContext
Everything in com.quantfinlib.alpha works cross-sectionally:
at a bar index a factor scores every symbol, and downstream steps
(evaluation, construction, backtest) consume those scores as arrays
aligned with symbols(). Symbols are sorted once here, so plain
double[] is the interchange type for the whole pipeline.
Alignment. Series must share one timeline (see
data.SeriesAligner). of(Map) checks both the length and every
bar timestamp against the first series: a one-bar offset in one name
silently turns a cross-sectional IC into a lead-lag estimate.
Fundamentals. Two sources:
- a
FundamentalsHistory(withFundamentalsHistory(FundamentalsHistory)), keyed by publication time, which makes fundamental factors point-in-time; - a static snapshot (
of(Map, Map)). Scoring history with today's snapshot is look-ahead, sofundamentalsAsOf(int, int)only serves the snapshot at the last bar and throws for earlier bars.
Survivorship. Attach a PointInTimeUniverse with
withUniverse(PointInTimeUniverse). Then:
isActive(int, int)is point-in-time membership, and every built-in factor scores inactive names as NaN;returnOver(int, int, int)is terminal-event aware: a name alive at the start of a window that delists inside it returnsclose(e−1)·(1 + delistingReturn)/close(from) − 1, whereeis the first bar at or after the event; a merged name returns its deal value (cash plus acquirer shares marked at the acquirer's price). Without this the forward-filled ghost bars after a delisting read as a zero return, which removes exactly the losses a short book is paid to capture.
The terminal value convention matches
backtest.portfolio.PortfolioBacktester: proceeds anchor on the
last close before the event bar.
-
Method Summary
Modifier and TypeMethodDescriptionintacquirerIndex(int i) Panel index of the acquirer receiving symboli's holders in a stock merger, or −1.intbars()Panel length in bars (every series has exactly this many).fundamentals(int i) Static snapshot fundamentals for symboli, ornull.fundamentalsAsOf(int i, int index) Fundamentals of symbolias known at barindex.booleanWhether a point-in-time fundamentals history is attached.booleanisActive(int i, int barIndex) Whether symboliis in the tradeable cross-section atbarIndex: always true without a universe, otherwise point-in-time membership (dead and dropped names excluded).static AlphaContextPanel without fundamentals (technical factors only).static AlphaContextPanel with a static fundamentals snapshot.doublereturnBetween(int i, int fromIndex, boolean fromOpen, int toIndex, boolean toOpen) Simple return between two price points, each the open or the close of a bar: the building block for fill-lagged backtests.doublereturnOver(int i, int fromIndex, int toIndex) Simple return of symboliover(fromIndex, toIndex], close to close, terminal-event aware (see the class doc).series(int i) Price series for symbol indexi(the panel axis, not the bar).intsymbols()The frozen symbol order every score/weight array aligns with.doubleterminalAcquirerShares(int i) Acquirer shares paid per share of symboliat its terminal event (0 if none).intterminalIndex(int i) First bar at or after symboli's terminal event, or −1 when there is no universe, no event, or the event falls after the panel.longtimestamp(int index) Bar timestamp atindex(all series share it).The same panel with point-in-time fundamentals attached.withUniverse(PointInTimeUniverse universe) The same panel with a point-in-time universe attached (see the class doc).
-
Method Details
-
of
Panel without fundamentals (technical factors only). -
of
Panel with a static fundamentals snapshot. The snapshot is only valid at the last bar (see the class doc); attach aFundamentalsHistoryto score fundamentals historically.- Throws:
IllegalArgumentException- when series differ in length or in any bar timestamp
-
withUniverse
The same panel with a point-in-time universe attached (see the class doc). Universe timestamps must be in the bar timestamps' units. -
withFundamentalsHistory
The same panel with point-in-time fundamentals attached. -
isActive
public boolean isActive(int i, int barIndex) Whether symboliis in the tradeable cross-section atbarIndex: always true without a universe, otherwise point-in-time membership (dead and dropped names excluded). -
symbols
-
symbolCount
public int symbolCount() -
bars
public int bars()Panel length in bars (every series has exactly this many). -
series
Price series for symbol indexi(the panel axis, not the bar). -
fundamentals
Static snapshot fundamentals for symboli, ornull. -
hasFundamentalsHistory
public boolean hasFundamentalsHistory()Whether a point-in-time fundamentals history is attached. -
fundamentalsAsOf
Fundamentals of symbolias known at barindex. With aFundamentalsHistory: the latest record published at or before the bar (nullwhen none). With only a static snapshot: the snapshot at the last bar; earlier bars throw, because using today's fundamentals to score the past is look-ahead.- Throws:
IllegalStateException- for a historical bar without a history
-
timestamp
public long timestamp(int index) Bar timestamp atindex(all series share it). -
terminalIndex
public int terminalIndex(int i) First bar at or after symboli's terminal event, or −1 when there is no universe, no event, or the event falls after the panel. -
acquirerIndex
public int acquirerIndex(int i) Panel index of the acquirer receiving symboli's holders in a stock merger, or −1. -
returnOver
public double returnOver(int i, int fromIndex, int toIndex) Simple return of symboliover(fromIndex, toIndex], close to close, terminal-event aware (see the class doc). NaN when the name was already dead atfromIndex. -
returnBetween
public double returnBetween(int i, int fromIndex, boolean fromOpen, int toIndex, boolean toOpen) Simple return between two price points, each the open or the close of a bar: the building block for fill-lagged backtests. Terminal-event aware: once the event bar is reached the position is worth its terminal value (cash, plus acquirer shares marked at the acquirer's price at the end point). NaN when the name was already dead at the start point.- Throws:
IllegalStateException- when a stock merger pays shares of an acquirer outside the panel
-