paramatrixtech / ksapi

Kotak Securities Trading APIs Integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cant connect to Websocket

PhoenixNK opened this issue · comments

2 days back I was able to get the ticks, but after updating the api, I am now getting below error.
Other error occurred: Expecting value: line 1 column 1 (char 0).

Pls suggest a solution to this.

Could you please share a snippet of the code youre running for the websocket?

Hi Dhwaj,
I used the documentation only to connect to websocket, nothing different. Only the thing I had to keep in mind while subscribing multiple tokens is, all tokens should be contained in a single string separated by comm "," for websocket.
Whereas if while using Quotes API, the tokens in the string has to be separated by hyphen "-".

Please see below code which i used:

from ks_api_client import ks_api
tokens = "745,754"
client = ks_api.KSTradeApi(access_token = access_token, userid = userid, consumer_key = consumer_key,ip = "127.0.0.1", app_id = app_id,
host = "https://tradeapi.kotaksecurities.com/apim", consumer_secret = consumer_secret)

try:
def callback_method(message):
print(message)
print("Your logic/computation will come here.")
# subscribe to the streamingAPI
client.subscribe(input_tokens=tokens, callback=callback_method, broadcast_host="https://wstreamer.kotaksecurities.com/feed")
except Exception as e:
print("Exception when calling StreamingApi->subscribe: %s\n" % e)

As I am facing difficulties formatting response data from websocket into dataframe, I stopped using this websocket.

Hope this may help you.

Couple of questions:
if left unformatted, is this script connecting and generating an output? or are you getting the error you stated earlier?

what error are you facing when trying to format it into a dataframe?

The script generates an output without any error. I used it to fetch option chain data. The output was one instrument details at once (most of the time) and that also without following the token in the subscribed token list. That way its difficult for me to arrange the latest quotes of each instrument in a single row of pandas dataframe.
If all the subscribed instrument quotes comes as a bundle (list within list or dictionary which is the case with other brokers) this becomes easy to iterate over each instrument and put the data in dataframe.
There could be some other ways to get the things done, but I am not a programmer.
If someone else could help us in doing so, we would appreciate his help.

I would be very much happy if the response structure of the Streaming API is made similar to the Quotes API.
The response of the Quotes API is something I like to have, but the Quotes API has limitation of number of requests per minute.

i see. I'll share your feedback with the developers. Meanwhile it might be useful to sort the output rows through your callback function using the instrument token and append to your respective dataframe accordingly.
for example, if you would prefer the output to be as a list within lists, you can make the necessary changes at the start of your callback function (which is executed the moment each output is generated) and then use that for your dataframe