macrosynergy.panel.panel_calculator#

Implementation of panel calculation functions for quantamental data. The functionality allows applying mathematical operations on time-series data.

time_series_check(formula, index)[source]#

Determine if the panel has any time-series methods applied. If a time-series conversion is applied, the function will return the terminal index of the respective category. Further, a boolean parameter is also returned to confirm the presence of a time-series operation.

Parameters:
  • formula (str) –

  • index (int) – starting index to iterate over.

Return <Tuple[int, bool]>:

xcat_isolator(expression, start_index, index)[source]#

Split the category from the time-series operation. The function will return the respective category.

Parameters:
  • expression (str) –

  • start_index (str) – starting index to search over.

  • index (int) – defines the end of the search space over the expression.

Return <str>:

xcat.

panel_calculator(df, calcs=None, cids=None, start=None, end=None, blacklist=None, external_func=None)[source]#

Calculates new data panels through operations on existing panels.

Parameters:
  • df (DataFrame) – standardized dataframe with following necessary columns: ‘cid’, ‘xcat’, ‘real_date’ and ‘value’.

  • calcs (List[str]) – list of formulas denoting operations on panels of categories. Words in capital letters denote category panels. Otherwise the formulas can include numpy functions and standard binary operators. See notes below.

  • cids (List[str]) – cross sections over which the panels are defined.

  • start (str) – earliest date in ISO format. Default is None and earliest date in df is used.

  • end (str) – latest date in ISO format. Default is None and latest date in df is used.

  • blacklist (dict) – cross sections with date ranges that should be excluded from the dataframe. If one cross section has several blacklist periods append numbers to the cross-section code.

  • external_func (dict) – dictionary of external functions to be used in the panel calculation. The key is the name of the function and the value is the function.

Return <pd.Dataframe>:

standardized dataframe with all new categories in standard format, i.e the columns ‘cid’, ‘xcat’, ‘real_date’ and ‘value’.

Notes: Panel calculation strings can use numpy functions and unary/binary operators on category panels. The category is indicated by capital letters, underscores and numbers. Panel category names that are not at the beginning or end of the string must always have a space before and after the name. Calculated category and panel operations must be separated by ‘=’. Examples:

“NEWCAT = ( OLDCAT1 + 0.5) * OLDCAT2” “NEWCAT = np.log( OLDCAT1 ) - np.abs( OLDCAT2 ) ** 1/2”

Panel calculation can also involve individual indicator series (to be applied to all series in the panel by using th ‘i’ as prefix), such as:

“NEWCAT = OLDCAT1 - np.sqrt( iUSD_OLDCAT2 )”

If more than one new category is calculated, the resulting panels can be used sequentially in the calculations, such as:

[“NEWCAT1 = 1 + OLDCAT1 / 100”, “NEWCAT2 = OLDCAT2 * NEWCAT1”]

Return type:

DataFrame