Helper Functions
Relevant source files
The following files were used as context for generating this wiki page:
Helper functions in the stolgo library provide essential utility support for data processing, date manipulation, and integration with financial data providers like Nasdaq and Samco. These utilities standardize how the library handles time-series data, date ranges, and API communications, ensuring consistency across different modules such as Candlestick Analysis and Trend Identification.
The primary scope of these helpers includes formatting dates for specific API requirements, calculating date offsets for historical data requests, and resampling OHLC (Open, High, Low, Close) data to different timeframes. They serve as the foundational layer that allows higher-level price action trading rules to operate on clean, well-formatted DataFrames.
Sources: lib/stolgo/helper.py, README.md:1-10
Date and Time Management
The library relies heavily on accurate date range calculations to fetch historical financial data. The helper.py module contains logic to determine start and end dates based on user-provided parameters like periods or specific date strings.
Date Range Calculation Logic
The get_date_range function resolves the timeframe for data requests by evaluating various combinations of start, end, and periods.
This flowchart illustrates the decision logic used to calculate date boundaries for data fetching.
Sources: lib/stolgo/helper.py:46-76
Key Date Functions
| Function | Purpose | Parameters |
|---|---|---|
get_formated_date | Converts various date types to a formatted string. | date, format, dayfirst |
get_formated_dateframe | Returns a pandas Timestamp for a given date string. | date, format, dayfirst |
get_date_offset | Calculates a date based on a frequency and period. | periods, start, end, freq |
get_date_range | Main entry point for resolving data request boundaries. | start, end, periods, format, dayfirst, freq |
Sources: lib/stolgo/helper.py:17-80
Data Processing and Resampling
Stolgo provides utilities to transform raw tick or minute data into higher timeframes, which is critical for technical analysis.
OHLC Resampling
The get_data_resample function uses a specific aggregation dictionary to ensure that financial data remains accurate during resampling. It maps 'Open' to the first value, 'High' to the maximum, 'Low' to the minimum, 'Close' to the last value, and 'Volume' to the sum.
Sources: lib/stolgo/helper.py:82-85
ohlc_dict = {'Open':'first', 'High':'max', 'Low':'min', 'Close': 'last','Volume':'sum'}
Sources: lib/stolgo/helper.py:84
Network and Request Utilities
To maintain robust connections with exchanges and brokers, stolgo implements a custom request layer in request.py. This layer handles retries, timeouts, and provides a fallback to curl for environments requiring it.
Request Architecture
The system utilizes a TimeoutHTTPAdapter to enforce consistent timeouts across all network calls, preventing the application from hanging on unresponsive servers.
The class diagram shows the relationship between the session-based requests and the timeout adapter used for reliability.
Sources: lib/stolgo/request.py:11-30, lib/stolgo/request.py:73-85
Asset Identification
The library includes helpers to distinguish between different types of financial instruments, particularly for the Indian market.
Indian Index Validation
The is_ind_index function checks if a given symbol belongs to the predefined list of Indian indices stored in common.py. This is used by the Samco module to determine whether to call index-specific or stock-specific API endpoints.
Sources: lib/stolgo/helper.py:13-15, lib/stolgo/common.py:1-12, lib/stolgo/samco.py:58-61
Integration with Data Providers
Helper functions are heavily utilized within provider-specific classes like Nasdaq and Samco to prepare data before it is returned to the user.
Data Adjustment Sequence
When fetching data from Nasdaq, the library uses internal helpers to ensure the requested number of periods is met, even if it requires multiple recursive calls.
This sequence shows how the Nasdaq module uses helpers to validate and potentially expand the data range.
Sources: lib/stolgo/nasdaq.py:65-85, lib/stolgo/nasdaq.py:115-120
Summary
The Helper Functions within stolgo form the backbone of the library's data acquisition and preparation pipeline. By centralizing date logic in helper.py, connection management in request.py, and common constants in common.py, the library provides a unified interface for complex price action analysis regardless of the underlying data source.