{"id":215979,"date":"2024-12-04T16:46:55","date_gmt":"2024-12-04T21:46:55","guid":{"rendered":"https:\/\/ibkrcampus.com\/campus\/?post_type=trading-lessons&#038;p=215979"},"modified":"2025-09-29T13:58:04","modified_gmt":"2025-09-29T17:58:04","slug":"introduction-to-donchian-channel","status":"publish","type":"trading-lessons","link":"https:\/\/www.interactivebrokers.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/","title":{"rendered":"Introduction to Donchian Channel"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import time\nimport threading\nfrom datetime import datetime\nfrom typing import Dict, Optional\nimport pandas as pd\nimport warnings\nwarnings.filterwarnings(\"ignore\")<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from ibapi.client import EClient\nfrom ibapi.wrapper import EWrapper\nfrom ibapi.contract import Contract\nfrom ibapi.order import Order\nfrom ibapi.common import BarData<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def donchian_channel(df: pd.DataFrame, period: int = 30) -> pd.DataFrame:\n    \"\"\"\n    Calculate the Donchian Channel for a given DataFrame.\n\n    The Donchian Channel is a trend-following indicator that plots the highest high and \n    the lowest low over a specified period, along with a middle line that is the average \n    of the upper and lower bands. This indicator is often used to identify breakouts \n    and determine potential trading signals.\n\n    Parameters\n    ----------\n    df : pandas.DataFrame\n        A DataFrame containing at least the following columns:\n        - 'high': The high prices of the asset.\n        - 'low': The low prices of the asset.\n        - 'close': The closing prices of the asset (not used directly in calculations but \n          generally present in the data).\n          \n    period : int, optional\n        The number of periods to calculate the channel. Default is 30.\n        This period determines the look-back window for the highest high \n        and the lowest low.\n\n    Returns\n    -------\n    pandas.DataFrame\n        The original DataFrame with three additional columns:\n        - 'upper': The upper band of the Donchian Channel (highest high over the period).\n        - 'lower': The lower band of the Donchian Channel (lowest low over the period).\n        - 'mid': The middle line, which is the average of the upper and lower bands.\n\n    Examples\n    --------\n    >>> data = {\n    ...     'high': [10, 12, 13, 14, 15, 13, 11, 10, 12, 14],\n    ...     'low': [8, 7, 6, 5, 8, 7, 6, 5, 6, 7],\n    ...     'close': [9, 10, 12, 13, 14, 12, 10, 9, 11, 13]\n    ... }\n    >>> df = pd.DataFrame(data)\n    >>> donchian_channel(df, period=5)\n       high  low  close  upper  lower   mid\n    0    10    8    9.0    NaN    NaN   NaN\n    1    12    7   10.0    NaN    NaN   NaN\n    2    13    6   12.0    NaN    NaN   NaN\n    3    14    5   13.0    NaN    NaN   NaN\n    4    15    8   14.0   15.0    5.0  10.0\n    5    13    7   12.0   15.0    5.0  10.0\n    6    11    6   10.0   15.0    5.0  10.0\n    7    10    5    9.0   15.0    5.0  10.0\n    8    12    6   11.0   15.0    5.0  10.0\n    9    14    7   13.0   14.0    5.0   9.5\n    \"\"\"\n    # Calculate the upper band (highest high over the period)\n    df[\"upper\"] = df[\"high\"].rolling(window=period).max()\n\n    # Calculate the lower band (lowest low over the period)\n    df[\"lower\"] = df[\"low\"].rolling(window=period).min()\n    \n    # Optional: Calculate the middle line (average of upper and lower bands)\n    df[\"mid\"] = (df[\"upper\"] + df[\"lower\"]) \/ 2\n    \n    return df<\/pre>\n\n\n\n<p><strong><em>This is a third-party open-source library. It is not associated, supported, or managed by Interactive Brokers, completely independent. And if you&#8217;re using Pandas with your IB API or your trading apps, Interactive Brokers cannot help or offer support with Pandas specifically.<\/em><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>PyQuant News Site:\u00a0<a href=\"https:\/\/www.pyquantnews.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.pyquantnews.com\/<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This lesson is about using the Interactive Brokers API to identify breakouts using the Donchian Channel indicator. Jason introduces Panda\u2019s and the Jupyter Notebook environment, then explains the concept of type hinting in Python before diving into the Donchian Channel indicator.<\/p>\n","protected":false},"author":1087,"featured_media":215981,"parent":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":""},"contributors-categories":[17813],"traders-academy":[13117,13125],"class_list":{"0":"post-215979","1":"trading-lessons","2":"type-trading-lessons","3":"status-publish","4":"has-post-thumbnail","6":"contributors-categories-pyquantnews","7":"traders-academy-api","8":"traders-academy-beginner-trading"},"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>Archives | Traders&#039; Academy | IBKR Campus<\/title>\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\/trading-lessons\/215979\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to Donchian Channel | IBKR Campus US\" \/>\n<meta property=\"og:description\" content=\"This lesson is about using the Interactive Brokers API to identify breakouts using the Donchian Channel indicator. Jason introduces Panda\u2019s and the Jupyter Notebook environment, then explains the concept of type hinting in Python before diving into the Donchian Channel indicator.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-29T17:58:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/12\/DonChan-thumb-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/introduction-to-donchian-channel\\\/\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/introduction-to-donchian-channel\\\/\",\n\t            \"name\": \"Introduction to Donchian Channel | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/introduction-to-donchian-channel\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/introduction-to-donchian-channel\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/12\\\/DonChan-thumb-2.jpg\",\n\t            \"datePublished\": \"2024-12-04T21:46:55+00:00\",\n\t            \"dateModified\": \"2025-09-29T17:58:04+00:00\",\n\t            \"breadcrumb\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/introduction-to-donchian-channel\\\/#breadcrumb\"\n\t            },\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/introduction-to-donchian-channel\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/introduction-to-donchian-channel\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/12\\\/DonChan-thumb-2.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/12\\\/DonChan-thumb-2.jpg\",\n\t            \"width\": 1920,\n\t            \"height\": 1080,\n\t            \"caption\": \"Introduction to Donchian Channel\"\n\t        },\n\t        {\n\t            \"@type\": \"BreadcrumbList\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/introduction-to-donchian-channel\\\/#breadcrumb\",\n\t            \"itemListElement\": [\n\t                {\n\t                    \"@type\": \"ListItem\",\n\t                    \"position\": 1,\n\t                    \"name\": \"Academy Lessons\",\n\t                    \"item\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/trading-lessons\\\/\"\n\t                },\n\t                {\n\t                    \"@type\": \"ListItem\",\n\t                    \"position\": 2,\n\t                    \"name\": \"Introduction to Donchian Channel\"\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}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Archives | Traders' Academy | IBKR Campus","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\/trading-lessons\/215979\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to Donchian Channel | IBKR Campus US","og_description":"This lesson is about using the Interactive Brokers API to identify breakouts using the Donchian Channel indicator. Jason introduces Panda\u2019s and the Jupyter Notebook environment, then explains the concept of type hinting in Python before diving into the Donchian Channel indicator.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/","og_site_name":"IBKR Campus US","article_modified_time":"2025-09-29T17:58:04+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/12\/DonChan-thumb-2.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/ibkrcampus.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/","url":"https:\/\/ibkrcampus.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/","name":"Introduction to Donchian Channel | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/#primaryimage"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/12\/DonChan-thumb-2.jpg","datePublished":"2024-12-04T21:46:55+00:00","dateModified":"2025-09-29T17:58:04+00:00","breadcrumb":{"@id":"https:\/\/ibkrcampus.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ibkrcampus.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/12\/DonChan-thumb-2.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/12\/DonChan-thumb-2.jpg","width":1920,"height":1080,"caption":"Introduction to Donchian Channel"},{"@type":"BreadcrumbList","@id":"https:\/\/ibkrcampus.com\/campus\/trading-lessons\/introduction-to-donchian-channel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Academy Lessons","item":"https:\/\/ibkrcampus.com\/campus\/trading-lessons\/"},{"@type":"ListItem","position":2,"name":"Introduction to Donchian Channel"}]},{"@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\/"}]}},"_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/trading-lessons\/215979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/trading-lessons"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/trading-lessons"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/1087"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=215979"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/trading-lessons\/215979\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/215981"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=215979"}],"wp:term":[{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=215979"},{"taxonomy":"traders-academy","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/traders-academy?post=215979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}