Marketfeed api - sample python code error

Hello @haricharan

Welcome to Dhan community! Glad to have you get started on Market Feed.

This error is primarily in case where Jupyter notebooks do not allow for multiple loops at once and we don’t have full control over the event loop. You can handle this using asyncio, adding reference for you here:

import nest_asyncio
nest_asyncio.apply()

import marketfeed
import asyncio

client_id = "Client ID"
access_token = "Access Token"

instruments = [(1, "1333"),(0,"13")]
subscription_code = marketfeed.Ticker

# Usage Example
async def on_connect(instance):
    print("Connected to websocket")

async def on_message(instance, message):
    print("Received:", message)

print("Subscription code :", subscription_code)
feed = marketfeed.DhanFeed(client_id, access_token, instruments, subscription_code,
                    on_connect=on_connect, on_message=on_message)

feed.run_forever()

asyncio.create_task(feed.run_forever())

Let me know if this helps! Would love to have your feedback here as well.