TAQ Responses Physical

The objective of this part of the code is to reproduce the sections 3.1 and 3.2 of the paper Cross–response in correlated financial markets: individual stocks.

All the results obtained with the TAQ Responses Physical module are the base to the other implementations (TAQ Physical Shift, TAQ Responses Time Shift, TAQ Responses Trade, TAQ Trade Shift, TAQ Responses Trade Shift, TAQ Responses Activity, TAQ Responses Time Short Long, TAQ Average Spread, TAQ Average Responses Physical and TAQ Statistics).

Run the code

To run the code, please follow the instructions of the README.md in the GitHub repository.

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

TAQ 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:
  • matplotlib
  • os
  • pandas
  • pickle
  • subprocess
The module contains the following functions:
  • taq_save_data - saves computed data.
  • taq_save_plot - saves figures.
  • taq_function_header_print_data - prints info about the function running.
  • taq_function_header_print_plot - prints info about the plot.
  • taq_start_folders - creates folders to save data and plots.
  • taq_initial_message - prints the initial message with basic information.
  • taq_business_days - creates a list of week days for a year.
  • taq_decompress - decompress original data format to CSV file.
  • main - the main function of the script.
taq_data_tools_responses_physical.main()[source]

The main function of the script.

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

Returns:None.
taq_data_tools_responses_physical.taq_bussiness_days(year)[source]

Generates a list with the dates of the bussiness days in a year.

Parameters:year – string of the year to be analyzed (i.e ‘2008’).
Returns:list.
taq_data_tools_responses_physical.taq_decompress(ticker, year, type)[source]

Decompress original data format to CSV file.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2008’).
  • type – string with the word ‘quotes’ or ‘trades’.
Returns:

None – The function run a code and does not return a value.

taq_data_tools_responses_physical.taq_function_header_print_data(function_name, ticker_i, ticker_j, year, month, day)[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.
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2016’).
  • month – string of the month to be analyzed (i.e ‘07’).
  • day – string of the day to be analyzed (i.e ‘07’).
Returns:

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

taq_data_tools_responses_physical.taq_function_header_print_plot(function_name, ticker_i, ticker_j, year, month, day)[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.
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2016’).
  • month – string of the month to be analyzed (i.e ‘07’).
  • day – string of the day to be analyzed (i.e ‘07’).
Returns:

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

taq_data_tools_responses_physical.taq_initial_message()[source]

Prints the initial message with basic information.

Returns:None – The function prints a message and does not return a value.
taq_data_tools_responses_physical.taq_save_data(function_name, data, ticker_i, ticker_j, year, month, day)[source]

Saves computed data in pickle files.

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

Parameters:
  • function_name – name of the function that generates the data.
  • data – data to be saved. The data can be of different types.
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2016’).
  • month – string of the month to be analyzed (i.e ‘07’).
  • day – string of the day to be analyzed (i.e ‘07’).
Returns:

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

taq_data_tools_responses_physical.taq_save_plot(function_name, figure, ticker_i, ticker_j, year, month)[source]

Saves plot in png files.

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

Parameters:
  • function_name – name of the function that generates the plot.
  • figure – figure object that is going to be save.
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • 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.

taq_data_tools_responses_physical.taq_start_folders(year)[source]

Creates the initial folders to save the data and plots.

Parameters:year – string of the year to be analyzed (i.e ‘2016’).
Returns:None – The function creates folders and does not return a value.

Analysis

TAQ data analysis module.

The functions in the module analyze the data from the NASDAQ stock market, computing the self- and cross-response functions and the trade sign self- and cross-correlator functions. This module reproduces the sections 3.1 and 3.2 of the paper.

This script requires the following modules:
  • itertools
  • multiprocessing
  • numpy
  • os
  • pandas
  • pickle
  • subprocess
  • taq_data_tools_responses_physical
The module contains the following functions:
  • taq_build_from_scratch - extract data to daily CSV files.
  • taq_data_extract - extracts the data for every day in a year.
  • taq_daily_data_extract - parallelize the taq_data_extract function.
  • taq_midpoint_trade_data - computes the midpoint price of every trade.
  • taq_midpoint_physical_data - computes the midpoint price of every second.
  • taq_trade_signs_trade_data - computes the trade signs of every trade.
  • taq_trade_signs_physical_data - computes the trade signs of every second.
  • taq_self_response_day_responses_physical_data - computes the self response of a day.
  • taq_self_response_year_responses_physical_data - computes the self response of a year.
  • taq_cross_response_day_responses_physical_data - computes the cross response of a day.
  • taq_cross_response_year_responses_physical_data - computes the cross response of a year.
  • taq_trade_sign_self_correlator_day_responses_physical_data - computes the trade sign self correlator of a day.
  • taq_trade_sign_self_correlator_year_responses_physical_data - computes the trade sign self correlator of a year.
  • taq_trade_sign_cross_correlator_day_responses_physical_data - computes the trade sign cross correlator of a day.
  • taq_trade_sign_cross_correlator_year_responses_physical_data - computes the trade sign cross correlator of a year.
  • main - the main function of the script.
taq_data_analysis_responses_physical.main()[source]

The main function of the script.

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

Returns:None.
taq_data_analysis_responses_physical.taq_build_from_scratch(tickers, year)[source]

Extracts data to year CSV files.

The original data must be decompressed. The function runs a script in C++ to decompress and then extract and filter the data for a year in CSV files.

Parameters:
  • tickers – list of the string abbreviation of the stocks to be analyzed (i.e. [‘AAPL’, ‘MSFT’]).
  • 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.

taq_data_analysis_responses_physical.taq_cross_response_day_responses_physical_data(ticker_i, ticker_j, date)[source]

Computes the cross-response of a day.

Using the midpoint price of ticker i and trade signs of ticker j computes the cross-response during different time lags (\(\tau\)) for a day.

Parameters:
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • date – string with the date of the data to be extracted (i.e. ‘2008-01-02’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_cross_response_year_responses_physical_data(ticker_i, ticker_j, year)[source]

Computes the cross-response of a year.

Using the taq_cross_response_day_responses_physical_data function computes the cross-response function for a year.

Parameters:
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2016’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_daily_data_extract(tickers, year)[source]

Extracts data to daily CSV files.

Extract and filter the data for every day of a year in HDF5 files.

Parameters:
  • tickers – list of the string abbreviation of the stocks to be analyzed (i.e. [‘AAPL’, ‘MSFT’]).
  • 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.

taq_data_analysis_responses_physical.taq_data_extract(ticker, type, year)[source]

Extracts the data for every day in a year.

Extracts the trades and quotes (TAQ) data for a day from a CSV file with the information of a whole year. The time range for each day is from 9:30 to 16:00, that means, the open market time.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • type – string with the type of the data to be extracted (i.e. ‘trades’ or ‘quotes’).
  • year – string of the year to be analyzed (i.e. ‘2016’).
Returns:

None – The function extracts the data and does not return a value.

taq_data_analysis_responses_physical.taq_midpoint_physical_data(ticker, date)[source]

Computes the midpoint price of every second.

Using the taq_midpoint_trade_data function computes the midpoint price of every second. To fill the time spaces when nothing happens I replicate the last value calculated until a change in the price happens.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • date – string with the date of the data to be extracted (i.e. ‘2008-01-02’).
Returns:

numpy array.

taq_data_analysis_responses_physical.taq_midpoint_trade_data(ticker, date)[source]

Computes the midpoint price of every trade.

Using the daily TAQ data computes the midpoint price of every trade in a day. For further calculations, the function returns the values for the time range from 9h40 to 15h50.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • date – string with the date of the data to be extracted (i.e. ‘2008-01-02’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_self_response_day_responses_physical_data(ticker, date)[source]

Computes the self-response of a day.

Using the midpoint price and trade signs of a ticker computes the self- response during different time lags (\(\tau\)) for a day.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • date – string with the date of the data to be extracted (i.e. ‘2008-01-02’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_self_response_year_responses_physical_data(ticker, year)[source]

Computes the self-response of a year.

Using the taq_self_response_day_responses_physical_data function computes the self-response function for a year.

Parameters:
  • ticker – string of the abbreviation of stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2016’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_trade_sign_cross_correlator_day_responses_physical_data(ticker_i, ticker_j, date)[source]

Computes the trade sign cross-correlator of a day.

Using the trade signs of ticker i and trade signs of ticker j computes the cross-correlator during different time lags (\(\tau\)) for a day.

Parameters:
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • date – string with the date of the data to be extracted (i.e. ‘2008-01-02).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_trade_sign_cross_correlator_year_responses_physical_data(ticker_i, ticker_j, year)[source]

Computes the trade sign-cross correlator of a year.

Using the taq_trade_sign_cross_correlator_day_responses_physical_data function computes the cross-correlator function for a year.

Parameters:
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2016’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_trade_sign_self_correlator_day_responses_physical_data(ticker, date)[source]

Computes the trade sign self-correlator of a year.

Using the trade signs of a ticker computes the self-correlator during different time lags (\(\tau\)) for a day.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • date – string with the date of the data to be extracted (i.e. ‘2008-01-02’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_trade_sign_self_correlator_year_responses_physical_data(ticker, year)[source]

Computes the trade sign self-correlator of a year.

Using the taq_trade_sign_self_correlator_day_responses_physical_data function computes the self-correlator function for a year.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2016’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_trade_signs_physical_data(ticker, date)[source]

Computes the trade signs of every second.

Using the taq_trade_signs_trade_data function computes the trade signs of every second. The trade signs are computed using Eq. 2 of the paper. As the trades signs are not directly given by the TAQ data, they must be inferred by the trades prices. For further calculations, the function returns the values for the time range from 9h40 to 15h50. To fill the time spaces when nothing happens I added zeros indicating that there were neither a buy nor a sell.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • date – string with the date of the data to be extracted (i.e. ‘2008-01-02’).
Returns:

tuple – The function returns a tuple with numpy arrays.

taq_data_analysis_responses_physical.taq_trade_signs_trade_data(ticker, date)[source]

Computes the trade signs of every trade.

Using the daily TAQ data computes the trade signs of every trade in a day. The trade signs are computed using Eq. 1 of the paper. As the trades signs are not directly given by the TAQ data, they must be inferred by the trades prices. For further calculations, the function returns the values for the time range from 9h40 to 15h50.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • date – string with the date of the data to be extracted (i.e. ‘2008-01-02’).
Returns:

tuple – The function returns a tuple with numpy arrays.

Plot

TAQ data plot module.

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

This script requires the following modules:
  • matplotlib
  • pickle
  • taq_data_tools_responses_physical
The module contains the following functions:
  • taq_self_response_year_avg_plot - plots the self-response average for a year.
  • taq_cross_response_year_avg_plot - plots the cross-response average for a year.
  • taq_trade_sign_self_correlator_year_avg_plot - plots the trade sign self- correlator average for a year.
  • taq_trade_sign_cross_correlator_year_avg_plot - plots the trade sign cross-correlator average for a year.
  • main - the main function of the script.
taq_data_plot_responses_physical.main()[source]

The main function of the script.

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

Returns:None.
taq_data_plot_responses_physical.taq_cross_response_year_avg_responses_physical_plot(ticker_i, ticker_j, year)[source]

Plots the cross-response average for a year.

Parameters:
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’)
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’)
  • year – string of the year to be analyzed (i.e ‘2008’)
Returns:

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

taq_data_plot_responses_physical.taq_self_response_year_avg_responses_physical_plot(ticker, year)[source]

Plots the self-response average for a year.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2008’).
Returns:

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

taq_data_plot_responses_physical.taq_trade_sign_cross_correlator_year_avg_responses_physical_plot(ticker_i, ticker_j, year)[source]

Plots the trade sign cross-correlator average for a year.

Parameters:
  • ticker_i – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’)
  • ticker_j – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’)
  • year – string of the year to be analyzed (i.e ‘2008’)
Returns:

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

taq_data_plot_responses_physical.taq_trade_sign_self_correlator_year_avg_responses_physical_plot(ticker, year)[source]

Plots the trade sign self-correlator average for a year.

Parameters:
  • ticker – string of the abbreviation of the stock to be analyzed (i.e. ‘AAPL’).
  • year – string of the year to be analyzed (i.e ‘2008’).
Returns:

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

Main

TAQ data main module.

The functions in the module run the complete extraction, analysis and plot of the TAQ data.

This script requires the following modules:
  • itertools
  • multiprocessing
  • os
  • pandas
  • taq_data_analysis_responses_physical
  • taq_data_plot_responses_physical
  • taq_data_tools_responses_physical
The module contains the following functions:
  • taq_data_plot_generator - generates all the analysis and plots from the TAQ data.
  • main - the main function of the script.
taq_data_main_responses_physical.main()[source]

The main function of the script.

The main function extract, analyze and plot the data.

Returns:None.
taq_data_main_responses_physical.taq_data_plot_generator(tickers, year)[source]

Generates all the analysis and plots from the TAQ data.

Parameters:
  • tickers – list of the string abbreviation of the stocks to be analyzed (i.e. [‘AAPL’, ‘MSFT’]).
  • 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.