Skip to content
Ben Bell edited this page Nov 15, 2024 · 1 revision

Utility Functions Documentation

The utils.py module contains various utility functions for calculating technical indicators, risk metrics, and other helper functions used in the backtesting framework.

Functions

calculateSMA(data: pd.Series, period: int) -> pd.Series

Calculates the Simple Moving Average (SMA) for the given data and period.

calculateEMA(data: pd.Series, period: int) -> pd.Series

Calculates the Exponential Moving Average (EMA) for the given data and period.

calculateRSI(data: pd.Series, period: int = 14) -> pd.Series

Calculates the Relative Strength Index (RSI) for the given data and period.

calculateBollingerBands(data: pd.Series, period: int = 20, stdDev: int = 2) -> Tuple[pd.Series, pd.Series, pd.Series]

Calculates the Bollinger Bands for the given data, period, and standard deviation.

calculateMACD(data: pd.Series, fastPeriod: int = 12, slowPeriod: int = 26, signalPeriod: int = 9) -> Tuple[pd.Series, pd.Series, pd.Series]

Calculates the Moving Average Convergence Divergence (MACD) indicator for the given data and periods.

calculateDrawdown(data: pd.Series) -> Tuple[float, float]

Calculates the maximum drawdown and current drawdown for the given data.

calculateSharpeRatio(returns: pd.Series, riskFreeRate: float = 0.01) -> float

Calculates the Sharpe Ratio for the given returns and risk-free rate.

calculateVolatility(returns: pd.Series, annualized: bool = True) -> float

Calculates the volatility for the given returns, optionally annualized.

calculateBeta(returns: pd.Series, marketReturns: pd.Series) -> float

Calculates the beta for the given returns and market returns.

calculateReturnStats(returns: pd.Series) -> dict

Calculates various return statistics for the given returns.

calculateSortinoRatio(returns: pd.Series, riskFreeRate: float = 0.01) -> float

Calculates the Sortino Ratio for the given returns and risk-free rate.

calculateTreynorRatio(returns: pd.Series, marketReturns: pd.Series, riskFreeRate: float = 0.01) -> float

Calculates the Treynor Ratio for the given returns, market returns, and risk-free rate.

calculateVaR(returns: pd.Series, confidence_level: float = 0.95) -> float

Calculates the Value at Risk (VaR) for the given returns and confidence level.

getPreviousRows(data: pd.DataFrame, date: datetime, periods: int) -> pd.DataFrame

Gets the previous rows of data up to the specified date and number of periods.

analyzeResults(results1: BacktestResult, results2: BacktestResult, initial_investment: float = 10000) -> dict

Analyzes and compares the results of two backtests.

compareBacktests(results1: BacktestResult, results2: BacktestResult) -> dict

Compares the results of two backtests and returns a summary of the differences.

getSP500Returns(start_date: datetime, end_date: datetime) -> pd.Series

Gets the historical returns of the S&P 500 index for the specified date range.

Clone this wiki locally