- Solve real problems with our hands-on interface
- Progress from basic puts and calls to advanced strategies
Lesson of
import time
import threading
from datetime import datetime
from typing import Dict, Optional
import pandas as pd
import warnings
warnings.filterwarnings("ignore")from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.contract import Contract from ibapi.order import Order from ibapi.common import BarData
def donchian_channel(df: pd.DataFrame, period: int = 30) -> pd.DataFrame:
"""
Calculate the Donchian Channel for a given DataFrame.
The Donchian Channel is a trend-following indicator that plots the highest high and
the lowest low over a specified period, along with a middle line that is the average
of the upper and lower bands. This indicator is often used to identify breakouts
and determine potential trading signals.
Parameters
----------
df : pandas.DataFrame
A DataFrame containing at least the following columns:
- 'high': The high prices of the asset.
- 'low': The low prices of the asset.
- 'close': The closing prices of the asset (not used directly in calculations but
generally present in the data).
period : int, optional
The number of periods to calculate the channel. Default is 30.
This period determines the look-back window for the highest high
and the lowest low.
Returns
-------
pandas.DataFrame
The original DataFrame with three additional columns:
- 'upper': The upper band of the Donchian Channel (highest high over the period).
- 'lower': The lower band of the Donchian Channel (lowest low over the period).
- 'mid': The middle line, which is the average of the upper and lower bands.
Examples
--------
>>> data = {
... 'high': [10, 12, 13, 14, 15, 13, 11, 10, 12, 14],
... 'low': [8, 7, 6, 5, 8, 7, 6, 5, 6, 7],
... 'close': [9, 10, 12, 13, 14, 12, 10, 9, 11, 13]
... }
>>> df = pd.DataFrame(data)
>>> donchian_channel(df, period=5)
high low close upper lower mid
0 10 8 9.0 NaN NaN NaN
1 12 7 10.0 NaN NaN NaN
2 13 6 12.0 NaN NaN NaN
3 14 5 13.0 NaN NaN NaN
4 15 8 14.0 15.0 5.0 10.0
5 13 7 12.0 15.0 5.0 10.0
6 11 6 10.0 15.0 5.0 10.0
7 10 5 9.0 15.0 5.0 10.0
8 12 6 11.0 15.0 5.0 10.0
9 14 7 13.0 14.0 5.0 9.5
"""
# Calculate the upper band (highest high over the period)
df["upper"] = df["high"].rolling(window=period).max()
# Calculate the lower band (lowest low over the period)
df["lower"] = df["low"].rolling(window=period).min()
# Optional: Calculate the middle line (average of upper and lower bands)
df["mid"] = (df["upper"] + df["lower"]) / 2
return dfThis is a third-party open-source library. It is not associated, supported, or managed by Interactive Brokers, completely independent. And if you’re using Pandas with your IB API or your trading apps, Interactive Brokers cannot help or offer support with Pandas specifically.
PyQuant News Site: https://www.pyquantnews.com/
For specific platform feedback and suggestions, please submit it directly to our team using these instructions.
If you have an account-specific question or concern, please reach out to Client Services.
We encourage you to look through our FAQs before posting. Your question may already be covered!
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from PyQuant News and is being posted with its permission. The views expressed in this material are solely those of the author and/or PyQuant News and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Throughout the lesson, please keep in mind that the examples discussed are purely for technical demonstration purposes, and do not constitute trading advice. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.
Hello, Isn’t course in French too.
Hello, thank you for reaching out. You can view our available courses in French on IBKR Campus by clicking Courses> French.