2

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

johnnyp
  • 119
  • 9

3 Answers3

1

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).

johnnyp
  • 119
  • 9
  • Actually, I mistakenly opened two connections, after that I am getting my connection aborted. Tried running your code, but I am still getting connection aborted. What I should do? – R Nanthak Nov 02 '20 at 12:52
0

in addition to reinstalling the fxcmpy module, installing "python-socketio" worked for me

Lars B.
  • 1
  • 1
-1

Working on the same problem these days. I guess the problem is that the session needs to be closed before opening a new.

Something like:

def disconnect():
global api


try:
    api.close()
except:
    pass

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

I am curious about how you override the disconnect callback function when you need to. for instance when doing KeyboardIntterupt and SystemExit?

Rob
  • 179
  • 9