Fetching Live Market Data using Quote API

Raunaak
Raunaak edited February 2018 in Python client
Hi everyone,
I am new to Zerodha and its API. My aim is to fetch live streaming market data (its depth/orderbook and ohlc values) for Nifty and BankNifty on 1 minute interval in python 2.7. After going through documentation, I found out about "quote" function which I think would solve my task to retrieve market data. After setting access token, I wrote the following line in python:
kite.quote(exchange='NSE',tradingsymbol='NIFTY')

It displays the following error:
Traceback (most recent call last):
File "C:/Users/HP/PycharmProjects/AutomatedTrading/main.py", line 31, in
kite.quote(exchange='NSE',tradingsymbol='NIFTY')
File "C:\Users\HP\Miniconda2\lib\site-packages\kiteconnect\__init__.py", line 501, in quote
return self._get("market.quote", {"exchange": exchange, "tradingsymbol": tradingsymbol})
File "C:\Users\HP\Miniconda2\lib\site-packages\kiteconnect\__init__.py", line 593, in _get
return self._request(route, "GET", params)
File "C:\Users\HP\Miniconda2\lib\site-packages\kiteconnect\__init__.py", line 684, in _request
raise(exp(data["message"], code=r.status_code))
kiteconnect.exceptions.DataException: Exception fetching quote

Please help to fetch live streaming market data on 1 minute interval. Also, my kiteconnect version is 3.6.2. Thanks!!
  • sujith
    The params you are sending is wrong. You can check out python documentation to know how to pass parameter.
    It needs to be an array of exchange:tradingsymbol
  • Raunaak
    Raunaak edited February 2018
    Hi @Sujith,

    Thanks for the response.
    1. I passed parameters as below:
    kite.quote(['NSE:NIFTY'])
    Now, it says that function needs 3 arguments:
    TypeError: quote() takes exactly 3 arguments (2 given)
    What am I doing wrong here?

    2. I need to get data in intervals of 1 minute. How to mention it in code?
  • zartimus
    @Raunaak Please update to the new kiteconnect version 3.7.0b5 as mentioned here. https://github.com/zerodhatech/pykiteconnect/tree/kite3#installing-the-client
    pip show kiteconnect -> will show you the version number

    https://github.com/zerodhatech/pykiteconnect/blob/kite3/kiteconnect/connect.py#L530-L543
    is written in such a way that even if you pass instruments as a list or just arguments it should work
  • tonystark
    You are using old version of library. Try running pip install kiteconnect --upgrade --pre.

    You can create a thread that fetches data and then sleep for 60 seconds.
  • Raunaak
    Hi @zartimus and @tonystark,
    To upgrade to 3.7.0b5, I ran pip install kiteconnect --upgrade --pre.
    It threw exception shown below:
    Exception:
    Traceback (most recent call last):
    File "C:\Users\HP\Miniconda2\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
    File "C:\Users\HP\Miniconda2\lib\site-packages\pip\commands\install.py", line 342, in run
    prefix=options.prefix_path,
    File "C:\Users\HP\Miniconda2\lib\site-packages\pip\req\req_set.py", line 784, in install
    **kwargs
    File "C:\Users\HP\Miniconda2\lib\site-packages\pip\req\req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
    File "C:\Users\HP\Miniconda2\lib\site-packages\pip\req\req_install.py", line 1064, in move_wheel_f
    iles
    isolated=self.isolated,
    File "C:\Users\HP\Miniconda2\lib\site-packages\pip\wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
    File "C:\Users\HP\Miniconda2\lib\site-packages\pip\wheel.py", line 323, in clobber
    shutil.copyfile(srcfile, destfile)
    File "C:\Users\HP\Miniconda2\lib\shutil.py", line 97, in copyfile
    with open(dst, 'wb') as fdst:
    IOError: [Errno 13] Permission denied: 'C:\\Users\\HP\\Miniconda2\\Lib\\site-packages\\_cffi_backend
    .pyd'


    How to go past it? Thanks.
  • zartimus
    zartimus edited February 2018
    @Raunaak Please run your command prompt/ PS as administrator.
    If it does not work. Try running pip as a python module
    python -m pip install kiteconnect --upgrade --pre
  • Raunaak
    Raunaak edited February 2018
    Hi @zartimus,

    I followed commands. version shows 3.6.2 when I run command pip show kiteconnect. version shows 3.7.05b when I run command python -m pip show kiteconnect. Now, I am not sure which version am I using?

    After importing from kiteconnect import KiteConnect
    and setting keys, when I run kite.quote(['NSE:NIFTY']).
    It shows the same error TypeError: quote() takes exactly 3 arguments (2 given).
  • zartimus
    zartimus edited February 2018
    @Raunaak I tried with miniconda2 and works fine. here is what i followed
    PS>conda -V
    conda 4.4.10

    PS>conda update conda
    Solving environment: done

    # All requested packages already installed.

    PS>conda create -n kiteenv

    PS>activate kiteenv

    PS>pip install kiteconnect --upgrade --pre
    Installing collected packages: kiteconnect
    Successfully installed kiteconnect-3.7.0b5

    PS>pip show kiteconnect
    Name: kiteconnect
    Version: 3.7.0b5
    Summary: The official Python client for the Kite Connect trading API
    Home-page: https://kite.trade
    Author: Zerodha Technology Pvt ltd. (India)
    Author-email: [email protected]
    License: MIT
    Location: c:\users\tawny\miniconda2\lib\site-packages
    Requires: Twisted, autobahn, six, python-dateutil, enum34, service-identity, requests, pyOpenSSL

    PS>python
    Python 2.7.14 |Anaconda, Inc.| (default, Nov 8 2017, 13:40:45) [MSC v.1500 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from kiteconnect import KiteConnect
    >>> client = KiteConnect("API-KEY", "ACCESS-TOKEN")
    >>> client.quote(['NSE:NIFTY'])
    {}
    >>> client.quote(['NSE:INFY'])
    {u'NSE:INFY': {u'oi': 0, u'timestamp': datetime.datetime(2018, 2, 23, 12, 10, 32), u'average_price': 1158.34, u'last_price': 1158.4, u'oi_day_low': 0, u'ohlc': {u'high': 1166, u'close': 1160.75, u'open': 1163.5, u'low': 1146.75}, u'oi_day_high': 0, u'volume': 2606296, u'depth': {u'sell': [{u'price': 1158.4, u'orders': 1, u'quantity': 24}, {u'price': 1158.45, u'orders': 4, u'quantity': 244}, {u'price': 1158.5, u'orders': 10, u'quantity': 2448}, {u'price': 1158.55, u'orders': 3, u'quantity': 20}, {u'price': 1158.6, u'orders': 8, u'quantity': 1453}], u'buy': [{u'price': 1158.15, u'orders': 6, u'quantity': 7}, {u'price': 1157.8, u'orders': 1, u'quantity': 1}, {u'price': 1157.7, u'orders': 1, u'quantity': 40}, {u'price': 1157.6, u'orders': 2, u'quantity': 27}, {u'price': 1157.55, u'orders': 1, u'quantity': 3}]}, u'instrument_token': 408065, u'last_trade_time': datetime.datetime(2018, 2, 23, 12, 10, 32), u'sell_quantity': 326410, u'buy_quantity': 210981, u'last_quantity': 1, u'net_change': 0}}

    Also pykiteconnect is tested against all versions of python and miniconda envs https://ci.appveyor.com/project/rainmattertech/pykiteconnect

    Hope it works
  • Raunaak
    Hi @zartimus , Thanks for the response.

    I followed the above commands. It throws the same exception as mentioned above. Before exception, cmd shows these lines:
    Found existing installation: cffi 1.10.0
    Uninstalling cffi-1.10.0:
    Successfully uninstalled cffi-1.10.0
    Rolling back uninstall of cffi


    I found a link on stackoverflow which may be help to solve this: https://stackoverflow.com/questions/45307110/pip-is-rolling-back-uninstall-of-setuptools

    If I am unable to upgrade to 3.7.05, then also 3.6.2 should have quote function and I might be able to use it?
  • Raunaak
    Raunaak edited February 2018
    Hi @zartimus Thanks for the help. I think some files were being used by kite and hence upgrade was not possible. When I restarted my pc, update worked nicely!!
    There is one small issue. I am trying to fetch values for NIFTY futures using quote. I use the following code:
    data = kite.quote(['NFO:NIFTY28MARFUT']). I recieve an empty dictionary whereas if I use data = kite.quote(['NSE:INFY']), it gives useful data. Could you please guide me to correct instrument for nifty futures?
  • prakash.jj
    It should be 18MARFUT
  • Raunaak
    Raunaak edited February 2018
    Hi @prakash.jj , you are absolutely right. 18 Mar gives useful data. But, as per my understanding, NIFTY futures expiry on the last Thursday of every month. And 29 Mar (Thursday) is a holiday. So, shouldn't trading symbol be NIFTY28MARFUT? I am assuming the appending date is the expiry date. What does 18 Mar signify?
  • sujith
    sujith edited February 2018
    @Raunaak,
    18 signifies the year of expiry.
  • Raunaak
    Thanks everyone for the help
This discussion has been closed.