alphapy package

Submodules

alphapy.alias module

class alphapy.alias.Alias(name, expr, replace=False)

Bases: object

Create a new alias as a key-value pair. All aliases are stored in Alias.aliases. Duplicate keys or values are not allowed, unless the replace parameter is True.

Parameters:
  • name (str) – Alias key.

  • expr (str) – Alias value.

  • replace (bool, optional) – Replace the current key-value pair if it already exists.

Variables:

Alias.aliases (dict) – Class variable for storing all known aliases

Examples

>>> Alias('atr', 'ma_truerange')
>>> Alias('hc', 'higher_close')
aliases = {}
alphapy.alias.get_alias(alias)

Find an alias value with the given key.

Parameters:

alias (str) – Key for finding the alias value.

Returns:

alias_value – Value for the corresponding key.

Return type:

str

Examples

>>> alias_value = get_alias('atr')
>>> alias_value = get_alias('hc')

alphapy.alphapy_main module

alphapy.calendrical module

Package : calendrical Created : July 11, 2017 Reference : Calendrical Calculations, Cambridge Press, 2002

Copyright 2020 ScottFree Analytics LLC Mark Conway & Robert D. Scott II

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

alphapy.calendrical.biz_day_month(rdate)

Calculate the business day of the month.

Parameters:

rdate (int) – RDate date format.

Returns:

bdm – Business day of month.

Return type:

int

alphapy.calendrical.biz_day_week(rdate)

Calculate the business day of the week.

Parameters:

rdate (int) – RDate date format.

Returns:

bdw – Business day of week.

Return type:

int

alphapy.calendrical.bizday(df, c)

Extract business day of month and week.

Parameters:
  • df (pandas.DataFrame) – Dataframe containing the column c.

  • c (str) – Name of the column in the dataframe df.

Returns:

date_features – The dataframe containing the date features.

Return type:

pandas.DataFrame

alphapy.calendrical.christmas_day(gyear, observed)

Get Christmas Day for a given year.

Parameters:
  • gyear (int) – Gregorian year.

  • observed (bool) – False if the exact date, True if the weekday.

Returns:

xmas – Christmas Day in RDate format.

Return type:

int

alphapy.calendrical.cinco_de_mayo(gyear)

Get Cinco de Mayo for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

cinco_de_mayo – Cinco de Mayo in RDate format.

Return type:

int

alphapy.calendrical.dateparts(df, c)

Extract date into its components: year, month, day, dayofweek.

Parameters:
  • df (pandas.DataFrame) – Dataframe containing the column c.

  • c (str) – Name of the column in the dataframe df.

Returns:

date_features – The dataframe containing the date features.

Return type:

pandas.DataFrame

alphapy.calendrical.day_of_week(rdate)

Get the ordinal day of the week.

Parameters:

rdate (int) – RDate date format.

Returns:

dw – Ordinal day of the week.

Return type:

int

alphapy.calendrical.day_of_year(gyear, gmonth, gday)

Calculate the day number of the given calendar year.

Parameters:
  • gyear (int) – Gregorian year.

  • gmonth (int) – Gregorian month.

  • gday (int) – Gregorian day.

Returns:

dy – Day number of year in RDate format.

Return type:

int

alphapy.calendrical.days_left_in_year(gyear, gmonth, gday)

Calculate the number of days remaining in the calendar year.

Parameters:
  • gyear (int) – Gregorian year.

  • gmonth (int) – Gregorian month.

  • gday (int) – Gregorian day.

Returns:

days_left – Calendar days remaining in RDate format.

Return type:

int

alphapy.calendrical.easter_day(gyear)

Get Easter Day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

ed – Easter Day in RDate format.

Return type:

int

alphapy.calendrical.expand_dates(date_list)
alphapy.calendrical.fathers_day(gyear)

Get Father’s Day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

fathers_day – Father’s Day in RDate format.

Return type:

int

alphapy.calendrical.first_kday(k, gyear, gmonth, gday)

Calculate the first kday in RDate format.

Parameters:
  • k (int) – Day of the week.

  • gyear (int) – Gregorian year.

  • gmonth (int) – Gregorian month.

  • gday (int) – Gregorian day.

Returns:

fkd – first-kday in RDate format.

Return type:

int

alphapy.calendrical.gdate_to_rdate(gyear, gmonth, gday)

Convert Gregorian date to RDate format.

Parameters:
  • gyear (int) – Gregorian year.

  • gmonth (int) – Gregorian month.

  • gday (int) – Gregorian day.

Returns:

rdate – RDate date format.

Return type:

int

alphapy.calendrical.get_holiday_names()

Get the list of defined holidays.

Returns:

holidays – List of holiday names.

Return type:

list of str

alphapy.calendrical.get_nth_kday_of_month(gday, gmonth, gyear)

Convert Gregorian date to RDate format.

Parameters:
  • gday (int) – Gregorian day.

  • gmonth (int) – Gregorian month.

  • gyear (int) – Gregorian year.

Returns:

nth – Ordinal number of a given day’s occurrence within the month, for example, the third Friday of the month.

Return type:

int

alphapy.calendrical.get_rdate(row)

Extract RDate from a dataframe.

Parameters:

row (pandas.DataFrame) – Row of a dataframe containing year, month, and day.

Returns:

rdate – RDate date format.

Return type:

int

alphapy.calendrical.good_friday(gyear)

Get Good Friday for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

gf – Good Friday in RDate format.

Return type:

int

alphapy.calendrical.halloween(gyear)

Get Halloween for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

halloween – Halloween in RDate format.

Return type:

int

alphapy.calendrical.independence_day(gyear, observed)

Get Independence Day for a given year.

Parameters:
  • gyear (int) – Gregorian year.

  • observed (bool) – False if the exact date, True if the weekday.

Returns:

d4j – Independence Day in RDate format.

Return type:

int

alphapy.calendrical.kday_after(rdate, k)

Calculate the day after a given RDate.

Parameters:
  • rdate (int) – RDate date format.

  • k (int) – Day of the week.

Returns:

kda – kday-after in RDate format.

Return type:

int

alphapy.calendrical.kday_before(rdate, k)

Calculate the day before a given RDate.

Parameters:
  • rdate (int) – RDate date format.

  • k (int) – Day of the week.

Returns:

kdb – kday-before in RDate format.

Return type:

int

alphapy.calendrical.kday_nearest(rdate, k)

Calculate the day nearest a given RDate.

Parameters:
  • rdate (int) – RDate date format.

  • k (int) – Day of the week.

Returns:

kdn – kday-nearest in RDate format.

Return type:

int

alphapy.calendrical.kday_on_after(rdate, k)

Calculate the day on or after a given RDate.

Parameters:
  • rdate (int) – RDate date format.

  • k (int) – Day of the week.

Returns:

kdoa – kday-on-or-after in RDate format.

Return type:

int

alphapy.calendrical.kday_on_before(rdate, k)

Calculate the day on or before a given RDate.

Parameters:
  • rdate (int) – RDate date format.

  • k (int) – Day of the week.

Returns:

kdob – kday-on-or-before in RDate format.

Return type:

int

alphapy.calendrical.labor_day(gyear)

Get Labor Day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

lday – Labor Day in RDate format.

Return type:

int

alphapy.calendrical.last_kday(k, gyear, gmonth, gday)

Calculate the last kday in RDate format.

Parameters:
  • k (int) – Day of the week.

  • gyear (int) – Gregorian year.

  • gmonth (int) – Gregorian month.

  • gday (int) – Gregorian day.

Returns:

lkd – last-kday in RDate format.

Return type:

int

alphapy.calendrical.leap_year(gyear)

Determine if this is a Gregorian leap year.

Parameters:

gyear (int) – Gregorian year.

Returns:

leap_year – True if a Gregorian leap year, else False.

Return type:

bool

alphapy.calendrical.memorial_day(gyear)

Get Memorial Day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

md – Memorial Day in RDate format.

Return type:

int

alphapy.calendrical.mlk_day(gyear)

Get Martin Luther King Day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

mlkday – Martin Luther King Day in RDate format.

Return type:

int

alphapy.calendrical.mothers_day(gyear)

Get Mother’s Day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

mothers_day – Mother’s Day in RDate format.

Return type:

int

alphapy.calendrical.new_years_day(gyear, observed)

Get New Year’s day for a given year.

Parameters:
  • gyear (int) – Gregorian year.

  • observed (bool) – False if the exact date, True if the weekday.

Returns:

nyday – New Year’s Day in RDate format.

Return type:

int

alphapy.calendrical.next_event(rdate, events)

Find the next event after a given date.

Parameters:
  • rdate (int) – RDate date format.

  • events (list of RDate (int)) – Monthly events in RDate format.

Returns:

event – Next event in RDate format.

Return type:

RDate (int)

alphapy.calendrical.next_holiday(rdate, holidays)

Find the next holiday after a given date.

Parameters:
  • rdate (int) – RDate date format.

  • holidays (dict of RDate (int)) – Holidays in RDate format.

Returns:

holiday – Next holiday in RDate format.

Return type:

RDate (int)

alphapy.calendrical.nth_bizday(n, gyear, gmonth)

Calculate the nth business day in a month.

Parameters:
  • n (int) – Number of the business day to get.

  • gyear (int) – Gregorian year.

  • gmonth (int) – Gregorian month.

Returns:

bizday – Nth business day of a given month in RDate format.

Return type:

int

alphapy.calendrical.nth_kday(n, k, gyear, gmonth, gday)

Calculate the nth-kday in RDate format.

Parameters:
  • n (int) – Occurrence of a given day counting in either direction.

  • k (int) – Day of the week.

  • gyear (int) – Gregorian year.

  • gmonth (int) – Gregorian month.

  • gday (int) – Gregorian day.

Returns:

nthkday – nth-kday in RDate format.

Return type:

int

alphapy.calendrical.presidents_day(gyear)

Get President’s Day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

prezday – President’s Day in RDate format.

Return type:

int

alphapy.calendrical.previous_event(rdate, events)

Find the previous event before a given date.

Parameters:
  • rdate (int) – RDate date format.

  • events (list of RDate (int)) – Monthly events in RDate format.

Returns:

event – Previous event in RDate format.

Return type:

RDate (int)

alphapy.calendrical.previous_holiday(rdate, holidays)

Find the previous holiday before a given date.

Parameters:
  • rdate (int) – RDate date format.

  • holidays (dict of RDate (int)) – Holidays in RDate format.

Returns:

holiday – Previous holiday in RDate format.

Return type:

RDate (int)

alphapy.calendrical.rdate_to_gdate(rdate)

Convert RDate format to Gregorian date format.

Parameters:

rdate (int) – RDate date format.

Returns:

  • gyear (int) – Gregorian year.

  • gmonth (int) – Gregorian month.

  • gday (int) – Gregorian day.

alphapy.calendrical.rdate_to_gyear(rdate)

Convert RDate format to Gregorian year.

Parameters:

rdate (int) – RDate date format.

Returns:

gyear – Gregorian year.

Return type:

int

alphapy.calendrical.saint_patricks_day(gyear)

Get Saint Patrick’s day for a given year.

Parameters:
  • gyear (int) – Gregorian year.

  • observed (bool) – False if the exact date, True if the weekday.

Returns:

patricks – Saint Patrick’s Day in RDate format.

Return type:

int

alphapy.calendrical.set_events(n, k, gyear, gday)

Define monthly events for a given year.

Parameters:
  • n (int) – Occurrence of a given day counting in either direction.

  • k (int) – Day of the week.

  • gyear (int) – Gregorian year for the events.

  • gday (int) – Gregorian day representing the first day to consider.

Returns:

events – Monthly events in RDate format.

Return type:

list of RDate (int)

Example

>>> # Options Expiration (Third Friday of every month)
>>> set_events(3, 5, 2017, 1)
alphapy.calendrical.set_holidays(gyear, observe)

Determine if this is a Gregorian leap year.

Parameters:
  • gyear (int) – Value for the corresponding key.

  • observe (bool) – True to get the observed date, otherwise False.

Returns:

holidays – Set of holidays in RDate format for a given year.

Return type:

dict of int

alphapy.calendrical.subtract_dates(gyear1, gmonth1, gday1, gyear2, gmonth2, gday2)

Calculate the difference between two Gregorian dates.

Parameters:
  • gyear1 (int) – Gregorian year of first date.

  • gmonth1 (int) – Gregorian month of first date.

  • gday1 (int) – Gregorian day of first date.

  • gyear2 (int) – Gregorian year of successive date.

  • gmonth2 (int) – Gregorian month of successive date.

  • gday2 (int) – Gregorian day of successive date.

Returns:

delta_days – Difference in days in RDate format.

Return type:

int

alphapy.calendrical.thanksgiving_day(gyear)

Get Thanksgiving Day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

tday – Thanksgiving Day in RDate format.

Return type:

int

alphapy.calendrical.timeparts(df, c)

Extract time into its components: hour, minute, second.

Parameters:
  • df (pandas.DataFrame) – Dataframe containing the column c.

  • c (str) – Name of the column in the dataframe df.

Returns:

time_features – The dataframe containing the time features.

Return type:

pandas.DataFrame

alphapy.calendrical.valentines_day(gyear)

Get Valentine’s day for a given year.

Parameters:

gyear (int) – Gregorian year.

Returns:

valentines – Valentine’s Day in RDate format.

Return type:

int

alphapy.calendrical.veterans_day(gyear, observed)

Get Veteran’s day for a given year.

Parameters:
  • gyear (int) – Gregorian year.

  • observed (bool) – False if the exact date, True if the weekday.

Returns:

veterans – Veteran’s Day in RDate format.

Return type:

int

alphapy.data module

alphapy.estimators module

class alphapy.estimators.Estimator(algorithm, model_type, estimator, grid)

Bases: object

Store information about each estimator.

Parameters:
  • algorithm (str) – Abbreviation representing the given algorithm.

  • model_type (enum ModelType) – The machine learning task for this algorithm.

  • estimator (function) – A scikit-learn, TensorFlow, or XGBoost function.

  • grid (dict) – The dictionary of hyperparameters for grid search.

alphapy.estimators.find_optional_packages()

Find optional machine learning packages.

Parameters:

None

Return type:

None

alphapy.estimators.get_algos_config(cfg_dir)

Read the algorithms configuration file.

Parameters:

cfg_dir (str) – The directory where the configuration file algos.yml is stored.

Returns:

specs – The specifications for determining which algorithms to run.

Return type:

dict

alphapy.estimators.get_estimators(alphapy_specs, model)

Define all the AlphaPy estimators based on the contents of the algos.yml file.

Parameters:
  • alphapy_specs (dict) – The specifications for controlling the AlphaPy pipeline.

  • model (alphapy.Model) – The model object containing global AlphaPy parameters.

Returns:

estimators – All of the estimators required for running the pipeline.

Return type:

dict

alphapy.features module

alphapy.frame module

alphapy.globals module

class alphapy.globals.Encoders(*values)

Bases: Enum

AlphaPy Encoders.

These are the encoders used in AlphaPy, as configured in the model.yml file (features:encoding:type) You can learn more about encoders here [ENC].

backdiff = 1
basen = 2
binary = 3
catboost = 4
hashing = 5
helmert = 6
jstein = 7
leaveone = 8
mestimate = 9
onehot = 10
ordinal = 11
polynomial = 12
sum = 13
target = 14
woe = 15
class alphapy.globals.ModelType(*values)

Bases: Enum

AlphaPy Model Types.

Note

Multiclass Classification multiclass is not yet implemented.

classification = 1
multiclass = 3
ranking = 2
regression = 4
class alphapy.globals.Objective(*values)

Bases: Enum

Scoring Function Objectives.

Best model selection is based on the scoring or Objective function, which must be either maximized or minimized. For example, roc_auc is maximized, while neg_log_loss is minimized.

maximize = 1
minimize = 2
class alphapy.globals.Partition(*values)

Bases: Enum

AlphaPy Partitions.

test = 2
train = 1
class alphapy.globals.PivotType(*values)

Bases: Enum

Pivot Types.

PivotHigh = 'High'
PivotLow = 'Low'
class alphapy.globals.Scalers(*values)

Bases: Enum

AlphaPy Scalers.

These are the scaling methods used in AlphaPy, as configured in the model.yml file (features:scaling:type) You can learn more about feature scaling here [SCALE].

minmax = 1
standard = 2

alphapy.group module

class alphapy.group.Group(name, space=<alphapy.space.Space object>, dynamic=True, members={})

Bases: object

Create a new Group that contains common members. All defined groups are stored in Group.groups. Group names must be unique.

Parameters:
  • name (str) – Group name.

  • space (alphapy.Space, optional) – Namespace for the given group.

  • dynamic (bool, optional, default True) – Flag for defining whether or not the group membership can change.

  • members (set, optional) – The initial members of the group, especially if the new group is fixed, e.g., not dynamic.

Variables:

groups (dict) – Class variable for storing all known groups

Examples

>>> Group('tech')
add(newlist)

Add new members to the group.

Parameters:

newlist (list) – New members or identifiers to add to the group.

Returns:

None

Return type:

None

Notes

New members cannot be added to a fixed or non-dynamic group.

groups = {}
member(item)

Find a member in the group.

Parameters:

item (str) – The member to find the group.

Returns:

member_exists – Flag indicating whether or not the member is in the group.

Return type:

bool

remove(remlist)

Read in data from the given directory in a given format.

Parameters:

remlist (list) – The list of members to remove from the group.

Returns:

None

Return type:

None

Notes

Members cannot be removed from a fixed or non-dynamic group.

alphapy.model module

alphapy.optimize module

alphapy.plots module

alphapy.space module

class alphapy.space.Space(subject='stock', source='prices', fractal='1d')

Bases: object

Create a new namespace.

Parameters:
  • subject (str) – An identifier for a group of related items.

  • source (str) – The data source of the subject.

  • fractal (str) – The time fractal of the data, e.g., “5m” or “1d”.

alphapy.space.space_name(subject, source, fractal)

Get the namespace string.

Parameters:
  • subject (str) – An identifier for a group of related items.

  • source (str) – The data source of the subject.

  • fractal (str) – The time fractal of the data, e.g., “5m” or “1d”.

Returns:

name – The joined namespace string.

Return type:

str

alphapy.utilities module

alphapy.utilities.datetime_stamp()

Returns today’s datetime stamp.

Returns:

dtstamp – The valid datetime string in YYYYmmdd_hhmmss format.

Return type:

str

alphapy.utilities.ensure_dir(directory_path)
alphapy.utilities.most_recent_file(directory, file_spec)

Find the most recent file in a directory.

Parameters:
  • directory (str) – Full directory specification.

  • file_spec (str) – Wildcard search string for the file to locate.

Returns:

file_name – Name of the file to read, excluding the extension.

Return type:

str

alphapy.utilities.remove_list_items(elements, alist)

Remove one or more items from the given list.

Parameters:
  • elements (list) – The items to remove from the list alist.

  • alist (list) – Any object of any type can be a list item.

Returns:

sublist – The subset of items after removal.

Return type:

list

Examples

>>> test_list = ['a', 'b', 'c', test_func]
>>> remove_list_items([test_func], test_list)  # ['a', 'b', 'c']
alphapy.utilities.split_duration(duration_str)

Subtract a number of days from a given date.

Parameters:

duration_str (str) – An alphanumeric string in the format of a Pandas offset alias.

Returns:

  • value (int) – The duration value.

  • unit (str) – The scale of the period.

Examples

>>> split_duration('5min')   # 5, 'min'
alphapy.utilities.subtract_days(date_string, ndays)

Subtract a number of days from a given date.

Parameters:
  • date_string (str) – An alphanumeric string in the format %Y-%m-%d.

  • ndays (int) – Number of days to subtract.

Returns:

new_date_string – The adjusted date string in the format %Y-%m-%d.

Return type:

str

Examples

>>> subtract_days('2017-11-10', 31)   # '2017-10-10'
alphapy.utilities.valid_date(date_string)

Determine whether or not the given string is a valid date.

Parameters:

date_string (str) – An alphanumeric string in the format %Y-%m-%d.

Returns:

date_string – The valid date string.

Return type:

str

Raises:

ValueError – Not a valid date.

Examples

>>> valid_date('2016-7-1')   # datetime.datetime(2016, 7, 1, 0, 0)
>>> valid_date('345')        # ValueError: Not a valid date
alphapy.utilities.valid_name(name)

Determine whether or not the given string is a valid alphanumeric string.

Parameters:

name (str) – An alphanumeric identifier.

Returns:

resultTrue if the name is valid, else False.

Return type:

bool

Examples

>>> valid_name('alpha')   # True
>>> valid_name('!alpha')  # False

alphapy.variables module

class alphapy.variables.Variable(name, expr, replace=False)

Bases: object

Create a new variable as a key-value pair. All variables are stored in Variable.variables. Duplicate keys or values are not allowed, unless the replace parameter is True.

Parameters:
  • name (str) – Variable key.

  • expr (str) – Variable value.

  • replace (bool, optional) – Replace the current key-value pair if it already exists.

Variables:

variables (dict) – Class variable for storing all known variables

Examples

>>> Variable('rrunder', 'rr_3_20 <= 0.9')
>>> Variable('hc', 'higher_close')
variables = {}
alphapy.variables.allvars(expr, match_fractal=True, match_lag=True)

Get the list of valid names in the expression.

Parameters:
  • expr (str) – A valid expression conforming to the Variable Definition Language.

  • match_fractal (bool) – Flag to match fractal special character.

  • match_lag (bool) – Flag to match fractal special character.

Returns:

vlist – List of valid variable names.

Return type:

list

alphapy.variables.build_feature_order(features)

Build topologically sorted list of all variables needed.

This function pre-computes the complete variable execution order for a list of features, eliminating redundant vtree calls when processing multiple symbols.

Parameters:

features (list) – List of feature names to compute.

Returns:

ordered – Ordered list of all variables to apply.

Return type:

list

alphapy.variables.cached_vtree(feature)

Cached version of vtree for performance.

Parameters:

feature (str) – The feature name to get antecedent variables for.

Returns:

Tuple of all antecedent variables (tuple for hashability).

Return type:

tuple

alphapy.variables.vapply_fast(symbols, frames, features, vfuncs=None, max_workers=8)

Apply variables to multiple dataframes with parallelization.

This is an optimized version of vapply that: 1. Pre-computes the variable execution order once 2. Processes symbols in parallel using ThreadPoolExecutor 3. Caches vtree results to avoid redundant computation

Parameters:
  • symbols (list) – List of symbol names.

  • frames (dict) – Dictionary mapping symbol names to DataFrames.

  • features (list) – List of features to compute.

  • vfuncs (dict, optional) – Dictionary of external modules and functions.

  • max_workers (int) – Maximum number of parallel workers (default 8).

Returns:

results – Dictionary mapping symbols to DataFrames with computed features.

Return type:

dict

alphapy.variables.vexec(f, v, vfuncs=None)

Add a variable to the given dataframe.

This is the core function for adding a variable to a dataframe. To define your own variable functions, populate the vfuncs parameter with the list of modules and functions to be imported and applied by the vexec function.

To write your own variable function, your function must have a pandas DataFrame as an input parameter and must return a pandas DataFrame with the new variable(s).

Parameters:
  • f (pandas.DataFrame) – Dataframe to contain the new variable.

  • v (str) – Variable to add to the dataframe.

  • vfuncs (dict, optional) – Dictionary of external modules and functions.

Returns:

f – Dataframe with the new variable.

Return type:

pandas.DataFrame

Other Parameters:

Variable.variables (dict) – Global dictionary of variables

alphapy.variables.vexpr(f, v)

Get the expanded expression for a variable.

Parameters:
  • f (pandas.DataFrame) – Dataframe containing the variables.

  • v (str) – Variable to add to the dataframe.

Returns:

expr_new – Expanded expression for evaluation.

Return type:

str

Other Parameters:

Variable.variables (dict) – Global dictionary of variables

alphapy.variables.vfunc(f, v, vfuncs)

Find a function for defining a variable.

Parameters:
  • f (pandas.DataFrame) – Dataframe to contain the new variable.

  • v (str) – Variable representing a function.

  • vfuncs (dict, optional) – Dictionary of external modules and functions.

Returns:

  • func (function) – Function to execute for defining the variable.

  • newlist (list) – Function parameter list.

Other Parameters:

Variable.variables (dict) – Global dictionary of variables

alphapy.variables.vparse(vname)

Parse a variable name into its respective components.

Parameters:

vname (str) – The name of the variable.

Returns:

  • vxlag (str) – Original variable name without the lag component.

  • root (str) – The base variable name without the parameters.

  • valias (str) – Expanded name with alias substitution.

  • plist (list) – The parameter list.

  • lag (int) – The offset starting with the current value [0] and counting back, e.g., an offset [1] means the previous value of the variable.

Notes

AlphaPy makes feature creation easy. The syntax of a variable name maps to a function call:

xma_20_50 => xma(20, 50)

Examples

>>> vparse('lmin_5[2]')
# (0, 'lmin_5', 'lmin', 'lowest_low', ['5'], 2)
alphapy.variables.vsub(v, expr)

Substitute the variable parameters into the expression.

This function performs the parameter substitution when applying features to a dataframe. It is a mechanism for the user to override the default values in any given expression when defining a feature, instead of having to programmatically call a function with new values.

Parameters:
  • v (str) – Variable name.

  • expr (str) – The expression for substitution.

Returns:

The expression with the new, substituted values.

Return type:

newexpr

alphapy.variables.vtree(vname)

Get all of the antecedent variables.

Before applying a variable to a dataframe, we have to recursively get all of the child variables, beginning with the starting variable’s expression. Then, we have to extract the variables from all the subsequent expressions. This process continues until all antecedent variables are obtained.

Parameters:

vname (str) – A valid variable stored in Variable.variables.

Returns:

all_variables – The variables that need to be applied before vname.

Return type:

list

Other Parameters:

Variable.variables (dict) – Global dictionary of variables

Module contents

AlphaPy: a domain-agnostic ML pipeline framework.

Trading, markets, and Alfi were split off into the private alphapy-finance repo in v4.0.0. See CHANGELOG.md and tag v3.1.1-monolith for the pre-split state.