stat_arb_tools package

Submodules

stat_arb_tools.adf module

class stat_arb_tools.adf.AutoLag[source]

Bases: enum.Enum

An enumeration.

aic = 'AIC'
bic = 'BIC'
none = None
tstat = 't-stat'
class stat_arb_tools.adf.Regression[source]

Bases: enum.Enum

An enumeration.

c = 'constant'
ct = 'constant and trend'
ctt = 'constant, and linear and quadratic trend'
nc = 'no constant and no trend'
stat_arb_tools.adf.adf(l1: List[float], maxlag: int = None, regression: stat_arb_tools.adf.Regression = <Regression.c: 'constant'>, autolag: stat_arb_tools.adf.AutoLag = <AutoLag.aic: 'AIC'>, store: bool = False, regresults: bool = False) → Tuple[float, float, int, int, dict, float, Optional[statsmodels.tsa.stattools.ResultsStore]][source]

Runs the augmented Dickey-Fuller test.

Parameters:
l1 : list of float

Time series of ut hat.

maxlag : int

Maximum lag which is included in test, default 12*(nobs/100)^{1/4}

regression : Regression enum

Constant and trend order to include in regression

  • ‘c’ : constant only (default)
  • ‘ct’ : constant and trend
  • ‘ctt’ : constant, and linear and quadratic trend
  • ‘nc’ : no constant, no trend
autolag : Autolag enum
  • if None, then maxlag lags are used
  • if ‘AIC’ (default) or ‘BIC’, then the number of lags is chosen to minimize the corresponding information criterion
  • ‘t-stat’ based choice of maxlag. Starts with maxlag and drops a lag until the t-statistic on the last lag length is significant using a 5%-sized test
store : bool

If True, then a result instance is returned additionally to the adf

regresults : bool

If True, the full regression results are returned.

Returns:
tuple of various values
  • adf (float) – Test statistic
  • pvalue (float) – MacKinnon’s approximate p-value based on MacKinnon (1994, 2010)
  • usedlag (int) – Number of lags used
  • nobs (int) – Number of observations used for the ADF regression and calculation of the critical values
  • critical values (dict) – Critical values for the test statistic at the 1 %, 5 %, and 10 % levels. Based on MacKinnon (2010)
  • icbest (float) – The maximized information criterion if autolag is not None.
  • resstore (ResultStore, optional) – A dummy class with results attached as attributes
Raises:
NilListException

If a list of length zero is provided.

stat_arb_tools.adf.isStationary(l1: List[float], l2: List[float], maxlag: int = None, regression: stat_arb_tools.adf.Regression = <Regression.c: 'constant'>, autolag: stat_arb_tools.adf.AutoLag = <AutoLag.aic: 'AIC'>, store: bool = False, regresults: bool = False) → Tuple[float, float, int, int, dict, float, Optional[statsmodels.tsa.stattools.ResultsStore]][source]

Runs the augmented Dickey-Fuller test on two time series of asset prices.

Parameters:
l1 : list of float

Time series of prices of asset #1.

l2 : list of float

Time series of prices of asset #2.

maxlag : int

Maximum lag which is included in test, default 12*(nobs/100)^{1/4}

regression : Regression enum

Constant and trend order to include in regression

  • ‘c’ : constant only (default)
  • ‘ct’ : constant and trend
  • ‘ctt’ : constant, and linear and quadratic trend
  • ‘nc’ : no constant, no trend
autolag : Autolag enum
  • if None, then maxlag lags are used
  • if ‘AIC’ (default) or ‘BIC’, then the number of lags is chosen to minimize the corresponding information criterion
  • ‘t-stat’ based choice of maxlag. Starts with maxlag and drops a lag until the t-statistic on the last lag length is significant using a 5%-sized test
store : bool

If True, then a result instance is returned additionally to the adf

regresults : bool

If True, the full regression results are returned.

Returns:
tuple of various values
  • adf (float) – Test statistic
  • pvalue (float) – MacKinnon’s approximate p-value based on MacKinnon (1994, 2010)
  • usedlag (int) – Number of lags used
  • nobs (int) – Number of observations used for the ADF regression and calculation of the critical values
  • critical values (dict) – Critical values for the test statistic at the 1 %, 5 %, and 10 % levels. Based on MacKinnon (2010)
  • icbest (float) – The maximized information criterion if autolag is not None.
  • resstore (ResultStore, optional) – A dummy class with results attached as attributes
Raises:
NilListException

If a list of length zero is provided.

MissMatchedLengthException

If the lists are not of equal length.

stat_arb_tools.beta module

stat_arb_tools.beta.calcBetaHat(l1: List[float], l2: List[float]) → float[source]

Calculates beta hat.

Parameters:
l1 : list of float

Time series list of prices of asset #1.

l2 : list of float

Time series list of prices of asset #2.

Returns:
float

Returns beta hat.

Raises:
NilListException

If a list of length zero is provided.

MissMatchedLengthException

If the lists are not of equal length.

stat_arb_tools.beta.calcHedgeRatio(betaHat: float, sigmaHat: float) → float[source]

Calculates the hedge ratio.

Parameters:
betaHat : float

Beta hat of two assets.

sigmaHat : float

Sigma hat of two assets.

Returns:
float

Returns the hedge ratio.

stat_arb_tools.exceptions module

stat_arb_tools.kellycritereon module

stat_arb_tools.kellycritereon.calcKellyCritereon(chanceOfWin: float, avgWin: float, avgLoss) → float[source]

Calculates the percent of a portfolio to commit to a given play.

Parameters:
chanceOfWin : float

The percent of times that a play results in a win.

avgWin : float

The average roi when the play wins.

avgLoss : float

The average roi when the play loses. This number should be positive (e.g. 0.1 for 10%).

Returns:
float

The percent of your portfolio to commit to a given play.

Raises:
DivisionByZeroException

The parameters provided would reult in a division by zero.

stat_arb_tools.kellycritereon.calcSimpKellyCritereon(p: float, r: float) → float[source]

Calculates the simplified kelly critereon, which can be interpreted to be the fraction of the portfolio that you can afford to lose.

Parameters:
p : float

The percent of times that a play results in a win.

r : float

The ratio of average loss to average gain.

Returns:
float

The percent of your portfolio that you can afford to lose.

Raises:
InvalidLossRatioException

The loss ratio must be less than one and greater than zero

stat_arb_tools.sigma module

stat_arb_tools.sigma.calcEffectiveStandardDeviation(betaHat: float, hedgeRatio: float, sigmaHat: float) → float[source]

Calculates effective standard deviation.

Parameters:
betaHat : float

Beta hat of the two asset series.

hedgeRatio : list of float

The hedge ratio of two assets.

sigmaHat : list of float

The sigma hat of two assets.

Returns:
float

Returns the effective standard deviation.

Raises:
ZeroHedgeRatioException

If the hedge ratio is zero.

stat_arb_tools.sigma.calcSigmaHat(l1: List[float], l2: List[float], betaHat: float) → float[source]

Calculates sigma hat (squared).

Parameters:
l1 : list of float

Time series list of prices of asset #1.

l2 : list of float

Time series list of prices of asset #2.

betaHat : float

Beta hat of the two asset series.

Returns:
float

Returns sigma hat (squared).

Raises:
NilListException

If a list of length zero is provided.

MissMatchedLengthException

If the lists are not of equal length.

L2ZeroValException

If a zero value is present in l2.

BetaHatLTEZeroException

If beta hat is less than or equal to zero.

stat_arb_tools.spread module

stat_arb_tools.spread.calcDist(zX: List[float], zY: List[float]) → float[source]

Calculates the sum of squared differences between two normalized price series. Normalization should be done through the z-transform.

Parameters:
zX : list of float

Normalized list of asset #1

zY : list of float

Normalized list of asset #2

Returns:
float

Returns the sum of squared differences.

Raises:
NilListException

If a list of length zero is provided.

MissMatchedLengthException

If the lists are not of equal length.

stat_arb_tools.spread.calcNotes(l1: List[float], l2: List[float]) → List[float][source]

At time t, the log price difference (spread) is a random variable ut plus a constant ln(𝛽). This function calculates those random variables, ut.

Parameters:
l1 : list of float

Time series of prices of asset #1.

l2 : list of float

Time series of prices of asset #2.

Returns:
list of float
Raises:
NilListException

If a list of length zero is provided.

MissMatchedLengthException

If the lists are not of equal length.

stat_arb_tools.spread.calcSpread(p: float, q: float, hedgeRatio: float) → float[source]

Calculates the spread between two asset prices at time t.

Parameters:
p : float

Price at time t of one of the two assets.

q : float

Price at time t of the other of the two assets.

hedgeRatio : float

The hedge ratio between the two assets.

Returns:
float

Returns the spread.

stat_arb_tools.spread.calcSpreadReturn(p: float, q: float, hedgeRatio: float) → float[source]

Calculates the spread return between two asset prices at time t.

Parameters:
p : float

Price at time t of one of the two assets.

q : float

Price at time t of the other of the two assets.

hedgeRatio : float

The hedge ratio between the two assets.

Returns:
float

Returns the spread return.

Raises:
ZeroHedgeRatioException

If the hedge ratio is zero.

ZeroQException

If Q is zero.

stat_arb_tools.stoploss module

stat_arb_tools.stoploss.calcOptimalStopLoss(M: float, gain: float, loss: float, chanceCorrect: float) → float[source]

Calculates the optimal stop loss amount.

Parameters:
M : float

The larges OL (Open minus Low) or HO (Hight minus Open) in the history of the data.

gain : float

The average gain of the historical plays that ended in a gain.

loss : float

The average loss of the historical plays that ended in a loss.

chanceCorrect : float

The percent of historical plays that ended in a gain.

Returns:
float

Returns the stop loss at which to close the play.

stat_arb_tools.stoploss.calcOptimalVarStopLoss(b: float, vol: float, entry: float) → Tuple[float, float][source]

Calculates the optimal stop loss amount using the value at risk approach.

Parameters:
b : float

The volatility multiplier.

vol : float

Historical volatility.

entry : float

The entry price

Returns:
tuple of float

Returns the stop loss at which to close the play using the value at risk approach for both short and long first.

stat_arb_tools.znorm module

stat_arb_tools.znorm.zNorm(l: List[float]) → List[float][source]

Normalizes a list using the z-transform.

Parameters:
l : list of float

Time series list of prices of asset.

Returns:
list of float

Returns the normalized list.

Raises:
NilListException

If a list of length zero is provided.

ZeroStdDevException

If a standard deviation is zero.

Module contents