Idirect-Tech / Breeze-Python-SDK

The official Python client library for the ICICI Securities trading APIs
MIT License
53 stars 28 forks source link

i_click_2_gain not working. No messages received. #24

Closed rollycodes closed 12 months ago

rollycodes commented 1 year ago

I tried using the latest SDK v1.0.36

import os
from dotenv import load_dotenv
from breeze_connect import BreezeConnect
import socketio
import asyncio
import traceback
from logger import log

load_dotenv()

ICICI_API_KEY = os.getenv('ICICI_API_KEY')
ICICI_SECRET = os.getenv('ICICI_SECRET')
ICICI_SESSION = os.getenv('ICICI_SESSION')

class Breeze:
    _instance = None

    @classmethod
    async def get_instance(cls):
        if cls._instance is None:
            cls._instance = BreezeConnect(api_key=ICICI_API_KEY)
            cls._instance.generate_session(api_secret=ICICI_SECRET, session_token=ICICI_SESSION)
            cls._instance.ws_connect()

            # Assign the callbacks.
            cls._instance.on_ticks = cls.on_ticks

            sub_response = cls._instance.subscribe_feeds(stock_token = "i_click_2_gain")
            print(sub_response)
        return cls._instance

    # Callback to receive ticks.
    @classmethod
    def on_ticks(self, ticks):
        log('iclicktogain.json', "Ticks: {}".format(ticks))
        print("Ticks: {}".format(ticks))

Though sub_response returns {'message': 'Stock i_click_2_gain subscribed successfully'}, but on_ticks is never triggered for i_click_2_gain. If I use a stock code like 1.1!500780, then on_ticks is triggered for quotes as expected.

I then also tried to use barebones call using:

import base64 
import socketio
import http.client
import json

conn = http.client.HTTPSConnection("api.icicidirect.com")
payload = "{\"SessionToken\": \"XXXXXX\",\r\n    \"AppKey\": \"YYYYYYY\"\r\n}"
headers = {
            "Content-Type": "application/json"
        }
conn.request("GET", "/breezeapi/api/v1/customerdetails", payload, headers)
res = conn.getresponse()
data = res.read()
json_data = json.loads(data.decode("utf-8"))
session_key = json_data['Success']['session_token']
user_id, session_token = base64.b64decode(session_key.encode('ascii')).decode('ascii').split(":")

# Python Socket IO Client
sio = socketio.Client()
auth = {"user": user_id, "token": session_token}
sio.connect("https://livefeeds.icicidirect.com", headers={"User-Agent":"python-socketio[client]/socket"}, 
                auth=auth, transports="websocket", wait_timeout=3)

# Script Code of Stock or Instrument  e.g 4.1!1594, 1.1!500209 , 13.1!5023, 6.1!247457. 
script_code = ["i_click_2_gain"] #Subscribe more than one stock at a time

channel_name = 'stock'

#parsing logic 
def parse_data(data):
    return(data)

#CallBack functions to receive feeds
def on_ticks(ticks):
    ticks = parse_data(ticks)
    print("Received")
    print(ticks)

#Connect to receive feeds
sio.emit('join', script_code)
sio.on(channel_name, on_ticks)

But this also fails to print any incoming i_click_2_gain message.

PS: I tried with one_click_fno and it worked!

ShrikantOneture commented 1 year ago

Dear User, subscribe for entire day and check. since throughout the day 25 packets at max will be sent in any random time.

rollycodes commented 1 year ago

Hi @ShrikantOneture Thanks for the response. Will try as suggested.

But to confirm, can you please clarify on the 25 packets limit? Is this limit applicable only to i_click_2_gain subscription? Or limit of 25 per subscription? Or a total limit of 25 across all the subscriptions, including individual stock code?

ShrikantOneture commented 12 months ago

it depends on backend , on an average u can exepect 25 to 50 packets. Thanks