# The Kite Connect API Python client - v4
[](https://pypi.python.org/pypi/kiteconnect)
[](https://travis-ci.org/zerodhatech/pykiteconnect)
[](https://ci.appveyor.com/project/rainmattertech/pykiteconnect)
[](https://codecov.io/gh/zerodhatech/pykiteconnect/branch/kite3)
The official Python client for communicating with the [Kite Connect API](https://kite.trade).
Kite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (WebSockets), and more, with the simple HTTP API collection.
[Zerodha Technology](https://zerodha.com) (c) 2021. Licensed under the MIT License.
## Documentation
- [Python client documentation](https://kite.trade/docs/pykiteconnect/v4)
- [Kite Connect HTTP API documentation](https://kite.trade/docs/connect/v3)
## v4 - Breaking changes
- Renamed ticker fields as per [kite connect doc](https://kite.trade/docs/connect/v3/websocket/#quote-packet-structure)
- Renamed `bsecds` to `bcd` in `ticker.EXCHANGE_MAP`
## v5 - Breaking changes
- **Drop Support for Python 2.7**: Starting from version v5, support for Python 2.7 has been discontinued. This decision was made due to the [announcement](https://github.com/actions/setup-python/issues/672) by `setup-python`, which stopped supporting Python 2.x since May 2023.
- **For Python 2.x Users**: If you are using Python 2.x, you can continue using the `kiteconnect` library, but please stick to the <= 4.x.x versions of the library. You can find the previous releases on the [PyKiteConnect GitHub Releases](https://github.com/zerodha/pykiteconnect/releases) page.
## Installing the client
You can install the pre release via pip
```
pip install --upgrade kiteconnect
```
Its recommended to update `setuptools` to latest if you are facing any issue while installing
```
pip install -U pip setuptools
```
Since some of the dependencies uses C extensions it has to compiled before installing the package.
### Linux, BSD and macOS
- On Linux, and BSDs, you will need a C compiler (such as GCC).
#### Debian/Ubuntu
```
apt-get install libffi-dev python-dev python3-dev
```
#### Centos/RHEL/Fedora
```
yum install libffi-devel python3-devel python-devel
```
#### macOS/OSx
```
xcode-select --install
```
### Microsoft Windows
Each Python version uses a specific compiler version (e.g. CPython 2.7 uses Visual C++ 9.0, CPython 3.3 uses Visual C++ 10.0, etc). So, you need to install the compiler version that corresponds to your Python version
- Python 2.6, 2.7, 3.0, 3.1, 3.2 - [Microsoft Visual C++ 9.0](https://wiki.python.org/moin/WindowsCompilers#Microsoft_Visual_C.2B-.2B-_9.0_standalone:_Visual_C.2B-.2B-_Compiler_for_Python_2.7_.28x86.2C_x64.29)
- Python 3.3, 3.4 - [Microsoft Visual C++ 10.0](https://wiki.python.org/moin/WindowsCompilers#Microsoft_Visual_C.2B-.2B-_10.0_standalone:_Windows_SDK_7.1_.28x86.2C_x64.2C_ia64.29)
- Python 3.5, 3.6 - [Microsoft Visual C++ 14.0](https://wiki.python.org/moin/WindowsCompilers#Microsoft_Visual_C.2B-.2B-_14.0_standalone:_Visual_C.2B-.2B-_Build_Tools_2015_.28x86.2C_x64.2C_ARM.29)
For more details check [official Python documentation](https://wiki.python.org/moin/WindowsCompilers).
## API usage
```python
import logging
from kiteconnect import KiteConnect
logging.basicConfig(level=logging.DEBUG)
kite = KiteConnect(api_key="your_api_key")
# Redirect the user to the login url obtained
# from kite.login_url(), and receive the request_token
# from the registered redirect url after the login flow.
# Once you have the request_token, obtain the access_token
# as follows.
data = kite.generate_session("request_token_here", api_secret="your_secret")
kite.set_access_token(data["access_token"])
# Place an order
try:
order_id = kite.place_order(tradingsymbol="INFY",
exchange=kite.EXCHANGE_NSE,
transaction_type=kite.TRANSACTION_TYPE_BUY,
quantity=1,
variety=kite.VARIETY_AMO,
order_type=kite.ORDER_TYPE_MARKET,
product=kite.PRODUCT_CNC,
validity=kite.VALIDITY_DAY)
logging.info("Order placed. ID is: {}".format(order_id))
except Exception as e:
logging.info("Order placement failed: {}".format(e.message))
# Fetch all orders
kite.orders()
# Get instruments
kite.instruments()
# Place an mutual fund order
kite.place_mf_order(
tradingsymbol="INF090I01239",
transaction_type=kite.TRANSACTION_TYPE_BUY,
amount=5000,
tag="mytag"
)
# Cancel a mutual fund order
kite.cancel_mf_order(order_id="order_id")
# Get mutual fund instruments
kite.mf_instruments()
```
Refer to the [Python client documentation](https://kite.trade/docs/pykiteconnect/v4) for the complete list of supported methods.
## WebSocket usage
```python
import logging
from kiteconnect import KiteTicker
logging.basicConfig(level=logging.DEBUG)
# Initialise
kws = KiteTicker("your_api_key", "your_access_token")
def on_ticks(ws, ticks):
# Callback to receive ticks.
logging.debug("Ticks: {}".format(ticks))
def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe([738561, 5633])
# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, [738561])
def on_close(ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`
ws.stop()
# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()
```
## Run unit tests
```sh
python setup.py test
```
or
```sh
pytest -s tests/unit --cov-report html:cov_html --cov=./
```
## Run integration tests
```sh
pytest -s tests/integration/ --cov-report html:cov_html --cov=./ --api-key api_key --access-token access_token
```
## Generate documentation
```sh
pip install pdoc
pdoc --html --html-dir docs kiteconnect
```
## Changelog
[Check release notes](https://github.com/zerodha/pykiteconnect/releases)
Raw data
{
"_id": null,
"home_page": "https://kite.trade",
"name": "kiteconnect",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Zerodha Technology Pvt. Ltd. (India)",
"author_email": "talk@zerodha.tech",
"download_url": "https://files.pythonhosted.org/packages/43/f0/3b7999b20dcb7cb05d9715d8874e40d093acc432dc4243e932b7cb75ee7f/kiteconnect-5.0.1.tar.gz",
"platform": null,
"description": "# The Kite Connect API Python client - v4\n\n[](https://pypi.python.org/pypi/kiteconnect)\n[](https://travis-ci.org/zerodhatech/pykiteconnect)\n[](https://ci.appveyor.com/project/rainmattertech/pykiteconnect)\n[](https://codecov.io/gh/zerodhatech/pykiteconnect/branch/kite3)\n\nThe official Python client for communicating with the [Kite Connect API](https://kite.trade).\n\nKite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (WebSockets), and more, with the simple HTTP API collection.\n\n[Zerodha Technology](https://zerodha.com) (c) 2021. Licensed under the MIT License.\n\n## Documentation\n\n- [Python client documentation](https://kite.trade/docs/pykiteconnect/v4)\n- [Kite Connect HTTP API documentation](https://kite.trade/docs/connect/v3)\n\n## v4 - Breaking changes\n\n- Renamed ticker fields as per [kite connect doc](https://kite.trade/docs/connect/v3/websocket/#quote-packet-structure)\n- Renamed `bsecds` to `bcd` in `ticker.EXCHANGE_MAP`\n\n## v5 - Breaking changes\n\n- **Drop Support for Python 2.7**: Starting from version v5, support for Python 2.7 has been discontinued. This decision was made due to the [announcement](https://github.com/actions/setup-python/issues/672) by `setup-python`, which stopped supporting Python 2.x since May 2023.\n\n- **For Python 2.x Users**: If you are using Python 2.x, you can continue using the `kiteconnect` library, but please stick to the <= 4.x.x versions of the library. You can find the previous releases on the [PyKiteConnect GitHub Releases](https://github.com/zerodha/pykiteconnect/releases) page.\n\n## Installing the client\n\nYou can install the pre release via pip\n\n```\npip install --upgrade kiteconnect\n```\n\nIts recommended to update `setuptools` to latest if you are facing any issue while installing\n\n```\npip install -U pip setuptools\n```\n\nSince some of the dependencies uses C extensions it has to compiled before installing the package.\n\n### Linux, BSD and macOS\n\n- On Linux, and BSDs, you will need a C compiler (such as GCC).\n\n#### Debian/Ubuntu\n\n```\napt-get install libffi-dev python-dev python3-dev\n```\n\n#### Centos/RHEL/Fedora\n\n```\nyum install libffi-devel python3-devel python-devel\n```\n\n#### macOS/OSx\n\n```\nxcode-select --install\n```\n\n### Microsoft Windows\n\nEach Python version uses a specific compiler version (e.g. CPython 2.7 uses Visual C++ 9.0, CPython 3.3 uses Visual C++ 10.0, etc). So, you need to install the compiler version that corresponds to your Python version\n\n- Python 2.6, 2.7, 3.0, 3.1, 3.2 - [Microsoft Visual C++ 9.0](https://wiki.python.org/moin/WindowsCompilers#Microsoft_Visual_C.2B-.2B-_9.0_standalone:_Visual_C.2B-.2B-_Compiler_for_Python_2.7_.28x86.2C_x64.29)\n- Python 3.3, 3.4 - [Microsoft Visual C++ 10.0](https://wiki.python.org/moin/WindowsCompilers#Microsoft_Visual_C.2B-.2B-_10.0_standalone:_Windows_SDK_7.1_.28x86.2C_x64.2C_ia64.29)\n- Python 3.5, 3.6 - [Microsoft Visual C++ 14.0](https://wiki.python.org/moin/WindowsCompilers#Microsoft_Visual_C.2B-.2B-_14.0_standalone:_Visual_C.2B-.2B-_Build_Tools_2015_.28x86.2C_x64.2C_ARM.29)\n\nFor more details check [official Python documentation](https://wiki.python.org/moin/WindowsCompilers).\n\n## API usage\n\n```python\nimport logging\nfrom kiteconnect import KiteConnect\n\nlogging.basicConfig(level=logging.DEBUG)\n\nkite = KiteConnect(api_key=\"your_api_key\")\n\n# Redirect the user to the login url obtained\n# from kite.login_url(), and receive the request_token\n# from the registered redirect url after the login flow.\n# Once you have the request_token, obtain the access_token\n# as follows.\n\ndata = kite.generate_session(\"request_token_here\", api_secret=\"your_secret\")\nkite.set_access_token(data[\"access_token\"])\n\n# Place an order\ntry:\n order_id = kite.place_order(tradingsymbol=\"INFY\",\n exchange=kite.EXCHANGE_NSE,\n transaction_type=kite.TRANSACTION_TYPE_BUY,\n quantity=1,\n variety=kite.VARIETY_AMO,\n order_type=kite.ORDER_TYPE_MARKET,\n product=kite.PRODUCT_CNC,\n validity=kite.VALIDITY_DAY)\n\n logging.info(\"Order placed. ID is: {}\".format(order_id))\nexcept Exception as e:\n logging.info(\"Order placement failed: {}\".format(e.message))\n\n# Fetch all orders\nkite.orders()\n\n# Get instruments\nkite.instruments()\n\n# Place an mutual fund order\nkite.place_mf_order(\n tradingsymbol=\"INF090I01239\",\n transaction_type=kite.TRANSACTION_TYPE_BUY,\n amount=5000,\n tag=\"mytag\"\n)\n\n# Cancel a mutual fund order\nkite.cancel_mf_order(order_id=\"order_id\")\n\n# Get mutual fund instruments\nkite.mf_instruments()\n```\n\nRefer to the [Python client documentation](https://kite.trade/docs/pykiteconnect/v4) for the complete list of supported methods.\n\n## WebSocket usage\n\n```python\nimport logging\nfrom kiteconnect import KiteTicker\n\nlogging.basicConfig(level=logging.DEBUG)\n\n# Initialise\nkws = KiteTicker(\"your_api_key\", \"your_access_token\")\n\ndef on_ticks(ws, ticks):\n # Callback to receive ticks.\n logging.debug(\"Ticks: {}\".format(ticks))\n\ndef on_connect(ws, response):\n # Callback on successful connect.\n # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).\n ws.subscribe([738561, 5633])\n\n # Set RELIANCE to tick in `full` mode.\n ws.set_mode(ws.MODE_FULL, [738561])\n\ndef on_close(ws, code, reason):\n # On connection close stop the main loop\n # Reconnection will not happen after executing `ws.stop()`\n ws.stop()\n\n# Assign the callbacks.\nkws.on_ticks = on_ticks\nkws.on_connect = on_connect\nkws.on_close = on_close\n\n# Infinite loop on the main thread. Nothing after this will run.\n# You have to use the pre-defined callbacks to manage subscriptions.\nkws.connect()\n```\n\n## Run unit tests\n\n```sh\npython setup.py test\n```\n\nor\n\n```sh\npytest -s tests/unit --cov-report html:cov_html --cov=./\n```\n\n## Run integration tests\n\n```sh\npytest -s tests/integration/ --cov-report html:cov_html --cov=./ --api-key api_key --access-token access_token\n```\n\n## Generate documentation\n\n```sh\npip install pdoc\n\npdoc --html --html-dir docs kiteconnect\n```\n\n## Changelog\n\n[Check release notes](https://github.com/zerodha/pykiteconnect/releases)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "The official Python client for the Kite Connect trading API",
"version": "5.0.1",
"project_urls": {
"Download": "https://github.com/zerodhatech/pykiteconnect",
"Homepage": "https://kite.trade"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4c5445fb78d0cf6650c74f9ab070147197bd37db8f92e6475e63be1ca837a4ff",
"md5": "21b5e38f2e9ede6e506623564cb36c80",
"sha256": "9362b950de8d3d3e9b49b93b1392113b3365a5a357caba6a67b9f20f41be0098"
},
"downloads": -1,
"filename": "kiteconnect-5.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "21b5e38f2e9ede6e506623564cb36c80",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 24986,
"upload_time": "2024-02-07T10:33:54",
"upload_time_iso_8601": "2024-02-07T10:33:54.742129Z",
"url": "https://files.pythonhosted.org/packages/4c/54/45fb78d0cf6650c74f9ab070147197bd37db8f92e6475e63be1ca837a4ff/kiteconnect-5.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "43f03b7999b20dcb7cb05d9715d8874e40d093acc432dc4243e932b7cb75ee7f",
"md5": "4adc074cc421479f0b28dbdfa48cc1ca",
"sha256": "e2a0eb0aafe38dd68c9eeafa095d794ea8046f3f30da44b0aca57ab8c176594b"
},
"downloads": -1,
"filename": "kiteconnect-5.0.1.tar.gz",
"has_sig": false,
"md5_digest": "4adc074cc421479f0b28dbdfa48cc1ca",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 26016,
"upload_time": "2024-02-07T10:33:56",
"upload_time_iso_8601": "2024-02-07T10:33:56.577459Z",
"url": "https://files.pythonhosted.org/packages/43/f0/3b7999b20dcb7cb05d9715d8874e40d093acc432dc4243e932b7cb75ee7f/kiteconnect-5.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-07 10:33:56",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "zerodhatech",
"github_project": "pykiteconnect",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "kiteconnect"
}