📑 API Reference¤
This section holds a comprehensive documentation of all of classes, methods and functions in the humbldata
package.
humbldata package.
humbldata.cli
¤
humbldata CLI.
humbldata.cli.say
¤
say(message: str = '') -> None
Say a message.
Source code in src\humbldata\cli.py
8 9 10 11 |
|
humbldata.core
¤
The core
module to contain logic & functions used in controllers.
This module is intended to contain sub-modules and functions that are not directly utilized from the package, but rather used in building the package itself. This means that the core module should not contain any code that is specific to the package's use case, but rather should be generic and reusable in other contexts.
humbldata.core.standard_models
¤
Models to represent core data structures of the Standardization Framework.
humbldata.core.standard_models.abstract
¤
Abstract core DATA MODELS to be inherited by other models.
humbldata.core.standard_models.abstract.data
¤
A wrapper around OpenBB Data Standardized Model to use with humbldata.
humbldata.core.standard_models.abstract.data.Data
¤
Bases: Data
An abstract standard_model to represent a base Data Model.
The Data Model should be used to define the data that is being
collected and analyzed in a context.category.command
call.
This Data model is meant to be inherited and built upon by other standard_models for a specific context.
Example
total_time = f"{end_time - start_time:.3f}"
class EquityHistoricalData(Data):
date: Union[dateType, datetime] = Field(
description=DATA_DESCRIPTIONS.get("date", "")
)
open: float = Field(description=DATA_DESCRIPTIONS.get("open", ""))
high: float = Field(description=DATA_DESCRIPTIONS.get("high", ""))
low: float = Field(description=DATA_DESCRIPTIONS.get("low", ""))
close: float = Field(description=DATA_DESCRIPTIONS.get("close", ""))
volume: Optional[Union[float, int]] = Field(
default=None, description=DATA_DESCRIPTIONS.get("volume", "")
)
@field_validator("date", mode="before", check_fields=False)
def date_validate(cls, v): # pylint: disable=E0213
v = parser.isoparse(str(v))
if v.hour == 0 and v.minute == 0:
return v.date()
return v
Source code in src\humbldata\core\standard_models\abstract\data.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
humbldata.core.standard_models.abstract.errors
¤
An ABSTRACT DATA MODEL to be inherited by custom errors.
humbldata.core.standard_models.abstract.errors.HumblDataError
¤
Bases: BaseException
Base Error for HumblData logic.
Source code in src\humbldata\core\standard_models\abstract\errors.py
4 5 6 7 8 9 |
|
humbldata.core.standard_models.abstract.query_params
¤
A wrapper around OpenBB QueryParams Standardized Model to use with humbldata.
humbldata.core.standard_models.abstract.query_params.QueryParams
¤
Bases: QueryParams
An abstract standard_model to represent a base QueryParams Data.
QueryParams model should be used to define the query parameters for a
context.category.command
call.
This QueryParams model is meant to be inherited and built upon by other standard_models for a specific context.
Examples:
class EquityHistoricalQueryParams(QueryParams):
symbol: str = Field(description=QUERY_DESCRIPTIONS.get("symbol", ""))
interval: Optional[str] = Field(
default="1d",
description=QUERY_DESCRIPTIONS.get("interval", ""),
)
start_date: Optional[dateType] = Field(
default=None,
description=QUERY_DESCRIPTIONS.get("start_date", ""),
)
end_date: Optional[dateType] = Field(
default=None,
description=QUERY_DESCRIPTIONS.get("end_date", ""),
)
@field_validator("symbol", mode="before", check_fields=False)
@classmethod
def upper_symbol(cls, v: Union[str, List[str], Set[str]]):
if isinstance(v, str):
return v.upper()
return ",".join([symbol.upper() for symbol in list(v)])
This would create a class that would be used to query historical price data for equities from any given command.
This could then be used to create a
MandelbrotChannelEquityHistoricalQueryParams
that would define what query
parameters are needed for the Mandelbrot Channel command.
Source code in src\humbldata\core\standard_models\abstract\query_params.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
humbldata.core.standard_models.abstract.singleton
¤
An ABSTRACT DATA MODEL, Singleton, to represent a class that should only have one instance.
humbldata.core.standard_models.abstract.singleton.SingletonMeta
¤
Bases: type
, Generic[T]
SingletonMeta is a metaclass that creates a Singleton instance of a class.
Singleton design pattern restricts the instantiation of a class to a single instance. This is useful when exactly one object is needed to coordinate actions across the system.
Source code in src\humbldata\core\standard_models\abstract\singleton.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
humbldata.core.standard_models.abstract.singleton.SingletonMeta.__call__
¤__call__(*args, **kwargs) -> T
Override the call method.
If the class exists, otherwise creates a new instance and stores it in the _instances dictionary.
Source code in src\humbldata\core\standard_models\abstract\singleton.py
21 22 23 24 25 26 27 28 29 30 31 32 |
|
humbldata.core.standard_models.abstract.tagged
¤
An ABSTRACT DATA MODEL, Tagged, to be inherited by other models as identifier.
humbldata.core.standard_models.abstract.tagged.Tagged
¤
Bases: BaseModel
A class to represent an object tagged with a uuid7.
Source code in src\humbldata\core\standard_models\abstract\tagged.py
7 8 9 10 |
|
humbldata.core.standard_models.toolbox
¤
Context: Toolbox || Category: Standardized Framework Model.
This module defines the QueryParams and Data classes for the Toolbox context. THis is where all of the context(s) of your project go. The STANDARD MODELS for categories and subsequent commands are nested here.
Classes:
Name | Description |
---|---|
ToolboxQueryParams |
Query parameters for the ToolboxController. |
ToolboxData |
A Pydantic model that defines the data returned by the ToolboxController. |
Attributes:
Name | Type | Description |
---|---|---|
symbol |
str
|
The symbol/ticker of the stock. |
interval |
Optional[str]
|
The interval of the data. Defaults to '1d'. |
start_date |
str
|
The start date of the data. |
end_date |
str
|
The end date of the data. |
humbldata.core.standard_models.toolbox.technical
¤
Context: Toolbox || Category: Technical.
humbldata.core.standard_models.toolbox.technical.mandelbrotchannel
¤
Mandelbrot Channel Standard Model.
Context: Toolbox || Category: Technical || Command: Mandelbrot Channel.
This module is used to define the QueryParams and Data model for the Mandelbrot Channel command.
humbldata.core.standard_models.toolbox.technical.mandelbrotchannel.MandelbrotChannelQueryParams
¤
Bases: QueryParams
QueryParams for the Mandelbrot Channel command.
Source code in src\humbldata\core\standard_models\toolbox\technical\mandelbrotchannel.py
21 22 23 24 25 26 |
|
humbldata.core.standard_models.toolbox.technical.mandelbrotchannel.MandelbrotChannelData
¤
Bases: Data
Data model for the Mandelbrot Channel command.
Source code in src\humbldata\core\standard_models\toolbox\technical\mandelbrotchannel.py
29 30 31 32 |
|
humbldata.core.standard_models.toolbox.technical.mandelbrotchannel.MandelbrotChannelFetcher
¤
Bases: MandelbrotChannelQueryParams
, MandelbrotChannelData
Fetcher for the Mandelbrot Channel command.
Source code in src\humbldata\core\standard_models\toolbox\technical\mandelbrotchannel.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
humbldata.core.standard_models.toolbox.technical.mandelbrotchannel.MandelbrotChannelFetcher.transform_query
¤transform_query()
Transform the params to the command-specific query.
Source code in src\humbldata\core\standard_models\toolbox\technical\mandelbrotchannel.py
50 51 |
|
humbldata.core.standard_models.toolbox.technical.mandelbrotchannel.MandelbrotChannelFetcher.extract_data
¤extract_data()
Extract the data from the provider.
Source code in src\humbldata\core\standard_models\toolbox\technical\mandelbrotchannel.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
humbldata.core.standard_models.toolbox.technical.mandelbrotchannel.MandelbrotChannelFetcher.transform_data
¤transform_data()
Transform the command-specific data.
Source code in src\humbldata\core\standard_models\toolbox\technical\mandelbrotchannel.py
68 69 |
|
humbldata.core.standard_models.toolbox.technical.realized_volatility
¤
Volatility Standard Model.
Context: Toolbox || Category: Technical || Command: Volatility.
This module is used to define the QueryParams and Data model for the Volatility command.
humbldata.core.standard_models.toolbox.technical.realized_volatility.RealizedVolatilityQueryParams
¤
Bases: QueryParams
QueryParams for the Realized Volatility command.
Source code in src\humbldata\core\standard_models\toolbox\technical\realized_volatility.py
21 22 23 24 |
|
humbldata.core.standard_models.toolbox.technical.realized_volatility.RealizedVolatilityData
¤
Bases: Data
Data model for the Realized Volatility command.
Source code in src\humbldata\core\standard_models\toolbox\technical\realized_volatility.py
27 28 29 30 |
|
humbldata.core.standard_models.toolbox.technical.realized_volatility.RealizedVolatilityFetcher
¤
Bases: RealizedVolatilityQueryParams
Fetcher for the Realized Volatility command.
Source code in src\humbldata\core\standard_models\toolbox\technical\realized_volatility.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
humbldata.core.standard_models.toolbox.technical.realized_volatility.RealizedVolatilityFetcher.transform_query
¤transform_query()
Transform the params to the command-specific query.
Source code in src\humbldata\core\standard_models\toolbox\technical\realized_volatility.py
48 49 |
|
humbldata.core.standard_models.toolbox.technical.realized_volatility.RealizedVolatilityFetcher.extract_data
¤extract_data()
Extract the data from the provider.
Source code in src\humbldata\core\standard_models\toolbox\technical\realized_volatility.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
humbldata.core.standard_models.toolbox.technical.realized_volatility.RealizedVolatilityFetcher.transform_data
¤transform_data()
Transform the command-specific data.
Source code in src\humbldata\core\standard_models\toolbox\technical\realized_volatility.py
68 69 |
|
humbldata.core.standard_models.toolbox.technical.realized_volatility.RealizedVolatilityFetcher.fetch_data
¤fetch_data()
Execute the TET pattern.
Source code in src\humbldata\core\standard_models\toolbox\technical\realized_volatility.py
72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
humbldata.core.standard_models.toolbox.ToolboxQueryParams
¤
Bases: QueryParams
Query parameters for the ToolboxController.
This class defines the query parameters used by the ToolboxController, including the stock symbol, data interval, start date, and end date. It also includes a method to ensure the stock symbol is in uppercase.
Attributes:
Name | Type | Description |
---|---|---|
symbol |
str
|
The symbol or ticker of the stock. |
interval |
Optional[str]
|
The interval of the data. Defaults to '1d'. Can be None. |
start_date |
str
|
The start date for the data query. |
end_date |
str
|
The end date for the data query. |
Methods:
Name | Description |
---|---|
upper_symbol |
A Pydantic |
Source code in src\humbldata\core\standard_models\toolbox\__init__.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
humbldata.core.standard_models.toolbox.ToolboxQueryParams.upper_symbol
classmethod
¤
upper_symbol(v: str | list[str] | set[str]) -> str | list[str]
Convert the stock symbol to uppercase.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v |
Union[str, List[str], Set[str]]
|
The stock symbol or collection of symbols to be converted. |
required |
Returns:
Type | Description |
---|---|
Union[str, List[str]]
|
The uppercase stock symbol or a comma-separated string of uppercase symbols. |
Source code in src\humbldata\core\standard_models\toolbox\__init__.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
humbldata.core.standard_models.toolbox.ToolboxData
¤
Bases: Data
The Data for the ToolboxController.
WIP: I'm thinking that this is the final layer around which the HumblDataObject will be returned to the user, with all necessary information about the query, command, data and charts that they should want. This HumblDataObject will return values in json/dict format, with methods to allow transformation into polars_df, pandas_df, a list, a dict...
Source code in src\humbldata\core\standard_models\toolbox\__init__.py
106 107 108 109 110 111 112 113 114 115 |
|
humbldata.core.utils
¤
humbldata core utils.
Utils is used to keep; helpers, descriptions, constants, and other useful tools.
humbldata.core.utils.constants
¤
A module to contain all project-wide constants.
humbldata.core.utils.core_helpers
¤
A module to contain core helper functions for the program.
humbldata.core.utils.core_helpers.is_debug_mode
¤
is_debug_mode() -> bool
Check if the current system is in debug mode.
Returns:
Type | Description |
---|---|
bool
|
True if the system is in debug mode, False otherwise. |
Source code in src\humbldata\core\utils\core_helpers.py
12 13 14 15 16 17 18 19 20 21 |
|
humbldata.core.utils.core_helpers.log_start_end
¤
log_start_end(func: Callable | None = None, *, log: Logger | None = None) -> Callable
Add logging at the start and end of any function it decorates, including time tracking.
Handles exceptions by logging them and modifies behavior based on the system's debug mode. Logs the total time taken by the function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Optional[Callable]
|
The function to decorate. |
None
|
log |
Optional[Logger]
|
The logger to use for logging. |
None
|
Returns:
Type | Description |
---|---|
Callable
|
The decorated function. |
Source code in src\humbldata\core\utils\core_helpers.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
humbldata.core.utils.descriptions
¤
Common descriptions for model fields.
humbldata.core.utils.env
¤
The Env Module, to control a single instance of environment variables.
humbldata.core.utils.env.Env
¤
A singleton environment to hold all Environment variables.
Source code in src\humbldata\core\utils\env.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
humbldata.core.utils.env.Env.str2bool
staticmethod
¤
str2bool(value: str | bool) -> bool
Match a value to its boolean correspondent.
Args: value (str): The string value to be converted to a boolean.
Returns:
Type | Description |
---|---|
bool: The boolean value corresponding to the input string.
|
|
Raises:
Type | Description |
---|---|
ValueError: If the input string does not correspond to a boolean
|
value. |
Source code in src\humbldata\core\utils\env.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
humbldata.core.utils.openbb_helpers
¤
Core Module - OpenBB Helpers.
This module contains functions used to interact with OpenBB, or wrap commands to have specific data outputs.
humbldata.core.utils.openbb_helpers.obb_login
¤
obb_login(pat: str | None = None) -> bool
Log into the OpenBB Hub using a Personal Access Token (PAT).
This function wraps the obb.account.login
method to provide a simplified
interface for logging into OpenBB Hub. It optionally accepts a PAT. If no PAT
is provided, it attempts to use the PAT stored in the environment variable
OBB_PAT
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pat |
str | None
|
The personal access token for authentication. If None, the token is
retrieved from the environment variable |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if login is successful, False otherwise. |
Raises:
Type | Description |
---|---|
HumblDataError
|
If an error occurs during the login process. |
Examples:
>>> # obb_login("your_personal_access_token_here")
True
>>> # obb_login() # Assumes `OBB_PAT` is set in the environment
True
Source code in src\humbldata\core\utils\openbb_helpers.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
humbldata.core.utils.openbb_helpers.get_latest_price
¤
get_latest_price(symbol: str | list[str] | Series, provider: Literal['fmp', 'intrinio'] | None = None) -> LazyFrame
Context: Core || Category: Utils || Subcategory: OpenBB Helpers || Command: get_latest_price.
This function queries the latest stock price data using the specified
provider. If no provider is specified, it defaults to using
FinancialModelingPrep (fmp
). The function returns a LazyFrame containing
the stock symbols and their corresponding latest prices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol |
str | list[str] | Series
|
The stock symbol(s) for which to fetch the latest price. Can be a single symbol, a list of symbols, or a Polars Series of symbols. |
required |
provider |
Literal['fmp', 'intrinio'] | None
|
The data provider to use for fetching the stock prices. If not specified, a default provider is used. |
None
|
Returns:
Type | Description |
---|---|
LazyFrame
|
A Polars LazyFrame containing columns for the stock symbols ('symbol') and their most recent prices ('last_price'). |
Source code in src\humbldata\core\utils\openbb_helpers.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
humbldata.toolbox
¤
Context: Toolbox.
A category to group all of the technical indicators available in the Toolbox()
Technical indicators rely on statistical transformations of time series data. These are raw math operations.
humbldata.toolbox.toolbox_controller
¤
Context: Toolbox.
The Toolbox Controller Module.
humbldata.toolbox.toolbox_controller.Toolbox
¤
Bases: ToolboxQueryParams
The top-level controller for all data analysis in the humbldata
package.
This module serves as the primary controller, routing user-specified ToolboxQueryParams as core arguments that are used to fetch time series data.
The Toolbox
controller also gives access to all sub-modules adn their
functions.
It is designed to facilitate the collection of data across various types such as stocks, options, or alternative time series by requiring minimal input from the user.
Submodules
The Toolbox
controller is composed of the following submodules:
technical
:quantitative
:fundamental
:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol |
str
|
The symbol or ticker of the stock. |
required |
interval |
str
|
The interval of the data. Defaults to '1d'. |
required |
start_date |
str
|
The start date for the data query. |
required |
end_date |
str
|
The end date for the data query. |
required |
Parameter Notes
The Parameters (symbol
, interval
, start_date
, end_date
)
are the ToolboxQueryParams
. They are used for data collection further
down the pipeline. to execute operations on core data sets.
This approach enables composable and standardized querying while
accommodating data-specific collection logic.
Source code in src\humbldata\toolbox\toolbox_controller.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
humbldata.toolbox.toolbox_controller.Toolbox.__init__
¤
__init__(*args, **kwargs)
Initialize the Toolbox module.
This method does not take any parameters and does not return anything.
Source code in src\humbldata\toolbox\toolbox_controller.py
55 56 57 58 59 60 61 |
|
humbldata.toolbox.toolbox_controller.Toolbox.technical
property
¤
technical
The technical submodule of the Toolbox controller.
Access to all the technical indicators.
humbldata.toolbox.toolbox_helpers
¤
Context: Toolbox || Category: Helpers.
These Toolbox()
helpers are used in various calculations in the toolbox
context. Most of the helpers will be mathematical transformations of data. These
functions should be DUMB functions.
humbldata.toolbox.toolbox_helpers.log_returns
¤
log_returns(data: Series | DataFrame | LazyFrame | None = None, _column_name: str = 'adj_close', *, _drop_nulls: bool = True, _sort: bool = True) -> Series | DataFrame | LazyFrame
Context: Toolbox || Category: Helpers || Command: log_returns.
This is a DUMB command. It can be used in any CONTEXT or CATEGORY. Calculates the logarithmic returns for a given Polars Series, DataFrame, or LazyFrame. Logarithmic returns are widely used in the financial industry to measure the rate of return on investments over time. This function supports calculations on both individual series and dataframes containing financial time series data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Series | DataFrame | LazyFrame
|
The input data for which to calculate the log returns. Default is None. |
None
|
_drop_nulls |
bool
|
Whether to drop null values from the result. Default is True. |
True
|
_column_name |
str
|
The column name to use for log return calculations in DataFrame or LazyFrame. Default is "adj_close". |
'adj_close'
|
_sort |
bool
|
If True, sorts the DataFrame or LazyFrame by |
True
|
Returns:
Type | Description |
---|---|
Series | DataFrame | LazyFrame
|
The original |
Raises:
Type | Description |
---|---|
HumblDataError
|
If neither a series, DataFrame, nor LazyFrame is provided as input. |
Examples:
>>> series = pl.Series([100, 105, 103])
>>> log_returns(data=series)
series([-inf, 0.048790, -0.019418])
>>> df = pl.DataFrame({"adj_close": [100, 105, 103]})
>>> log_returns(data=df)
shape: (3, 2)
┌───────────┬────────────┐
│ adj_close ┆ log_returns│
│ --- ┆ --- │
│ f64 ┆ f64 │
╞═══════════╪════════════╡
│ 100.0 ┆ NaN │
├───────────┼────────────┤
│ 105.0 ┆ 0.048790 │
├───────────┼────────────┤
│ 103.0 ┆ -0.019418 │
└───────────┴────────────┘
Improvements
Add a parameter _sort_cols: list[str] | None = None
to make the function even
dumber. This way you could specify certain columns to sort by instead of
using default date
and symbol
. If _sort_cols=None
and _sort=True
,
then the function will use the default date
and symbol
columns for
sorting.
Source code in src\humbldata\toolbox\toolbox_helpers.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
humbldata.toolbox.toolbox_helpers.detrend
¤
detrend(data: DataFrame | LazyFrame | Series, _detrend_col: str = 'log_returns', _detrend_value_col: str | Series | None = 'window_mean', *, _sort: bool = False) -> DataFrame | LazyFrame | Series
Context: Toolbox || Category: Helpers || Command: detrend.
This is a DUMB command. It can be used in any CONTEXT or CATEGORY.
Detrends a column in a DataFrame, LazyFrame, or Series by subtracting the values of another column from it. Optionally sorts the data by 'symbol' and 'date' before detrending if _sort is True.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[DataFrame, LazyFrame, Series]
|
The data structure containing the columns to be processed. |
required |
_detrend_col |
str
|
The name of the column from which values will be subtracted. |
'log_returns'
|
_detrend_value_col |
str | Series | None
|
The name of the column whose values will be subtracted OR if you
pass a pl.Series to the |
'window_mean'
|
_sort |
bool
|
If True, sorts the data by 'symbol' and 'date' before detrending. Default is False. |
False
|
Returns:
Type | Description |
---|---|
Union[DataFrame, LazyFrame, Series]
|
The detrended data structure with the same type as the input,
with an added column named |
Notes
Function doesn't use .over()
in calculation. Once the data is sorted,
subtracting _detrend_value_col from _detrend_col is a simple operation
that doesn't need to be grouped, because the sorting has already aligned
the rows for subtraction
Source code in src\humbldata\toolbox\toolbox_helpers.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
|
humbldata.toolbox.toolbox_helpers.cum_sum
¤
cum_sum(data: DataFrame | LazyFrame | Series | None = None, _column_name: str = 'detrended_returns', *, _sort: bool = True, _mandelbrot_usage: bool = True) -> LazyFrame | DataFrame | Series
Context: Toolbox || Category: Helpers || Command: cum_sum.
This is a DUMB command. It can be used in any CONTEXT or CATEGORY.
Calculate the cumulative sum of a series or column in a DataFrame or LazyFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame | Series | None
|
The data to process. |
None
|
_column_name |
str
|
The name of the column to calculate the cumulative sum on, applicable if df is provided. |
'detrended_returns'
|
_sort |
bool
|
If True, sorts the DataFrame or LazyFrame by date and symbol before calculation. Default is True. |
True
|
_mandelbrot_usage |
bool
|
If True, performs additional checks specific to the Mandelbrot Channel calculation. This should be set to True when you have a cumulative deviate series, and False when not. Please check 'Notes' for more information. Default is True. |
True
|
Returns:
Type | Description |
---|---|
DataFrame | LazyFrame | Series
|
The DataFrame or Series with the cumulative deviate series added as a new column or as itself. |
Notes
This function is used to calculate the cumulative sum for the deviate series
of detrended returns for the data in the pipeline for
calc_mandelbrot_channel
.
So, although it is calculating a cumulative sum, it is known as a cumulative deviate because it is a cumulative sum on a deviate series, meaning that the cumulative sum should = 0 for each window. The _mandelbrot_usage parameter allows for checks to ensure the data is suitable for Mandelbrot Channel calculations, i.e that the deviate series was calculated correctly by the end of each series being 0, meaning the trend (the mean over the window_index) was successfully removed from the data.
Source code in src\humbldata\toolbox\toolbox_helpers.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
|
humbldata.toolbox.toolbox_helpers.std
¤
std(data: LazyFrame | DataFrame | Series, _column_name: str = 'cum_sum') -> LazyFrame | DataFrame | Series
Context: Toolbox || Category: Helpers || Command: std.
Calculate the standard deviation of the cumulative deviate series within each window of the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
LazyFrame
|
The LazyFrame from which to calculate the standard deviation. |
required |
_column_name |
str
|
The name of the column from which to calculate the standard deviation, with "cumdev" as the default value. |
'cum_sum'
|
Returns:
Type | Description |
---|---|
LazyFrame
|
A LazyFrame with the standard deviation of the specified column for each window, added as a new column named "S". |
Improvements
Just need to parametrize .over()
call in the function if want an even
dumber function, that doesn't calculate each window_index
.
Source code in src\humbldata\toolbox\toolbox_helpers.py
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
|
humbldata.toolbox.toolbox_helpers.mean
¤
mean(data: DataFrame | LazyFrame | Series, _column_name: str = 'log_returns', *, _sort: bool = True) -> DataFrame | LazyFrame
Context: Toolbox || Category: Helpers || Function: mean.
This is a DUMB command. It can be used in any CONTEXT or CATEGORY.
This function calculates the mean of a column (<_column_name>) over a
each window in the dataset, if there are any.
This window is intended to be the window
that is passed in the
calc_mandelbrot_channel()
function. The mean calculated is meant to be
used as the mean of each window
within the time series. This
way, each block of windows has their own mean, which can then be used to
normalize the data (i.e remove the mean) from each window section.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame
|
The DataFrame or LazyFrame to calculate the mean on. |
required |
_column_name |
str
|
The name of the column to calculate the mean on. |
'log_returns'
|
_sort |
bool
|
If True, sorts the DataFrame or LazyFrame by date before calculation. Default is False. |
True
|
Returns:
Type | Description |
---|---|
DataFrame | LazyFrame
|
The original DataFrame or LazyFrame with a |
Notes
Since this function is an aggregation function, it reduces the # of observations in the dataset,thus, unless I take each value and iterate each window_mean value to correlate to the row in the original dataframe, the function will return a dataframe WITHOUT the original data.
Source code in src\humbldata\toolbox\toolbox_helpers.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
|
humbldata.toolbox.toolbox_helpers.range_
¤
range_(data: LazyFrame | DataFrame | Series, _column_name: str = 'cum_sum', *, _sort: bool = True) -> LazyFrame | DataFrame | Series
Context: Toolbox || Category: Technical || Sub-Category: MandelBrot Channel || Sub-Category: Helpers || Function: mandelbrot_range.
Calculate the range (max - min) of the cumulative deviate values of a specified column in a DataFrame for each window in the dataset, if there are any.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
LazyFrame
|
The DataFrame to calculate the range from. |
required |
_column_name |
str
|
The column to calculate the range from, by default "cumdev". |
'cum_sum'
|
Returns:
Type | Description |
---|---|
LazyFrame | DataFrame
|
A DataFrame with the range of the specified column for each window. |
Source code in src\humbldata\toolbox\toolbox_helpers.py
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 |
|
humbldata.toolbox.fundamental
¤
Context: Toolbox || Category: Fundamental.
A category to group all of the fundamental indicators available in the
Toolbox()
.
Fundamental indicators relies on earnings data, valuation models of companies, balance sheet metrics etc...
humbldata.toolbox.quantitative
¤
Context: Toolbox || Category: Quantitative.
Quantitative indicators rely on statistical transformations of time series data.
humbldata.toolbox.technical
¤
humbldata.toolbox.technical.technical_controller
¤
Context: Toolbox || Category: Technical.
A controller to manage and compile all of the technical indicator models
available. This will be passed as a @property
to the Toolbox()
class, giving
access to the technical module and its functions.
humbldata.toolbox.technical.technical_controller.Technical
¤
Module for all technical analysis.
Attributes:
Name | Type | Description |
---|---|---|
standard_params |
ToolboxQueryParams
|
The standard query parameters for toolbox data. |
Methods:
Name | Description |
---|---|
mandelbrot_channel |
Calculate the rescaled range statistics. |
Source code in src\humbldata\toolbox\technical\technical_controller.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
humbldata.toolbox.technical.technical_controller.Technical.mandelbrot_channel
¤
mandelbrot_channel(command_params: MandelbrotChannelQueryParams)
Calculate the rescaled range statistics.
Explain the math...
Source code in src\humbldata\toolbox\technical\technical_controller.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
humbldata.toolbox.technical.mandelbrot_channel
¤
humbldata.toolbox.technical.mandelbrot_channel.helpers
¤
Context: Toolbox || Category: Technical || Sub-Category: MandelBrot Channel || Sub-Category: Helpers.
These Toolbox()
helpers are used in various calculations in the toolbox
context. Most of the helpers will be mathematical transformations of data. These
functions should be DUMB functions.
humbldata.toolbox.technical.mandelbrot_channel.helpers.add_window_index
¤
add_window_index(data: LazyFrame | DataFrame, window: str) -> LazyFrame | DataFrame
Context: Toolbox || Category: MandelBrot Channel || Sub-Category: Helpers || Command: **add_window_index**.
Add a column to the dataframe indicating the window grouping for each row in a time series.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
LazyFrame | DataFrame
|
The input data frame or lazy frame to which the window index will be added. |
required |
window |
str
|
The window size as a string, used to determine the grouping of rows into windows. |
required |
Returns:
Type | Description |
---|---|
LazyFrame | DataFrame
|
The original data frame or lazy frame with an additional column named "window_index" indicating the window grouping for each row. |
Notes
- This function is essential for calculating the Mandelbrot Channel, where the dataset is split into numerous 'windows', and statistics are calculated for each window.
- The function adds a dummy
symbol
column if the data contains only one symbol, to avoid errors in thegroup_by_dynamic()
function. - It is utilized within the
log_mean()
function for window-based calculations.
Examples:
>>> data = pl.DataFrame({"date": ["2021-01-01", "2021-01-02"], "symbol": ["AAPL", "AAPL"], "value": [1, 2]})
>>> window = "1d"
>>> add_window_index(data, window)
shape: (2, 4)
┌────────────┬────────┬───────┬──────────────┐
│ date ┆ symbol ┆ value ┆ window_index │
│ --- ┆ --- ┆ --- ┆ --- │
│ date ┆ str ┆ i64 ┆ i64 │
╞════════════╪════════╪═══════╪══════════════╡
│ 2021-01-01 ┆ AAPL ┆ 1 ┆ 0 │
├────────────┼────────┼───────┼──────────────┤
│ 2021-01-02 ┆ AAPL ┆ 2 ┆ 1 │
└────────────┴────────┴───────┴──────────────┘
Source code in src\humbldata\toolbox\technical\mandelbrot_channel\helpers.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
humbldata.toolbox.technical.mandelbrot_channel.helpers.vol_buckets
¤
vol_buckets(data: DataFrame | LazyFrame, lo_quantile: float = 0.4, hi_quantile: float = 0.8, _column_name_volatility: str = 'realized_volatility', *, _boundary_group_down: bool = False) -> LazyFrame
Context: Toolbox || Category: MandelBrot Channel || Sub-Category: Helpers || Command: vol_buckets.
Splitting data observations into 3 volatility buckets: low, mid and high.
The function does this for each symbol
present in the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
LazyFrame | DataFrame
|
The input dataframe or lazy frame. |
required |
lo_quantile |
float
|
The lower quantile for bucketing. Default is 0.4. |
0.4
|
hi_quantile |
float
|
The higher quantile for bucketing. Default is 0.8. |
0.8
|
_column_name_volatility |
str
|
The name of the column to apply volatility bucketing. Default is "realized_volatility". |
'realized_volatility'
|
_boundary_group_down |
bool
|
If True, then group boundary values down to the lower bucket, using
|
False
|
Returns:
Type | Description |
---|---|
LazyFrame
|
The |
Source code in src\humbldata\toolbox\technical\mandelbrot_channel\helpers.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
humbldata.toolbox.technical.mandelbrot_channel.helpers.vol_buckets_alt
¤
vol_buckets_alt(data: DataFrame | LazyFrame, lo_quantile: float = 0.4, hi_quantile: float = 0.8, _column_name_volatility: str = 'realized_volatility') -> LazyFrame
Context: Toolbox || Category: MandelBrot Channel || Sub-Category: Helpers || Command: vol_buckets_alt.
This is an alternative implementation of vol_buckets()
using expressions,
and not using .qcut()
.
The biggest difference is how the function groups values on the boundaries
of quantiles. This function groups boundary values down
Splitting data observations into 3 volatility buckets: low, mid and high.
The function does this for each symbol
present in the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
LazyFrame | DataFrame
|
The input dataframe or lazy frame. |
required |
lo_quantile |
float
|
The lower quantile for bucketing. Default is 0.4. |
0.4
|
hi_quantile |
float
|
The higher quantile for bucketing. Default is 0.8. |
0.8
|
_column_name_volatility |
str
|
The name of the column to apply volatility bucketing. Default is "realized_volatility". |
'realized_volatility'
|
Returns:
Type | Description |
---|---|
LazyFrame
|
The |
Notes
The biggest difference is how the function groups values on the boundaries
of quantiles. This function groups boundary values down to the lower bucket.
So, if there is a value that lies on the mid/low border, this function will
group it with low
, whereas vol_buckets()
will group it with mid
This function is also slightly less performant.
Source code in src\humbldata\toolbox\technical\mandelbrot_channel\helpers.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
humbldata.toolbox.technical.mandelbrot_channel.helpers.vol_filter
¤
vol_filter(data: DataFrame | LazyFrame) -> LazyFrame
Context: Toolbox || Category: MandelBrot Channel || Sub-Category: Helpers || Command: vol_filter.
If _rv_adjustment
is True, then filter the data to only include rows
that are in the same vol_bucket as the latest row for each symbol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame
|
The input dataframe or lazy frame. This should be the output of
|
required |
Returns:
Type | Description |
---|---|
LazyFrame
|
The data with only observations in the same volatility bucket as the most recent data observation |
Source code in src\humbldata\toolbox\technical\mandelbrot_channel\helpers.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
humbldata.toolbox.technical.mandelbrot_channel.helpers.price_range
¤
price_range(data: LazyFrame | DataFrame, recent_price_data: DataFrame | LazyFrame | None = None, rs_method: Literal['RS', 'RS_mean', 'RS_max', 'RS_min'] = 'RS', _detrended_returns: str = 'detrended_log_returns', _column_name_cum_sum_max: str = 'cum_sum_max', _column_name_cum_sum_min: str = 'cum_sum_min', *, _rv_adjustment: bool = False, _sort: bool = True, **kwargs) -> LazyFrame
Context: Toolbox || Category: MandelBrot Channel || Sub-Category: Helpers || Command: price_range.
Calculate the price range for a given dataset using the Mandelbrot method.
This function computes the price range based on the recent price data, cumulative sum max and min, and RS method specified. It supports adjustments for real volatility and sorting of the data based on symbols and dates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
LazyFrame | DataFrame
|
The dataset containing the financial data. |
required |
recent_price_data |
DataFrame | LazyFrame | None
|
The dataset containing the most recent price data. If None, the most recent prices are extracted from |
None
|
rs_method |
Literal['RS', 'RS_mean', 'RS_max', 'RS_min']
|
The RS value to use. Must be one of 'RS', 'RS_mean', 'RS_max', 'RS_min'. RS is the column that is the Range/STD of the detrended returns. |
"RS"
|
_detrended_returns |
str
|
The column name for detrended returns in |
"detrended_log_returns"
|
_column_name_cum_sum_max |
str
|
The column name for cumulative sum max in |
"cum_sum_max"
|
_column_name_cum_sum_min |
str
|
The column name for cumulative sum min in |
"cum_sum_min"
|
_rv_adjustment |
bool
|
If True, calculated the |
False
|
_sort |
bool
|
If True, sorts the data based on symbols and dates. |
True
|
**kwargs |
Arbitrary keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
LazyFrame
|
The dataset with calculated price range, including columns for top and bottom prices. |
Raises:
Type | Description |
---|---|
HumblDataError
|
If the RS method specified is not supported. |
Examples:
>>> price_range_data = price_range(data, recent_price_data=None, _rs_method="RS")
>>> print(price_range_data.columns)
['symbol', 'bottom_price', 'recent_price', 'top_price']
Notes
For rs_method
, you should know how this affects the mandelbrot channel
that is produced. Selecting RS uses the most recent RS value to calculate
the price range, whereas selecting RS_mean, RS_max, or RS_min uses the mean,
max, or min of the RS values, respectively.
Source code in src\humbldata\toolbox\technical\mandelbrot_channel\helpers.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
humbldata.toolbox.technical.mandelbrot_channel.model
¤
Context: Toolbox || Category: Technical || Command: calc_mandelbrot_channel.
A command to generate a Mandelbrot Channel for any time series.
humbldata.toolbox.technical.mandelbrot_channel.model.calc_mandelbrot_channel
¤
calc_mandelbrot_channel(data: DataFrame | LazyFrame, window: str = '1m', rv_adjustment: bool = True, _rv_method: str = 'std', _rs_method: Literal['RS', 'RS_mean', 'RS_max', 'RS_min'] = 'RS', *, _rv_grouped_mean: bool = True, _live_price: bool = True) -> LazyFrame
Context: Toolbox || Category: Technical || Sub-Category: Mandelbrot Channel || **Command: calc_mandelbrot_channel`.
Calculates the Mandelbrot Channel for a given time series based on the provided standard and extra parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame
|
The time series data for which to calculate the Mandelbrot Channel. |
required |
window |
str
|
The window size for the calculation, specified as a string. |
'1m'
|
rv_adjustment |
bool
|
Whether to adjust the calculation for realized volatility. |
True
|
_rv_grouped_mean |
bool
|
Whether to use the grouped mean in the realized volatility calculation. |
True
|
_rv_method |
str
|
The method to use for calculating realized volatility. You only need to
supply a value if |
'std'
|
_rs_method |
Literal['RS', 'RS_mean', 'RS_max', 'RS_min']
|
The method to use for calculating the range over standard deviation. You can choose either RS/RS_mean/RS_min/RS_max. This changes the width of the calculated Mandelbrot Channel |
'RS'
|
_live_price |
bool
|
Whether to use live price data in the calculation. This may add a significant amount of time to the calculation (1-3s) |
True
|
Returns:
Type | Description |
---|---|
LazyFrame
|
The calculated Mandelbrot Channel data for the given time series. |
Notes
Since the function returns a pl.LazyFrame, don't forget to run .collect()
on the output to get a DataFrame. Lazy logic saves the calculation for when
it is needed.
Source code in src\humbldata\toolbox\technical\mandelbrot_channel\model.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
humbldata.toolbox.technical.mandelbrot_channel.model.acalc_mandelbrot_channel
async
¤
acalc_mandelbrot_channel(data: DataFrame | LazyFrame, window: str = '1m', rv_adjustment: bool = True, _rv_method: str = 'std', _rs_method: Literal['RS', 'RS_mean', 'RS_max', 'RS_min'] = 'RS', *, _rv_grouped_mean: bool = True, _live_price: bool = True) -> DataFrame | LazyFrame
Context: Toolbox || Category: Technical || Sub-Category: Mandelbrot Channel || Command: acalc_mandelbrot_channel.
Asynchronous wrapper for calc_mandelbrot_channel. This function allows calc_mandelbrot_channel to be called in an async context.
Notes
This does not make calc_mandelbrot_channel()
non-blocking or asynchronous.
Source code in src\humbldata\toolbox\technical\mandelbrot_channel\model.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
humbldata.toolbox.technical.mandelbrot_channel.model.calc_mandelbrot_channel_historical
¤
calc_mandelbrot_channel_historical(data: DataFrame | LazyFrame, window: str = '1m', rv_adjustment: bool = True, _rv_method: str = 'std', _rs_method: Literal['RS', 'RS_mean', 'RS_max', 'RS_min'] = 'RS', *, _rv_grouped_mean: bool = True, _live_price: bool = True) -> DataFrame | LazyFrame
Context: Toolbox || Category: Technical || Sub-Category: Mandelbrot Channel || Command: calc_mandelbrot_channel_historical.
Calculates the Mandelbrot Channel for a given time series based on the provided standard and extra parameters, over time! This means that instead of using the dataset to calculate one statistic at the current point in time, this function starts at the beginning of the dataset and calculates the statistic for date present in the dataset, up to the current point in time.
Source code in src\humbldata\toolbox\technical\mandelbrot_channel\model.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
humbldata.toolbox.technical.volatility
¤
humbldata.toolbox.technical.volatility.realized_volatility_helpers
¤
Context: Toolbox || Category: Technical || Sub-Category: Volatility Helpers.
All of the volatility estimators used in calc_realized_volatility()
.
These are various methods to calculate the realized volatility of financial data.
humbldata.toolbox.technical.volatility.realized_volatility_helpers.std
¤
std(data: DataFrame | LazyFrame | Series, window: str = '1m', trading_periods=252, _drop_nulls: bool = True, _avg_trading_days: bool = False, _column_name_returns: str = 'log_returns', _sort: bool = True) -> LazyFrame | Series
Context: Toolbox || Category: Technical || Sub-Category: Volatility Helpers || Command: _std.
This function computes the standard deviation of returns, which is a common measure of volatility.It calculates the rolling standard deviation for a given window size, optionally adjusting for the average number of trading days and scaling the result to an annualized volatility percentage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[DataFrame, LazyFrame, Series]
|
The input data containing the returns. It can be a DataFrame, LazyFrame, or Series. |
required |
window |
str
|
The rolling window size for calculating the standard deviation. The default is "1m" (one month). |
'1m'
|
trading_periods |
int
|
The number of trading periods in a year, used for annualizing the volatility. The default is 252. |
252
|
_drop_nulls |
bool
|
If True, null values will be dropped from the result. The default is True. |
True
|
_avg_trading_days |
bool
|
If True, the average number of trading days will be used when calculating the window size. The default is True. |
False
|
_column_name_returns |
str
|
The name of the column containing the returns. This parameter is used
when |
'log_returns'
|
Returns:
Type | Description |
---|---|
Union[DataFrame, LazyFrame, Series]
|
The input data structure with an additional column for the rolling standard deviation of returns, or the modified Series with the rolling standard deviation values. |
Source code in src\humbldata\toolbox\technical\volatility\realized_volatility_helpers.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
humbldata.toolbox.technical.volatility.realized_volatility_helpers.parkinson
¤
parkinson(data: DataFrame | LazyFrame, window: str = '1m', _column_name_high: str = 'high', _column_name_low: str = 'low', *, _drop_nulls: bool = True, _avg_trading_days: bool = False, _sort: bool = True) -> LazyFrame
Calculate Parkinson's volatility over a specified window.
Parkinson's volatility is a measure that uses the stock's high and low prices of the day rather than just close to close prices. It is particularly useful for capturing large price movements during the day.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame
|
The input data containing the stock prices. |
required |
window |
int
|
The rolling window size for calculating volatility, by default 30. |
'1m'
|
trading_periods |
int
|
The number of trading periods in a year, by default 252. |
required |
_column_name_high |
str
|
The name of the column containing the high prices, by default "high". |
'high'
|
_column_name_low |
str
|
The name of the column containing the low prices, by default "low". |
'low'
|
_drop_nulls |
bool
|
Whether to drop null values from the result, by default True. |
True
|
_avg_trading_days |
bool
|
Whether to use the average number of trading days when calculating the window size, by default True. |
False
|
Returns:
Type | Description |
---|---|
DataFrame | LazyFrame
|
The calculated Parkinson's volatility, with an additional column "parkinson_volatility_pct_{window_int}D" indicating the percentage volatility. |
Notes
This function requires the input data to have 'high' and 'low' columns to calculate the logarithm of their ratio, which is squared and scaled by a constant to estimate volatility. The result is then annualized and expressed as a percentage.
Usage
If you pass "1m
as a window
argument and _avg_trading_days=False
.
The result will be 30
. If _avg_trading_days=True
, the result will be
21
.
Examples:
>>> data = pl.DataFrame({'high': [120, 125], 'low': [115, 120]})
>>> _parkinson(data)
A DataFrame with the calculated Parkinson's volatility.
Source code in src\humbldata\toolbox\technical\volatility\realized_volatility_helpers.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
humbldata.toolbox.technical.volatility.realized_volatility_helpers.garman_klass
¤
garman_klass(data: DataFrame | LazyFrame, window: str = '1m', _column_name_high: str = 'high', _column_name_low: str = 'low', _column_name_open: str = 'open', _column_name_close: str = 'adj_close', _drop_nulls: bool = True, _avg_trading_days: bool = False, _sort: bool = True) -> LazyFrame
Context: Toolbox || Category: Technical || Sub-Category: Volatility Helpers || Command: _garman_klass.
Calculates the Garman-Klass volatility for a given dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame
|
The input data containing the price information. |
required |
window |
str
|
The rolling window size for volatility calculation, by default "1m". |
'1m'
|
_column_name_high |
str
|
The name of the column containing the high prices, by default "high". |
'high'
|
_column_name_low |
str
|
The name of the column containing the low prices, by default "low". |
'low'
|
_column_name_open |
str
|
The name of the column containing the opening prices, by default "open". |
'open'
|
_column_name_close |
str
|
The name of the column containing the adjusted closing prices, by default "adj_close". |
'adj_close'
|
_drop_nulls |
bool
|
Whether to drop null values from the result, by default True. |
True
|
_avg_trading_days |
bool
|
Whether to use the average number of trading days when calculating the window size, by default True. |
False
|
Returns:
Type | Description |
---|---|
DataFrame | LazyFrame | Series
|
The calculated Garman-Klass volatility, with an additional column "volatility_pct" indicating the percentage volatility. |
Notes
Garman-Klass volatility extends Parkinson’s volatility by considering the opening and closing prices in addition to the high and low prices. This approach provides a more accurate estimation of volatility, especially in markets with significant activity at the opening and closing of trading sessions.
Source code in src\humbldata\toolbox\technical\volatility\realized_volatility_helpers.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
humbldata.toolbox.technical.volatility.realized_volatility_helpers.hodges_tompkins
¤
hodges_tompkins(data: DataFrame | LazyFrame | Series, window: str = '1m', trading_periods=252, _column_name_returns: str = 'log_returns', *, _drop_nulls: bool = True, _avg_trading_days: bool = False, _sort: bool = True) -> LazyFrame | Series
Context: Toolbox || Category: Technical || Sub-Category: Volatility Helpers || Command: _hodges_tompkins.
Hodges-Tompkins volatility is a bias correction for estimation using an overlapping data sample that produces unbiased estimates and a substantial gain in efficiency.
Source code in src\humbldata\toolbox\technical\volatility\realized_volatility_helpers.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
humbldata.toolbox.technical.volatility.realized_volatility_helpers.rogers_satchell
¤
rogers_satchell(data: DataFrame | LazyFrame, window: str = '1m', _column_name_high: str = 'high', _column_name_low: str = 'low', _column_name_open: str = 'open', _column_name_close: str = 'adj_close', _drop_nulls: bool = True, _avg_trading_days: bool = False, _sort: bool = True) -> LazyFrame
Context: Toolbox || Category: Technical || Sub-Category: Volatility Helpers || Command: _rogers_satchell.
Rogers-Satchell is an estimator for measuring the volatility of securities with an average return not equal to zero. Unlike Parkinson and Garman-Klass estimators, Rogers-Satchell incorporates a drift term (mean return not equal to zero). This function calculates the Rogers-Satchell volatility estimator over a specified window and optionally drops null values from the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame
|
The input data for which to calculate the Rogers-Satchell volatility estimator. This can be either a DataFrame or a LazyFrame. There need to be OHLC columns present in the data. |
required |
window |
str
|
The window over which to calculate the volatility estimator. The window is specified as a string, such as "1m" for one month. |
"1m"
|
_column_name_high |
str
|
The name of the column representing the high prices in the data. |
"high"
|
_column_name_low |
str
|
The name of the column representing the low prices in the data. |
"low"
|
_column_name_open |
str
|
The name of the column representing the opening prices in the data. |
"open"
|
_column_name_close |
str
|
The name of the column representing the adjusted closing prices in the data. |
"adj_close"
|
_drop_nulls |
bool
|
Whether to drop null values from the result. If True, rows with null values in the calculated volatility column will be removed from the output. |
True
|
_avg_trading_days |
bool
|
Indicates whether to use the average number of trading days per window.
This affects how the window size is interpreted. i.e instead of "1mo"
returning |
True
|
Returns:
Type | Description |
---|---|
DataFrame | LazyFrame
|
The input data with an additional column containing the calculated Rogers-Satchell volatility estimator. The return type matches the input type (DataFrame or LazyFrame). |
Source code in src\humbldata\toolbox\technical\volatility\realized_volatility_helpers.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|
humbldata.toolbox.technical.volatility.realized_volatility_helpers.yang_zhang
¤
yang_zhang(data: DataFrame | LazyFrame, window: str = '1m', trading_periods: int = 252, _column_name_high: str = 'high', _column_name_low: str = 'low', _column_name_open: str = 'open', _column_name_close: str = 'adj_close', _avg_trading_days: bool = False, _drop_nulls: bool = True, _sort: bool = True) -> LazyFrame
Context: Toolbox || Category: Technical || Sub-Category: Volatility Helpers || Command: _yang_zhang.
Yang-Zhang volatility is the combination of the overnight (close-to-open volatility), a weighted average of the Rogers-Satchell volatility and the day’s open-to-close volatility.
Source code in src\humbldata\toolbox\technical\volatility\realized_volatility_helpers.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
|
humbldata.toolbox.technical.volatility.realized_volatility_helpers.squared_returns
¤
squared_returns(data: DataFrame | LazyFrame, window: str = '1m', trading_periods: int = 252, _drop_nulls: bool = True, _avg_trading_days: bool = False, _column_name_returns: str = 'log_returns', _sort: bool = True) -> LazyFrame
Calculate squared returns over a rolling window.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame
|
The input data containing the price information. |
required |
window |
str
|
The rolling window size for calculating squared returns, by default "1m". |
'1m'
|
trading_periods |
int
|
The number of trading periods in a year, used for scaling the result. The default is 252. |
252
|
_drop_nulls |
bool
|
Whether to drop null values from the result, by default True. |
True
|
_column_name_returns |
str
|
The name of the column containing the price data, by default "adj_close". |
'log_returns'
|
Returns:
Type | Description |
---|---|
DataFrame | LazyFrame
|
The input data structure with an additional column for the rolling squared returns. |
Source code in src\humbldata\toolbox\technical\volatility\realized_volatility_helpers.py
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 |
|
humbldata.toolbox.technical.volatility.realized_volatility_model
¤
Context: Toolbox || Category: Technical || Command: calc_realized_volatility.
A command to generate Realized Volatility for any time series. A complete set of volatility estimators based on Euan Sinclair's Volatility Trading
humbldata.toolbox.technical.volatility.realized_volatility_model.calc_realized_volatility
¤
calc_realized_volatility(data: DataFrame | LazyFrame, window: str = '1m', method: Literal['std', 'parkinson', 'garman_klass', 'gk', 'hodges_tompkins', 'ht', 'rogers_satchell', 'rs', 'yang_zhang', 'yz', 'squared_returns', 'sq'] = 'std', grouped_mean: list[int] | None = None, _trading_periods: int = 252, _column_name_returns: str = 'log_returns', _column_name_close: str = 'close', _column_name_high: str = 'high', _column_name_low: str = 'low', _column_name_open: str = 'open', *, _sort: bool = True) -> LazyFrame | DataFrame
Context: Toolbox || Category: Technical || Command: calc_realized_volatility.
Calculates the Realized Volatility for a given time series based on the provided standard and extra parameters. This function adds ONE rolling volatility column to the input DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | LazyFrame
|
The time series data for which to calculate the Realized Volatility. |
required |
window |
str
|
The window size for a rolling volatility calculation, default is |
'1m'
|
method |
Literal['std', 'parkinson', 'garman_klass', 'hodges_tompkins', 'rogers_satchell', 'yang_zhang', 'squared_returns']
|
The volatility estimator to use. You can also use abbreviations to
access the same methods. The abbreviations are: |
'std'
|
grouped_mean |
list[int] | None
|
A list of window sizes to use for calculating volatility. If provided,
the volatility method will be calculated across these various windows,
and then an averaged value of all the windows will be returned. If |
None
|
_sort |
bool
|
If True, the data will be sorted before calculation. Default is True. |
True
|
_trading_periods |
int
|
The number of trading periods in a year, default is 252 (the typical number of trading days in a year). |
252
|
_column_name_returns |
str
|
The name of the column containing the returns. Default is "log_returns". |
'log_returns'
|
_column_name_close |
str
|
The name of the column containing the close prices. Default is "close". |
'close'
|
_column_name_high |
str
|
The name of the column containing the high prices. Default is "high". |
'high'
|
_column_name_low |
str
|
The name of the column containing the low prices. Default is "low". |
'low'
|
_column_name_open |
str
|
The name of the column containing the open prices. Default is "open". |
'open'
|
Returns:
Type | Description |
---|---|
VolatilityData
|
The calculated Realized Volatility data for the given time series. |
Notes
-
Rolling calculations are used to show a time series of recent volatility that captures only a certain number of data points. The window size is used to determine the number of data points to use in the calculation. We do this because when looking at the volatility of a stock, you get a better insight (more granular) into the characteristics of the volatility seeing how 1-month or 3-month rolling volatility looked over time.
-
This function does not accept
pl.Series
because the methods used to calculate volatility require, high, low, close, open columns for the data. It would be too cumbersome to pass each series needed for the calculation as a separate argument. Therefore, the function only acceptspl.DataFrame
orpl.LazyFrame
as input.
Source code in src\humbldata\toolbox\technical\volatility\realized_volatility_model.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|