这是用户在 2024-11-12 21:05 为 https://platform.worldquantbrain.com/learn/operators/operators 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Operators

Arithmetic Operators
Operator
Description
+, -, *, /, ^
Arithmetic operators: add, subtract, multiply, divide, powerArithmetic operators: add, subtract, multiply, divide, power
<, <=, >, >=, ==, !=
Logic comparison operators: Less Than (or Equal), Greater Than (or Equal), Equal, Not EqualLogic comparison operators: Less Than (or Equal), Greater Than (or Equal), Equal, Not Equal
cond ? expr1 : expr2
If cond is True, then expr1, else expr2. For example: close < open ? close : openIf cond is True, then expr1, else expr2. For example: close < open ? close : open
abs(x)
Absolute value of xAbsolute value of x
add(x, y, filter = false), x + y
Add all inputs (at least 2 inputs required). If filter = true, filter all input NaN to 0 before addingAdd all inputs (at least 2 inputs required). If filter = true, filter all input NaN to 0 before adding
ceiling(x)ceiling(x)
Nearest larger integerNearest larger integer
divide(x, y), x / ydivide(x, y), x / y
x / y
exp(x)
Natural exponential function: e^xNatural exponential function: e^x
floor(x)
Nearest smaller integerNearest smaller integer
fraction(x)fraction(x)

This operator removes the whole number part and returns the remaining fraction part with sign. Detailed description

inverse(x)inverse(x)
1 / x
log(x)
Natural logarithm. For example: Log(high/low) uses natural logarithm of high/low ratio as stock weights.Natural logarithm. For example: Log(high/low) uses natural logarithm of high/low ratio as stock weights.
log_diff(x)
Returns log(current value of input or x[t] ) - log(previous value of input or x[t-1]). Detailed description
max(x, y, ..)
Maximum value of all inputs. At least 2 inputs are required. Detailed description
min(x, y ..)min(x, y ..)
Minimum value of all inputs. At least 2 inputs are required. Detailed description
multiply(x ,y, ... , filter=false), x * y
Multiply all inputs. At least 2 inputs are required. Filter sets the NaN values to 1. Detailed description
nan_mask(x, y)
replace input with NAN if input's corresponding mask value or the second input here, is negative. Detailed description
nan_out(x, lower=0, upper=0)
If x < lower or x > upper return NaN, else return x. At least one of "lower", "upper" is required. Detailed description
power(x, y)power(x, y)
purify(x)purify(x)
Clear infinities (+inf, -inf) by replacing with NaN. Detailed description
replace(x, target="v1 v2 ..vn", dest="d1,d2,..dn"); replace(x, target="v1 v2 ..vn", dest="d1,d2,..dn");
Replace target values in input with destination values. Detailed description
reverse(x)reverse(x)
- x
round(x)
Round input to closest integer.Round input to closest integer.
round_down(x, f=1)
Round input to greatest multiple of f less than input;Round input to greatest multiple of f less than input;
sign(x)sign(x)
if input = NaN; return NaN
else if input > 0, return 1
else if input < 0, return -1
else if input = 0, return 0
signed_power(x, y)

x raised to the power of y such that final result preserves sign of x. Detailed description

s_log_1p(x)

Confine function to a shorter range using logarithm such that higher input remains higher and negative input remains negative as an output of resulting function and -1 or 1 is an asymptotic value. Detailed description

sqrt(x)
Square root of xSquare root of x
subtract(x, y, filter=false), x - y
x-y. If filter = true, filter all input NaN to 0 before subtractingx-y. If filter = true, filter all input NaN to 0 before subtracting
to_nan(x, value=0, reverse=false)
Convert value to NaN or NaN to value if reverse=true Convert value to NaN or NaN to value if reverse=true
densify(x)densify(x)

Converts a grouping field of many buckets into lesser number of only available buckets so as to make working with grouping fields computationally efficient. Detailed description

Logical Operators
Operator
Description
and(input1, input2)and(input1, input2)
Logical AND operator, returns true if both operands are true and returns false otherwiseLogical AND operator, returns true if both operands are true and returns false otherwise
or(input1, input2)or(input1, input2)
Logical OR operator returns true if either or both inputs are true and returns false otherwiseLogical OR operator returns true if either or both inputs are true and returns false otherwise
equal(input1, input2), input1 == input2
Returns true if both inputs are same and returns false otherwiseReturns true if both inputs are same and returns false otherwise
negate(input)negate(input)
The result is true if the converted operand is false; the result is false if the converted operand is trueThe result is true if the converted operand is false; the result is false if the converted operand is true
less(input1, input2), input1 < input2
If input1 < input2 return true, else return falseIf input1 < input2 return true, else return false
if_else(input1, input2, input 3)if_else(input1, input2, input 3)
If input1 is true then return input2 else return input3.If input1 is true then return input2 else return input3.
is_not_nan(input)
If (input != NaN) return 1 else return 0If (input != NaN) return 1 else return 0
is_nan(input)
If (input == NaN) return 1 else return 0If (input == NaN) return 1 else return 0
is_finite(input)
If (input NaN or input == INF) return 0, else return 1If (input NaN or input == INF) return 0, else return 1
is_not_finite(input)
If (input NAN or input == INF) return 1 else return 0If (input NAN or input == INF) return 1 else return 0
Time Series Operators

For time series operator with look back days parameter (d) , d must be <512

Operator
Description
days_from_last_change(x)days_from_last_change(x)
Amount of days since last change of xAmount of days since last change of x
ts_weighted_decay(x, k=0.5)

Instead of replacing today’s value with yesterday’s as in ts_delay(x, 1), it assigns weighted average of today’s and yesterday’s values with weight on today’s value being k and yesterday’s being (1-k). Detailed description

hump(x, hump = 0.01)hump(x, hump = 0.01)

Limits amount and magnitude of changes in input (thus reducing turnover). Detailed description

hump_decay(x, p=0)hump_decay(x, p=0)

This operator helps to ignore the values that changed too little corresponding to previous ones. Detailed description

inst_tvr(x, d)
Total trading value / Total holding value in the past d days
jump_decay(x, d, sensitivity=0.5, force=0.1)

If there is a huge jump in current data compare to previous one Detailed description

kth_element(x, d, k)
Returns K-th value of input by looking through lookback days. This operator can be used to backfill missing data if k=1 Detailed description
last_diff_value(x, d)
Returns last x value not equal to current x value from last d days
ts_arg_max(x, d)

Returns the relative index of the max value in the time series for the past d days. If the current day has the max value for the past d days, it returns 0. If previous day has the max value for the past d days, it returns 1. Detailed description

ts_arg_min(x, d)
Returns the relative index of the min value in the time series for the past d days; If the current day has the min value for the past d days, it returns 0; If previous day has the min value for the past d days, it returns 1.
ts_av_diff(x, d)

Returns x - tsmean(x, d), but deals with NaNs carefully. That is NaNs are ignored during mean computation. Detailed description

ts_backfill(x,lookback = d, k=1, ignore="NAN")
Backfill is the process of replacing the NAN or 0 values by a meaningful value (i.e., a first non-NaN value)
ts_co_kurtosis(y, x, d)
Returns cokurtosis of y and x for the past d days. Detailed description
ts_corr(x, y, d)
Returns correlation of x and y for the past d days
ts_co_skewness(y, x, d)
Returns coskewness of y and x for the past d days. Detailed description
ts_count_nans(x ,d)
Returns the number of NaN values in x for the past d days
ts_covariance(y, x, d)
Returns covariance of y and x for the past d days
ts_decay_exp_window(x, d, factor = f)
Returns exponential decay of x with smoothing factor for the past d days. Detailed description
ts_decay_linear(x, d, dense = false)
Returns the linear decay on x for the past d days. Dense parameter=false means operator works in sparse mode and we treat NaN as 0. In dense mode we do not.
ts_delay(x, d)
Returns x value d days ago
ts_delta(x, d)
Returns x - ts_delay(x, d)
ts_ir(x, d)
Return information ratio ts_mean(x, d) / ts_std_dev(x, d)
ts_kurtosis(x, d)
Returns kurtosis of x for the last d days. Detailed description
ts_max(x, d)
Returns max value of x for the past d days
ts_max_diff(x, d)
Returns x - ts_max(x, d)
ts_mean(x, d)
Returns average value of x for the past d days.
ts_median(x, d)
Returns median value of x for the past d days
ts_min(x, d)
Returns min value of x for the past d days
ts_min_diff(x, d)
Returns x - ts_min(x, d)
ts_min_max_cps(x, d, f = 2)
Returns (ts_min(x, d) + ts_max(x, d)) - f * x. If not specified, by default f = 2
ts_min_max_diff(x, d, f = 0.5)
Returns x - f * (ts_min(x, d) + ts_max(x, d)). If not specified, by default f = 0.5
ts_moment(x, d, k=0)

Returns K-th central moment of x for the past d days. Detailed description

ts_partial_corr(x, y, z, d)
Returns partial correlation of x, y, z for the past d days. Detailed description
ts_percentage(x, d, percentage=0.5)
Returns percentile value of x for the past d days. Detailed description
ts_poly_regression(y, x, d, k = 1)
Returns y - Ey, where Ey = x + x^2 + … + x^k over d days; k must be specified as a keyword argument
ts_product(x, d)
Returns product of x for the past d days
ts_rank(x, d, constant = 0)
Rank the values of x for each instrument over the past d days, then return the rank of the current value + constant. If not specified, by default, constant = 0.
ts_regression(y, x, d, lag = 0, rettype = 0)

Returns various parameters related to regression function. Detailed description

ts_returns (x, d, mode = 1)

Returns the relative change in the x value .
Detailed description

ts_scale(x, d, constant = 0)

Returns (x – ts_min(x, d)) / (ts_max(x, d) – ts_min(x, d)) + constant
This operator is similar to scale down operator but acts in time series space. Detailed description

ts_skewness(x, d)
Return skewness of x for the past d days. Detailed description
ts_std_dev(x, d)
Returns standard deviation of x for the past d days
ts_step(1), step(1)
Returns days' counter
ts_sum(x, d)
Sum values of x for the past d days.
ts_theilsen(x, y, d)
Theil Sen slope estimator of inputs for the past n days. More details on wiki
ts_triple_corr(x, y, z, d)
Returns triple correlation of x, y, z for the past d days. Detailed description
ts_zscore(x, d)
Z-score is a numerical measurement that describes a value's relationship to the mean of a group of values. Z-score is measured in terms of standard deviations from the mean: (x - tsmean(x,d)) / tsstddev(x,d)
ts_entropy(x,d)

For each instrument, we collect values of input in the past d days and calculate the probability distribution then the information entropy via a histogram as a result. Detailed description

ts_vector_neut(x,y,d)
Returns x- ts_vector_proj(x,y,d)
ts_vector_proj(x,y,d)
Returns vector projection of x onto y in time-series space. Algebraic and geometric details can be found on wiki
ts_rank_gmean_amean_diff(input1, input2, input3,...,d)

Returns Geometric Mean of ts_rank(input,d) of all input - Arithmetic Mean of ts_rank(input,d) of all input. This is similar to rank_gmean_amean_diff operator but in time-series space. Detailed description

ts_quantile(x,d, driver="gaussian" )
It calculates ts_rank and apply to its value an inverse cumulative density function from driver distribution. Possible values of driver (optional ) are "gaussian", "uniform", "cauchy" distribution where "gaussian" is the default.
ts_delta_limit(x, y, limit_volume=0.1)
Limit the change in the Alpha position x between dates to a specified fraction of y. The “limit_volume” can be in the range of 0 to 1. Also, please be aware of the scaling for x and y. Besides setting y as adv20 or volume related data, you can also set y as a constant.
ts_target_tvr_delta_limit(x, y, lambda_min=0, lambda_max=1, target_tvr=0.1)
Tune "ts_delta_limit" to have a turnover equal to a certain target with optimization weight range between lambda_min, lambda_max. Also, please be aware of the scaling for x and y. Besides setting y as adv20 or volume related data, you can also set y as a constant.
ts_target_tvr_hump(x, lambda_min=0, lambda_max=1, target_tvr=0.1)
Tune "hump" to have a turnover equal to a certain target with optimization weight range between lambda_min, lambda_max.
Cross Sectional Operators
Operator
Description
normalize(x, useStd = false, limit = 0.0)

Calculates the mean value of all valid alpha values for a certain date, then subtracts that mean from each element. Detailed description

one_side(x , side = long )
Shifts all instruments up or down so that the Alpha becomes long-only or short-only
(if side = short), respectively.
quantile(x, driver = gaussian, sigma = 1.0)
Rank the raw vector, shift the ranked Alpha vector, apply distribution ( gaussian, cauchy, uniform ). If driver is uniform, it simply subtract each Alpha value with the mean of all Alpha values in the Alpha vector. Detailed description
rank(x, rate=2)
Ranks the input among all the instruments and returns an equally distributed number between 0.0 and 1.0. For precise sort, use the rate as 0. Detailed description
rank_by_side(x, rate=2,scale=1)
Ranks positive and negative input separately and scale to book. For precise sorting use rate=0. Detailed description
generalized_rank(open, m=1)
The idea is that difference between instrument values raised to the power of m is added to the rank of instrument with bigger value and subtracted from the rank of instrument with lesser value. More details in the notes at the end of page. Detailed description
regression_neut(y, x)
Conducts the cross-sectional regression on the stocks with Y as target and X as the independent variable. Detailed description
regression_proj(y, x)
Conducts the cross-sectional regression on the stocks with Y as target and X as the independent variable. Detailed description
scale(x, scale=1, longscale=1, shortscale=1)
Scales input to booksize. We can also scale the long positions and short positions to separate scales by mentioning additional parameters to the operator. Detailed description
scale_down(x,constant=0)

Scales all values in each day proportionately between 0 and 1 such that minimum value maps to 0 and maximum value maps to 1. Constant is the offset by which final result is subtracted. Detailed description

truncate(x,maxPercent=0.01)

Operator truncates all values of x to maxPercent. Here, maxPercent is in decimal notation. Detailed description

vector_neut(x, y)
For given vectors x and y, it finds a new vector x* (output) such that x* is orthogonal to y. Detailed description
vector_proj(x, y)
Returns vector projection of x onto y. Algebraic and geometric details can be found on wiki
winsorize(x, std=4)
Winsorizes x to make sure that all values in x are between the lower and upper limits, which are specified as multiple of std. Details can be found on wiki
zscore(x)
Z-score is a numerical measurement that describes a value's relationship to the mean of a group of values. Z-score is measured in terms of standard deviations from the mean
rank_gmean_amean_diff(input1, input2, input3,...)

Operator returns difference of geometric mean and arithmetic mean of cross sectional rank of inputs. Detailed description

Vector Operators

Only to be used with Vector data fields

Operator
Description
vec_avg(x)
Taking mean of the vector field x
vec_choose(x,nth=k)
Choosing kth item(indexed at 0) from each vector field x
vec_count(x)
Number of elements in vector field x
vec_ir(x)
Information Ratio (Mean / Standard Deviation) of vector field x
vec_kurtosis(x)
Kurtosis of vector field x
vec_max(x)
Maximum value form vector field x
vec_min(x)
Minimum value form vector field x
vec_norm(x)
Sum of all absolute values of vector field x
vec_percentage(x,percentage=0.5)
Percentile of vector field x
vec_powersum(x,constant=2)
Sum of power of vector field x
vec_range(x)
Difference between maximum and minimum element in vector field x
vec_skewness(x)
Skewness of vector field x
vec_stddev(x)
Standard Deviation of vector field x
vec_sum(x)
Sum of vector field x
vec_filter(vec, value=nan)
Filter vector by values, such as nan. Can filter many values at once, for example: vec_filter(vec, value="nan 0 10"). Please note the output of this vector operators will still be vector data.
Transformational Operators
Operator
Description
arc_cos(x)
If -1 <= x <= 1: arccos(x); else NaN
arc_sin(x)
If -1 <= x <= 1: arcsin(x); else NaN
arc_tan(x)

This operator does inverse tangent of input. Detailed description

bucket(rank(x), range="0, 1, 0.1" or buckets = "2,5,6,7,10")

Convert float values into indexes for user-specified buckets. Bucket is useful for creating group values, which can be passed to group operators as input. Detailed description

clamp(x, lower = 0, upper = 0, inverse = False, mask = ")

Limits input value between lower and upper bound in inverse = false mode (which is default). Alternatively, when inverse = true, values between bounds are replaced with mask, while values outside bounds are left as is. Detailed description

filter(x, h = "1, 2, 3, 4", t="0.5")

Used to filter the value and allows to create filters like linear or exponential decay. Detailed description

keep(x, f, period = 5)

This operator outputs value x when f changes and continues to do that for “period” days after f stopped changing. After “period” days since last change of f, NaN is output. Detailed description

left_tail(x, maximum = 0)
NaN everything greater than maximum, maximum should be constant. Detailed description
pasteurize(x)
Set to NaN if x is INF or if the underlying instrument is not in the Alpha universe
right_tail(x, minimum = 0)
NaN everything less than minimum, minimum should be constant. Detailed description
sigmoid(x)
Returns 1 / (1 + exp(-x))
tail(x, lower = 0, upper = 0, newval = 0)
If (x > lower AND x < upper) return newval, else return x. Lower, upper, newval should be constants. Detailed description
tanh(x)
Hyperbolic tangent of x
trade_when(x, y, z)

Used in order to change Alpha values only under a specified condition and to hold Alpha values in other cases. It also allows to close Alpha positions (assign NaN values) under a specified condition. Detailed description

Group Operators
Operator
Description
group_backfill(x, group, d, std = 4.0)

If a certain value for a certain date and instrument is NaN, from the set of same group instruments, calculate winsorized mean of all non-NaN values over last d days. Detailed description

group_coalesce(original_group, group2, group3,…)

Regroup remaining stocks based on group2 and so on if originial_group does not cover all stocks in target universe. Detailed description

group_count(x, group)
Gives the number of instruments in the same group (e.g. sector) which have valid values of x. For example, x=1 gives the number of instruments in each group (without regard for whether any particular field has valid data).
group_extra(x, weight, group)
Replaces NaN values by their corresponding group means.
group_max(x, group)
Maximum of x for all instruments in the same group.
group_mean(x, weight, group)
All elements in group equals to the mean value of the group. Mean = sum(data*weight) / sum(weight) in each group.
group_median(x, group)
All elements in group equals to the median value of the group.
group_min(x, group)
All elements in group equals to the min value of the group.
group_neutralize(x, group)

Neutralizes Alpha against groups. These groups can be subindustry, industry, sector, country or a constant. Detailed description

group_normalize(x, group, constantCheck=False, tolerance=0.01, scale=1)

Normalizes input such that each group's absolute sum is 1. Detailed description

group_percentage(x, group, percentage=0.5)
All elements in group equals to the value over the percentage of the group.
Percentage = 0.5 means value is equal to group_median(x, group)
group_vector_proj(x,y,g)
Similar to vector_proj(x, y) but x projection to y for each group which can be any classifier such as subindustry, industry, sector, etc. Refer wiki  for more details
group_rank(x, group)
Each elements in a group is assigned the corresponding rank in this group
group_scale(x, group)
Normalizes the values in a group to be between 0 and 1. (x - groupmin) / (groupmax - groupmin)
group_std_dev(x, group)
All elements in group equals to the standard deviation of the group.
group_sum(x, group)
Sum of x for all instruments in the same group.
group_vector_neut(x,y,g)
Similar to vector_neut(x, y) but x neutralize to y for each group g which can be any classifier such as subindustry, industry, sector, etc.
group_zscore(x, group)
Calculates group Z-score - numerical measurement that describes a value's relationship to the mean of a group of values. Z-score is measured in terms of standard deviations from the mean. zscore = (data - mean) / stddev of x for each instrument within its group.
group_cartesian_product(g1, g2)
Merge two groups into one group. If originally there are len_1 and len_2 group indices in g1 and g2, there will be len_1 * len_2 indices in the new group.
Special Operators
Operator
Description
convert(x, mode = "dollar2share")
Convert dollars to share or share to dollar when mode = "share2dollar". Please note that the use of the convert() operator in an Alpha Expression is considered as utilizing the pv1 dataset (Price Volume Data for Equity) since it relies on pv1 data for calculations
inst_pnl(x)
Generate pnl per instruments. Please note that the use of the inst_pnl() operator in an Alpha Expression is considered as utilizing the pv1 dataset (Price Volume Data for Equity) since it relies on pv1 data for calculations.

Notes:

  1. All operator calls are to be made using the following order of arguments:
  • Required amount of data fields
  • Group (optional)
  • Lookback days (optional)
  • Keyword arguments (optional)
    • In the “Operator” column, they are shown as key=default_value.
    • They are passed in the form key=value.
    • Keyword argument may be omitted, in this case its default value will be passed to operator.

  • Examples:
    • ts_delta(close, 21)
    • ts_regression(close, ts_step(1), 21, lag = 63, rettype = 1)
    • ts_regression(close, ts_step(1), 21)
      • same as ts_regression(close, ts_step(1), 21, lag=0, rettype=0)
    • ts_covariance(close, vwap, 20)
    • group_rank(close, industry)
    • group_backfill(sales / assets, subindustry, 252, std = 3)

2. Variable assignment is possible in the following form:

a = sales / assets; ts_delta(a, 252)

Variable re-assignment is not permitted.

3. Underscores in operator names can be omitted for ease of typing.