HIST Data Extraction

This module extracts the information from the data downloaded with the module HIST Download Data. We obtain the time, best ask, best bid, midpoint price, spread and trade signs in trade time scale.

To run this part of the code is necessary to have the results from the module HIST Download Data.

Modules

The code is divided in four parts:
  • Tools: some functions for repetitive actions.
  • Analysis: code to analyze the data.
  • Plot: code to plot the data.
  • Main: code to run the implementation.

Tools

HIST data tools module.

The functions in the module do small repetitive tasks, that are used along the whole implementation. These tools improve the way the tasks are standardized in the modules that use them.

This script requires the following modules:
  • os
  • pickle
  • typing
  • matplotlib
  • pandas
The module contains the following functions:
  • hist_save_data - saves computed data.
  • hist_save_plot - saves figures.
  • hist_function_header_print_data - prints info about the function running.
  • hist_function_header_print_plot - prints info about the plot.
  • hist_start_folders - creates folders to save data and plots.
  • hist_initial_message - prints the initial message with basic information.
  • hist_sundays - generates a tuple with the dates of every sunday in a year.
  • hist_weeks - generates a tuple with the number of weeks in a year.
  • main - the main function of the script.
hist_data_tools_extraction.hist_function_header_print_data(function_name: str, fx_pair: str, year: str, week: str) → None[source]

Prints a header of a function that generates data when it is running.

Parameters:
  • function_name – name of the function that generates the data.
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e ‘2016’).
  • week – string of the week to be analyzed (i.e ‘07’).
Returns:

None – The function prints a message and does not return a value.

hist_data_tools_extraction.hist_function_header_print_plot(function_name: str, fx_pair: str, year: str, month: str) → None[source]

Prints a header of a function that generates a plot when it is running.

Parameters:
  • function_name – name of the function that generates the plot.
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e ‘2016’).
  • month – string of the month to be analyzed (i.e ‘07’).
Returns:

None – The function prints a message and does not return a value.

hist_data_tools_extraction.hist_initial_message() → None[source]

Prints the initial message with basic information.

Returns:None – The function prints a message and does not return a value.
hist_data_tools_extraction.hist_save_data(data: Any, fx_pair: str, year: str, week: str) → None[source]

Saves computed data in pickle files.

Saves the data generated in the functions of the hist_data_analysis_extraction module in pickle files.

Parameters:
  • data – data to be saved. The data can be of different types.
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e ‘2016’).
  • week – string of the week to be analyzed (i.e ‘07’).
Returns:

None – The function saves the data in a file and does not return a value.

hist_data_tools_extraction.hist_save_plot(function_name: str, figure: matplotlib.figure.Figure, fx_pair: str, year: str, month: str) → None[source]

Saves plot in png files.

Saves the plot generated in the functions of the hist_data_plot_data_extraction module in png files.

Parameters:
  • function_name – name of the function that generates the plot.
  • figure – figure object that is going to be save.
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e ‘2016’).
  • month – string of the month to be analyzed (i.e ‘07’).
Returns:

None – The function save the plot in a file and does not return a value.

hist_data_tools_extraction.hist_start_folders(years: List[str]) → None[source]

Creates the initial folders to save the data and plots.

Parameters:years – List of the strings of the year to be analyzed (i.e [‘2016’, ‘2017’]).
Returns:None – The function creates folders and does not return a value.
hist_data_tools_extraction.hist_sundays(year: str) → Tuple[str, ...][source]

Generates a list with the dates of every sunday in a year.

Parameters:year – string of the year to be analyzed (i.e ‘2016’).
Returns:tuple.
hist_data_tools_extraction.hist_weeks() → Tuple[str, ...][source]
Generates a tuple with the numbers from 0 to 53 representing the weeks
in a year.
Returns:tuple.
hist_data_tools_extraction.main() → None[source]

The main function of the script.

The main function is used to test the functions in the script.

Returns:None.

Analysis

HIST data analysis module.

The functions in the module extract the bid and ask from the Historic Rate Data from HIST Capital in a year.

This script requires the following modules:
  • os
  • pickle
  • typing
  • zipfile
  • datetime
  • numpy
  • pandas
  • hist_data_tools_extraction
The module contains the following functions:
  • hist_fx_data_extraction_year - extracts the bid and ask for a year.
  • hist_fx_data_extraction_week - extracts the bid and ask for a week.
  • hist_fx_week_start - extracts data that starts in a week day.
  • hist_fx_weekend_start - extracts data that starts in a weekend day.
  • hist_fx_midpoint_trade_data - extracts the midpoint price for a year
  • hist_fx_trade_signs_trade_data - extracts the midpoint price for a year
  • main - the main function of the script.

..moduleauthor:: Juan Camilo Henao Londono <www.github.com/juanhenao21>

hist_data_analysis_extraction.hist_fx_data_extraction_week(fx_pair: str, year: str) → None[source]

Extracts the bid and ask for a week.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e. ‘2016’).
Returns:

None – The function saves the data in a file and does not return a value.

hist_data_analysis_extraction.hist_fx_data_extraction_year(fx_pair: str, year: str) → pandas.core.frame.DataFrame[source]

Extracts the bid and ask for a year.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e. ‘2016’).
Returns:

None – The function saves the data in a file and does not return a value.

hist_data_analysis_extraction.hist_fx_midpoint_trade_data(fx_pair: str, year: str, week: str) → None[source]

Extracts the midpoint price for a year.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e. ‘2019’).
  • week – string of the week to be analyzed (i.e. ‘01’).
Returns:

None – The function saves the data in a file and does not return a value.

hist_data_analysis_extraction.hist_fx_trade_signs_trade_data(fx_pair: str, year: str, week: str) → None[source]

Extracts the trade signs price for a year.

The trade signs are obtained from the midpoint price as \(\epsilon(t) = sign(m(t) - m(t - 1))\), where +1 indicates the trade was triggered by a market order to buy, and -1 indicates the trade was triggered by a market order to sell.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e. ‘2016’).
Returns:

None – The function saves the data in a file and does not return a value.

hist_data_analysis_extraction.hist_fx_week_start(fx_pair: str, function_name: str, year: str, fx_data: pandas.core.frame.DataFrame, weeks: List[datetime.datetime]) → None[source]

Extracts the bid and ask for a week that starts in a week day.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • function_name – string of the function used to analyze the data.
  • year – string of the year to be analyzed (i.e. ‘2016’).
  • fx_data – pd.DataFrame with the data.
  • weeks – List with the dates of the sundays in a year.
Returns:

None – The function saves the data in a file and does not return a value.

hist_data_analysis_extraction.hist_fx_weekend_start(fx_pair: str, function_name: str, year: str, fx_data: pandas.core.frame.DataFrame, weeks: List[datetime.datetime]) → None[source]

Extracts the bid and ask for a weekend that starts in a week day.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • function_name – string of the function used to analyze the data.
  • year – string of the year to be analyzed (i.e. ‘2016’).
  • fx_data – pd.DataFrame with the data.
  • weeks – List with the dates of the sundays in a year.
Returns:

None – The function saves the data in a file and does not return a value.

hist_data_analysis_extraction.main() → None[source]

The main function of the script.

The main function is used to test the functions in the script.

Returns:None.

Plot

HIST data plot module.

The functions in the module plot the data obtained in the hist_data_analysis_extraction module.

This script requires the following modules:
  • gc
  • pickle
  • typing
  • matplotlib
  • pandas
  • hist_data_tools_data_extract
The module contains the following functions:
  • hist_fx_quotes_year_plot - plots the forex quotes for a year.
  • hist_fx_midpoint_year_plot - plots the forex quotes for a year.
  • hist_fx_spread_year_plot - plots the forex quotes for a year.
  • main - the main function of the script.
hist_data_plot_extraction.hist_fx_midpoint_year_plot(fx_pair: str, year: str, weeks: Tuple[str, ...]) → None[source]

Plots the midpoint price for a year.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e. ‘2016’).
  • weeks – tuple of the strings of the weeks to be analyzed (i.e. [‘01’, ‘02’]).
Returns:

None – The function saves the plot in a file and does not return a value.

hist_data_plot_extraction.hist_fx_quotes_year_plot(fx_pair: str, year: str, weeks: Tuple[str, ...]) → None[source]

Plots the quotes price for a year.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e. ‘2016’).
  • weeks – tuple of the strings of the weeks to be analyzed (i.e. [‘01’, ‘02’]).
Returns:

None – The function saves the plot in a file and does not return a value.

hist_data_plot_extraction.hist_fx_spread_year_plot(fx_pair: str, year: str, weeks: Tuple[str, ...]) → None[source]

Plots the spread for a year.

Parameters:
  • fx_pair – string of the abbreviation of the forex pair to be analyzed (i.e. ‘eur_usd’).
  • year – string of the year to be analyzed (i.e. ‘2016’).
  • weeks – tuple of the strings of the weeks to be analyzed (i.e. [‘01’, ‘02’]).
Returns:

None – The function saves the plot in a file and does not return a value.

hist_data_plot_extraction.main() → None[source]

The main function of the script.

The main function is used to test the functions in the script.

Returns:None.

Main

HIST data main module.

The functions in the module extract and plot the Historic Rate data from HIST Capital in a year.

This script requires the following modules:
  • itertools
  • multiprocessing
  • typing
  • hist_data_analysis_extraction
  • hist_data_plot_extraction
  • hist_data_tools_extraction
The module contains the following functions:
  • hist_data_plot_generator - generates all the analysis and plots from the HIST data.
  • main - the main function of the script.
hist_data_main_extraction.hist_data_plot_generator(fx_pairs: List[str], years: List[str], weeks: Tuple[str, ...]) → None[source]

Generates all the analysis and plots from the HIST data.

Parameters:
  • fx_pairs – list of the string abbreviation of the forex pairs to be analyzed (i.e. [‘eur_usd’, ‘gbp_usd’]).
  • years – list of the strings of the years to be analyzed (i.e. [‘2016’, ‘2017’]).
  • weeks – tuple of the strings of the weeks to be analyzed (i.e. (‘01’, ‘02’)).
Returns:

None – The function saves the data in a file and does not return a value.

hist_data_main_extraction.main() → None[source]

The main function of the script.

The main function extract, analyze and plot the data.

Returns:None.