strategy.utils.volatility_model
pd
BaseModel
VolatilityModel Objects
class VolatilityModel(BaseModel)
A class for calculating various volatility models on financial data.
This class inherits from BaseModel and provides methods to calculate different volatility indicators such as ATR, Standard Deviation, Bollinger Bands, RVI, and Normalized Standard Deviation.
Attributes:
datapd.DataFrame or pd.Series - The input financial data.data_formatstr - The type of input data ('ohlcv' or 'close').
__init__
def __init__(data)
Initialize the VolatilityModel with the given data.
Arguments:
datapd.DataFrame or pd.Series - The input financial data.
atr
def atr(window, scalar=1)
Calculate the Average True Range (ATR) and its bounds.
Arguments:
windowint - The rolling window for ATR calculation.scalarfloat, optional - Multiplier for the upper and lower bounds. Defaults to 1.
Returns:
pd.DataFrame- A DataFrame containing ATR, Upper Bound, and Lower Bound.
std
def std(window, scalar=1)
Calculate the Standard Deviation (STD) and its bounds.
Arguments:
windowint - The rolling window for STD calculation.scalarfloat, optional - Multiplier for the upper and lower bounds. Defaults to 1.
Returns:
pd.DataFrame- A DataFrame containing STD, Upper Bound, and Lower Bound.
bollinger_bands
def bollinger_bands(window, scalar=2)
Calculate the Bollinger Bands (BB) and Bollinger Bandwidth (BBW).
Arguments:
windowint - The rolling window for BB calculation.scalarfloat, optional - Number of standard deviations for the bands. Defaults to 2.
Returns:
pd.DataFrame- A DataFrame containing BBW, Upper Bound, and Lower Bound.
rvi
def rvi(window, scalar=1)
Calculate the Relative Volatility Index (RVI) and its bounds.
Arguments:
windowint - The rolling window for RVI calculation.scalarfloat, optional - Multiplier for the upper and lower bounds. Defaults to 1.
Returns:
pd.DataFrame- A DataFrame containing RVI, Upper Bound, and Lower Bound.
normalized_std
def normalized_std(window, scalar=1)
Calculate the Normalized Standard Deviation and its bounds.
The Normalized STD is calculated as mean(std) / max(std) over the given window.
Arguments:
windowint - The rolling window for the calculation.scalarfloat, optional - Multiplier for the upper and lower bounds. Defaults to 1.
Returns:
pd.DataFrame- A DataFrame containing Normalized STD, Upper Bound, and Lower Bound.
model
def model(model_type, window, scalar=1)
Select and calculate the specified volatility model.
Arguments:
model_typestr - The type of model to use.Options- 'std', 'atr', 'bb', 'rvi', or 'normalized_std'.windowint - The rolling window for the model calculation.scalarfloat, optional - Multiplier for bounds or bands. Defaults to 1.
Returns:
pd.DataFrame- A DataFrame containing the calculated model values and bounds.
Raises:
ValueError- If an unrecognized model type is provided.
get_data_window
@classmethod
def get_data_window(cls, model_type, window)
Calculate the required minimum data length for each model type.
Arguments:
model_type: String, the type of model (e.g. 'std', 'atr', 'bb')window: Integer, the window size for the model.
Returns:
Integer, the required data length for the specified model.