macrosynergy.management.utils.core#

This module contains core utility functions, as well as stand-alone functions that are used across the package.

split_ticker(ticker: str) str[source]#
split_ticker(ticker: Iterable[str]) List[str]

Returns either the cross-sectional identifier (cid) or the category (xcat) from a ticker. The function is overloaded to accept either a single ticker or an iterable (e.g. list, tuple, pd.Series, np.array) of tickers.

Parameters:
  • ticker (str) – The ticker to be converted.

  • mode (str) – The mode to be used. Must be either “cid” or “xcat”. Returns

Returns:

The cross-sectional identifier or category.

Return type:

str

get_cid(ticker: str) str[source]#
get_cid(ticker: Iterable[str]) List[str]

Returns the cross-sectional identifier (cid) from a ticker.

Parameters:

ticker (str) – The ticker to be converted. Returns

Returns:

The cross-sectional identifier.

Return type:

str

get_xcat(ticker: str) str[source]#
get_xcat(ticker: Iterable[str]) List[str]

Returns the category (xcat) from a ticker.

Parameters:

ticker (str) – The ticker to be converted. Returns

Returns:

The category.

Return type:

str

is_valid_iso_date(date)[source]#
Return type:

bool

convert_iso_to_dq(date)[source]#
Return type:

str

convert_dq_to_iso(date)[source]#
Return type:

str

form_full_url(url, params={})[source]#

Forms a full URL from a base URL and a dictionary of parameters. Useful for logging and debugging.

Parameters:
  • url (str) – base URL.

  • params (dict) – dictionary of parameters.

Returns:

full URL

Return type:

str

common_cids(df, xcats)[source]#

Returns a list of cross-sectional identifiers (cids) for which the specified categories (xcats) are available.

Parameters:
  • df (pd.Dataframe) – Standardized JPMaQS DataFrame with necessary columns: ‘cid’, ‘xcat’, ‘real_date’ and ‘value’.

  • xcats (List[str]) – A list with least two categories whose cross-sectional identifiers are being considered. return <List[str]>: List of cross-sectional identifiers for which all categories in xcats are available.

generate_random_date(start='1990-01-01', end='2020-01-01')[source]#

Generates a random date between two dates.

Parameters:
  • start (str) – The start date, in the ISO format (YYYY-MM-DD).

  • end (str) – The end date, in the ISO format (YYYY-MM-DD). Returns

Returns:

The random date.

Return type:

str

get_dict_max_depth(d)[source]#

Returns the maximum depth of a dictionary.

Parameters:

d (dict) – The dictionary to be searched. Returns

Returns:

The maximum depth of the dictionary.

Return type:

int

rec_search_dict(d, key, match_substring=False, match_type=None)[source]#

Recursively searches a dictionary for a key and returns the value associated with it.

Parameters:
  • d (dict) – The dictionary to be searched.

  • key (str) – The key to be searched for.

  • match_substring (bool) – If True, the function will return the value of the first key that contains the substring specified by the key parameter. If False, the function will return the value of the first key that matches the key parameter exactly. Default is False.

  • match_type (Any) – If not None, the function will look for a key that matches the search parameters and has the specified type. Default is None.

Returns:

The value associated with the key, or None if the key is not found.

Return type:

Any

class Timer[source]#

Bases: object

timer()[source]#
Return type:

Tuple[float, float]

lap()[source]#
Return type:

float

check_package_version(required_version)[source]#