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.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.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.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.BarType(*values)

Bases: Enum

Bar Types.

Bar Types for running models, usually translated from a normal OHLC bar to a weighted bar based on volume, dollar amount, etc.

dollar = 2
heikinashi = 3
time = 1
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
system = 5
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.Orders

Bases: object

System Order Types.

Variables:
  • le (str) – long entry

  • se (str) – short entry

  • lx (str) – long exit

  • sx (str) – short exit

  • lh (str) – long exit at the end of the holding period

  • sh (str) – short exit at the end of the holding period

le = 'le'
lh = 'lh'
lx = 'lx'
se = 'se'
sh = 'sh'
sx = 'sx'
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.metalabel module

alphapy.mflow_main module

alphapy.model module

alphapy.nlp module

alphapy.optimize module

alphapy.plots module

alphapy.portfolio 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.system module

alphapy.transforms module

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

Module contents