{"id":81554,"date":"2021-03-29T13:40:52","date_gmt":"2021-03-29T17:40:52","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=81554"},"modified":"2023-03-14T15:13:38","modified_gmt":"2023-03-14T19:13:38","slug":"a-python-package-for-optimal-mean-reversion-trading","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/","title":{"rendered":"A Python Package for Optimal Mean Reversion Trading"},"content":{"rendered":"\n<p>Pairs trading is among the most popular trading strategies in many markets, ranging from equities and ETFs to currencies and futures markets. It involves taking simultaneous positions in two correlated assets. The idea is that while typically it is difficult to accurately capture the price evolution of a single asset, a pairs position may exhibit mean reversion that can be better modeled. In short, pairs trading is a market-neutral strategy that seeks to profit from the price convergence between the two assets.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Practical examples of mean reverting price spread include pairs of stocks\/ETFs, futures and its spot, physical commodity and associated ETFs, and more. There are also automated approaches for identifying mean-reverting portfolios.&nbsp;<\/p>\n\n\n\n<p>In this new python package called Machine Learning Financial Laboratory (mlfinlab), there is a module that automatically solves for the optimal trading strategies (entry &amp; exit price thresholds) when the underlying assets\/portfolios have mean-reverting price dynamics. It covers a few mean-reverting models, including the Ornstein-Uhlenbeck&nbsp;(OU) model. The trading model and computations are based on the results from this&nbsp;<a href=\"https:\/\/papers.ssrn.com\/sol3\/papers.cfm?abstract_id=2222196\" target=\"_blank\" rel=\"noreferrer noopener\">journal article<\/a>.&nbsp;<\/p>\n\n\n\n<p>The module includes three main steps:&nbsp;<\/p>\n\n\n\n<p><strong>Model Fitting<\/strong>&nbsp;<\/p>\n\n\n\n<p>We fit any given portfolio value to the mean-reverting Ornstein-Uhlenbeck&nbsp;(OU) process. The statistical technique involved is the maximum likelihood estimation (MLE) method where we optimize the average log-likelihood. While pairs trading is an intuitive strategy, any serious pairs trading system must include a procedure for optimizing the positions along with timing for entry and exit. The goal of this model fitting step is to select the portfolio weights so as to optimize the level of mean reversion.&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"513\" height=\"174\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-142.png\" alt=\"\" class=\"wp-image-81564 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-142.png 513w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-142-300x102.png 300w\" data-sizes=\"(max-width: 513px) 100vw, 513px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 513px; aspect-ratio: 513\/174;\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"511\" height=\"420\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-143.png\" alt=\"\" class=\"wp-image-81567 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-143.png 511w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-143-300x247.png 300w\" data-sizes=\"(max-width: 511px) 100vw, 511px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 511px; aspect-ratio: 511\/420;\" \/><\/figure>\n\n\n\n<p><em>Source:\u00a0MLfinlab\u00a0documentation<\/em>: https:\/\/mlfinlab.readthedocs.io\/en\/latest\/optimal_mean_reversion\/ou_model.html<\/p>\n\n\n\n<p><strong>Determining the Optimal Entry &amp; Exit Levels<\/strong>&nbsp;<\/p>\n\n\n\n<p>The optimal entry &amp; exit levels are computed based on your data. The user can call one of the functions mentioned below. They present the solutions to the equations established in this&nbsp;<a href=\"https:\/\/papers.ssrn.com\/sol3\/papers.cfm?abstract_id=2222196\" target=\"_blank\" rel=\"noreferrer noopener\">paper<\/a>. Stop-loss level can be added and optimal levels are adjusted accordingly.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large img-twothird\"><img decoding=\"async\" width=\"494\" height=\"660\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-144.png\" alt=\"A Python Package for Optimal Mean Reversion Trading\" class=\"wp-image-81570 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-144.png 494w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-144-300x401.png 300w\" data-sizes=\"(max-width: 494px) 100vw, 494px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 494px; aspect-ratio: 494\/660;\" \/><\/figure>\n\n\n\n<p><em>Source:\u00a0MLfinlab\u00a0documentation<\/em>: https:\/\/mlfinlab.readthedocs.io\/en\/latest\/optimal_mean_reversion\/ou_model.html<\/p>\n\n\n\n<p><strong>Summary of Results &amp; Plotting<\/strong>&nbsp;<\/p>\n\n\n\n<p>The description function returns all the model parameters, optimal pair ratio, allocated trading costs, stop-loss level, along with the optimal levels.&nbsp;<\/p>\n\n\n\n<p>The&nbsp;plotl_levelsfunction&nbsp;illustrates the optimal exit and entry levels on the graph alongside with the given data&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"624\" height=\"466\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-145.png\" alt=\"\" class=\"wp-image-81572 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-145.png 624w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-145-300x224.png 300w\" data-sizes=\"(max-width: 624px) 100vw, 624px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 624px; aspect-ratio: 624\/466;\" \/><\/figure>\n\n\n\n<p>The documentation provides an example for instant implementation. The ETF pair (GLD, GDX) and dates are chosen to coincide with that in the book, but one can change them easily.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"624\" height=\"770\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-146.png\" alt=\"\" class=\"wp-image-81576 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-146.png 624w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/image-146-300x370.png 300w\" data-sizes=\"(max-width: 624px) 100vw, 624px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 624px; aspect-ratio: 624\/770;\" \/><\/figure>\n\n\n\n<p><em>Source:\u00a0MLfinlab\u00a0documentation<\/em>: https:\/\/mlfinlab.readthedocs.io\/en\/latest\/optimal_mean_reversion\/ou_model.html<\/p>\n\n\n\n<p>For additional pairs trading examples based on the approach presented above, with real data and performance summary, we refer to this\u00a0<a href=\"https:\/\/towardsdatascience.com\/pairs-trading-with-optimized-positions-exit-rules-811fa0c6be91\" target=\"_blank\" rel=\"noreferrer noopener\">article on Towards Data Science<\/a>:\u00a0<\/p>\n\n\n\n<p><strong>References:<\/strong>&nbsp;<\/p>\n\n\n\n<p>T. Leung T. and X. Li (2015),&nbsp;<a href=\"https:\/\/papers.ssrn.com\/sol3\/papers.cfm?abstract_id=2222196\" target=\"_blank\" rel=\"noreferrer noopener\">Optimal Mean Reversion Trading with Transaction Costs &amp; Stop-Loss Exit<\/a>, International Journal of Theoretical &amp; Applied Finance, vol 18, issue 3, p.1550020. \u2190 Python package is developed based on this paper.&nbsp;<\/p>\n\n\n\n<p>D. Lee and T. Leung (2020), On the Efficacy of Optimized Exit Rule for Mean Reversion Trading, International Journal of Financial  Engineering.&nbsp; <a href=\"https:\/\/doi.org\/10.1142\/S2424786320500243\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/doi.org\/10.1142\/S2424786320500243<\/a>&nbsp; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how the Python package called Machine Learning Financial Laboratory (mlfinlab) is designed to find ways for the optimal trading strategies (entry &#038; exit price thresholds) when the underlying assets\/portfolios have mean-reverting price dynamics.<\/p>\n","protected":false},"author":189,"featured_media":81558,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338,341,352,344],"tags":[1226,9459,8212,9457,9456,8445,595,4939],"contributors-categories":[13668],"class_list":{"0":"post-81554","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"category-quant-development","12":"category-quant-north-america","13":"category-quant-regions","14":"tag-financial-engineering","15":"tag-mean-reverting-ornstein-uhlenbeck-ou-process","16":"tag-mlfinlab","17":"tag-model-fitting","18":"tag-optimal-mean-reversion-trading","19":"tag-pairs-trading","20":"tag-python","21":"tag-statistics","22":"contributors-categories-computational-finance-risk-management-university-of-washington"},"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.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>A Python Package for Optimal Mean Reversion Trading<\/title>\n<meta name=\"description\" content=\"Learn how the Python package called Machine Learning Financial Laboratory (mlfinlab) is designed to find ways for the optimal trading strategies (entry...\" \/>\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\/81554\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Python Package for Optimal Mean Reversion Trading | IBKR Quant Blog\" \/>\n<meta property=\"og:description\" content=\"Learn how the Python package called Machine Learning Financial Laboratory (mlfinlab) is designed to find ways for the optimal trading strategies (entry &amp; exit price thresholds) when the underlying assets\/portfolios have mean-reverting price dynamics.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2021-03-29T17:40:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-14T19:13:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-world.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Tim Leung\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tim Leung\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 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\\\/a-python-package-for-optimal-mean-reversion-trading\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-python-package-for-optimal-mean-reversion-trading\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Tim Leung\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/63efb8a271eea609385bade5cbb12b7b\"\n\t            },\n\t            \"headline\": \"A Python Package for Optimal Mean Reversion Trading\",\n\t            \"datePublished\": \"2021-03-29T17:40:52+00:00\",\n\t            \"dateModified\": \"2023-03-14T19:13:38+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-python-package-for-optimal-mean-reversion-trading\\\/\"\n\t            },\n\t            \"wordCount\": 551,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-python-package-for-optimal-mean-reversion-trading\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/python-world.jpg\",\n\t            \"keywords\": [\n\t                \"Financial Engineering\",\n\t                \"mean-reverting Ornstein-Uhlenbeck (OU) process\",\n\t                \"MlFinLab\",\n\t                \"Model Fitting\",\n\t                \"Optimal Mean Reversion Trading\",\n\t                \"Pairs Trading\",\n\t                \"Python\",\n\t                \"statistics\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"Quant North America\",\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\\\/a-python-package-for-optimal-mean-reversion-trading\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-python-package-for-optimal-mean-reversion-trading\\\/\",\n\t            \"name\": \"A Python Package for Optimal Mean Reversion Trading | IBKR Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-python-package-for-optimal-mean-reversion-trading\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-python-package-for-optimal-mean-reversion-trading\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/python-world.jpg\",\n\t            \"datePublished\": \"2021-03-29T17:40:52+00:00\",\n\t            \"dateModified\": \"2023-03-14T19:13:38+00:00\",\n\t            \"description\": \"Learn how the Python package called Machine Learning Financial Laboratory (mlfinlab) is designed to find ways for the optimal trading strategies (entry & exit price thresholds) when the underlying assets\\\/portfolios have mean-reverting price dynamics.\",\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\\\/a-python-package-for-optimal-mean-reversion-trading\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-python-package-for-optimal-mean-reversion-trading\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/python-world.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/python-world.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Python\"\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\\\/63efb8a271eea609385bade5cbb12b7b\",\n\t            \"name\": \"Tim Leung\",\n\t            \"description\": \"Prof. Tim Leung is the Director, Computational Finance &amp; Risk Management (CFRM) Program https:\\\/\\\/faculty.washington.edu\\\/timleung\\\/\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/faculty.washington.edu\\\/timleung\\\/\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/timleung\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"A Python Package for Optimal Mean Reversion Trading","description":"Learn how the Python package called Machine Learning Financial Laboratory (mlfinlab) is designed to find ways for the optimal trading strategies (entry...","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\/81554\/","og_locale":"en_US","og_type":"article","og_title":"A Python Package for Optimal Mean Reversion Trading | IBKR Quant Blog","og_description":"Learn how the Python package called Machine Learning Financial Laboratory (mlfinlab) is designed to find ways for the optimal trading strategies (entry & exit price thresholds) when the underlying assets\/portfolios have mean-reverting price dynamics.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/","og_site_name":"IBKR Campus US","article_published_time":"2021-03-29T17:40:52+00:00","article_modified_time":"2023-03-14T19:13:38+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-world.jpg","type":"image\/jpeg"}],"author":"Tim Leung","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tim Leung","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/"},"author":{"name":"Tim Leung","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/63efb8a271eea609385bade5cbb12b7b"},"headline":"A Python Package for Optimal Mean Reversion Trading","datePublished":"2021-03-29T17:40:52+00:00","dateModified":"2023-03-14T19:13:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/"},"wordCount":551,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-world.jpg","keywords":["Financial Engineering","mean-reverting Ornstein-Uhlenbeck (OU) process","MlFinLab","Model Fitting","Optimal Mean Reversion Trading","Pairs Trading","Python","statistics"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development","Quant North America","Quant Regions"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/","name":"A Python Package for Optimal Mean Reversion Trading | IBKR Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-world.jpg","datePublished":"2021-03-29T17:40:52+00:00","dateModified":"2023-03-14T19:13:38+00:00","description":"Learn how the Python package called Machine Learning Financial Laboratory (mlfinlab) is designed to find ways for the optimal trading strategies (entry & exit price thresholds) when the underlying assets\/portfolios have mean-reverting price dynamics.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-python-package-for-optimal-mean-reversion-trading\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-world.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-world.jpg","width":1000,"height":563,"caption":"Python"},{"@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\/63efb8a271eea609385bade5cbb12b7b","name":"Tim Leung","description":"Prof. Tim Leung is the Director, Computational Finance &amp; Risk Management (CFRM) Program https:\/\/faculty.washington.edu\/timleung\/","sameAs":["https:\/\/faculty.washington.edu\/timleung\/"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/timleung\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-world.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/81554","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\/189"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=81554"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/81554\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/81558"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=81554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=81554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=81554"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=81554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}