InputException while receiving Historical Market Data

debon1410
debon1410 edited July 2020 in Java client
Hi All,
I am new to Zerodha API - I was trying to fetch historical data - and receiving the below error :
Exception in thread "main" com.zerodhatech.kiteconnect.kitehttp.exceptions.InputException
at com.zerodhatech.kiteconnect.kitehttp.KiteResponseHandler.dealWithException(KiteResponseHandler.java:54)
at com.zerodhatech.kiteconnect.kitehttp.KiteResponseHandler.handle(KiteResponseHandler.java:20)
at com.zerodhatech.kiteconnect.kitehttp.KiteRequestHandler.getRequest(KiteRequestHandler.java:69)
at com.zerodhatech.kiteconnect.KiteConnect.getHistoricalData(KiteConnect.java:732)
at Examples.getHistoricalData(Examples.java:68)
at userConnect.main(userConnect.java:50)
-------------------------------------------------------------------------------------------------
Below is my code (Copied from GitHub Example):
  /** Get historical data for an instrument.*/
public void getHistoricalData(KiteConnect kiteConnect) throws KiteException, IOException {
/** Get historical data dump, requires from and to date, intrument token, interval, continuous (for expired F&O contracts), oi (open interest)
* returns historical data object which will have list of historical data inside the object.*/
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date from = new Date();
Date to = new Date();
try {
from = formatter.parse("2019-09-20 09:15:00");
to = formatter.parse("2019-09-20 15:30:00");
}catch (ParseException e) {
e.printStackTrace();
}
HistoricalData historicalData = kiteConnect.getHistoricalData(from, to, "54872327", "15minute", false, true);
System.out.println(historicalData.dataArrayList.size());
System.out.println(historicalData.dataArrayList.get(0).volume);
System.out.println(historicalData.dataArrayList.get(historicalData.dataArrayList.size() - 1).volume);
System.out.println(historicalData.dataArrayList.get(0).oi);
}

-------------------------------------------------------------------------------------------------------------
Note : I am Subscribed to Historical Data -



Can someone help.

  • sujith
    One has to re-login after subscribing to historical data, in order to use it.
  • debon1410
    I Subscribed yesterday for the data - Today I have logged out and in multiple time.

    How to test if my subscription is working or not. Please help.
  • sujith
    Your subscription seems fine. Can you run in debug mode and paste the complete stack trace here?
    Make sure to remove app and client-specific tokens.
  • debon1410
    below is the logs - I masked the authorization token
    -----------------------------------------------------------------------
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: --> GET https://api.kite.trade/instruments/historical/54872327/15minute?continuous=0&from=2019-09-20 09:15:00&oi=1&to=2019-09-20 15:30:00
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: User-Agent: javakiteconnect/3.0.0
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: X-Kite-Version: 3
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: Authorization: token xxxxxxxxxxxxx:xxxxxxxxxxxxxxxx
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: --> END GET
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: <-- 400 https://api.kite.trade/instruments/historical/54872327/15minute?continuous=0&amp;from=2019-09-20 09:15:00&amp;oi=1&amp;to=2019-09-20 15:30:00 (75ms)
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: date: Mon, 27 Jul 2020 10:22:59 GMT
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: content-type: application/json
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: content-length: 91
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: set-cookie: __cfduid=d0b544e640bb2892d8a23483ee2f638211595845379; expires=Wed, 26-Aug-20 10:22:59 GMT; path=/; domain=.kite.trade; HttpOnly; SameSite=Lax
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: cf-cache-status: DYNAMIC
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: cf-request-id: 0431651beb0000899d3c254200000001
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: strict-transport-security: max-age=15552000; includeSubDomains
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: server: cloudflare
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: cf-ray: 5b95a473190f899d-MAA
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO:
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: {"status":"error","message":"contract not found","data":null,"error_type":"InputException"}
    Jul 27, 2020 3:52:58 PM okhttp3.internal.platform.Platform log
    INFO: <-- END HTTP (91-byte body)
    Exception in thread "main" com.zerodhatech.kiteconnect.kitehttp.exceptions.InputException
    at com.zerodhatech.kiteconnect.kitehttp.KiteResponseHandler.dealWithException(KiteResponseHandler.java:54)
    at com.zerodhatech.kiteconnect.kitehttp.KiteResponseHandler.handle(KiteResponseHandler.java:20)
    at com.zerodhatech.kiteconnect.kitehttp.KiteRequestHandler.getRequest(KiteRequestHandler.java:69)
    at com.zerodhatech.kiteconnect.KiteConnect.getHistoricalData(KiteConnect.java:732)
    at Examples.getHistoricalData(Examples.java:81)
    at userConnect.main(userConnect.java:51)
    -----------------------------------------------------------------------------------------------------------
    Hope this helps
  • sujith
    contract not found As the error message says, you seem to be fetching data for an invalid instrument.
  • debon1410
    debon1410 edited July 2020
    Can you please let me know which are valid instruments - I tried "RELIANCE", "NSE:RELIANCE", "NSE-RELIANCE" - all are giving same error . I am able to fetch OHLC data with "NSE:RELIANCE " - is naming convention of historical data is different. Where do I find a list?
  • rakeshr
    @debon1410
    You need to use instrument_token not symbol for historical data fetch. Check all input param detail for Historical data APIs here.
    You can get complete instrument dump from here.
  • gautamsatpathy
    @rakeshr @sujith

    I am having problems with the Historical Data API. Just started testing it today. And I have access to it and accessing it using the Instrument Token retrieved from the Kite Connect Instrument API. There should not be any problems on that front. But something is off.


    private void testHistoricalData( KiteConnect pKiteConnect, int year, int month, int day, String period ) {
    ZoneOffset offset = ZoneOffset.of( "+05:30" ) ;
    long token = 738561 ; // RELIANCE, NSE, EQ
    OffsetDateTime from = OffsetDateTime.of(
    LocalDateTime.of(year, month, day, 9, 15),
    offset ) ;
    OffsetDateTime to = OffsetDateTime.now( offset ) ;
    try {
    Date fromDate = new Date(from.toInstant().toEpochMilli()) ;
    Date toDate = new Date(to.toInstant().toEpochMilli()) ;
    logger.info( "Period: " + period + ", From: " + fromDate + " to: " + toDate ) ;
    HistoricalData historicalData = pKiteConnect.getHistoricalData(
    fromDate, toDate,
    String.valueOf(token),
    period, false, false ) ;
    List<HistoricalData> dataList = historicalData.dataArrayList ;
    int numData = dataList.size() ;
    logger.info( "Received " + numData + " bhavs for " + token ) ;
    logger.info( "First: " + (numData > 0 ? dataList.get(0).timeStamp : "NO DATA") ) ;
    logger.info( "Last: " + (numData > 0 ? dataList.get(numData - 1).timeStamp : "NO DATA") ) ;
    }
    catch ( KiteException | IOException pE ) {
    logger.error( "Exception while getting Historical Data for " + token, pE );
    }
    }


    Calling this function with:

    testHistoricalData( trader.getKiteConnect(), 2016, 1, 7, "day" ) ;

    gives me:

    [22:49:51.792 [main] INFO satpathy.charanna.Tester (134) (testHistoricalData)] Period: day, From: Thu Jan 07 09:15:00 IST 2016 to: Tue Jun 29 22:49:51 IST 2021
    21/06/29 22:49:51 INFO kiteconnect.KiteConnect: Historical Data URL: https://api.kite.trade/instruments/historical/738561/day
    [22:49:52.536 [main] INFO satpathy.charanna.Tester (141) (testHistoricalData)] Received 1354 bhavs for 738561
    [22:49:52.536 [main] INFO satpathy.charanna.Tester (142) (testHistoricalData)] First: 2016-01-08T00:00:00+0530
    [22:49:52.537 [main] INFO satpathy.charanna.Tester (143) (testHistoricalData)] Last: 2021-06-29T00:00:00+0530

    BUT, calling with
    testHistoricalData( trader.getKiteConnect(), 2016, 1, 6, "day" ) ;

    Gives me:


    [22:55:43.056 [main] INFO satpathy.charanna.Tester (134) (testHistoricalData)] Period: day, From: Wed Jan 06 09:15:00 IST 2016 to: Tue Jun 29 22:55:43 IST 2021
    21/06/29 22:55:43 INFO kiteconnect.KiteConnect: Historical Data URL: https://api.kite.trade/instruments/historical/738561/day
    [22:55:43.259 [main] ERROR satpathy.charanna.Tester (146) (testHistoricalData)] Exception while getting Historical Data for 738561
    com.zerodhatech.kiteconnect.kitehttp.exceptions.InputException: null
    at com.zerodhatech.kiteconnect.kitehttp.KiteResponseHandler.dealWithException(KiteResponseHandler.java:54) ~[Charanna/:?]
    at com.zerodhatech.kiteconnect.kitehttp.KiteResponseHandler.handle(KiteResponseHandler.java:20) ~[Charanna/:?]
    at com.zerodhatech.kiteconnect.kitehttp.KiteRequestHandler.getRequest(KiteRequestHandler.java:70) ~[Charanna/:?]
    at com.zerodhatech.kiteconnect.KiteConnect.getHistoricalData(KiteConnect.java:841) ~[Charanna/:?]
    at in.satpathy.charanna.Tester.testHistoricalData(Tester.java:135) [Charanna/:?]
    at in.satpathy.charanna.Tester.main(Tester.java:115) [Charanna/:?]


    And with 1 Minute data, I am not able to retrieve anything beyond the last 3 months!
  • gautamsatpathy
    Testing for 1 Minute Data on 2021.06.29:
    Called with 2021, 4, 30



    Called with 2021, 4, 29



    So, you have only 3 months of 1 Minute Data for NSE Stocks? I am calling for Token "738561"

    The screen shot above is from the CSV file retrieved from the Kite Connect Instrument API - retrieved today.
  • gautamsatpathy
    Two issues here:

    1. The Historical Data API is returning only 3 months of 1 Minute Candles
    2. It is throwing an error if a date prior to 3 months is given as input instead of just returning the data that is actually present.

    Or, am I missing something here?
  • gautamsatpathy
    @rakeshr @sujith Is there a limit of 60 (Calendar) Day limit set on the 1 Minute Historical Data API? Are there similar limits on the other time frames?

    If there are, then this call will only work for a 60 day difference between "From" and "To". I have to test this. Just thought of this and haven't tested it yet.
  • gautamsatpathy
    @rakeshr @sujith Confirmed that the 1 Minute Historical Data API has a 60 Day Limit set.

    When called with a 61 day window:




    And when called with a 60 day window,



    Is this documented somewhere? What are the limits on the other time frames?
    For 1 Minute time frame, 60 Calendar Day, and in this case (from = 2015.1.1, to = from + 60 days), I got 7501 Bars. So, is the limit based on calendar days or number of bars? If these aren't documented then would you please document them for every time frame?
  • rakeshr
    Is this documented somewhere? What are the limits on the other time frames?
    Yes, you can go through this thread. You can visit here for all historical data FAQs.
  • gautamsatpathy
    Thanks @rakeshr

    I had gone through those FAQs and lost this piece in all the clutter.

    Why is Zerodha's documentation in such poor state? No self respecting professional setup can be directing users to random Forum posts, even when they are organized into a FAQ page.

    Please tell Nithin that he is dealing with traders who are software developers with a product like Kite Connect. We are used to a different standard of professionalism when it comes to products and their documentation.
  • gautamsatpathy
    And @rakeshr , the very fact that you have these FAQ pages in your Developer's forum shows that you are depending on a discovery process by your users to document these hidden gems that rightly belong to the core documentation!

    You are wasting our time! The most precious thing we have.
Sign In or Register to comment.