Search code examples
pythonforexpython-socketio

Connection issues with fxcmpy REST api


I am using the REST api from fxcmpy to connect to my fxcmpy account. Since the upgrade to version 1.2.6, I have issues with reconnection when I am accidentally deconnected from the server.

I detect deconnection through the command

api.socket.on('disconnect',disconnect)

where disconnect is my callback function where I reconnect :

def disconnect():
    FLAG=False
    while not FLAG:
    try :
        api=fxcmpy.fxcmpy(access_token=API_ACCESS_TOKEN,log_level='error',server='demo')
        api.subscribe_market_data(symbol,(automated_strategy,))
        FLAG=True
    except:
        print('be patient')
        time.sleep(60)
        FLAG=False

Since the new version I get either a "ServerError: Can not connect to FXCM Server." or a "packet queue is empty, Aborting" message.

If I restart my python console, I can restart my script until the next disconnection. I tried this on Windows 10, Raspbian and android : same issue in all the cases.

I have updated both python-socketio and python-engineio to their latest version : no change.

I am looking for a way of restarting the client when I have disconnection issues. Does someone have the same issue / a clue to solve it ?

Thanks


Solution

  • it took me a while, but I finally found a workaround. The idea is to reset completely the fxcmpy librairy : remove it and then import it again.

    Here is how I do that (code is still not optimized, you can improve it but the idea is here) :

    while not FLAG:
        try :
            import sys
            a_del=[]
            for module in sys.modules.keys():
                if 'fxcm' in module:
                    a_del.append(module)
    
            for module in a_del:
                del sys.modules[module]
    
            del fxcmpy
    
        except:
            print('error in reinitialization')
        try:
            del api
        except:
            print('could not delete api')
    
        try :
            import fxcmpy
            api=fxcmpy.fxcmpy(access_token=API_ACCESS_TOKEN,log_level='error',server='demo')
            api.subscribe_market_data(symbol,(automated_strategy,))
    
            FLAG=True
        except:
            print('try again')
            time.sleep(10)
            FLAG=False
    

    this should do it (adapt of course your api object name and automated strategy function name).