I have developed and back-tested a strategy for NSE equities. In order to deploy the same, I need the following data at minute level granularity in real-time:
1. OHLC for the last minute 2. Volume traded in the last minute 3. percent change over the last 5 minutes
Could someone let me know the best way to get the above information at minute granularity in real time?
I am developing the framework in Python and have been able to get the latest quote information using kite.quote() method. In this regard, what is the maximum number of stocks that I can query at one point in time? I read in some thread that the limit is 500 stocks at once - please confirm the same.
OHLC data available in quote API call is for whole day, not for last minute.To get any minute OHLC data, you need to use Historical APIs, to fetch specific minute candle OHLC data.
Possibly two ways, Using Historical data APIs, get Open and close price for specific 5 minute candle and calculate change as (Close - Open)/Open. Secondly, you can make ltp API call exact at start and end of every minute , and store ltp value at your end(may be in DB),and calculate change over any period.
1. So, I will be able to query the historical Api in real time to get last minutes OHLC data?
2. Is it possible to query multiple instruments at the same time to the historical Api? I ask this because I need to get OHLC for multiple tickers for the last minute. If I have to query the historical Api again and again, it might increase the latency.
1. if you query historical data for 1 minute you will get data for previous trading day (and same trading day after 5-6 pm).
i see you need 1 minute data for live trading session.. so as suggested by @rakeshr you should "you can make ltp API call exact at start and end of every minute , and store ltp value at your end(may be in DB),and calculate change over any period."
2. yes you can query multiple instruments at the same time to the historical Api and as well as live api.
Thanks @Imran. Please find a couple of follow-up questions below:
2. yes you can query multiple instruments at the same time to the historical Api and as well as live api. >> Could you please provide an example of getting multiple instruments from historical API at the same time? >> It will be very helpful if you could point me to the ltp api >> I also need the volume traded in the last minute - I am guessing I can get this by quering the quote api and subtracting the cumulative volumes at the start and end of the minute?
hii @daniamarkumar 1. ws.subscribe([738561, 5633]) using this data can be retrived for multiple instument token (here RELIANCE, ACC )
2. syntax in point 1 will work for historical as well as live api.
3 . to get volume traded in last minute start your calculation at when exactly a minute starts: if tick_data['timestamp'].second%60==0 get the volume at that moment by using: tick_data['volume']) at the end of minute .....again get the volume subtract initial volume and final volume
1. So, I will be able to query the historical Api in real time to get last minutes OHLC data?
Yeah, you can get last minute candle OHLC in real time. Eg. You can use Python Datetime module, to get today date and current time. curl "https://api.kite.trade/instruments/historical/3375873/minute?from='today_date'&to='today_date or current_time'.
@daniamarkumar No, there is a restriction of max 1 instrument per request.You can run loop with list of instrument , to fetch multiple instrument data.
@rakeshr, I could do that but it will increase the latency of the strategy. I have to fetch the data for about 100 instruments every minute and place an order based on some calculations.
I read in the documentation that websockets are the least latency method to get realtime data. Using websockets, I can get upto 1000 tickers at once - I only have to maintain data structures locally to store information every minute.
Do you think the above would work? Could you also confirm if kite.quote() uses websockets internally?
Using websockets, I can get upto 1000 tickers at once - I only have to maintain data structures locally to store information every minute.
Yeah, this can be done, have look to this thread, for better understanding.
Could you also confirm if kite.quote() uses websockets internally?
Every quote call coming to kite connect is proxied to WebSocket and WebSocket has an in-memory data structure for all such type of calls.So, yeah all quote call make use of websocket.
Secondly, you can make ltp API call exact at start and end of every minute , and store ltp value at your end(may be in DB),and calculate change over any period.
Two follow on questions:
1. So, I will be able to query the historical Api in real time to get last minutes OHLC data?
2. Is it possible to query multiple instruments at the same time to the historical Api? I ask this because I need to get OHLC for multiple tickers for the last minute. If I have to query the historical Api again and again, it might increase the latency.
Thanks,
Amar
1. if you query historical data for 1 minute you will get data for previous trading day (and same trading day after 5-6 pm).
i see you need 1 minute data for live trading session.. so as suggested by @rakeshr you should
"you can make ltp API call exact at start and end of every minute , and store ltp value at your end(may be in DB),and calculate change over any period."
2. yes you can query multiple instruments at the same time to the historical Api and as well as live api.
2. yes you can query multiple instruments at the same time to the historical Api and as well as live api.
>> Could you please provide an example of getting multiple instruments from historical API at the same time?
>> It will be very helpful if you could point me to the ltp api
>> I also need the volume traded in the last minute - I am guessing I can get this by quering the quote api and subtracting the cumulative volumes at the start and end of the minute?
Thanks,
Amar
1. ws.subscribe([738561, 5633])
using this data can be retrived for multiple instument token (here RELIANCE, ACC )
2. syntax in point 1 will work for historical as well as live api.
3 . to get volume traded in last minute
start your calculation at when exactly a minute starts:
if tick_data['timestamp'].second%60==0
get the volume at that moment by using:
tick_data['volume'])
at the end of minute .....again get the volume
subtract initial volume and final volume
Eg.
You can use Python Datetime module, to get today date and current time.
curl "https://api.kite.trade/instruments/historical/3375873/minute?from='today_date'&to='today_date or current_time'
.@rakeshr , Can we retrieve data for multiple instruments at the same time using the code that you have shared?
I am using the following code snippet to get historical data:
kite.set_access_token(access_token)
#EDL testing
req = "https://api.kite.trade/instruments/historical/4569857/minute?from=2018-01-03&to=2018-01-03&api_key=" + api_key + "&access_token=" + access_token
response = requests.get(req)
data_ticker = response.json()['data']['candles']
How do I pass multiple instruments in this request?
@Imran
Can you provide the complete code snippet for getting multiple instrument data - I am using the historical api like above.
Thanks,
Amar
No, there is a restriction of max 1 instrument per request.You can run loop with list of instrument , to fetch multiple instrument data.
I read in the documentation that websockets are the least latency method to get realtime data. Using websockets, I can get upto 1000 tickers at once - I only have to maintain data structures locally to store information every minute.
Do you think the above would work?
Could you also confirm if kite.quote() uses websockets internally?
Thanks,
Amar
I will try this and ping again if I run into roadblocks.
Thanks,
Amar