forex_data_aggregator package
Subpackages
- forex_data.config package
- forex_data.data_management package
- Submodules
- forex_data.data_management.common module
DEFAULT_PATHSDATA_TYPEBASE_DATA_COLUMN_NAMEBASE_DATA_COLUMN_NAME.TIMESTAMPBASE_DATA_COLUMN_NAME.OPENBASE_DATA_COLUMN_NAME.HIGHBASE_DATA_COLUMN_NAME.LOWBASE_DATA_COLUMN_NAME.CLOSEBASE_DATA_COLUMN_NAME.ASKBASE_DATA_COLUMN_NAME.BIDBASE_DATA_COLUMN_NAME.VOLBASE_DATA_COLUMN_NAME.P_VALUEBASE_DATA_COLUMN_NAME.TRANSACTIONSBASE_DATA_COLUMN_NAME.VWAPBASE_DATA_COLUMN_NAME.OTC
DATA_FILE_COLUMN_INDEXASSET_TYPEDTYPE_DICTPYARROW_DTYPE_DICTPOLARS_DTYPE_DICTDATA_COLUMN_NAMESFILENAME_TEMPLATEDATA_KEYSQL_COMPARISON_OPERATORSSQL_CONDITION_AGGREGATION_MODESSUPPORTED_HISTORICAL_DATA_PROVIDERSvalidator_file_path()validator_dir_path()validate_timedelta_str()get_attrs_names()check_timeframe_str()any_date_to_datetime64()empty_dataframe()is_empty_dataframe()shape_dataframe()get_dataframe_column()get_dataframe_row()get_dataframe_element()astype()read_csv()sort_dataframe()concat_data()list_remove_duplicates()reframe_data()write_csv()write_parquet()read_parquet()to_pandas_dataframe()get_pair_symbols()to_source_symbol()get_date_interval()random_date_between()get_histdata_tickers()TickerNotFoundErrorTickerDataNotFoundErrorTickerDataBadTypeExceptionTickerDataInvalidExceptionbusiness_days_data()update_ticker_years_dict()get_class_attr_keys()get_class_attr_values()get_class_attr_dict()get_forex_holidays()adjust_end_date_to_business_days()adjust_start_date_to_business_days()estimate_start_date_to_business_days()estimate_end_date_to_business_days()TimestampTimestamp.astimezone()Timestamp.ceil()Timestamp.combine()Timestamp.ctime()Timestamp.date()Timestamp.daysinmonthTimestamp.dst()Timestamp.floor()Timestamp.fromordinal()Timestamp.fromtimestamp()Timestamp.isocalendar()Timestamp.isoweekday()Timestamp.now()Timestamp.replace()Timestamp.round()Timestamp.strftime()Timestamp.strptime()Timestamp.time()Timestamp.timetuple()Timestamp.timetz()Timestamp.to_julian_date()Timestamp.today()Timestamp.toordinal()Timestamp.tzTimestamp.tz_convert()Timestamp.tz_localize()Timestamp.tzinfoTimestamp.tzname()Timestamp.utcfromtimestamp()Timestamp.utcnow()Timestamp.utcoffset()Timestamp.utctimetuple()Timestamp.weekday()Timestamp.weekofyearTimestamp.clsTimestamp.ts_inputTimestamp.return
PolarsDatetimePolarsFloat32PolarsDataFramePolarsLazyFrame
- forex_data.data_management.database module
DatabaseConnectorDatabaseConnector.data_pathDatabaseConnector.data_typeDatabaseConnector.engineDatabaseConnector.__init__()DatabaseConnector.connect()DatabaseConnector.check_connection()DatabaseConnector.write_data()DatabaseConnector.read_data()DatabaseConnector.read_data_year()DatabaseConnector.read_data_window()DatabaseConnector.read_last_timestamp()DatabaseConnector.get_tickers_list()DatabaseConnector.get_ticker_keys()DatabaseConnector.get_ticker_timeframes_list()DatabaseConnector.get_ticker_years_list()DatabaseConnector.create_tickers_years_dict()DatabaseConnector.save_tickers_years_info()DatabaseConnector.add_tickers_years_info_to_file()DatabaseConnector.clear_tickers_years_info()DatabaseConnector.clear_database()DatabaseConnector.load_tickers_years_info()
LocalDBConnectorLocalDBYearConnector
- forex_data.data_management.historicaldata module
HistoricalManagerDBHistoricalManagerDB.configHistoricalManagerDB.data_typeHistoricalManagerDB.engineHistoricalManagerDB.data_pathHistoricalManagerDB.db_files_year_partitioningHistoricalManagerDB.ssl_verifyHistoricalManagerDB.connector_idHistoricalManagerDB.max_discrepancy_with_nowHistoricalManagerDB.__init__()HistoricalManagerDB.get_source_connectors()HistoricalManagerDB.clear_database()HistoricalManagerDB.add_timeframe()HistoricalManagerDB.get_data()HistoricalManagerDB.get_data_window()HistoricalManagerDB.plot()HistoricalManagerDB.close()
- forex_data.data_management.realtimedata module
- Module contents
HistoricalManagerDBHistoricalManagerDB.configHistoricalManagerDB.data_typeHistoricalManagerDB.engineHistoricalManagerDB.data_pathHistoricalManagerDB.db_files_year_partitioningHistoricalManagerDB.ssl_verifyHistoricalManagerDB.connector_idHistoricalManagerDB.max_discrepancy_with_nowHistoricalManagerDB.__init__()HistoricalManagerDB.get_source_connectors()HistoricalManagerDB.clear_database()HistoricalManagerDB.add_timeframe()HistoricalManagerDB.get_data()HistoricalManagerDB.get_data_window()HistoricalManagerDB.plot()HistoricalManagerDB.close()
BASE_DATA_COLUMN_NAMEBASE_DATA_COLUMN_NAME.TIMESTAMPBASE_DATA_COLUMN_NAME.OPENBASE_DATA_COLUMN_NAME.HIGHBASE_DATA_COLUMN_NAME.LOWBASE_DATA_COLUMN_NAME.CLOSEBASE_DATA_COLUMN_NAME.ASKBASE_DATA_COLUMN_NAME.BIDBASE_DATA_COLUMN_NAME.VOLBASE_DATA_COLUMN_NAME.P_VALUEBASE_DATA_COLUMN_NAME.TRANSACTIONSBASE_DATA_COLUMN_NAME.VWAPBASE_DATA_COLUMN_NAME.OTC
DATA_FILE_COLUMN_INDEXDEFAULT_PATHSSQL_COMPARISON_OPERATORSSQL_CONDITION_AGGREGATION_MODESempty_dataframe()is_empty_dataframe()shape_dataframe()get_dataframe_column()get_dataframe_row()get_dataframe_element()get_attrs_names()any_date_to_datetime64()get_db_key_elements()check_timeframe_str()DatabaseConnectorDatabaseConnector.data_pathDatabaseConnector.data_typeDatabaseConnector.engineDatabaseConnector.__init__()DatabaseConnector.connect()DatabaseConnector.check_connection()DatabaseConnector.write_data()DatabaseConnector.read_data()DatabaseConnector.read_data_year()DatabaseConnector.read_data_window()DatabaseConnector.read_last_timestamp()DatabaseConnector.get_tickers_list()DatabaseConnector.get_ticker_keys()DatabaseConnector.get_ticker_timeframes_list()DatabaseConnector.get_ticker_years_list()DatabaseConnector.create_tickers_years_dict()DatabaseConnector.save_tickers_years_info()DatabaseConnector.add_tickers_years_info_to_file()DatabaseConnector.clear_tickers_years_info()DatabaseConnector.clear_database()DatabaseConnector.load_tickers_years_info()
LocalDBConnectorconcat_data()reframe_data()validator_dir_path()TickerNotFoundErrorTickerDataNotFoundErrorTickerDataBadTypeExceptionTickerDataInvalidExceptionget_histdata_tickers()POLARS_DTYPE_DICTbusiness_days_data()get_class_attr_keys()get_class_attr_values()get_class_attr_dict()random_date_between()RemoteConnectorRemoteConnector.data_pathRemoteConnector.data_typeRemoteConnector.engineRemoteConnector.__init__()RemoteConnector.connect()RemoteConnector.check_connection()RemoteConnector.clear_temporary_folder()RemoteConnector.get_available_tickers()RemoteConnector.get_data()RemoteConnector.get_recent_data()RemoteConnector.download_month_raw()
RealTimeDBConnectorTwelveDataRealTimeDBConnectorTwelveData.api_keyRealTimeDBConnectorTwelveData.planRealTimeDBConnectorTwelveData.tierRealTimeDBConnectorTwelveData.__init__()RealTimeDBConnectorTwelveData.chunk_sizeRealTimeDBConnectorTwelveData.max_requests_per_minuteRealTimeDBConnectorTwelveData.get_realtime_price()RealTimeDBConnectorTwelveData.get_data()RealTimeDBConnectorTwelveData.get_recent_data()
HistDataConnectorDukascopyConnector
Module contents
Created on Sun Jul 17 17:07:39 2022
@author: fiora
- forex_data.read_config_folder(folder_path=None, file_pattern='appconfig.yaml')[source]
- Return type:
- class forex_data.HistoricalManagerDB(**kwargs)[source]
Bases:
object- add_timeframe(timeframe)[source]
Add and cache a new timeframe to the database.
Creates aggregated data for the specified timeframe from tick data and caches it in the database for faster future access. The timeframe is added to the internal list of available timeframes.
- Parameters:
timeframe (
Union[str,List[str]]) – Timeframe(s) to add. Can be a single string or list of strings. Supported values: ‘1m’, ‘5m’, ‘15m’, ‘30m’, ‘1h’, ‘4h’, ‘1D’, ‘1W’, ‘1M’- Return type:
- Returns:
None
- Raises:
TypeError – If timeframe is not a string or list of strings
Example
>>> manager = HistoricalManagerDB(config='data_config.yaml') >>> manager.add_timeframe('1W') # Add weekly timeframe >>> manager.add_timeframe(['4h', '1D']) # Add multiple timeframes
Note
Only new timeframes (not already in the list) will be processed
Aggregation can take time for large datasets
Once added, the timeframe is permanently cached in the database
- get_data(ticker, timeframe, start, end, comparison_column_name=None, check_level=None, comparison_operator=None, aggregation_mode=None)[source]
Retrieve OHLC historical data for the specified ticker and timeframe.
Fetches historical forex data from the database, automatically downloading and aggregating data if not already available. Supports multiple timeframes and date ranges.
- Parameters:
ticker (
str) – Currency pair symbol (e.g., ‘EURUSD’, ‘GBPUSD’, ‘NZDUSD’). Case-insensitive.timeframe (
str) – Candle timeframe for data aggregation. Supported frames: 1s (1 second) 1m (1 minute) 1h (1 hour) 1d (1 calendar day) 1w (1 calendar week) 1mo (1 calendar month) 1q (1 calendar quarter) 1y (1 calendar year)integer (and any multiple of these values by a positive) – ‘2m’, ‘3m’, ‘2h’, ‘3h’, etc.
e.g. – ‘2m’, ‘3m’, ‘2h’, ‘3h’, etc.
start (
str | datetime) – Start date for data retrieval. Accepts: - ISO format: ‘YYYY-MM-DD’ or ‘YYYY-MM-DD HH:MM:SS’ - datetime objectend (
str | datetime) – End date for data retrieval. Same format as start. Must be after start date.comparison_column_name (
Union[List[str],str,None]) – Column names to retrieve. Default is None.check_level (
Union[List[int|float],int,float,None]) – Check level for conditions. Default is None.comparison_operator (
Union[List[Literal['>','<','>=','<=','==','!=']],Literal['>','<','>=','<=','==','!='],None]) – Condition for data retrieval. Default is None.aggregation_mode (
Optional[Literal['AND','OR']]) – Aggregation mode for data retrieval. Default is None.
- Returns:
- DataFrame containing OHLC data with columns:
timestamp: datetime column with candle timestamps
open: Opening price (float32)
high: Highest price (float32)
low: Lowest price (float32)
close: Closing price (float32)
- Return type:
Union[DataFrame,LazyFrame]- Raises:
TickerNotFoundError – If the ticker is not available in the historical database
ValueError – If timeframe is invalid or end date is before start date
Example
>>> manager = HistoricalManagerDB(config='data_config.yaml') >>> data = manager.get_data( ... ticker='EURUSD', ... timeframe='1h', ... start='2020-01-01', ... end='2020-01-31' ... ) >>> print(f"Retrieved {len(data)} hourly candles") Retrieved 744 hourly candles
Note
Data is automatically downloaded from histdata.com if not cached locally
First call for a new timeframe may take longer as it builds the aggregation
Downloaded data is cached for faster subsequent access
Ticker names are case-insensitive and automatically normalized
- get_data_window(ticker, date, timeframe, periods, direction, comparison_column_name=None, check_level=None, comparison_operator=None, comparison_aggregation_mode=None)[source]
Retrieve OHLC historical window data for the specified ticker. The unit resoluton of the window is set equal to the timeframe. Unit resolution is the timespan between two candles (rows) in normal conditions: during weekends the rule does not apply. The window total number of candles (rows) is specified by timeframe * periods.
Fetches historical forex data from the database, automatically downloading and aggregating data if not already available. Supports multiple timeframes
- Parameters:
ticker (
str) – Currency pair symbol (e.g., ‘EURUSD’, ‘GBPUSD’, ‘NZDUSD’). Case-insensitive.date (
date) – date for data retrieval. Accepts: - ISO format: ‘YYYY-MM-DD’ or ‘YYYY-MM-DD HH:MM:SS’ - datetime objecttimeframe (
str) – Candle timeframe for data aggregation. Supported frames: 1s (1 second) 1m (1 minute) 1h (1 hour) 1d (1 calendar day) 1w (1 calendar week) 1mo (1 calendar month) 1q (1 calendar quarter) 1y (1 calendar year) periods (int): Number of timeframe units to look back or forward.direction (
Literal['backward','forward']) – Direction to look back (‘backward’ or ‘forward’).comparison_column_name (
Union[List[str],str,None]) – List of column names to compare. If None, no comparison is performed.check_level (
Union[List[int|float],int,float,None]) – List of values to compare against. If None, no comparison is performed.| (comparison_operator (List[SUPPORTED_SQL_COMPARISON_OPERATORS]) – SUPPORTED_SQL_COMPARISON_OPERATORS | None): List of comparison operators to use for comparison. If None, no comparison is performed.
(SUPPORTED_SQL_CONDITION_AGGREGATION_MODES (comparison_aggregation_mode) –
None): Aggregation mode to use for comparison.If None, no comparison is performed.
- Returns:
- DataFrame with the historical
data.
- Return type:
Union[DataFrame,LazyFrame]- Raises:
TickerNotFoundError – If the ticker is not found.
TickerDataNotFoundError – If the ticker data is not found.
TickerDataBadTypeException – If the ticker data is not of the expected type.
TickerDataInvalidException – If the ticker data is invalid.
Examples
>>> get_data_window( ... ticker='EURUSD', ... date='2022-01-01', ... timeframe='1m', ... window=10, ... direction='backward' ... )
- plot(ticker, timeframe, start_date, end_date)[source]
Plot candlestick chart for the specified ticker and date range.
Generates an interactive candlestick chart using mplfinance, displaying OHLC (Open, High, Low, Close) data for the specified time period.
- Parameters:
- Returns:
Displays the chart using matplotlib
- Return type:
Example
>>> manager = HistoricalManagerDB(config='data_config.yaml') >>> manager.plot( ... ticker='EURUSD', ... timeframe='1D', ... start_date='2020-01-01', ... end_date='2020-12-31' ... )
Note
The chart will be displayed in a matplotlib window. The data is automatically fetched using get_data() and converted to the appropriate format for plotting.
- class forex_data.BASE_DATA_COLUMN_NAME[source]
Bases:
object- TIMESTAMP = 'timestamp'
- OPEN = 'open'
- HIGH = 'high'
- LOW = 'low'
- CLOSE = 'close'
- ASK = 'ask'
- BID = 'bid'
- VOL = 'vol'
- P_VALUE = 'p'
- TRANSACTIONS = 'transactions'
- VWAP = 'vwap'
- OTC = 'otc'
- class forex_data.DEFAULT_PATHS[source]
Bases:
object- BASE_PATH = '/home/runner/.database'
- HIST_DATA_FOLDER = 'HistoricalData'
- REALTIME_DATA_FOLDER = 'RealtimeData'
- class forex_data.SQL_COMPARISON_OPERATORS[source]
Bases:
object- GREATER_THAN = '>'
- LESS_THAN = '<'
- GREATER_THAN_OR_EQUAL = '>='
- LESS_THAN_OR_EQUAL = '<='
- EQUAL = '=='
- NOT_EQUAL = '!='
- forex_data.any_date_to_datetime64(any_date, date_format='ISO8601', unit=None, to_pydatetime=False)[source]
- class forex_data.DatabaseConnector(**kwargs)[source]
Bases:
object- check_connection()[source]
Check database connection - must be implemented by subclasses.
- Return type:
- write_data(target_table, dataframe, clean=False)[source]
Write data to database - must be implemented by subclasses.
- Return type:
- read_data(market, ticker, timeframe, start, end)[source]
Read data from database - must be implemented by subclasses.
- Return type:
LazyFrame
- read_data_year(market, ticker, timeframe, years)[source]
Read data for specific year(s) - must be implemented by subclasses.
- Return type:
LazyFrame
- read_data_window(market, ticker, timeframe, date, periods, direction, comparison_column_name=None, check_level=None, comparison_operator=None, comparison_aggregation_mode=None)[source]
Read window of data - must be implemented by subclasses.
- Return type:
LazyFrame
- read_last_timestamp(market, ticker, timeframe=None)[source]
Read last timestamp from database - must be implemented by subclasses.
- Return type:
- get_ticker_timeframes_list(ticker)[source]
Get timeframes list for ticker - must be implemented by subclasses.
- create_tickers_years_dict()[source]
Create a dictionary containing ticker years data, structured as: {ticker: {timeframe: [year1, year2, …]}}
If no data files exist yet, returns an empty dictionary.
- save_tickers_years_info(ticker_years_dict)[source]
Save ticker years list to a JSON file.
- Return type:
Parameters
- ticker_years_dictDict[str, Dict[str, List[int]]]
Dictionary containing ticker years data, structured as: {ticker: {timeframe: [year1, year2, …]}}
- filenamestr, optional
Name of the JSON file to save the data, by default ‘tickers_years.json’
Raises
- TypeError
If ticker_years_dict is not a dictionary
- IOError
If there’s an error writing the file
- add_tickers_years_info_to_file(ticker, timeframe, year)[source]
In local info filepath, update just the years list of the given ticker and timeframe by adding the year(s) specified if not already present
- Return type:
Parameters
- tickerstr
The ticker symbol to update
- timeframestr
The timeframe for the ticker data
- yearUnion[int, List[int]]
The year or list of years to add to the years list
Raises
- TypeError
If year is not an integer or list of integers
- clear_tickers_years_info(filter=None)[source]
Clear the tickers years info file. If filter is specified, it has to be a ticker value and so only the tickers years info related to the filter are cleared. If filter is not specified, the entire file is cleared. Parameters ———- filter : Optional[str], optional
Filter to apply to the tickers years info file, by default None Filter has to be a ticker value
- Return type:
- clear_database(filter=None)[source]
Clear database files If filter is provided and is a ticker present in database (files present) delete only files related to that ticker
- Return type:
- load_tickers_years_info()[source]
Load ticker years list from a JSON file.
Returns
- Dict[str, Dict[str, List[int]]]
Dictionary containing ticker years data, structured as: {ticker: {timeframe: [year1, year2, …]}}
Raises
- FileNotFoundError
If the JSON file doesn’t exist
- IOError
If there’s an error reading the file
- class forex_data.LocalDBConnector(**kwargs)[source]
Bases:
DatabaseConnector- write_data(target_table, dataframe, clean=False)[source]
Write data to database - must be implemented by subclasses.
- Return type:
- read_data(market, ticker, timeframe, start, end, comparison_column_name=None, check_level=None, comparison_operator=None, comparison_aggregation_mode=None)[source]
Read data from database - must be implemented by subclasses.
- Return type:
LazyFrame
- read_data_year(market, ticker, timeframe, years)[source]
Read data for specific year(s) using SQL YEAR() filter.
- Return type:
LazyFrame
- read_data_window(market, ticker, timeframe, date, periods, direction, comparison_column_name=None, check_level=None, comparison_operator=None, comparison_aggregation_mode=None)[source]
Read window of data specified by input requirements: the data window has timespan in order to return a dataframe with rows size equal to periods. Query the local db to calculate the start date of the window if direction is forward, or end date if backward.
- Return type:
LazyFrame
- class forex_data.RealTimeDBConnectorTwelveData(**kwargs)[source]
Bases:
RemoteConnectorClass to read real-time data from the database using TwelveData API.
- get_realtime_price(symbol)[source]
Fetches the instantaneous real-time price and outputs as a 1-row LazyFrame.
- Return type:
LazyFrame
- class forex_data.DukascopyConnector(**kwargs)[source]
Bases:
RemoteConnectorConnector class that encapsulates all HTTP interactions with Dukascopy’s historical datafeed.
Wraps connectivity checks, ticker registry, and tick data downloading via tick_vault library behind a single RemoteConnector-derived interface.
- get_available_tickers()[source]
Get list of available tickers from tick_vault’s registry and Dukascopy’s tools page.
Returns
- List[str]
Sorted, deduplicated list of ticker symbols (e.g. [‘BTCUSD’, ‘EURUSD’, …]).
- download_month_raw(ticker, year, month_num, temp_filepath='', engine='polars_lazy')[source]
Downloads tick data for a specific year and month from Dukascopy using tick_vault.
- Return type:
Union[DataFrame,LazyFrame]
Parameters
- ticker: str
The currency pair (e.g., ‘EURUSD’).
- year: int
Year of the data to download.
- month_num: int
Month of the data (1-12).
- engine: str
Either ‘polars’ or ‘polars_lazy’.
Returns
- Union[PolarsDataFrame, PolarsLazyFrame]
Polars DataFrame or LazyFrame with column names matching HistDataConnector.
- get_recent_data(symbol, timeframe, interval_window, engine='polars_lazy')[source]
Fetches recent data relative to the current time minus the interval_window.
- Return type:
Union[DataFrame,LazyFrame]
Parameters
- symbol: str
The currency pair (e.g. ‘EURUSD’).
- timeframe: str
The target timeframe (e.g. ‘TICK’, ‘1m’, ‘5m’).
- interval_window: timedelta
The duration of data to fetch (e.g., timedelta(days=90)).
- engine: str
Either ‘polars’ or ‘polars_lazy’.
Returns
- Union[PolarsDataFrame, PolarsLazyFrame]
Polars DataFrame or LazyFrame containing recent data.
- forex_data.concat_data(data_list=_CountingAttr(counter=27, _default=NOTHING, repr=True, eq=True, order=True, hash=None, init=True, on_setattr=None, alias=None, metadata={}))[source]
- forex_data.get_histdata_tickers(verify=True)[source]
Get all available tickers from HistData.com.
Parameters
- verifybool, optional
Whether to verify SSL certificates. Default is True.
Returns
- List[str]
List of all available tickers (e.g., [‘EURUSD’, ‘GBPUSD’, …]).
- class forex_data.POLARS_DTYPE_DICT[source]
Bases:
object- TICK_DTYPE = {'ask': Float32, 'bid': Float32, 'p': Float32, 'vol': Float32}
- TF_DTYPE = {'close': Float32, 'high': Float32, 'low': Float32, 'open': Float32}
- TIME_TICK_DTYPE = {'ask': Float32, 'bid': Float32, 'p': Float32, 'timestamp': Datetime(time_unit='ms', time_zone=None), 'vol': Float32}
- TIME_TF_DTYPE = {'close': Float32, 'high': Float32, 'low': Float32, 'open': Float32, 'timestamp': Datetime(time_unit='ms', time_zone=None)}
- forex_data.business_days_data(dataframe)[source]
Remove non-business days data from the input dataframe. Filter out weekends data: saturday and sunday. Use holidays to get list of country holidays. Consider dataframe always have a column named ‘timestamp’ of type datetime.
- Return type:
DataFrame|LazyFrame