Joby Ingram-Dodd
Joby Ingram-Dodd

Reputation: 730

Use Lightstreamer with IG Index API

I am trying to use the streaming API from IG Index their documentation is here. The Api requires the light streamer client to be included in the app. So I have used this version and added it to my project. I have created a function which connects to the server. (I believe)


def connect_light_stream_client():
    if cst == None or xt == None:
        create_session()
    global client
    client = lsc.LightstreamerClient(lightstreamer_username=stream_ident,
                                     lightstreamer_password=stream_password,
                                     lightstreamer_url=light_stream_server)

    try:
        client.connect()
    except Exception as e:
        print("Unable to connect to Lightstreamer Server")
        return

Then I call a second function which should fetch a stream of stock data printing the results after each tick.


def listner(item_info):
    print(item_info)

def light_stream_chart_tick():
    sub = lsc.LightstreamerSubscription(mode="DISTINCT", items={"CHART:CS.D.XRPUSD.TODAY.IP:TICK"},
                                        fields={"BID"})
    sub.addlistener(listner)
    sub_key = client.subscribe(sub)
    print(sub_key)

The print at the end produces an output of 1. I get nothing from the listener. Any suggestions what I am doing wrong?

Upvotes: 2

Views: 931

Answers (1)

BugOrFeature
BugOrFeature

Reputation: 329

There's a few things wrong:

  1. You must wait for the subscription request to respond with any updates. In your code, execution ends before any ticks are received. I put the code from light_stream_chart_tick() into the connect method, with a request for input as a wait
  2. The items and fields parameters need to be lists not dicts
  3. The Ripple epic is offline (at least when I tried) - I have substituted Bitcoin
def connect_light_stream_client():
    if cst == None or xt == None:
        create_session()
    global client
    client = lsc.LightstreamerClient(lightstreamer_username=stream_ident,
                                     lightstreamer_password=stream_password,
                                     lightstreamer_url=light_stream_server)

    try:
        client.connect()
    except Exception as e:
        print("Unable to connect to Lightstreamer Server")
        return

    sub = lsc.LightstreamerSubscription(
        mode="DISTINCT", 
        items=["CHART:CS.D.BITCOIN.TODAY.IP:TICK"],
        fields=["BID"]
    )
    sub.addlistener(listner)
    sub_key = client.subscribe(sub)
    print(sub_key)

    input("{0:-^80}\n".format("Hit CR to unsubscribe and disconnect"))
    client.disconnect()


def listner(item_info):
    print(item_info)

There's a python project here that makes it a bit easier to interact with the IG APIs, and there's a streaming sample included. The project is up to date and actively maintained.

Full disclosure: I'm the maintainer of the project

Upvotes: 0

Related Questions