Skip to main content

Quantitative Analysis

The qa module is the Terminal's Quantitative Analysis menu for the SDK environment. It provides users with more ways to interact with the library of functions, and provides cross-disciplinary utility. To activate the code completion for the menu, enter . after, openbb.qa.

How to Use

The functions of the qa module are grouped into categories, listed below along with a short description.

PathTypeDescription
openbb.qa.acfPlotPlots Auto and Partial Correlation of Returns and Change in Returns
openbb.qa.bwPlotBox and Whisker Plot
openbb.qa.calculate_adjusted_varRiskCalculates VaR, adjusted for skew and kurtosis (Cornish-Fisher-Expansion)
openbb.qa.cdfPlotPlots the Cumulative Distribution Function
openbb.qa.cusumPlotPlots the Cumulative Sum Algorithm
openbb.qa.decomposePlotDecomposition in Cyclic-Trend, Season & Residuals of Prices
openbb.qa.esStatisticsExpected Shortfall per percentile
openbb.qa.kurtosisRolling MetricsRolling Kurtosis of Distribution of Prices
openbb.qa.linePlotCustomizable Line Plot, With Annotations
openbb.qa.normalityStatisticsNormality Statistics and Tests
openbb.qa.omegaRiskOmega Ratio (Risk/Return Performance Measure)
openbb.qa.quantileRolling MetricsRolling Median and Quantile of Prices
openbb.qa.qqplotPlotQQ Plot for Data Against Normal Quantiles
openbb.qa.rollingRolling MetricsRolling Mean and Standard Deviation of Prices
openbb.qa.sharpeRiskSharpe Ratio (Measure of Risk-Adjusted Return)
openbb.qa.skewRolling MetricsRolling Skewness of Distribution of Prices
openbb.qa.sortinoRiskSortino Ratio Risk Adjustment Metric
openbb.qa.spreadRolling MetricsRolling Variance and Standard Deviation of Prices
openbb.qa.summaryStatisticsA Brief Summary of Statistics for the DataFrame
openbb.qa.unitrootStatisticsNormality Statistics and Tests
openbb.qa.varRiskValue at Risk

Examples

Import Statements

The examples below will assume that the following statements are included in the first block of code:

import quandl
import pandas as pd
from openbb_terminal.sdk import openbb
from openbb_terminal import config_terminal as cfg

# %matplotlib inline (uncomment if using a Jupyter environment)

Get Data

This example collects data from Nasdaq Data Link, and requires registering for a free API key. Qunadl is the Python client for the Nasdaq Data Link API.

shiller_pe_rdiff = quandl.get('MULTPL/SHILLER_PE_RATIO_MONTH', collapse = 'monthly', transform = 'rdiff', api_key = cfg.API_KEY_QUANDL)
shiller_pe_rdiff.rename(columns={'Value':'P/E % Change'}, inplace = True)
shiller_pe_ratio = quandl.get('MULTPL/SHILLER_PE_RATIO_MONTH', collapse = 'monthly', api_key = cfg.API_KEY_QUANDL)
shiller_pe_ratio.rename(columns={'Value':'P/E Ratio'}, inplace = True)

sp500_inf_adj = quandl.get('MULTPL/SP500_INFLADJ_MONTH', collapse = 'monthly', api_key = cfg.API_KEY_QUANDL)
sp500_inf_adj.rename(columns = {'Value': 'S&P Inflation-Adjusted Value'}, inplace = True)
sp500_inf_adj_rdiff = quandl.get('MULTPL/SP500_INFLADJ_MONTH', collapse = 'monthly', transform = 'rdiff', api_key = cfg.API_KEY_QUANDL)
sp500_inf_adj_rdiff.rename(columns = {'Value':'S&P 500 % Change'}, inplace = True)

shiller_pe = shiller_pe_ratio.join(shiller_pe_rdiff)
sp500_inf_adj = sp500_inf_adj.join(sp500_inf_adj_rdiff)

sp500_df = sp500_inf_adj.join(shiller_pe)

sp500_df
DateS&P Inflation-Adjusted ValueS&P 500 % ChangeP/E RatioP/E % Change
2022-08-31 00:00:0039550.009250429.640.022069
2022-09-30 00:00:003585.62-0.093395726.84-0.0944669
2022-10-31 00:00:003871.980.079863528.530.0629657
2022-11-30 00:00:003856.1-0.0041012628.37-0.00560813

This particular data series contains 150 years of monthly values. It is among the longest uninterrupted timeseries available in the public domain, and it is cited frequently in macroeconomic research.

Summary

Get a summary of statistics for each column with qa.summary:

openbb.qa.summary(sp500_df)
S&P Inflation-Adjusted ValueS&P 500 % ChangeP/E RatioP/E % Change
count1823182218221821
mean697.3960.002870316.99140.00137084
std834.5010.04243787.070940.0412196
min80.31-0.2647384.78-0.268992
10%152.404-0.04474949.31-0.0449735
25%203.47-0.017280311.7-0.0183028
50%309.830.0055312215.8950.00446999
75%778.420.026000920.55750.0246575
90%1907.870.0450526.4670.0426357
max4786.790.51408544.190.511986
var6963920.0018009749.99820.00169906

Spread

Add the variance and standard deviation, over a specified window (three-months), to the DataFrame:

std,variance = openbb.qa.spread(data = sp500_df['S&P 500 % Change'], window = 3)
std.rename(columns = {'STDEV_3':'Three-Month Standard Deviation'}, inplace = True)
variance.rename(columns = {'VAR_3': 'Three-Month Variance'}, inplace =True)
sp500_df = sp500_df.join([std,variance])
sp500_df.rename_axis('date', inplace = True)
sp500_df.tail(2)
dateS&P Inflation-Adjusted ValueS&P 500 % ChangeP/E RatioP/E % ChangeThree-Month Standard DeviationThree-Month Variance
2022-10-31 00:00:003871.980.079863528.530.06296570.08712170.00759019
2022-11-30 00:00:003856.1-0.0041012628.37-0.005608130.08664320.00750705

The rolling mean average and standard deviation is calculated with the rolling command. Adding, _chart, to this will return an inline chart within a Jupyter Notebook. For the example below, window, 60, represents a five-year period.

Rolling

openbb.qa.rolling_chart(sp500_df, target = 'P/E Ratio', window = 60, symbol = '')

openbb.qa.rolling_chart

Unit Root Test

Perform a unit root test with unitroot:

help(openbb.qa.unitroot)

Parameters
----------
data : pd.DataFrame
DataFrame of target variable
fuller_reg : str
Type of regression of ADF test. Can be ‘c’,’ct’,’ctt’,’nc’ 'c' - Constant and t - trend order
kpss_reg : str
Type of regression for KPSS test. Can be ‘c’,’ct'

Returns
-------
pd.DataFrame
Dataframe with results of ADF test and KPSS test

openbb.qa.unitroot(sp500_df['P/E % Change'])
ADFKPSS
Test Statistic-10.70750.34061374135067696
P-Value3.3972e-190.1
NLags202
Nobs1800
ICBest-6491.14