{"id":22832,"date":"2019-10-23T10:41:59","date_gmt":"2019-10-23T14:41:59","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=22832"},"modified":"2022-11-21T09:44:27","modified_gmt":"2022-11-21T14:44:27","slug":"k-means-clustering-algo-python-part-v","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/","title":{"rendered":"K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V"},"content":{"rendered":"\n<p><em>See the prior installment in this series <\/em><a href=\"\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-iv\/\"><em>here<\/em><\/a><em>.<\/em><\/p>\n\n\n\n<p><strong>Spread is not Cointegrated<\/strong><\/p>\n\n\n\n<p>The results of the Augmented Dickey Fuller Test showed that Walmart and Target were not cointegrated. This is determined by a test statistic that is not less than one of the critical values. If you would like to view the actual print out of the ADF test you can do so by keying ADF. In the above example, we use indexing to decipher between the t-statistic and critical values. The statsmodels ADF Test provides you with other useful information such as the p-value. You can learn more about the ADF test&nbsp;<a href=\"https:\/\/www.statsmodels.org\/stable\/generated\/statsmodels.tsa.stattools.adfuller.html\">here<\/a><\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:11px\" class=\"has-background\">#printing out the results of the adf test<br>\nadf<br>\n(-0.38706825965317432,<br>\n0.91223562790079438,<br>\n0,<br>\n503,<br>\n{&#8216;1%&#8217;: -3.4434175660489905,<br>\n&#8216;10%&#8217;: -2.5698395516760275,<br>\n&#8216;5%&#8217;: -2.8673031724657454},<br>\n1190.4266834075452)<\/p>\n\n\n\n<p>Okay, let&#8217;s try one more. Maybe we&#8217;ll have better luck identifying a tradable relationship in a brute force manner. How about Dollar Tree and Dollar General. They&#8217;re both discount retailers and look they both even have a dollar in their names. Since we&#8217;ve gotten the hang of things, we jump right into the ADF test. Let&#8217;s first import the data for DLTR and DG.<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:11px\" class=\"has-background\">#importing dltr and dg<br>\ndltr=pdr.get_data_yahoo(&#8216;DLTR&#8217;,start, end)<br>\ndg=pdr.get_data_yahoo(&#8216;DG&#8217;,start, end)<\/p>\n\n\n\n<p><em>Stay tuned -for the next installment in this series. Lamarcus will demonstrate how to add these stocks to the <code>newDF<\/code> and create their spread. <\/em><\/p>\n\n\n\n<p><em>Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.<\/em><\/p>\n\n\n\n<p><em>Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The results of the Augmented Dickey Fuller Test showed that Walmart and Target were not cointegrated. This is determined by a test statistic that is not less than one of the critical values.<\/p>\n","protected":false},"author":261,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338,350,344],"tags":[851,4658,4656,806,4582,4581,4124,852,4659,1225,4657,1224,595,4580,2536],"contributors-categories":[13654],"class_list":{"0":"post-22832","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-data-science","7":"category-programing-languages","8":"category-python-development","9":"category-ibkr-quant-news","10":"category-quant-asia-pacific","11":"category-quant-regions","12":"tag-algo-trading","13":"tag-augmented-dickey-fuller-test","14":"tag-correlation-coefficient","15":"tag-data-science","16":"tag-dataframe","17":"tag-heatmap","18":"tag-k-means-clustering","19":"tag-machine-learning","20":"tag-matplotlib","21":"tag-numpy","22":"tag-p-value","23":"tag-pandas","24":"tag-python","25":"tag-seaborn","26":"tag-visualization","27":"contributors-categories-quantinsti"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V<\/title>\n<meta name=\"description\" content=\"The results of the Augmented Dickey Fuller Test showed that Walmart and Target were not cointegrated. This is determined by a test statistic.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/22832\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V\" \/>\n<meta property=\"og:description\" content=\"The results of the Augmented Dickey Fuller Test showed that Walmart and Target were not cointegrated. This is determined by a test statistic.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-23T14:41:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:44:27+00:00\" \/>\n<meta name=\"author\" content=\"Lamarcus Coleman\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lamarcus Coleman\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/k-means-clustering-algo-python-part-v\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/k-means-clustering-algo-python-part-v\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Lamarcus Coleman\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d25f2d2d80f2efd41c14efd1767f840d\"\n\t            },\n\t            \"headline\": \"K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V\",\n\t            \"datePublished\": \"2019-10-23T14:41:59+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:44:27+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/k-means-clustering-algo-python-part-v\\\/\"\n\t            },\n\t            \"wordCount\": 332,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"keywords\": [\n\t                \"Algo Trading\",\n\t                \"Augmented Dickey Fuller Test\",\n\t                \"Correlation Coefficient\",\n\t                \"Data Science\",\n\t                \"Dataframe\",\n\t                \"Heatmap\",\n\t                \"K-Means Clustering\",\n\t                \"Machine Learning\",\n\t                \"Matplotlib\",\n\t                \"NumPy\",\n\t                \"P-Value\",\n\t                \"Pandas\",\n\t                \"Python\",\n\t                \"Seaborn\",\n\t                \"Visualization\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Asia Pacific\",\n\t                \"Quant Regions\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/k-means-clustering-algo-python-part-v\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/k-means-clustering-algo-python-part-v\\\/\",\n\t            \"name\": \"K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"datePublished\": \"2019-10-23T14:41:59+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:44:27+00:00\",\n\t            \"description\": \"The results of the Augmented Dickey Fuller Test showed that Walmart and Target were not cointegrated. This is determined by a test statistic.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/k-means-clustering-algo-python-part-v\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d25f2d2d80f2efd41c14efd1767f840d\",\n\t            \"name\": \"Lamarcus Coleman\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/lamarcuscoleman\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V","description":"The results of the Augmented Dickey Fuller Test showed that Walmart and Target were not cointegrated. This is determined by a test statistic.","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:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/22832\/","og_locale":"en_US","og_type":"article","og_title":"K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V","og_description":"The results of the Augmented Dickey Fuller Test showed that Walmart and Target were not cointegrated. This is determined by a test statistic.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/","og_site_name":"IBKR Campus US","article_published_time":"2019-10-23T14:41:59+00:00","article_modified_time":"2022-11-21T14:44:27+00:00","author":"Lamarcus Coleman","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Lamarcus Coleman","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/"},"author":{"name":"Lamarcus Coleman","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d25f2d2d80f2efd41c14efd1767f840d"},"headline":"K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V","datePublished":"2019-10-23T14:41:59+00:00","dateModified":"2022-11-21T14:44:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/"},"wordCount":332,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"keywords":["Algo Trading","Augmented Dickey Fuller Test","Correlation Coefficient","Data Science","Dataframe","Heatmap","K-Means Clustering","Machine Learning","Matplotlib","NumPy","P-Value","Pandas","Python","Seaborn","Visualization"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Asia Pacific","Quant Regions"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/","name":"K-Means Clustering Algorithm For Pair Selection In Python \u2013 Part V","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"datePublished":"2019-10-23T14:41:59+00:00","dateModified":"2022-11-21T14:44:27+00:00","description":"The results of the Augmented Dickey Fuller Test showed that Walmart and Target were not cointegrated. This is determined by a test statistic.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/k-means-clustering-algo-python-part-v\/"]}]},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d25f2d2d80f2efd41c14efd1767f840d","name":"Lamarcus Coleman","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/lamarcuscoleman\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/22832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/261"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=22832"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/22832\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=22832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=22832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=22832"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=22832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}