azapy.Util package

Submodules

azapy.Util.add_cash_security module

azapy.Util.add_cash_security.add_cash_security(data, name='_CASH_', value=1)

Add cash like security to an existent market data.

Parameters:
datapandas.DataFrame or dict

Initial market data to which a cash like security will be added.

namestr, optional

The symbol of the cash like security. Must be different than any symbol in data. Note that CASH is a valid stock symbol. The default is ‘_CASH_’.

valuefloat, optional

Nominal value of the cash like security. It is constant overtime (no dividends and no splits). The default is 1.

Returns:
`pandas.DataFrame` or `dict`The new MkT data updated with the cash like security,in the same format of data input format, i.e., pandas.DataFrame or ‘dict’.

azapy.Util.drawdown module

azapy.Util.drawdown.drawdown(mktdata, col=None, top=10)

Computes the largest drawdowns for time-series of prices.

Parameters:
mktdatapandas.Series or pandas.DataFrame

Historical daily time-series of prices as a pandas.Series or as a pandas.DataFrame

colstr, optional

The column name if mktdata is a pandas.DataFrame. If it is set to None, then mktdata is assumed to be a pandas.Series. The default is None.

`top`int, optional

Maximum number of largest drawdowns to be reported. The default is 10.

Returns:
`pandas.DataFrame`Table containing the drawdowns ordered from the
largest to smallest.

Table columns are:

  • ‘DD’: (float) drawdown max value

  • ‘Date’: (pandas.Timestamp) drawdown max value date

  • ‘Start’: (pandas.Timestamp) drawdown start date

  • ‘End’: (pandas.Timestamp) drawdown recovery date

  • ‘NrDays’: (int) drawdown length in number of days

The number of rows is <= top

azapy.Util.drawdown.max_drawdown(mktdata, col=None)

Computes the maximum drawdown for a time-series of prices.

Parameters:
mktdatapandas.Series or pandas.DataFrame

Time-series of prices as a pandas.Series or as column in a pandas.DataFrame`

colstr, optional

Column name if mktdata is a pandas.DataFrame. If is set to None, then mktdata is assumed to be a pandas.Series. The default is None.

Returns:
(`float`, `pandas.Timestamp`, `pandas.Timestamp`, `pandas.Timestamp`, `int`)Tuple
  • value of the drawdown,

  • maximum drawdown date,

  • drawdown start date,

  • drawdown end date. It is set to nan if the drawdown is still in progress.

  • drawdown length in number of days

azapy.Util.gamblingKelly module

azapy.Util.gamblingKelly.gamblingKelly(pp=[0.6])

Computes the Kelly allocation for multiple binary games.

Parameters:
pplist, optional

List of winning probabilities for each game. The default is [0.6].

Returns:
`pandas.Series`Bets size as percentage of the capital.

azapy.Util.schedule module

azapy.Util.schedule.schedule_offset(sdate='2010-01-01', edate='today', freq='Q', noffset=-3, fixoffset=-1, calendar=None, hlength=1.25)

Creates a simple schedule (‘Droll’, ‘Dfix’) with an offset start.

Parameters:
sdatestr, optional

Start date (reference) to which the offset is added. The default is ‘2010-01-01’.

edatestr, optional

End date (reference) of the schedule. The default is ‘today’.

freqstr, optional

Rolling period. It can take 2 values: ‘Q’ for quarterly and ‘M’ for monthly rolling periods. The default is ‘Q’.

noffsetint, optional

Offset in number of business days for Droll relative to the end of calendar period (quarter or month). The default is -3.

fixoffsetint, optional

Offset in number of business days for Dfix relative to Droll. It can be zero or negative. The default is -1.

calendarnumpy.busdaycalendar, optional

Business days calendar. If is it None then the calendar will be set to NYSE business calendar. The default is None.

hlengthfloat, optional

Offset, in number of years, for first ‘Droll’ relative to ‘sdate’. A fractional value will be rounded to an integer number of months via round(hlength * 12, 0). hlength must be non-negative. The default is 1.25 years.

Returns:
`pandas.DataFrame`Table containing 2 datetime columns
  • ‘Droll’ the rolling date,

  • ‘Dfix’ the fixing date.

azapy.Util.schedule.schedule_roll(sdate='2010-01-01', edate='today', freq='Q', noffset=-3, fixoffset=-1, calendar=None, hlength=1.25)

Creates a schedule with rolling history: ‘Droll’, ‘Dfix’ and ‘Dhist’.

Parameters:
sdatestr, optional

Start date (reference) of the schedule. The default is ‘2010-01-01’.

edatestr, optional

End date (reference) of the schedule. The default is ‘today’.

freqstr, optional

Rolling period. It can take 2 values: ‘Q’ for quarterly and ‘M’ for monthly rolling periods. The default is ‘Q’.

noffsetint, optional

Offset in number of business days for Droll relative to the end of calendar period (quarter or month). The default is -3.

fixoffsetint, optional

Offset in number of business days for Dfix relative to Droll. It can be zero or negative. The default is -1.

calendarnumpy.busdaycalendar, optional

Business days calendar. If is it None then the calendar will be set to NYSE business calendar. The default is None.

hlengthfloat, optional

Offset in number of years for ‘Dhist’ relative to ‘Dfix’. A fractional value will be rounded to an integer number of months via round(hlength * 12, 0). hlength must be non-negative. The default is 1.25 years.

Returns:
`pandas.DataFrame`Table containing 3 datetime columns,
  • ‘Droll’ the rolling date,

  • ‘Dfix’ the fixing date,

  • ‘Dhist’ start date for calibration period.

azapy.Util.schedule.schedule_simple(sdate='2010-01-01', edate='today', freq='Q', noffset=-3, fixoffset=-1, calendar=None)

Creates a simple schedule: ‘Droll’, ‘Dfix’.

Parameters:
sdatestr, optional

Start date (reference) of the schedule. The default is ‘2010-01-01’.

edatestr, optional

End date (reference) of the schedule. The default is ‘today’.

freqstr, optional

Rolling period. It can take 2 values: ‘Q’ for quarterly and ‘M’ for monthly rolling periods. The default is ‘Q’.

noffsetint, optional

Offset in number of business days for Droll relative to the end of calendar period (quarter or month). The default is -3.

fixoffsetint, optional

Offset in number of business days for Dfix relative to Droll. It must be <=0. The default is -1.

calendarnumpy.busdaycalendar, optional

Business days calendar. If is it None then the calendar will be set to NYSE business calendar. The default is None.

Returns:
`pandas.DataFrame`Table containing 2 datetime columns,
  • ‘Droll’ the rolling date,

  • ‘Dfix’ the fixing date.

azapy.Util.randomgen module

azapy.Util.randomgen.random_simplex(rng, simplex_dim, size=1, antithetic=False, dirichlet_alpha=None)

Returns random vectors in the n-simplex (based on Dirichlet generator) sum_{k=1}^n x_k = 1 for x_k >= 0

Parameters:
rngnumpy.random.mtrand.RandomState

Random number generator (e.g. as returned by numpy.random.RandomState(42)).

simplex_dimpositive int

Simplex dimension.

sizepositive int, optional

Sample size. The default is 1.

antitheticBoolean, optional

If it is set to True, then all the vector permutation will be added to the sample. The actual size of the sample will be size * factorial(simplex_dim). The default is False.

dirichlet_alphalist, optional

Dirichlet alpha coefficients. The length of the list must be equal to simplex_dim. If it is None, then uniform distributed random vectors are generated (equivalent to dirichlet_alpha = [1] * simple_dim). The default is None.

Returns:
numpy.array

A sample of size size of random vectors of length simplex_dim.

azapy.Util.randomgen.runif_simplex(rng, simplex_dim, size=1)

Returns uniformly distributed random vectors in the n-simplex sum_{k=1}^n x_k = 1 for x_k >= 0

Parameters:
rngnumpy.random.mtrand.RandomState

Random number generator (e.g. as returned by numpy.random.RandomState(42)).

simplex_dimpositive int

Simplex dimension.

sizepositive int, optional

Sample size. The default is 1.

Returns:
numpy.array

A sample of size size of random vectors of length simplex_dim.

Module contents