{"id":4786,"date":"2024-02-01T11:00:07","date_gmt":"2024-02-01T11:00:07","guid":{"rendered":"https:\/\/eodhd.com\/financial-academy\/?p=4786"},"modified":"2025-02-05T11:34:20","modified_gmt":"2025-02-05T11:34:20","slug":"benchmark-tracking-and-cointegration-analysis-with-python","status":"publish","type":"post","link":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python","title":{"rendered":"Benchmark Tracking and Cointegration Analysis with Python"},"content":{"rendered":"\n<p>Cointegration analysis is a sophisticated statistical approach widely used in the field of financial time series analysis. It&#8217;s particularly useful for identifying and analyzing the long-term relationship between two or more time series. This technique is essential in the field of finance, where it&#8217;s used to model and predict market behavior, assess risk, and inform investment strategies. The concept of cointegration becomes particularly important in the study of pairs trading, hedge funds strategies, and risk management.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a class=\"maxbutton-1 maxbutton maxbutton-subscribe-to-api external-css btn\" href=\"https:\/\/eodhd.com\/register\"><span class='mb-text'>Register &amp; Get Data<\/span><\/a><\/p>\n\n\n\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cointegration\">Cointegration<\/h2>\n\n\n\n<p>Cointegration is a statistical property of a collection (two or more) of time series variables. Two or more time series are cointegrated if they share a common stochastic drift. In simpler terms, while each series can wander randomly over time, if they are cointegrated, there is a constant equilibrium mechanism that ties them together. This is crucial in finance, where asset prices are often non-stationary (their statistical properties change over time) but can be bound together by economic or market forces.<\/p>\n\n\n\n<p>Firstly, consider two time series, <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=X_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"X_t\" class=\"latex\" \/> and <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=Y_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"Y_t\" class=\"latex\" \/>. These series are said to be <strong>non-stationary<\/strong> if their mean, variance, or covariance with respect to time (t) is not constant. This is typically modeled as a unit root process, often checked using unit root tests like the Augmented Dickey-Fuller (ADF) test.<\/p>\n\n\n\n<p>Two or more time series <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=X_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"X_t\" class=\"latex\" \/> and <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=Y_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"Y_t\" class=\"latex\" \/> are <strong>cointegrated<\/strong> if:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Both <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=X_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"X_t\" class=\"latex\" \/> and <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=Y_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"Y_t\" class=\"latex\" \/> are individually non-stationary.<\/li>\n\n\n\n<li>A linear combination <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=aX_t+%2B+bY_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"aX_t + bY_t\" class=\"latex\" \/> is stationary, for some non-zero coefficients a and b.<\/li>\n<\/ul>\n\n\n\n<p>In other words, there exists a vector (a, b) such that the time series <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=U_t+%3D+aX_t+%2B+bY_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"U_t = aX_t + bY_t\" class=\"latex\" \/> is stationary.<\/p>\n\n\n\n<p>The stationarity of  <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=X_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"X_t\" class=\"latex\" \/> can be formally tested using standard unit root tests. If <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=X_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"X_t\" class=\"latex\" \/> is found to be stationary, then  <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=X_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"X_t\" class=\"latex\" \/> and <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=Y_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"Y_t\" class=\"latex\" \/> are cointegrated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing Cointegration<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Engle-Granger<\/h3>\n\n\n\n<p>The Engle-Granger method is a two-step approach used for cointegration analysis in time series data. In the first step, it involves estimating a long-run relationship between two or more non-stationary variables using ordinary least squares (OLS). This estimation results in a residual series. The second step tests the stationarity of these residuals using a unit root test, such as the Augmented Dickey-Fuller (ADF) test. If the residuals are found to be stationary, it implies that the variables are cointegrated, meaning they have a long-run equilibrium relationship despite being non-stationary individually.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-johansen-s-method\">Johansen&#8217;s Method<\/h3>\n\n\n\n<p>The Johansen method is another statistical approach used for determining the presence of cointegration among multiple non-stationary time series variables. Unlike the Engle-Granger method, which is limited to examining two variables, the Johansen method can handle multiple variables simultaneously. It is based on the vector autoregression (VAR) model of order ( p ) for ( n ) variables:<\/p>\n\n\n\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5CDelta+Y_t+%3D+%5CPi+Y_%7Bt-1%7D+%2B+%5Csum_%7Bi%3D1%7D%5E%7Bp-1%7D+%5CGamma_i+%5CDelta+Y_%7Bt-i%7D+%2B+%5Cepsilon_t+&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;Delta Y_t = &#92;Pi Y_{t-1} + &#92;sum_{i=1}^{p-1} &#92;Gamma_i &#92;Delta Y_{t-i} + &#92;epsilon_t \" class=\"latex\" \/><\/p>\n\n\n\n<p>Here, <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=Y_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"Y_t\" class=\"latex\" \/> is an <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=n+%5Ctimes+1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"n &#92;times 1\" class=\"latex\" \/> vector of time series, <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5CDelta&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;Delta\" class=\"latex\" \/> is the difference operator, <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5CPi&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;Pi\" class=\"latex\" \/> and <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5CGamma_i&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;Gamma_i\" class=\"latex\" \/> are matrices of coefficients, and <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5Cepsilon_t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;epsilon_t\" class=\"latex\" \/> is a vector of error terms.<\/p>\n\n\n\n<p>The core of the Johansen method lies in the analysis of the matrix <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5CPi&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;Pi\" class=\"latex\" \/>. This matrix can be decomposed into <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5Calpha+%5Cbeta%27&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;alpha &#92;beta&#039;\" class=\"latex\" \/>, where <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;alpha\" class=\"latex\" \/> and <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;beta\" class=\"latex\" \/> are <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=n+%5Ctimes+r+&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"n &#92;times r \" class=\"latex\" \/> matrices, and  r  represents the number of cointegrating relationships. The Johansen method tests the rank of the <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5CPi+&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;Pi \" class=\"latex\" \/> matrix (equivalent to testing the number of cointegrating vectors). This is achieved through two likelihood ratio tests: the trace test and the maximum eigenvalue test, both of which assess the significance of the cointegrating vectors. If the rank of <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5CPi+&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;Pi \" class=\"latex\" \/> is found to be greater than zero, it implies that there are cointegrating relationships among the variables, indicating a long-run equilibrium linkage. <\/p>\n\n\n\n<p>As correctly indicated by Alexander in <a href=\"https:\/\/www.coalexander.com\/books\" target=\"_blank\" rel=\"noreferrer noopener\">Market Risk Analysis Volume III<\/a> &#8220;<em>it is important to recognize that the two tests have different objectives. The Johansen tests seek the linear combination which is most stationary whereas the Engle\u2013Granger tests, being based on OLS, seek the stationary linear combination that has the minimum variance.<\/em>&#8220;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cointegration vs Correlation<\/h2>\n\n\n\n<p>Cointegration and correlation are both statistical concepts used in the analysis of time series data, but they refer to different types of relationships between variables.<\/p>\n\n\n\n<p>Correlation measures the strength and direction of a linear relationship between two variables. It tells us how much one variable tends to change when the other one does, but it doesn&#8217;t imply causation or a stable long-term relationship. Correlation is often represented by the Pearson correlation coefficient, which ranges from -1 (perfect negative correlation) to +1 (perfect positive correlation). A value of 0 indicates no correlation.<\/p>\n\n\n\n<p>Cointegration, on the other hand, refers to a situation where two or more non-stationary time series are linked together in such a way that their linear combination is stationary. Even if the individual series themselves wander randomly over time, if they are cointegrated, there is a consistent, long-term equilibrium relationship between them. This concept is crucial in econometrics and finance as it suggests a predictable, long-term balance between the series, despite short-term fluctuations.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"854\" height=\"525\" data-id=\"4788\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-01-11T124637.657.png\" alt=\"\" class=\"wp-image-4788\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"854\" height=\"525\" data-id=\"4789\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-01-11T124507.593.png\" alt=\"\" class=\"wp-image-4789\"\/><\/figure>\n<\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-of-cointegration-in-finance\">Example of Cointegration in Finance<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Stock Indices and Tracking Portfolios:<\/strong><br>In the case of stock indices and tracking portfolios, consider the relationship between a major stock index like the S&amp;P 500 and a mutual fund that aims to track its performance. Cointegration in this context would suggest that over the long term, the movements of the mutual fund are closely aligned with the movements of the S&amp;P 500. Any short-term discrepancies between the fund and the index are expected to be temporary, with the fund consistently reflecting the performance of the index over an extended period.<\/li>\n\n\n\n<li><strong>Pairs Trading in Stocks:<\/strong><br>For pairs trading involving stocks, an example could be the shares of two major, competing corporations in the same industry, such as Coca-Cola and Pepsi. If these stocks are cointegrated, it indicates that while their individual stock prices might diverge due to short-term market factors, they exhibit a long-term parallel trend. This long-term relationship forms the basis of pairs trading strategies, where temporary price discrepancies between the stocks are seen as opportunities for arbitrage.<\/li>\n\n\n\n<li><strong>Spot and Futures Prices:<\/strong><br>In the realm of commodities, an example of cointegration can be observed between the spot and futures prices of a commodity, such as crude oil. Here, cointegration suggests a long-term equilibrium between the spot prices and futures prices of crude oil. Despite potential short-term divergences due to factors like storage costs or interest rates, these prices are expected to converge as the futures contract nears its expiration, reflecting their intrinsic linkage.<\/li>\n\n\n\n<li><strong>Commodities:<\/strong><br>When considering commodities like gold and silver, cointegration implies that their prices, though subject to individual market dynamics in the short term, share a long-term equilibrium relationship. This could be due to their similar market roles as precious metals and safe-haven assets, leading their prices to move in tandem over the long term despite short-term fluctuations.<\/li>\n\n\n\n<li><strong>Foreign Exchange:<\/strong><br>For foreign exchange, an example can be drawn from the cointegration between currency pairs like EUR\/USD and GBP\/USD. This cointegration would indicate that, over the long term, these currency pairs move together, maintaining a stable relationship. Short-term fluctuations may occur due to immediate economic events or policy changes, but the long-term movement reflects the enduring economic interrelations among the Eurozone, the UK, and the USA.<\/li>\n\n\n\n<li><strong>Economics:<\/strong><br>In an economic context, consider the Gross Domestic Product (GDP) and Consumer Price Index (CPI) of a country. Cointegration between these two indicators would imply that over a long period, the country&#8217;s economic growth (as measured by GDP) and inflation rate (as indicated by CPI) move in a correlated manner. While short-term deviations may arise from various economic policies or external factors, the long-term trend shows a consistent relationship between economic growth and inflation.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cointegration-on-dax30\">Cointegration on DAX30 <\/h2>\n\n\n\n<p>After defining cointegration and providing several examples, this section presents a case study of cointegration analysis on the German stock market index, the DAX 30. Initially, we examine the cointegration of the index and individual stocks. Subsequently, we introduce two methodologies used for index tracking: one focused on minimizing tracking error variance, and the other based on cointegration.<\/p>\n\n\n\n<p>To retrieve data, we utilize the <a href=\"https:\/\/eodhd.com\/financial-apis\/python-financial-libraries-and-code-samples\/#Official_EODHD_APIs_Python_Financial_Library_by_Michael_Whittle\" target=\"_blank\" rel=\"noreferrer noopener\">Official EOD Python API <\/a>library. Our process begins with gathering data for the index and its constituents dating back to January 2010. Information on the constituents is accessible through the use of the <em>get_fundamentals_data<\/em> function.<\/p>\n\n\n\n<p class=\"bordered_paragraph\">1. Use the \u201c<strong>demo<\/strong>\u201d API key to test our data from a limited set of tickers without registering: <a href=\"https:\/\/eodhistoricaldata.com\/financial-summary\/AAPL.US\" target=\"_blank\" rel=\"noreferrer noopener\">AAPL.US<\/a> | <a href=\"https:\/\/eodhistoricaldata.com\/financial-summary\/TSLA.US\" target=\"_blank\" rel=\"noreferrer noopener\">TSLA.US <\/a>| <a href=\"https:\/\/eodhistoricaldata.com\/financial-summary\/VTI.US\" target=\"_blank\" rel=\"noreferrer noopener\">VTI.US<\/a> | <a href=\"https:\/\/eodhistoricaldata.com\/financial-summary\/AMZN.US\" target=\"_blank\" rel=\"noreferrer noopener\">AMZN.US<\/a> | <a href=\"https:\/\/eodhistoricaldata.com\/financial-summary\/BTC-USD.CC\" target=\"_blank\" rel=\"noreferrer noopener\">BTC-USD<\/a> | <a href=\"https:\/\/eodhistoricaldata.com\/financial-summary\/EURUSD.FOREX\" target=\"_blank\" rel=\"noreferrer noopener\">EUR-USD<\/a><br><a href=\"https:\/\/eodhistoricaldata.com\/financial-summary\/AAPL.US\" target=\"_blank\" rel=\"noreferrer noopener\">Real-Time Data<\/a> and all of the APIs (except Bulk) are included without API calls limitations with these tickers and the demo key.<br>2. Register to get your free API key (limited to 20 API calls per day) with access to: <a href=\"https:\/\/eodhistoricaldata.com\/financial-apis\/api-for-historical-data-and-volumes\/\" target=\"_blank\" rel=\"noreferrer noopener\">End-Of-Day Historical Data<\/a> with only the past year for any ticker, and the <a href=\"https:\/\/eodhistoricaldata.com\/financial-apis\/api-for-historical-data-and-volumes\/\" target=\"_blank\" rel=\"noreferrer noopener\">List of tickers per Exchange<\/a>.<br>3. To unlock your API key, we recommend to choose the subscription plan which covers your needs.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-ce25d4dd774e273e39ef4c7cbd5fbd6f\"><code lang=\"python\" class=\"language-python\"># get Dax30 data\nstart = \"2010-01-01\"\ndax = api.get_historical_data(\"GDAXI.INDX\", interval=\"d\", iso8601_start=start,\niso8601_end=\"2024-01-09\")[\"adjusted_close\"].astype(float)\ndax.name = \"DAX\"\n# get constituents\ncomps = api.get_fundamentals_data(\"GDAXI.INDX\")\ncomps = pd.DataFrame.from_dict(comps[\"Components\"], orient=\"index\")\n# get times series data for Dax stocks\ndax_comps_ts = pd.DataFrame()\nfor symbol in comps[\"Code\"][:-4]:\n    print(symbol)\n    ts = api.get_historical_data(\"{}.XETRA\".format(symbol), interval=\"d\", iso8601_start=start,\n                                 iso8601_end=\"2024-01-01\")[\"adjusted_close\"]\n    ts.name = symbol\n    dax_comps_ts = pd.concat([dax_comps_ts, ts], axis=1)\n\ndax_comps_ts = dax_comps_ts.sort_index()\ndax_comps_ts.index = pd.to_datetime(dax_comps_ts.index)\n#merge the index and constituents data\ndf = pd.merge(dax, dax_comps_ts, \n                        left_on=dax.index, \n                        right_on=dax_comps_ts.index).set_index(\"key_0\").astype(float)\ndf.index.name = \"Date\"<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/baglinifinance.com\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-14-114212.png\" alt=\"\" class=\"wp-image-2596\"\/><\/figure>\n\n\n\n<p>Next, we examine the dataset for missing values and remove those series that contribute the most to these gaps. This approach aids in creating a more extensive dataset, which is particularly beneficial in the context of regression analysis.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/baglinifinance.com\/wp-content\/uploads\/2024\/01\/missings.png\" alt=\"\" class=\"wp-image-2597\" style=\"width:431px;height:auto\"\/><\/figure>\n<\/div>\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-c0915203dbe6496c48a3beafb24ff680\"><code lang=\"python\" class=\"language-python\">df = df.drop(columns= [\"ZAL\", \"1COV\", \"VNA\"], axis=1)\ndf = df.dropna()\nlogprice_all = np.log(df)[1:] \nret_all = df.pct_change()[1:]<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<p>Finally we run the Engle-Granger test. In this case, we test for cointegration between each of the companies one-by-one and the index, by running the following regression and the checking for stationarity of residuals through the ADF test.<\/p>\n\n\n\n<p>$$ ln(I_{t}) = \\alpha + B_{k} ln(P_{kt}) + \\epsilon $$<\/p>\n\n\n\n<p>for each <em>k<\/em>, where <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=P_%7Bk%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"P_{k}\" class=\"latex\" \/> is a Dax30 constituents. <\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">X_all = ret_all.drop(\"DAX\", axis=1)\nt_stats = {}\n\nfor col in X_all.columns:\n    X = X_all[col]\n    y=logprice_all[\"DAX\"]\n    model = sm.OLS(y, X).fit()\n    t_stats[col] =adf_test(model.resid)[0]<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/baglinifinance.com\/wp-content\/uploads\/2024\/01\/newplot-2024-01-19T153445.213.png\" alt=\"\" class=\"wp-image-2603\"\/><\/figure>\n\n\n\n<p>We plot the t-stats and critical values. Surprisingly, only two stocks &#8211; HEN3 and BRN &#8211; appear to be cointegrated at 5% significance level.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-application-to-benchmark-tracking\">Application to Benchmark Tracking<\/h2>\n\n\n\n<p>In this paragraph we introduce two approaches for index tracking. The first is a cointegration-based method which regresses the index log price on the log prices of selected stocks over a calibration period. This log transformation ensures homogeneity and valid application of OLS in the presence of cointegration. The second, namely TEVM, method is the traditional OLS estimation where the index returns are regressed on the index stocks returns and by construction minimizes the tracking error. <\/p>\n\n\n\n<p>In both cases, we employed calibration periods of from 2 years of data preceding the portfolio construction date. The initial portfolios based on cointegration tracking were established on March 7, 2012, and the most recent ones were created on December 29, 2023. To maintain their relevance and accuracy, all portfolios underwent rebalancing at intervals of every 10 trading days. The new rebalancing weights are based the new Ordinary Least Squares (OLS) coefficients, which were recalculated for each rolling calibration period in the cointegration regression analysis.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cointegration-0\">Cointegration<\/h3>\n\n\n\n<p>Let&#8217;s start with the cointegration approach describe above.  In this case, we regress the index log prices, in this case, the Dax 30,  on the price of the historical DAX constituents and test for stationarity of residuals with ADF check as prescribed by the Engle-Granger test. <\/p>\n\n\n\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=ln%28I_%7Bt%7D%29+%3D+%5Calpha+%2B+%5Csum_%7Bk%3D1%7D%5E%7Bn%7DB_%7Bk%7D+ln%28P_%7Bkt%7D%29+%2B+%5Cepsilon+&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"ln(I_{t}) = &#92;alpha + &#92;sum_{k=1}^{n}B_{k} ln(P_{kt}) + &#92;epsilon \" class=\"latex\" \/><\/p>\n\n\n\n<p>Note that:<br><em>&#8220;the application of OLS to non-stationary dependent variables such as log(index) is only valid in the special case of a cointegration relationship. The residuals are stationary if, and only if, the log(index) and the tracking portfolio are cointegrated. Unless the residuals from the above regression are found to be stationary, the OLS coefficients will be inconsistent and further inference based on them will be invalid. Therefore, testing for cointegration is an essential step in constructing cointegration-based tracking portfolios.&#8221;<\/em> <a href=\"https:\/\/papers.ssrn.com\/sol3\/papers.cfm?abstract_id=315619\" target=\"_blank\" rel=\"noreferrer noopener\">Alexander and Dimitriu (2002)<\/a><\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">\"\"\" Cointegration\"\"\"\ndax_comps_tss = [] \ndate = []\nx = logprice_all.drop(\"DAX\", axis=1)\nwindow_size = 500\nfor i in range(0, len(ret_all) - window_size + 1,  10):\n    \n    # Extract the current rolling window\n    X_window = x.iloc[i:i+window_size]\n    y_window = logprice_all[\"DAX\"].iloc[i:i+window_size]\n    X_window = sm.add_constant(X_window)\n    model = sm.OLS(y_window, X_window).fit()\n    dax_comps_tss.append(model.params)\n    date.append(X_window.index[-1])\n    print(adf_test(model.resid)[0])\nparams = pd.DataFrame(dax_comps_tss, index=date)\nparams = params.drop(\"const\", axis=1)\nweights_coin = params.div(params.sum(axis=1), axis=0)\n\nfig  = px.line(params)\nfig.update_layout(title_text=\"Index Tracking Weights - Cointegration\", xaxis_title=\"\", yaxis_title=\"\")\nfig.show()<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/baglinifinance.com\/wp-content\/uploads\/2024\/01\/newplot-2024-01-19T155750.678.png\" alt=\"\" class=\"wp-image-2604\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tevm\">TEVM<\/h3>\n\n\n\n<p>Traditional benchmark tracking optimization problem is to minimize the tracking error with respect to the benchmark. This approach is also called TEVM. In this case, ordinary least squares is used to estimate a linear regression of benchmark returns against asset returns. <\/p>\n\n\n\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=ln%28r_%7Bit%7D%29+%3D+%5Calpha+%2B+%5Csum_%7Bk%3D1%7D%5E%7Bn%7DB_%7Bk%7D+ln%28r_%7Bkt%7D%29+%2B+%5Cepsilon+&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"ln(r_{it}) = &#92;alpha + &#92;sum_{k=1}^{n}B_{k} ln(r_{kt}) + &#92;epsilon \" class=\"latex\" \/><\/p>\n\n\n\n<p>The calculated regression betas define the portfolio weights, with the residual representing the tracking error. However, this approach does not guarantee that the tracking error will exhibit mean-reversion characteristics.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"856\" height=\"525\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115025.714.png\" alt=\"\" class=\"wp-image-4795\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Results and Conclusion<\/h2>\n\n\n\n<p>Finally we compare the results obtained with the two methods. As shown in the below figure, appropriate replicas can be constructed for the market index, provided that a minimum number of stocks is included in the tracking portfolio and an appropriate calibration period is used. Also it&#8217;s clear how the cointegration approach yields a better tracking performance compared to the variance minimization method. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"856\" height=\"525\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-1.png\" alt=\"\" class=\"wp-image-4794\"\/><\/figure>\n<\/div>\n\n\n<p>In conclusion, this article presents a comprehensive study of cointegration and its application in the context of the German stock market, particularly focusing on the DAX 30 index. The application of the Engle-Granger test for cointegration between the DAX 30 and its constituents over the sample period  revealed that only a few stocks demonstrated significant cointegration.<\/p>\n\n\n\n<p>The exploration of index tracking methodologies, namely cointegration-based tracking and tracking error variance minimization (TEVM), highlighted the nuances and effectiveness of each approach. The cointegration-based method, which involves selecting stocks and determining portfolio holdings through cointegration optimization, has proven to be effective, especially when rebalanced regularly based on recalculated OLS coefficients. Our backtesting results underscore the efficacy of these methods in constructing market index replicas. Notably, the cointegration approach showed superior tracking performance compared to TEVM, underlining its potential in the realm of portfolio management. <\/p>\n\n\n\n<p class=\"has-text-align-center\"><a class=\"maxbutton-1 maxbutton maxbutton-subscribe-to-api external-css btn\" href=\"https:\/\/eodhd.com\/register\"><span class='mb-text'>Register &amp; Get Data<\/span><\/a><\/p>\n\n\n\n<p>Full code available <a href=\"https:\/\/github.com\/gbaglini\/baglinifinance\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><br>Website: <a href=\"https:\/\/baglinifinance.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">baglinifinance.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cointegration analysis is a sophisticated statistical approach widely used in the field of financial time series analysis. It&#8217;s particularly useful for identifying and analyzing the long-term relationship between two or more time series. This technique is essential in the field of finance, where it&#8217;s used to model and predict market behavior, assess risk, and inform [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":4799,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[65,77,48],"tags":[],"coding-language":[30],"ready-to-go-solution":[56],"qualification":[31],"financial-apis-category":[36],"financial-apis-manuals":[39],"class_list":["post-4786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backtesting-strategies-examples","category-portfolio-performance-examples","category-stocks-data-processing-examples","coding-language-python","ready-to-go-solution-eodhd-python-financial-library","qualification-experienced","financial-apis-category-stock-market-prices","financial-apis-manuals-end-of-day","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.9 (Yoast SEO v26.7) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Benchmark Tracking and Cointegration Analysis with Python | EODHD APIs Academy<\/title>\n<meta name=\"description\" content=\"In this paragraph we introduce two approaches for index tracking. The first is a cointegration-based method used for benchmark replication.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Benchmark Tracking and Cointegration Analysis with Python\" \/>\n<meta property=\"og:description\" content=\"In this paragraph we introduce two approaches for index tracking. The first is a cointegration-based method used for benchmark replication.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python\" \/>\n<meta property=\"og:site_name\" content=\"Financial Academy\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/eodhistoricaldata\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-01T11:00:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-05T11:34:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"856\" \/>\n\t<meta property=\"og:image:height\" content=\"525\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Gianluca Baglini\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@EOD_data\" \/>\n<meta name=\"twitter:site\" content=\"@EOD_data\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gianluca Baglini\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#article\",\"isPartOf\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python\"},\"author\":{\"name\":\"Gianluca Baglini\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/6c95b0cef2acc2fbaebccff191d7027f\"},\"headline\":\"Benchmark Tracking and Cointegration Analysis with Python\",\"datePublished\":\"2024-02-01T11:00:07+00:00\",\"dateModified\":\"2025-02-05T11:34:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python\"},\"wordCount\":2251,\"publisher\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#primaryimage\"},\"thumbnailUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png\",\"articleSection\":[\"Backtesting Strategies Examples\",\"Portfolio Performance Examples\",\"Stocks Data Processing Examples\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python\",\"name\":\"Benchmark Tracking and Cointegration Analysis with Python | EODHD APIs Academy\",\"isPartOf\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#primaryimage\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#primaryimage\"},\"thumbnailUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png\",\"datePublished\":\"2024-02-01T11:00:07+00:00\",\"dateModified\":\"2025-02-05T11:34:20+00:00\",\"description\":\"In this paragraph we introduce two approaches for index tracking. The first is a cointegration-based method used for benchmark replication.\",\"breadcrumb\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#primaryimage\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png\",\"contentUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png\",\"width\":856,\"height\":525,\"caption\":\"cointegration analysis\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/eodhd.com\/financial-academy\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Benchmark Tracking and Cointegration Analysis with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#website\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/\",\"name\":\"Financial APIs Academy | EODHD\",\"description\":\"Financial Stock Market Academy\",\"publisher\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/eodhd.com\/financial-academy\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\",\"name\":\"EODHD (EOD Historical Data)\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png\",\"contentUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png\",\"width\":159,\"height\":82,\"caption\":\"EODHD (EOD Historical Data)\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/eodhistoricaldata\",\"https:\/\/x.com\/EOD_data\",\"https:\/\/www.reddit.com\/r\/EODHistoricalData\/\",\"https:\/\/eod-historical-data.medium.com\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/6c95b0cef2acc2fbaebccff191d7027f\",\"name\":\"Gianluca Baglini\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2a3c6038fdaa1636ec4863020f9191a4991c58f0935313853fd01a4b88bfbee9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2a3c6038fdaa1636ec4863020f9191a4991c58f0935313853fd01a4b88bfbee9?s=96&d=mm&r=g\",\"caption\":\"Gianluca Baglini\"},\"description\":\"Financial &amp; Risk Analyst, Data Scientist, Python Developer, Owner of BagliniFinance blog\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/gianluca-baglini\/\"],\"url\":\"https:\/\/eodhd.com\/financial-academy\/author\/gianlucabaglini\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Benchmark Tracking and Cointegration Analysis with Python | EODHD APIs Academy","description":"In this paragraph we introduce two approaches for index tracking. The first is a cointegration-based method used for benchmark replication.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python","og_locale":"en_US","og_type":"article","og_title":"Benchmark Tracking and Cointegration Analysis with Python","og_description":"In this paragraph we introduce two approaches for index tracking. The first is a cointegration-based method used for benchmark replication.","og_url":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python","og_site_name":"Financial Academy","article_publisher":"https:\/\/www.facebook.com\/eodhistoricaldata","article_published_time":"2024-02-01T11:00:07+00:00","article_modified_time":"2025-02-05T11:34:20+00:00","og_image":[{"width":856,"height":525,"url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png","type":"image\/png"}],"author":"Gianluca Baglini","twitter_card":"summary_large_image","twitter_creator":"@EOD_data","twitter_site":"@EOD_data","twitter_misc":{"Written by":"Gianluca Baglini","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#article","isPartOf":{"@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python"},"author":{"name":"Gianluca Baglini","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/6c95b0cef2acc2fbaebccff191d7027f"},"headline":"Benchmark Tracking and Cointegration Analysis with Python","datePublished":"2024-02-01T11:00:07+00:00","dateModified":"2025-02-05T11:34:20+00:00","mainEntityOfPage":{"@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python"},"wordCount":2251,"publisher":{"@id":"https:\/\/eodhd.com\/financial-academy\/#organization"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#primaryimage"},"thumbnailUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png","articleSection":["Backtesting Strategies Examples","Portfolio Performance Examples","Stocks Data Processing Examples"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python","url":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python","name":"Benchmark Tracking and Cointegration Analysis with Python | EODHD APIs Academy","isPartOf":{"@id":"https:\/\/eodhd.com\/financial-academy\/#website"},"primaryImageOfPage":{"@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#primaryimage"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#primaryimage"},"thumbnailUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png","datePublished":"2024-02-01T11:00:07+00:00","dateModified":"2025-02-05T11:34:20+00:00","description":"In this paragraph we introduce two approaches for index tracking. The first is a cointegration-based method used for benchmark replication.","breadcrumb":{"@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#primaryimage","url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png","contentUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png","width":856,"height":525,"caption":"cointegration analysis"},{"@type":"BreadcrumbList","@id":"https:\/\/eodhd.com\/financial-academy\/portfolio-performance-examples\/benchmark-tracking-and-cointegration-analysis-with-python#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/eodhd.com\/financial-academy\/"},{"@type":"ListItem","position":2,"name":"Benchmark Tracking and Cointegration Analysis with Python"}]},{"@type":"WebSite","@id":"https:\/\/eodhd.com\/financial-academy\/#website","url":"https:\/\/eodhd.com\/financial-academy\/","name":"Financial APIs Academy | EODHD","description":"Financial Stock Market Academy","publisher":{"@id":"https:\/\/eodhd.com\/financial-academy\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/eodhd.com\/financial-academy\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/eodhd.com\/financial-academy\/#organization","name":"EODHD (EOD Historical Data)","url":"https:\/\/eodhd.com\/financial-academy\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/","url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png","contentUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png","width":159,"height":82,"caption":"EODHD (EOD Historical Data)"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/eodhistoricaldata","https:\/\/x.com\/EOD_data","https:\/\/www.reddit.com\/r\/EODHistoricalData\/","https:\/\/eod-historical-data.medium.com\/"]},{"@type":"Person","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/6c95b0cef2acc2fbaebccff191d7027f","name":"Gianluca Baglini","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2a3c6038fdaa1636ec4863020f9191a4991c58f0935313853fd01a4b88bfbee9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2a3c6038fdaa1636ec4863020f9191a4991c58f0935313853fd01a4b88bfbee9?s=96&d=mm&r=g","caption":"Gianluca Baglini"},"description":"Financial &amp; Risk Analyst, Data Scientist, Python Developer, Owner of BagliniFinance blog","sameAs":["https:\/\/www.linkedin.com\/in\/gianluca-baglini\/"],"url":"https:\/\/eodhd.com\/financial-academy\/author\/gianlucabaglini"}]}},"jetpack_featured_media_url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2024\/02\/newplot-2024-02-01T115027.672-2.png","jetpack_shortlink":"https:\/\/wp.me\/pdOdVT-1fc","jetpack_sharing_enabled":true,"acf":[],"_links":{"self":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/4786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/comments?post=4786"}],"version-history":[{"count":12,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/4786\/revisions"}],"predecessor-version":[{"id":6225,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/4786\/revisions\/6225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/media\/4799"}],"wp:attachment":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/media?parent=4786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/categories?post=4786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/tags?post=4786"},{"taxonomy":"coding-language","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/coding-language?post=4786"},{"taxonomy":"ready-to-go-solution","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/ready-to-go-solution?post=4786"},{"taxonomy":"qualification","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/qualification?post=4786"},{"taxonomy":"financial-apis-category","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/financial-apis-category?post=4786"},{"taxonomy":"financial-apis-manuals","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/financial-apis-manuals?post=4786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}