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
313 stars 197 forks source link

lighstreamer error in Docker container - EOFError: EOF when reading a line #291

Closed marcolondon closed 1 year ago

marcolondon commented 1 year ago

Hi all, I'm receiving the error below (EOFError: EOF when reading a line) when running streaming api code inside a docker container (locally or deployed to gcloud). All works fine on local env running from teminal.

I have tryed to replace the input() function with an always true loop to keep the code running #while True: time.sleep(1) This solution works on local as well but produce no result on docker (in this case there is no errors but stream doesn;t work)

Any suggestion is much appreciated. Thanks!

Traceback (most recent call last):
 File "/lightstreamtest/lightstream2.py", line 98, in <module>
 main()
 File "/lightstreamtest/lightstream2.py", line 84, in main
input(
EOFError: EOF when reading a line

Full code below:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging
from trading_ig import IGService, IGStreamService
from trading_ig.config import config
from trading_ig.lightstreamer import Subscription
from datetime import datetime
import time 

# A simple function acting as a Subscription listener
def on_prices_update(item_update):
    print("----> on_prices_update")
    #Timestamp
    timestamp_ms = int(item_update['values']['UTM'])
    timestamp_s = timestamp_ms / 1000  # Convert to seconds
    dt = datetime.fromtimestamp(timestamp_s)
    dt_str = dt.strftime('%Y-%m-%d %H:%M:%S')

    # Get current timestamp
    timestamp_now = datetime.now()
    dt_str_now = timestamp_now.strftime('%Y-%m-%d %H:%M:%S')

    stock_name = item_update['name']
    bid_close = item_update['values']['BID_CLOSE']
    ofr_close = item_update['values']['OFR_CLOSE']
    cons_tick = item_update['values']['CONS_TICK_COUNT']
    candle_close = item_update['values']['CONS_END'] # 1 = candle close, 0 = candle open

    if(candle_close == '1' and len(bid_close)>0):
        print(f"Time: {dt_str_now}")
        print(f"{stock_name:<19}: Candle {dt_str} - Bid Close {bid_close:>5} - Ask Close {ofr_close:>5}")
    #else:
        #print(f"Time: {dt_str_now} - Tick: {cons_tick} - candle_close: {candle_close}")

def on_account_update(balance_update):
    print("----> on_account_update")
    print("balance: %s " % balance_update)

def main():
    print("----> main")
    logging.basicConfig(level=logging.INFO)
    # logging.basicConfig(level=logging.DEBUG)

    ig_service = IGService(
        config.username, config.password, config.api_key, config.acc_type, acc_number=config.acc_number
    )

    ig_stream_service = IGStreamService(ig_service)
    ig_stream_service.create_session()
    #ig_stream_service.create_session(version='3')

    # Making a new Subscription in MERGE mode
    #https://labs.ig.com/sample-apps/streaming-companion/index.html
    subscription_prices = Subscription(
        mode="MERGE",
        items=["CHART:IX.D.SPTRD.DAILY.IP:1MINUTE"],
        fields=["UTM", "OFR_CLOSE", "BID_OPEN","BID_HIGH","BID_LOW","BID_CLOSE","CONS_END","CONS_TICK_COUNT"]
    )
    #LTV,TTV,UTM,DAY_OPEN_MID,DAY_NET_CHG_MID,DAY_PERC_CHG_MID,DAY_HIGH,DAY_LOW,OFR_OPEN,OFR_HIGH,OFR_LOW,OFR_CLOSE,BID_OPEN,BID_HIGH,BID_LOW,BID_CLOSE,LTP_OPEN,LTP_HIGH,LTP_LOW,LTP_CLOSE,CONS_END,CONS_TICK_COUNT

    # Adding the "on_price_update" function to Subscription
    subscription_prices.addlistener(on_prices_update)

    # Registering the Subscription
    sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices)

    # Making an other Subscription in MERGE mode
    subscription_account = Subscription(
        mode="MERGE", items=["ACCOUNT:" + config.acc_number], fields=["AVAILABLE_CASH"],
    )

    # Adding the "on_balance_update" function to Subscription
    subscription_account.addlistener(on_account_update)

    # Registering the Subscription
    sub_key_account = ig_stream_service.ls_client.subscribe(subscription_account)

    input(
        "{0:-^80}\n".format(
            "HIT CR TO UNSUBSCRIBE AND DISCONNECT FROM LIGHTSTREAMER"
        )
    )

    #while True:
    #    time.sleep(1)

    # Disconnecting
    ig_stream_service.disconnect()

if __name__ == "__main__":
    print("----> run main")
    main()
marcolondon commented 1 year ago

running the container with "-i -t" seams to address this issue. I'll close.

docker run --name streamingtest -d -i -t containername