ig-python / trading-ig

A lightweight Python wrapper for the IG Markets API
https://trading-ig.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
307 stars 196 forks source link

Deprecation error on lightreamer.Subscription #321

Open alhennessey92 opened 5 months ago

alhennessey92 commented 5 months ago

Getting a deprecation warning on my code that was previously working fine.

Error: /app/app.py:2127: DeprecationWarning: trading_ig.lightstreamer.Subscription is deprecated, and will be removed in a future version; use the official Lightstreamer Python client instead [server] subscription_prices = Subscription( [server] Traceback (most recent call last): [server] File "/app/app.py", line 2134, in <module> [server] sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices) [server] File "/usr/local/lib/python3.9/site-packages/lightstreamer/client/ls_python_client_wrapper.py", line 1138, in subscribe [server] self.delegate.subscribe(subscription.delegate) [server] AttributeError: 'Subscription' object has no attribute 'delegate'

Code around them lines:

`main_stream = "CHART:CS.D.GBPUSD.TODAY.IP:{}".format(stream_scale_time)
    subscription_prices = Subscription(
        mode="MERGE",
        items=[main_stream],
        fields=["LTV", "UTM", "OFR_OPEN", "OFR_HIGH", "OFR_LOW", "OFR_CLOSE", "BID_OPEN", "BID_HIGH", "BID_LOW", "BID_CLOSE", "CONS_END", "CONS_TICK_COUNT"]
    )

    subscription_prices.addlistener(on_prices_update)
    sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices)`

I have tried removing the line sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices) However still getting the below error.

Error: /app/app.py:2127: DeprecationWarning: trading_ig.lightstreamer.Subscription is deprecated, and will be removed in a future version; use the official Lightstreamer Python client instead [server] subscription_prices = Subscription(

Everything was working fine before i restarted my Docker Desktop and it had to rebuild the container, so not sure if this is because the library has been updated.

Thanks for any help

galburn commented 5 months ago

I had this, or very similar, when I updated trading-ig from 0.0.20 to 0.0.21. I took the opportunity to rework the code rather than resolve or work around the issue - version 0.0.21 added lightstreamer auto-reconnects which is a very useful thing to me.

To help with diagnosis, you could try installing the specific version 0.0.20 and see is that get's things working again:- pip install --force-reinstall -v "trading-ig==0.0.20"

If and when you upgrade trading-ig to 0.0.21 (or higher), please remember to stick to version 1.0.3 of the lightstreamer library (compatible with Lightstreamer Server 7.3.3), as version 2.0.0 requires Lightstreamer Server 7.4.0 (not yet deployed by IG).

9gix commented 5 months ago

subscription.delegate is only available in the the official Lighstreamer library. (lightstreamer.client.Subscription, and not trading_ig.lightstreamer.Subscription`)

So, you could try changing your Subscription import from/to the following

#from trading_ig.lightstreamer import Subscription
from lightstreamer.client import Subscription

because of this changes, the subscription.addlistener also need to be change to subscription.addListener with the listener parameter instead of a simple callable function, you now need to create a SubscriptionListener object with onItemUpdate method instead. (see the lightstreamer-client-lib and sample/stream_ig.py)

PS: the change from trading_ig.lightstreamer to official lightstreamer happen at trading-ig>=0.0.21 onwards. So if your code isn't ready for this migration, you should keep it at trading-ig==0.0.20, it should work fine.