quantlab.factor.kunquant_cs.CrossSectionalTopN#

class quantlab.factor.kunquant_cs.CrossSectionalTopN(v, n)[source]#

Bases: GenericCrossSectionalOp

1 for the n symbols with the largest v on each bar, 0 elsewhere.

NaN and infinite values are never selected and come out 0; when fewer than n values are valid, all of them are selected. A tie at the boundary is broken by symbol order: the earlier symbol on the axis wins. The output is a 0/1 mask, such as an estimation universe of the n largest companies by the previous bar’s market cap.

The C++ body cannot read an op parameter (see _CrossSectionalQuantileBounds), so constructing CrossSectionalTopN(v, 3000) returns an instance of a subclass created once per n (CrossSectionalTopN_3000) with n written into its code. isinstance(op, CrossSectionalTopN) still holds. The batch-start and SIMD-width notes of CrossSectionalZScore apply.

Parameters:
vOpBase

The values to rank, larger is better.

nint

How many symbols to select per bar, at least 1.

Raises:
ValueError

If n < 1.

Examples

>>> Output(CrossSectionalTopN(BackRef(Input("marketcap"), 1), 3000), "estu")
generate_body()[source]#

Return the C++ loop that marks the top n symbols of one bar.

generate_head()[source]#

Return the C++ set-up: a buffer of the valid symbols’ indices.