paramatrixtech / ksapi

Kotak Securities Trading APIs Integration
Apache License 2.0
51 stars 50 forks source link

Cant connect to Websocket #9

Closed PhoenixNK closed 2 years ago

PhoenixNK commented 2 years ago

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.

dhwajsinghal commented 2 years ago

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

PhoenixNK commented 2 years ago

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.

dhwajsinghal commented 2 years ago

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?

PhoenixNK commented 2 years ago

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.

dhwajsinghal commented 2 years ago

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